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(Cogs::TextureFormat format, uint8_t numSamples)
31 GLint maxSampleCount = 0;
32 glGetInternalformativ(GL_RENDERBUFFER, fmt.internalFormat, GL_SAMPLES, 1, &maxSampleCount);
33 assert(0 <= maxSampleCount);
35 if (uint32_t(maxSampleCount) < numSamples) {
36 LOG_DEBUG(logger,
"Requested sample count %u greater than maximum %d for format %s, clamping.",
37 numSamples, maxSampleCount, getFormatInfo(format)->name);
38 return maxSampleCount;
50 texture.width = desc.width;
51 texture.height = desc.height;
53 texture.estimatedByteSize = uint32_t(desc.estimateMemorySize());
54 texture.flags = desc.flags;
55 texture.numSamples = uint8_t(desc.samples);
56 texture.textureId = 0;
57 texture.renderBuffer = 0;
59 texture.format = desc.format;
60 texture.cubeMap =
false;
61 texture.hasMipmaps = generateMips || desc.levels > 1;
65 if (fmt.isTextureFormat == 0) {
68 if (data && fmt.isCompressed) {
69 texture.estimatedByteSize = uint32_t(data->getSize());
72 switch (desc.target) {
73 case ResourceDimensions::Unknown:
74 case ResourceDimensions::Buffer:
75 case ResourceDimensions::Texture1D:
76 case ResourceDimensions::Texture1DArray:
77 case ResourceDimensions::Texture3DArray:
78 case ResourceDimensions::TextureCubeArray:
79 case ResourceDimensions::Texture2DMSArray:
80 LOG_ERROR(logger,
"Unsupported texture resource dimension %s", getResourceDimensionsName(desc.target));
83 case ResourceDimensions::Texture2DMS:
84 if (desc.width == 0 || desc.height == 0 || desc.depth != 1 || desc.layers != 1 || desc.faces != 1 || desc.samples == 0) {
85 return logAndReturnInvalidHandle(desc);
88 texture.target = GL_TEXTURE_2D_MULTISAMPLE;
90 texture.numSamples =
static_cast<uint8_t
>(clampFormatSampleCount(texture.format, texture.numSamples));
93 glGenRenderbuffers(1, &texture.renderBuffer);
94 glBindRenderbuffer(GL_RENDERBUFFER, texture.renderBuffer);
95 glRenderbufferStorageMultisample(GL_RENDERBUFFER, texture.numSamples, fmt.internalFormat, texture.width, texture.height);
96 glBindRenderbuffer(GL_RENDERBUFFER, 0);
100 glGenFramebuffers(1, &texture.fbo);
101 glBindFramebuffer(GL_READ_FRAMEBUFFER, texture.fbo);
102 glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, framebufferAttachment, GL_RENDERBUFFER, texture.renderBuffer);
103 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
105 GLenum status = glCheckFramebufferStatus(GL_READ_FRAMEBUFFER);
106 if (status != GL_FRAMEBUFFER_COMPLETE) {
107 LOG_ERROR(logger,
"Failed to create multisample texture rendertarget: %s", OpenGLES30::framebuffersStatusString(status));
108 glDeleteFramebuffers(1, &texture.fbo);
109 glDeleteRenderbuffers(1, &texture.renderBuffer);
113 return textures.addResource(texture);
117 case ResourceDimensions::Texture2D:
118 if (desc.width == 0 || desc.height == 0 || desc.depth != 1 || desc.layers != 1 || desc.faces != 1 || desc.samples != 1) {
119 return logAndReturnInvalidHandle(desc);
121 texture.target = GL_TEXTURE_2D;
123 case ResourceDimensions::Texture2DArray:
124 if (desc.width == 0 || desc.height == 0 || desc.depth != 1 || desc.layers == 0 || desc.faces != 1 || desc.samples != 1) {
125 return logAndReturnInvalidHandle(desc);
127 texture.target = GL_TEXTURE_2D_ARRAY;
128 texture.depth = desc.layers;
130 case ResourceDimensions::Texture3D:
131 if (desc.width == 0 || desc.height == 0 || desc.depth == 0 || desc.layers != 1 || desc.faces != 1 || desc.samples != 1) {
132 return logAndReturnInvalidHandle(desc);
134 texture.target = GL_TEXTURE_3D;
135 texture.depth = desc.depth;
137 case ResourceDimensions::TextureCube:
138 if (desc.width == 0 || desc.height == 0 || desc.depth != 1 || desc.layers != 1 || desc.faces != 6 || desc.samples != 1) {
139 return logAndReturnInvalidHandle(desc);
141 texture.target = GL_TEXTURE_CUBE_MAP;
142 texture.depth = desc.depth;
143 texture.cubeMap =
true;
146 case ResourceDimensions::RenderBuffer:
147 if (desc.width == 0 || desc.height == 0 || desc.depth != 1 || desc.layers != 1 || desc.faces != 1 || desc.levels != 1) {
148 return logAndReturnInvalidHandle(desc);
151 LOG_ERROR(logger,
"RenderBuffers do not support mipmaps");
154 texture.target = GL_RENDERBUFFER;
155 texture.numSamples =
static_cast<uint8_t
>(clampFormatSampleCount(texture.format, texture.numSamples));
158 assert(
false &&
"Invalid enum");
162 GLuint textureLevels;
164 textureLevels = 1 +
static_cast<GLuint
>(std::floor(std::log((
double)std::max(desc.width, desc.height)) / std::log(2.0)));
167 textureLevels =
static_cast<GLuint
>(desc.levels);
174 if (texture.target == GL_RENDERBUFFER) {
176 texture.textureId = 0;
178 assert(data !=
nullptr);
179 texture.renderBuffer =
static_cast<GLuint
>(data->externalHandle);
182 glGenRenderbuffers(1, &texture.renderBuffer);
183 glBindRenderbuffer(GL_RENDERBUFFER, texture.renderBuffer);
184 glRenderbufferStorageMultisample(GL_RENDERBUFFER, texture.numSamples, fmt.internalFormat, texture.width, texture.height);
185 glBindRenderbuffer(GL_RENDERBUFFER, 0);
187 textureHandle = textures.addResource(texture);
193 assert(data !=
nullptr);
194 texture.textureId =
static_cast<GLuint
>(data->externalHandle);
195 textureHandle = textures.addResource(texture);
198 if (
TextureGLES30* tex = context->bindTexture(textureHandle); tex) {
199 glGenerateMipmap(tex->target);
204 glGenTextures(1, &texture.textureId);
205 textureHandle = textures.addResource(texture);
207 if (
TextureGLES30* tex = context->bindTexture(textureHandle); tex) {
208 defineTextureData(tex, textureLevels);
210 uploadTextureData(*tex, *data, 0, 0, 0);
212 else if (generateMips) {
213 glGenerateMipmap(tex->target);
219 textureMemoryConsumption += texture.estimatedByteSize;
220 return textureHandle;
223void Cogs::TexturesGLES30::uploadTextureData(
TextureGLES30& tex,
225 uint32_t layer_offset,
226 uint32_t face_offset,
227 uint32_t level_offset)
229 assert(face_offset == 0 &&
"face_offset not handled");
230 assert(level_offset == 0 &&
"level_offset not handled");
233 const bool isCompressed = fmt.isCompressed;
234 const GLenum target = tex.target;
235 const GLenum textureFormat = fmt.internalFormat;
236 const GLenum pixelType = fmt.type;
237 const GLenum pixelFormat = fmt.format;
239 if(context->uploadStatisticsEnabled) context->uploadStatisticsTextureUpload(data.getSize());
244 for (
size_t j = 0; j < data.levels; ++j) {
246 glCompressedTexSubImage2D(target,
static_cast<GLint
>(j), 0, 0, ext.width, ext.height, textureFormat,
static_cast<GLsizei
>(data.getLevelSize(j)), data.getData(0, 0, j));
249 case GL_TEXTURE_CUBE_MAP:
250 for (
size_t j = 0; j < data.levels; ++j) {
251 for (GLint c = 0; c < 6; ++c) {
253 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));
257 case GL_TEXTURE_2D_ARRAY: [[fallthrough]];
258 case GL_TEXTURE_2D_MULTISAMPLE: [[fallthrough]];
259 case GL_TEXTURE_3D: [[fallthrough]];
260 case GL_INVALID_ENUM:
261 LOG_ERROR(logger,
"Texture target #%04x cannot use compressed formats", target);
268 for (
size_t j = 0; j < data.levels; ++j) {
270 glTexSubImage2D(target,
static_cast<GLint
>(j), 0, 0, ext.width, ext.height, pixelFormat, pixelType, data.getData(0, 0, j));
273 case GL_TEXTURE_2D_ARRAY:
274 for (
size_t i = 0; i < data.layers; ++i) {
275 for (
size_t j = 0; j < data.levels; ++j) {
277 glTexSubImage3D(target,
static_cast<GLint
>(j),
278 0, 0,
static_cast<GLint
>(layer_offset + i), ext.width, ext.height, 1,
279 pixelFormat, pixelType, data.getData(i, 0, j));
284 for (
size_t j = 0; j < data.levels; ++j) {
286 glTexSubImage3D(target,
static_cast<GLint
>(j),
287 0, 0, 0, ext.width, ext.height, ext.depth,
288 pixelFormat, pixelType, data.getData(0, 0, j));
291 case GL_TEXTURE_CUBE_MAP:
292 for (
size_t j = 0; j < data.levels; ++j) {
293 for (GLint c = 0; c < 6; ++c) {
295 glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + c, (GLint)j,
296 0, 0, ext.width, ext.height,
297 pixelFormat, pixelType, data.getData(0, c, j));
301 case GL_TEXTURE_2D_MULTISAMPLE: [[fallthrough]];
302 case GL_INVALID_ENUM:
303 LOG_ERROR(logger,
"Texture target #%04x cannot get data uploaded", target);
310 glGenerateMipmap(target);
316 uint32_t layer_offset,
317 uint32_t face_offset,
318 uint32_t level_offset)
321 const GLenum target = texture.target;
322 glBindTexture(target, texture.textureId);
323 uploadTextureData(texture, data, layer_offset, face_offset, level_offset);
324 glBindTexture(target, 0);
328void Cogs::TexturesGLES30::defineTextureData(
TextureGLES30* tex, GLuint textureLevels)
331 assert(fmt.isTextureFormat);
333 switch (tex->target) {
336 glTexStorage2D(GL_TEXTURE_2D, textureLevels, fmt.internalFormat, tex->width, tex->height);
339 case GL_TEXTURE_CUBE_MAP:
340 glTexStorage2D(tex->target, textureLevels, fmt.internalFormat, tex->width, tex->height);
343 case GL_TEXTURE_2D_ARRAY:
344 glTexStorage3D(tex->target, textureLevels, fmt.internalFormat, tex->width, tex->height, tex->depth);
348 glTexStorage3D(tex->target, textureLevels, fmt.internalFormat, tex->width, tex->height, tex->depth);
351 case GL_TEXTURE_2D_MULTISAMPLE: [[fallthrough]];
353 LOG_ERROR(logger,
"Texture target #%04x cannot get data uploaded", tex->target);
360 if (HandleIsValid(textureHandle)) {
363 for (GLuint i = 0; i < OpenGLES30::maxTexUnits; i++) {
364 if (context->texUnits[i].texture == textureHandle) {
365 if (context->activeTexUnit != i) {
366 context->activeTexUnit = i;
367 glActiveTexture(GL_TEXTURE0 + i);
369 glBindTexture(context->texUnits[i].target, 0);
371 context->texUnits[i].target = GL_TEXTURE_2D;
381 glDeleteFramebuffers(1, &texture.fbo);
384 if (texture.renderBuffer) {
388 glDeleteRenderbuffers(1, &texture.renderBuffer);
390 texture.renderBuffer = 0;
394 glDeleteTextures(1, &texture.textureId);
396 if (texture.textureId != 0) {
397 textureMemoryConsumption -= texture.estimatedByteSize;
399 texture.textureId = 0;
401 textures.removeResource(textureHandle);
407 GLuint
id = (GLuint)(intptr_t)nativeHandle;
408 glDeleteTextures(1, &
id);
414 LOG_WARNING_ONCE(logger,
"loadSamplerState: ES30 does not support border color.");
418 glGenSamplers(1, &sampler);
420 static const GLenum AddressModes[] = {
426 static_assert(
sizeof(AddressModes) ==
sizeof(AddressModes[0]) * Cogs::SamplerState::AddressMode_Size);
428 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, AddressModes[state.
addressModeS]);
429 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, AddressModes[state.
addressModeT]);
430 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_R, AddressModes[state.
addressModeW]);
432 static const GLenum ComparisonFunctions[] = {
442 static_assert(
sizeof(ComparisonFunctions) ==
sizeof(ComparisonFunctions[0]) * Cogs::SamplerState::ComparisonFunction_Size);
446 float maxAnisotropy = std::min(deviceCaps.MaxAnisotropy,
float(state.
maxAnisotropy));
447 if (1.f < maxAnisotropy) {
448 glSamplerParameterf(sampler, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy);
454 glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
455 glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
456 glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_MODE, GL_NONE);
459 glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
460 glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
461 glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_MODE, GL_NONE);
464 glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
465 glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
466 glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
467 glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_FUNC, ComparisonFunctions[state.
comparisonFunction]);
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_COMPARE_REF_TO_TEXTURE);
474 glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_FUNC, ComparisonFunctions[state.
comparisonFunction]);
485 if (HandleIsValid(samplerHandle)) {
487 glDeleteSamplers(1, &sampler.glName);
488 samplers.removeResource(samplerHandle);
495 LOG_WARNING_ONCE(logger,
"Views with nonzero layer index not supported, ignoring");
497 return views.addResource(viewDescription);
502 if (HandleIsValid(handle)) {
503 views.removeResource(handle);
509 if (HandleIsValid(textureHandle)) {
511 if (tex.target == GL_RENDERBUFFER) {
512 return reinterpret_cast<void*
>(
static_cast<size_t>(tex.renderBuffer));
514 return reinterpret_cast<void*
>(
static_cast<size_t>(tex.textureId));
521 TextureGLES30* texture = context->bindTexture(textureHandle);
523 glGenerateMipmap(texture->target);
529 std::vector<TextureHandle> handles;
531 handles.push_back(textures.getHandle(texture));
534 releaseTexture(handle);
Log implementation class.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains all Cogs related functionality.
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.