1#include "TexturesGLES30.h"
5#include "FormatsGLES30.h"
6#include "CapabilitiesGLES30.h"
7#include "ContextGLES30.h"
9#include "Foundation/Logging/Logger.h"
20 LOG_ERROR(logger,
"Illegal %s description (width=%u, height=%u, depth=%u, layers=%u, faces=%u, levels=%u, samples=%u)",
21 getResourceDimensionsName(desc.target), desc.width, desc.height, desc.depth, desc.layers, desc.faces, desc.levels, desc.samples);
26 uint32_t clampFormatSampleCount(
CapabilitiesGLES30* caps, Cogs::TextureFormat format, uint8_t numSamples)
31 GLint maxSampleCount = 0;
32 glGetInternalformativ(GL_RENDERBUFFER, fmt.internalFormat, GL_SAMPLES, 1, &maxSampleCount);
33 assert(0 <= maxSampleCount);
36 LOG_WARNING_ONCE(logger,
"getInternalFormat(fmt=0x%04x) returned maxSampleCount=%d, but glGetIntegerv(GL_MAX_SAMPLES) returned %u, clamping.",
40 if (uint32_t(maxSampleCount) < numSamples) {
41 LOG_DEBUG_ONCE(logger,
"Requested sample count %u greater than maximum %d for format %s, clamping.",
42 numSamples, maxSampleCount, getFormatInfo(format)->name);
43 return maxSampleCount;
55 texture.width = desc.width;
56 texture.height = desc.height;
58 texture.estimatedByteSize = uint32_t(desc.estimateMemorySize());
59 texture.flags = desc.flags;
60 texture.numSamples = uint8_t(desc.samples);
61 texture.textureId = 0;
62 texture.renderBuffer = 0;
64 texture.format = desc.format;
65 texture.cubeMap =
false;
66 texture.hasMipmaps = generateMips || desc.levels > 1;
70 if (fmt.isTextureFormat == 0) {
73 if (data && fmt.isCompressed) {
74 texture.estimatedByteSize = uint32_t(data->getSize());
77 switch (desc.target) {
78 case ResourceDimensions::Unknown:
79 case ResourceDimensions::Buffer:
80 case ResourceDimensions::Texture1D:
81 case ResourceDimensions::Texture1DArray:
82 case ResourceDimensions::Texture3DArray:
83 case ResourceDimensions::TextureCubeArray:
84 case ResourceDimensions::Texture2DMSArray:
85 LOG_ERROR(logger,
"Unsupported texture resource dimension %s", getResourceDimensionsName(desc.target));
88 case ResourceDimensions::Texture2DMS:
89 if (desc.width == 0 || desc.height == 0 || desc.depth != 1 || desc.layers != 1 || desc.faces != 1 || desc.samples == 0) {
90 return logAndReturnInvalidHandle(desc);
93 texture.target = GL_TEXTURE_2D_MULTISAMPLE;
95 texture.numSamples =
static_cast<uint8_t
>(clampFormatSampleCount(caps, texture.format, texture.numSamples));
98 glGenRenderbuffers(1, &texture.renderBuffer);
99 glBindRenderbuffer(GL_RENDERBUFFER, texture.renderBuffer);
100 glRenderbufferStorageMultisample(GL_RENDERBUFFER, texture.numSamples, fmt.internalFormat, texture.width, texture.height);
101 glBindRenderbuffer(GL_RENDERBUFFER, 0);
105 glGenFramebuffers(1, &texture.fbo);
106 glBindFramebuffer(GL_READ_FRAMEBUFFER, texture.fbo);
107 glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, framebufferAttachment, GL_RENDERBUFFER, texture.renderBuffer);
108 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
110 GLenum status = glCheckFramebufferStatus(GL_READ_FRAMEBUFFER);
111 if (status != GL_FRAMEBUFFER_COMPLETE) {
112 LOG_ERROR(logger,
"Failed to create multisample texture rendertarget: %s", OpenGLES30::framebuffersStatusString(status));
113 glDeleteFramebuffers(1, &texture.fbo);
114 glDeleteRenderbuffers(1, &texture.renderBuffer);
118 return textures.addResource(texture);
122 case ResourceDimensions::Texture2D:
123 if (desc.width == 0 || desc.height == 0 || desc.depth != 1 || desc.layers != 1 || desc.faces != 1 || desc.samples != 1) {
124 return logAndReturnInvalidHandle(desc);
126 texture.target = GL_TEXTURE_2D;
128 case ResourceDimensions::Texture2DArray:
129 if (desc.width == 0 || desc.height == 0 || desc.depth != 1 || desc.layers == 0 || desc.faces != 1 || desc.samples != 1) {
130 return logAndReturnInvalidHandle(desc);
132 texture.target = GL_TEXTURE_2D_ARRAY;
133 texture.depth = desc.layers;
135 case ResourceDimensions::Texture3D:
136 if (desc.width == 0 || desc.height == 0 || desc.depth == 0 || desc.layers != 1 || desc.faces != 1 || desc.samples != 1) {
137 return logAndReturnInvalidHandle(desc);
139 texture.target = GL_TEXTURE_3D;
140 texture.depth = desc.depth;
142 case ResourceDimensions::TextureCube:
143 if (desc.width == 0 || desc.height == 0 || desc.depth != 1 || desc.layers != 1 || desc.faces != 6 || desc.samples != 1) {
144 return logAndReturnInvalidHandle(desc);
146 texture.target = GL_TEXTURE_CUBE_MAP;
147 texture.depth = desc.depth;
148 texture.cubeMap =
true;
151 case ResourceDimensions::RenderBuffer:
152 if (desc.width == 0 || desc.height == 0 || desc.depth != 1 || desc.layers != 1 || desc.faces != 1 || desc.levels != 1) {
153 return logAndReturnInvalidHandle(desc);
156 LOG_ERROR(logger,
"RenderBuffers do not support mipmaps");
159 texture.target = GL_RENDERBUFFER;
160 texture.numSamples =
static_cast<uint8_t
>(clampFormatSampleCount(caps, texture.format, texture.numSamples));
163 assert(
false &&
"Invalid enum");
167 GLuint textureLevels;
169 textureLevels = 1 +
static_cast<GLuint
>(std::floor(std::log((
double)std::max(desc.width, desc.height)) / std::log(2.0)));
172 textureLevels =
static_cast<GLuint
>(desc.levels);
179 if (texture.target == GL_RENDERBUFFER) {
181 texture.textureId = 0;
183 assert(data !=
nullptr);
184 texture.renderBuffer =
static_cast<GLuint
>(data->externalHandle);
187 glGenRenderbuffers(1, &texture.renderBuffer);
188 glBindRenderbuffer(GL_RENDERBUFFER, texture.renderBuffer);
189 glRenderbufferStorageMultisample(GL_RENDERBUFFER, texture.numSamples, fmt.internalFormat, texture.width, texture.height);
190 glBindRenderbuffer(GL_RENDERBUFFER, 0);
192 textureHandle = textures.addResource(texture);
198 assert(data !=
nullptr);
199 texture.textureId =
static_cast<GLuint
>(data->externalHandle);
200 textureHandle = textures.addResource(texture);
203 if (
TextureGLES30* tex = context->bindTexture(textureHandle); tex) {
204 glGenerateMipmap(tex->target);
209 glGenTextures(1, &texture.textureId);
210 textureHandle = textures.addResource(texture);
212 if (
TextureGLES30* tex = context->bindTexture(textureHandle); tex) {
213 defineTextureData(tex, textureLevels);
215 uploadTextureData(*tex, *data, 0, 0, 0);
217 else if (generateMips) {
218 glGenerateMipmap(tex->target);
224 textureMemoryConsumption += texture.estimatedByteSize;
225 return textureHandle;
228void Cogs::TexturesGLES30::uploadTextureData(
TextureGLES30& tex,
230 uint32_t layer_offset,
231 uint32_t face_offset,
232 uint32_t level_offset)
234 assert(face_offset == 0 &&
"face_offset not handled");
235 assert(level_offset == 0 &&
"level_offset not handled");
238 const bool isCompressed = fmt.isCompressed;
239 const GLenum target = tex.target;
240 const GLenum textureFormat = fmt.internalFormat;
241 const GLenum pixelType = fmt.type;
242 const GLenum pixelFormat = fmt.format;
244 if(context->uploadStatisticsEnabled) context->uploadStatisticsTextureUpload(data.getSize());
249 for (
size_t j = 0; j < data.levels; ++j) {
251 glCompressedTexSubImage2D(target,
static_cast<GLint
>(j), 0, 0, ext.width, ext.height, textureFormat,
static_cast<GLsizei
>(data.getLevelSize(j)), data.getData(0, 0, j));
254 case GL_TEXTURE_CUBE_MAP:
255 for (
size_t j = 0; j < data.levels; ++j) {
256 for (GLint c = 0; c < 6; ++c) {
258 glCompressedTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + c,
static_cast<GLint
>(j), 0, 0, ext.width, ext.height, textureFormat, GLsizei(data.getLevelSize(j)), data.getData(0, c, j));
262 case GL_TEXTURE_2D_ARRAY:
263 for (
size_t j = 0; j < data.levels; ++j) {
264 for (
size_t l = 0; l < data.layers; ++l) {
266 glCompressedTexSubImage3D(target,
static_cast<GLint
>(j), 0, 0,
static_cast<GLint
>(l), ext.width, ext.height, 1, textureFormat, GLsizei(data.getLevelSize(j)), data.getData(l, 0, j));
270 case GL_TEXTURE_2D_MULTISAMPLE: [[fallthrough]];
271 case GL_TEXTURE_3D: [[fallthrough]];
272 case GL_INVALID_ENUM:
273 LOG_ERROR(logger,
"Texture target #%04x cannot use compressed formats", target);
280 for (
size_t j = 0; j < data.levels; ++j) {
282 glTexSubImage2D(target,
static_cast<GLint
>(j), 0, 0, ext.width, ext.height, pixelFormat, pixelType, data.getData(0, 0, j));
285 case GL_TEXTURE_2D_ARRAY:
286 for (
size_t i = 0; i < data.layers; ++i) {
287 for (
size_t j = 0; j < data.levels; ++j) {
289 glTexSubImage3D(target,
static_cast<GLint
>(j),
290 0, 0,
static_cast<GLint
>(layer_offset + i), ext.width, ext.height, 1,
291 pixelFormat, pixelType, data.getData(i, 0, j));
296 for (
size_t j = 0; j < data.levels; ++j) {
298 glTexSubImage3D(target,
static_cast<GLint
>(j),
299 0, 0, 0, ext.width, ext.height, ext.depth,
300 pixelFormat, pixelType, data.getData(0, 0, j));
303 case GL_TEXTURE_CUBE_MAP:
304 for (
size_t j = 0; j < data.levels; ++j) {
305 for (GLint c = 0; c < 6; ++c) {
307 glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + c, (GLint)j,
308 0, 0, ext.width, ext.height,
309 pixelFormat, pixelType, data.getData(0, c, j));
313 case GL_TEXTURE_2D_MULTISAMPLE: [[fallthrough]];
314 case GL_INVALID_ENUM:
315 LOG_ERROR(logger,
"Texture target #%04x cannot get data uploaded", target);
322 glGenerateMipmap(target);
328 uint32_t layer_offset,
329 uint32_t face_offset,
330 uint32_t level_offset)
333 const GLenum target = texture.target;
334 glBindTexture(target, texture.textureId);
335 uploadTextureData(texture, data, layer_offset, face_offset, level_offset);
336 glBindTexture(target, 0);
340void Cogs::TexturesGLES30::defineTextureData(
TextureGLES30* tex, GLuint textureLevels)
343 assert(fmt.isTextureFormat);
345 switch (tex->target) {
348 glTexStorage2D(GL_TEXTURE_2D, textureLevels, fmt.internalFormat, tex->width, tex->height);
351 case GL_TEXTURE_CUBE_MAP:
352 glTexStorage2D(tex->target, textureLevels, fmt.internalFormat, tex->width, tex->height);
355 case GL_TEXTURE_2D_ARRAY:
356 glTexStorage3D(tex->target, textureLevels, fmt.internalFormat, tex->width, tex->height, tex->depth);
360 glTexStorage3D(tex->target, textureLevels, fmt.internalFormat, tex->width, tex->height, tex->depth);
363 case GL_TEXTURE_2D_MULTISAMPLE: [[fallthrough]];
365 LOG_ERROR(logger,
"Texture target #%04x cannot get data uploaded", tex->target);
372 if (HandleIsValid(textureHandle)) {
375 for (GLuint i = 0; i < OpenGLES30::maxTexUnits; i++) {
376 if (context->texUnits[i].texture == textureHandle) {
377 if (context->activeTexUnit != i) {
378 context->activeTexUnit = i;
379 glActiveTexture(GL_TEXTURE0 + i);
381 glBindTexture(context->texUnits[i].target, 0);
383 context->texUnits[i].target = GL_TEXTURE_2D;
393 glDeleteFramebuffers(1, &texture.fbo);
396 if (texture.renderBuffer) {
400 glDeleteRenderbuffers(1, &texture.renderBuffer);
402 texture.renderBuffer = 0;
406 glDeleteTextures(1, &texture.textureId);
408 if (texture.textureId != 0) {
409 textureMemoryConsumption -= texture.estimatedByteSize;
411 texture.textureId = 0;
413 textures.removeResource(textureHandle);
419 GLuint
id = (GLuint)(intptr_t)nativeHandle;
420 glDeleteTextures(1, &
id);
426 LOG_WARNING_ONCE(logger,
"loadSamplerState: ES30 does not support border color.");
430 glGenSamplers(1, &sampler);
432 static const GLenum AddressModes[] = {
438 static_assert(
sizeof(AddressModes) ==
sizeof(AddressModes[0]) * Cogs::SamplerState::AddressMode_Size);
440 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, AddressModes[state.
addressModeS]);
441 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, AddressModes[state.
addressModeT]);
442 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_R, AddressModes[state.
addressModeW]);
444 static const GLenum ComparisonFunctions[] = {
454 static_assert(
sizeof(ComparisonFunctions) ==
sizeof(ComparisonFunctions[0]) * Cogs::SamplerState::ComparisonFunction_Size);
458 float maxAnisotropy = std::min(deviceCaps.MaxAnisotropy,
float(state.
maxAnisotropy));
459 if (1.f < maxAnisotropy) {
460 glSamplerParameterf(sampler, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy);
466 glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
467 glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
468 glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_MODE, GL_NONE);
471 glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
472 glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
473 glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_MODE, GL_NONE);
476 glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
477 glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
478 glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
479 glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_FUNC, ComparisonFunctions[state.
comparisonFunction]);
483 glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
484 glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
485 glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
486 glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_FUNC, ComparisonFunctions[state.
comparisonFunction]);
497 if (HandleIsValid(samplerHandle)) {
499 glDeleteSamplers(1, &sampler.glName);
500 samplers.removeResource(samplerHandle);
507 LOG_WARNING_ONCE(logger,
"Views with nonzero layer index not supported, ignoring");
509 return views.addResource(viewDescription);
514 if (HandleIsValid(handle)) {
515 views.removeResource(handle);
521 if (HandleIsValid(textureHandle)) {
523 if (tex.target == GL_RENDERBUFFER) {
524 return reinterpret_cast<void*
>(
static_cast<size_t>(tex.renderBuffer));
526 return reinterpret_cast<void*
>(
static_cast<size_t>(tex.textureId));
533 TextureGLES30* texture = context->bindTexture(textureHandle);
535 glGenerateMipmap(texture->target);
541 std::vector<TextureHandle> handles;
543 handles.push_back(textures.getHandle(texture));
546 releaseTexture(handle);
Log implementation class.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains all Cogs related functionality.
const GraphicsDeviceCapabilities & getDeviceCapabilities() const override
Gets the device capabilities in a structure.
Contains device capabilities.
static const Handle_t NoHandle
Represents a handle to nothing.
static const Handle_t InvalidHandle
Represents an invalid handle.
Encapsulates state for texture sampling in a state object.
ComparisonFunction comparisonFunction
Specifies the comparison function to use when applying a comparison sampler.
unsigned int maxAnisotropy
Specifies the maximum number of anisotropic samples to use when sampling a texture.
AddressMode addressModeW
Specifies the addressing mode along the W axis in texture coordinate space.
AddressMode addressModeS
Specifies the addressing mode along the S axis in texture coordinate space.
@ Border
Texture color is set to the border color when outside [0, 1] range.
@ ComparisonMinMagMipPoint
Comparison filter for depth sample comparisons using point sampling.
@ ComparisonMinMagMipLinear
Comparison filter for depth sample comparisons using linear interpolation sampling.
@ MinMagMipPoint
Point sampling for both minification and magnification.
@ MinMagMipLinear
Linear sampling for both minification and magnification.
FilterMode filter
Specifies the filter to use for texture sampling.
AddressMode addressModeT
Specifies the addressing mode along the T axis in texture coordinate space.
@ DepthBuffer
The texture can be used as a depth target and have depth buffer values written into.
@ NoDelete
The ownership of the underlying texture resource is outside of cogs and cogs will not delete it.
@ GenerateMipMaps
The texture supports automatic mipmap generation performed by the graphics device.
Describes how to fetch data from a texture in shaders.
uint32_t layerIndex
Index of the first layer (if array) to fetch from.
void releaseNativeTexture(TextureNativeHandle nativeHandle) override
Release a native texture handle.
void releaseSamplerState(SamplerStateHandle handle)
Release the sampler state with the given handle.
void releaseTexture(TextureHandle textureHandle)
Release the texture with the given textureHandle.
void generateMipmaps(TextureHandle texureHandle)
Use the graphics device to generate mipmaps for the texture with the given texture handle.
void releaseResources()
Release all allocated texture resources.
TextureViewHandle createTextureView(TextureViewDescription &viewDescription) override
Create a texture view used to bind a limited view of the texture data to the rendering pipeline.
void * getNativeHandle(TextureHandle textureHandle) override
Get the device-specific handle (D3D texture pointer, OpenGL texture ID etc) associated with the given...
TextureHandle loadTexture(const TextureDescription &desc, const TextureData *data)
Load a texture from the given description.
void releaseTextureView(const TextureViewHandle &handle) override
Release the given texture view.
SamplerStateHandle loadSamplerState(const SamplerState &state)
Load a sampler state object.