From 1b9b32c7a756db7fea4d9b372dfee843a6aad6b8 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Wed, 31 Mar 2021 20:38:37 -0300 Subject: [PATCH] metal: clean up some shader code --- src/modules/graphics/metal/Graphics.h | 1 + src/modules/graphics/metal/Shader.mm | 47 +++++++++++++++++---------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/modules/graphics/metal/Graphics.h b/src/modules/graphics/metal/Graphics.h index b37204ca5..a5aa0bb82 100644 --- a/src/modules/graphics/metal/Graphics.h +++ b/src/modules/graphics/metal/Graphics.h @@ -114,6 +114,7 @@ public: StreamBuffer *getUniformBuffer() const { return uniformBuffer; } Buffer *getDefaultAttributesBuffer() const { return defaultAttributesBuffer; } + Texture *getDefaultTexture(TextureType textype) const { return defaultTextures[textype]; } static Graphics *getInstance() { return graphicsInstance; } diff --git a/src/modules/graphics/metal/Shader.mm b/src/modules/graphics/metal/Shader.mm index 3f3bef8ae..dc6de9d25 100644 --- a/src/modules/graphics/metal/Shader.mm +++ b/src/modules/graphics/metal/Shader.mm @@ -347,6 +347,8 @@ void Shader::compileFromGLSLang(id device, const glslang::TProgram &p using namespace glslang; using namespace spirv_cross; + auto gfx = Graphics::getInstance(); + std::map varyings; int nextVaryingLocation = 0; @@ -389,41 +391,45 @@ void Shader::compileFromGLSLang(id device, const glslang::TProgram &p u.name = resource.name; u.count = type.array.empty() ? 1 : type.array[0]; u.location = 0; - u.data = malloc(sizeof(int) * u.count); - for (int i = 0; i < u.count; i++) - u.ints[i] = -1; // Will initialize below. - -// printf("%s binding: %d, set: %d\n", u.name.c_str(), msl.get_decoration(resource.id, spv::DecorationBinding), msl.get_decoration(resource.id, spv::DecorationDescriptorSet)); switch (basetype.image.dim) { case spv::Dim2D: u.textureType = basetype.image.arrayed ? TEXTURE_2D_ARRAY : TEXTURE_2D; u.textures = new love::graphics::Texture*[u.count]; - for (int i = 0; i < u.count; i++) - u.textures[i] = nullptr; break; case spv::Dim3D: u.textureType = TEXTURE_VOLUME; u.textures = new love::graphics::Texture*[u.count]; - for (int i = 0; i < u.count; i++) - u.textures[i] = nullptr; break; case spv::DimCube: if (basetype.image.arrayed) throw love::Exception("Cubemap Arrays are not currently supported."); u.textureType = TEXTURE_CUBE; u.textures = new love::graphics::Texture*[u.count]; - for (int i = 0; i < u.count; i++) - u.textures[i] = nullptr; break; case spv::DimBuffer: // TODO: are texel buffers sampled images in glslang? break; default: + // TODO: error? continue? break; } + u.data = malloc(sizeof(int) * u.count); + for (int i = 0; i < u.count; i++) + u.ints[i] = -1; // Initialized below, after compiling. + + if (u.textures != nullptr) + { + auto tex = gfx->getDefaultTexture(u.textureType); + for (int i = 0; i < u.count; i++) + { + tex->retain(); + u.textures[i] = tex; + } + } + uniforms[u.name] = u; BuiltinUniform builtin; @@ -619,8 +625,8 @@ void Shader::compileFromGLSLang(id device, const glslang::TProgram &p u.ints[i] = (int)textureBindings.size(); TextureBinding b = {}; - b.texture = nil; // TODO: matching default texture - b.sampler = nil; + b.texture = getMTLTexture(u.textures[i]); + b.sampler = getMTLSampler(u.textures[i]); BuiltinUniform builtin = BUILTIN_MAX_ENUM; if (getConstant(u.name.c_str(), builtin) && builtin == BUILTIN_TEXTURE_MAIN) @@ -662,6 +668,11 @@ Shader::~Shader() if (it.second.textures != nullptr) { free(it.second.data); + for (int i = 0; i < it.second.count; i++) + { + if (it.second.textures[i] != nullptr) + it.second.textures[i]->release(); + } delete[] it.second.textures; } } @@ -673,8 +684,8 @@ void Shader::attach() { if (current != this) { - Graphics *gfx = Module::getInstance(Module::M_GRAPHICS); - gfx->flushBatchedDrawsGlobal(); + Graphics *gfx = Graphics::getInstance(); + gfx->flushBatchedDraws(); gfx->attachShader(this); current = this; } @@ -725,13 +736,15 @@ void Shader::sendTextures(const UniformInfo *info, love::graphics::Texture **tex { if (!validateTexture(info, tex, false)) continue; - tex->retain(); } else { - // TODO: matching default texture + auto gfx = Graphics::getInstance(); + tex = gfx->getDefaultTexture(info->textureType); } + tex->retain(); + if (info->textures[i] != nullptr) info->textures[i]->release();