From 7f8144494e0b9ff558edb7f1ca995abae3f42858 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 12 Jun 2021 14:10:57 -0700 Subject: [PATCH] Draw: Use constants for texture slots. GL actually had a check for 16 but then an array for 8. This should make it easier to figure out if we ever hit those limits. --- Common/GPU/D3D11/thin3d_d3d11.cpp | 4 ++-- Common/GPU/OpenGL/GLQueueRunner.cpp | 4 ++-- Common/GPU/OpenGL/GLRenderManager.cpp | 1 + Common/GPU/OpenGL/GLRenderManager.h | 6 +++++- Common/GPU/OpenGL/thin3d_gl.cpp | 8 ++++---- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Common/GPU/D3D11/thin3d_d3d11.cpp b/Common/GPU/D3D11/thin3d_d3d11.cpp index 14f361d902..b9f829e6c7 100644 --- a/Common/GPU/D3D11/thin3d_d3d11.cpp +++ b/Common/GPU/D3D11/thin3d_d3d11.cpp @@ -1331,7 +1331,7 @@ Framebuffer *D3D11DrawContext::CreateFramebuffer(const FramebufferDesc &desc) { void D3D11DrawContext::BindTextures(int start, int count, Texture **textures) { // Collect the resource views from the textures. - ID3D11ShaderResourceView *views[8]; + ID3D11ShaderResourceView *views[MAX_BOUND_TEXTURES]; _assert_(start + count <= ARRAY_SIZE(views)); for (int i = 0; i < count; i++) { D3D11Texture *tex = (D3D11Texture *)textures[i]; @@ -1341,7 +1341,7 @@ void D3D11DrawContext::BindTextures(int start, int count, Texture **textures) { } void D3D11DrawContext::BindSamplerStates(int start, int count, SamplerState **states) { - ID3D11SamplerState *samplers[8]; + ID3D11SamplerState *samplers[MAX_BOUND_TEXTURES]; _assert_(start + count <= ARRAY_SIZE(samplers)); for (int i = 0; i < count; i++) { D3D11SamplerState *samp = (D3D11SamplerState *)states[i]; diff --git a/Common/GPU/OpenGL/GLQueueRunner.cpp b/Common/GPU/OpenGL/GLQueueRunner.cpp index 3f3dec4c4d..2fe56bfac6 100644 --- a/Common/GPU/OpenGL/GLQueueRunner.cpp +++ b/Common/GPU/OpenGL/GLQueueRunner.cpp @@ -17,7 +17,7 @@ #include "GLRenderManager.h" #include "DataFormatGL.h" -#define TEXCACHE_NAME_CACHE_SIZE 16 +static constexpr int TEXCACHE_NAME_CACHE_SIZE = 16; #if PPSSPP_PLATFORM(IOS) extern void bindDefaultFBO(); @@ -800,7 +800,7 @@ void GLQueueRunner::PerformRenderPass(const GLRStep &step, bool first, bool last GLuint blendEqColor = (GLuint)-1; GLuint blendEqAlpha = (GLuint)-1; - GLRTexture *curTex[8]{}; + GLRTexture *curTex[MAX_GL_TEXTURE_SLOTS]{}; CHECK_GL_ERROR_IF_DEBUG(); auto &commands = step.commands; diff --git a/Common/GPU/OpenGL/GLRenderManager.cpp b/Common/GPU/OpenGL/GLRenderManager.cpp index 6229aa676a..1475d8d5ff 100644 --- a/Common/GPU/OpenGL/GLRenderManager.cpp +++ b/Common/GPU/OpenGL/GLRenderManager.cpp @@ -357,6 +357,7 @@ void GLRenderManager::BindFramebufferAsRenderTarget(GLRFramebuffer *fb, GLRRende void GLRenderManager::BindFramebufferAsTexture(GLRFramebuffer *fb, int binding, int aspectBit, int attachment) { _dbg_assert_(curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER); + _dbg_assert_(binding < MAX_GL_TEXTURE_SLOTS); GLRRenderData data{ GLRRenderCommand::BIND_FB_TEXTURE }; data.bind_fb_texture.slot = binding; data.bind_fb_texture.framebuffer = fb; diff --git a/Common/GPU/OpenGL/GLRenderManager.h b/Common/GPU/OpenGL/GLRenderManager.h index 69cd0e9d37..9edd5af35c 100644 --- a/Common/GPU/OpenGL/GLRenderManager.h +++ b/Common/GPU/OpenGL/GLRenderManager.h @@ -21,6 +21,8 @@ namespace Draw { class DrawContext; } +constexpr int MAX_GL_TEXTURE_SLOTS = 8; + class GLRTexture { public: GLRTexture(int width, int height, int numMips); @@ -571,8 +573,8 @@ public: void BindTexture(int slot, GLRTexture *tex) { _dbg_assert_(curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER); + _dbg_assert_(slot < MAX_GL_TEXTURE_SLOTS); GLRRenderData data{ GLRRenderCommand::BINDTEXTURE }; - _dbg_assert_(slot < 16); data.texture.slot = slot; data.texture.texture = tex; curRenderStep_->commands.push_back(data); @@ -824,6 +826,7 @@ public: // Modifies the current texture as per GL specs, not global state. void SetTextureSampler(int slot, GLenum wrapS, GLenum wrapT, GLenum magFilter, GLenum minFilter, float anisotropy) { _dbg_assert_(curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER); + _dbg_assert_(slot < MAX_GL_TEXTURE_SLOTS); GLRRenderData data{ GLRRenderCommand::TEXTURESAMPLER }; data.textureSampler.slot = slot; data.textureSampler.wrapS = wrapS; @@ -836,6 +839,7 @@ public: void SetTextureLod(int slot, float minLod, float maxLod, float lodBias) { _dbg_assert_(curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER); + _dbg_assert_(slot < MAX_GL_TEXTURE_SLOTS); GLRRenderData data{ GLRRenderCommand::TEXTURELOD}; data.textureLod.slot = slot; data.textureLod.minLod = minLod; diff --git a/Common/GPU/OpenGL/thin3d_gl.cpp b/Common/GPU/OpenGL/thin3d_gl.cpp index cff1535c8d..161c04eecf 100644 --- a/Common/GPU/OpenGL/thin3d_gl.cpp +++ b/Common/GPU/OpenGL/thin3d_gl.cpp @@ -299,7 +299,7 @@ public: // TODO: Optimize by getting the locations first and putting in a custom struct UniformBufferDesc dynamicUniforms; - GLint samplerLocs_[8]{}; + GLint samplerLocs_[MAX_TEXTURE_SLOTS]{}; std::vector dynamicUniformLocs_; GLRProgram *program_ = nullptr; @@ -1149,13 +1149,13 @@ bool OpenGLPipeline::LinkShaders() { queries.push_back({ &samplerLocs_[0], "sampler0" }); queries.push_back({ &samplerLocs_[1], "sampler1" }); queries.push_back({ &samplerLocs_[2], "sampler2" }); + _assert_(queries.size() >= MAX_TEXTURE_SLOTS); for (size_t i = 0; i < dynamicUniforms.uniforms.size(); ++i) { queries.push_back({ &dynamicUniformLocs_[i], dynamicUniforms.uniforms[i].name }); } std::vector initialize; - initialize.push_back({ &samplerLocs_[0], 0, 0 }); - initialize.push_back({ &samplerLocs_[1], 0, 1 }); - initialize.push_back({ &samplerLocs_[2], 0, 2 }); + for (int i = 0; i < MAX_TEXTURE_SLOTS; ++i) + initialize.push_back({ &samplerLocs_[i], 0, i }); program_ = render_->CreateProgram(linkShaders, semantics, queries, initialize, false); return true; }