From 577e3375e218dafb089ea4ed53c64179f7ebfee5 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Fri, 11 Sep 2020 11:12:32 +0800 Subject: [PATCH] update binding mapping info (#2777) --- native/CMakeLists.txt | 11 ++++-- .../cocos/bindings/manual/jsb_gfx_manual.cpp | 5 +++ native/cocos/renderer/core/gfx/GFXDef.h | 7 +--- native/cocos/renderer/core/gfx/GFXDevice.h | 2 +- .../cocos/renderer/gfx-gles2/GLES2Commands.cc | 10 ++--- .../cocos/renderer/gfx-gles2/GLES2Device.cc | 2 + .../renderer/gfx-gles2/GLES2StateCache.h | 14 ++++--- .../cocos/renderer/gfx-gles3/GLES3Commands.cc | 38 ++++++++++--------- .../cocos/renderer/gfx-gles3/GLES3Device.cc | 2 + .../renderer/gfx-gles3/GLES3StateCache.h | 21 ++++++---- .../renderer/gfx-metal/MTLPipelineState.mm | 2 +- .../platforms/android/app/build.gradle | 4 ++ 12 files changed, 73 insertions(+), 45 deletions(-) diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index 3fad5e1c1c..9e433e7d8d 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -1088,12 +1088,17 @@ if(CC_USE_VULKAN) else() target_compile_definitions(cocos2d PUBLIC VK_USE_PLATFORM_XCB_KHR) endif() +endif() -elseif(CC_USE_METAL) +if(CC_USE_METAL) target_compile_definitions(cocos2d PUBLIC CC_USE_METAL) -elseif(CC_USE_GLES3) +endif() + +if(CC_USE_GLES3) target_compile_definitions(cocos2d PUBLIC CC_USE_GLES3) -elseif(CC_USE_GLES2) +endif() + +if(CC_USE_GLES2) target_compile_definitions(cocos2d PUBLIC CC_USE_GLES2) endif() diff --git a/native/cocos/bindings/manual/jsb_gfx_manual.cpp b/native/cocos/bindings/manual/jsb_gfx_manual.cpp index 5a9e655560..5b62a19362 100644 --- a/native/cocos/bindings/manual/jsb_gfx_manual.cpp +++ b/native/cocos/bindings/manual/jsb_gfx_manual.cpp @@ -72,6 +72,11 @@ static bool js_gfx_BindingMappingInfo_constructor(se::State &s) { ok &= seval_to_std_vector(field, &cobj->samplerOffsets); } + json->getProperty("flexibleSet", &field); + if (!field.isUndefined()) { + ok &= seval_to_uint32(field, &cobj->flexibleSet); + } + if (!ok) { JSB_FREE(cobj); SE_REPORT_ERROR("Argument convertion error"); diff --git a/native/cocos/renderer/core/gfx/GFXDef.h b/native/cocos/renderer/core/gfx/GFXDef.h index 446ff43877..fda791298c 100755 --- a/native/cocos/renderer/core/gfx/GFXDef.h +++ b/native/cocos/renderer/core/gfx/GFXDef.h @@ -23,10 +23,7 @@ class Queue; class Window; class Context; -#define GFX_MAX_VERTEX_ATTRIBUTES 16 -#define GFX_MAX_TEXTURE_UNITS 16 #define GFX_MAX_ATTACHMENTS 4 -#define GFX_MAX_BUFFER_BINDINGS 24 #define GFX_INVALID_BINDING ((uint8_t)-1) #define GFX_INVALID_HANDLE ((uint)-1) #define MAX_INFLIGHT_BUFFER 3 @@ -666,8 +663,8 @@ typedef vector ColorList; * exact offsets for each descriptor type in each set. */ struct BindingMappingInfo { - vector bufferOffsets; - vector samplerOffsets; + vector bufferOffsets; + vector samplerOffsets; uint flexibleSet = 0u; }; diff --git a/native/cocos/renderer/core/gfx/GFXDevice.h b/native/cocos/renderer/core/gfx/GFXDevice.h index 05f8209381..8361d68e13 100755 --- a/native/cocos/renderer/core/gfx/GFXDevice.h +++ b/native/cocos/renderer/core/gfx/GFXDevice.h @@ -101,7 +101,7 @@ protected: uint _maxFragmentUniformVectors = 0u; uint _maxTextureUnits = 0u; uint _maxVertexTextureUnits = 0u; - uint _maxUniformBufferBindings = GFX_MAX_BUFFER_BINDINGS; + uint _maxUniformBufferBindings = 0u; uint _maxUniformBlockSize = 0u; uint _maxTextureSize = 0u; uint _maxCubeMapTextureSize = 0u; diff --git a/native/cocos/renderer/gfx-gles2/GLES2Commands.cc b/native/cocos/renderer/gfx-gles2/GLES2Commands.cc index 66707b088b..35077d33fa 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2Commands.cc +++ b/native/cocos/renderer/gfx-gles2/GLES2Commands.cc @@ -1049,7 +1049,7 @@ void GLES2CmdFuncCreateShader(GLES2Device *device, GLES2GPUShader *gpuShader) { for (uint i = 0u; i < gpuShader->samplers.size(); i++) { const UniformSampler &sampler = gpuShader->samplers[i]; - uint glLoc = glGetUniformLocation(gpuShader->glProgram, sampler.name.c_str()); + GLint glLoc = glGetUniformLocation(gpuShader->glProgram, sampler.name.c_str()); if (glLoc >= 0) { glActiveSamplers.push_back(gpuShader->glSamplers[i]); glActiveSamplerLocations.push_back(glLoc); @@ -1063,7 +1063,7 @@ void GLES2CmdFuncCreateShader(GLES2Device *device, GLES2GPUShader *gpuShader) { } if (glActiveSamplers.size()) { - bool usedTexUnits[GFX_MAX_TEXTURE_UNITS]{}; + vector usedTexUnits(device->getMaxTextureUnits(), false); // try to reuse existing mappings first for (uint i = 0u; i < glActiveSamplers.size(); i++) { GLES2GPUUniformSampler &glSampler = glActiveSamplers[i]; @@ -1147,7 +1147,7 @@ void GLES2CmdFuncCreateInputAssembler(GLES2Device *device, GLES2GPUInputAssemble } } - uint streamOffsets[GFX_MAX_VERTEX_ATTRIBUTES] = {0}; + vector streamOffsets(device->getMaxVertexAttributes(), 0u); gpuInputAssembler->glAttribs.resize(gpuInputAssembler->attributes.size()); for (size_t i = 0; i < gpuInputAssembler->glAttribs.size(); ++i) { @@ -1965,7 +1965,7 @@ void GLES2CmdFuncExecuteCmds(GLES2Device *device, GLES2CmdPackage *cmdPackage) { cache->glVAO = glVAO; } } else { - for (uint a = 0; a < GFX_MAX_VERTEX_ATTRIBUTES; ++a) { + for (uint a = 0; a < cache->glCurrentAttribLocs.size(); ++a) { cache->glCurrentAttribLocs[a] = false; } @@ -2003,7 +2003,7 @@ void GLES2CmdFuncExecuteCmds(GLES2Device *device, GLES2CmdPackage *cmdPackage) { } } - for (uint a = 0; a < GFX_MAX_VERTEX_ATTRIBUTES; ++a) { + for (uint a = 0; a < cache->glCurrentAttribLocs.size(); ++a) { if (cache->glEnabledAttribLocs[a] != cache->glCurrentAttribLocs[a]) { glDisableVertexAttribArray(a); cache->glEnabledAttribLocs[a] = false; diff --git a/native/cocos/renderer/gfx-gles2/GLES2Device.cc b/native/cocos/renderer/gfx-gles2/GLES2Device.cc index 16709c4505..849a73899f 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2Device.cc +++ b/native/cocos/renderer/gfx-gles2/GLES2Device.cc @@ -143,6 +143,8 @@ bool GLES2Device::initialize(const DeviceInfo &info) { glGetIntegerv(GL_STENCIL_BITS, (GLint *)&_stencilBits); _uboOffsetAlignment = 1u; + stateCache->initialize(_maxTextureUnits, _maxVertexAttributes); + return true; } diff --git a/native/cocos/renderer/gfx-gles2/GLES2StateCache.h b/native/cocos/renderer/gfx-gles2/GLES2StateCache.h index b17ce6c87e..1d5493b64d 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2StateCache.h +++ b/native/cocos/renderer/gfx-gles2/GLES2StateCache.h @@ -11,14 +11,12 @@ public: GLuint glArrayBuffer = 0; GLuint glElementArrayBuffer = 0; GLuint glUniformBuffer = 0; - GLuint glBindUBOs[GFX_MAX_BUFFER_BINDINGS] = {0}; GLuint glVAO = 0; uint texUint = 0; - GLuint glTextures[GFX_MAX_TEXTURE_UNITS] = {0}; - GLuint glSamplers[GFX_MAX_TEXTURE_UNITS] = {0}; + vector glTextures; GLuint glProgram = 0; - bool glEnabledAttribLocs[GFX_MAX_VERTEX_ATTRIBUTES] = {false}; - bool glCurrentAttribLocs[GFX_MAX_VERTEX_ATTRIBUTES] = {false}; + vector glEnabledAttribLocs; + vector glCurrentAttribLocs; GLuint glFramebuffer = 0; GLuint glReadFBO = 0; Viewport viewport; @@ -30,7 +28,11 @@ public: bool isStencilTestEnabled = false; map texUnitCacheMap; - GLES2StateCache() {} + void initialize(size_t texUnits, size_t vertexAttributes) { + glTextures.resize(texUnits, 0u); + glEnabledAttribLocs.resize(vertexAttributes, false); + glCurrentAttribLocs.resize(vertexAttributes, false); + } }; } // namespace gfx diff --git a/native/cocos/renderer/gfx-gles3/GLES3Commands.cc b/native/cocos/renderer/gfx-gles3/GLES3Commands.cc index a0199ebfff..f94be4f83f 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Commands.cc +++ b/native/cocos/renderer/gfx-gles3/GLES3Commands.cc @@ -574,8 +574,8 @@ void GLES3CmdFuncDestroyBuffer(GLES3Device *device, GLES3GPUBuffer *gpuBuffer) { device->stateCache->glElementArrayBuffer = 0; } } else if (gpuBuffer->usage & BufferUsageBit::UNIFORM) { - GLuint *ubo = device->stateCache->glBindUBOs; - for (auto i = 0; i < GFX_MAX_BUFFER_BINDINGS; i++) { + vector &ubo = device->stateCache->glBindUBOs; + for (size_t i = 0; i < ubo.size(); i++) { if (ubo[i] == gpuBuffer->glBuffer) { glBindBufferBase(GL_UNIFORM_BUFFER, i, 0); device->stateCache->glUniformBuffer = 0; @@ -1146,7 +1146,7 @@ void GLES3CmdFuncCreateShader(GLES3Device *device, GLES3GPUShader *gpuShader) { for (uint i = 0u; i < gpuShader->samplers.size(); i++) { const UniformSampler &sampler = gpuShader->samplers[i]; - uint glLoc = glGetUniformLocation(gpuShader->glProgram, sampler.name.c_str()); + GLint glLoc = glGetUniformLocation(gpuShader->glProgram, sampler.name.c_str()); if (glLoc >= 0) { glActiveSamplers.push_back(gpuShader->glSamplers[i]); glActiveSamplerLocations.push_back(glLoc); @@ -1160,7 +1160,7 @@ void GLES3CmdFuncCreateShader(GLES3Device *device, GLES3GPUShader *gpuShader) { } if (glActiveSamplers.size()) { - bool usedTexUnits[GFX_MAX_TEXTURE_UNITS]{}; + vector usedTexUnits(device->getMaxTextureUnits(), false); // try to reuse existing mappings first for (uint i = 0u; i < glActiveSamplers.size(); i++) { GLES3GPUUniformSampler &glSampler = glActiveSamplers[i]; @@ -1168,10 +1168,12 @@ void GLES3CmdFuncCreateShader(GLES3Device *device, GLES3GPUShader *gpuShader) { if (texUnitCacheMap.count(glSampler.name)) { uint cachedUnit = texUnitCacheMap[glSampler.name]; glSampler.glLoc = glActiveSamplerLocations[i]; - for (uint t = 0u, offset = 0u; t < glSampler.count; t++) { - while (usedTexUnits[cachedUnit + t + offset]) offset++; - glSampler.units.push_back(cachedUnit + t + offset); - usedTexUnits[cachedUnit + t + offset] = true; + for (uint t = 0u; t < glSampler.count; t++) { + while (usedTexUnits[cachedUnit]) { + cachedUnit = (cachedUnit + 1) % device->getMaxTextureUnits(); + } + glSampler.units.push_back(cachedUnit); + usedTexUnits[cachedUnit] = true; } } } @@ -1183,12 +1185,14 @@ void GLES3CmdFuncCreateShader(GLES3Device *device, GLES3GPUShader *gpuShader) { if (glSampler.glLoc < 0) { glSampler.glLoc = glActiveSamplerLocations[i]; for (uint t = 0u; t < glSampler.count; t++) { - while (usedTexUnits[unitIdx + t]) unitIdx++; - glSampler.units.push_back(unitIdx + t); - usedTexUnits[unitIdx + t] = true; - } - if (!texUnitCacheMap.count(glSampler.name)) { - texUnitCacheMap[glSampler.name] = unitIdx; + while (usedTexUnits[unitIdx]) { + unitIdx = (unitIdx + 1) % device->getMaxTextureUnits(); + } + if (!texUnitCacheMap.count(glSampler.name)) { + texUnitCacheMap[glSampler.name] = unitIdx; + } + glSampler.units.push_back(unitIdx); + usedTexUnits[unitIdx] = true; } } } @@ -1236,7 +1240,7 @@ void GLES3CmdFuncCreateInputAssembler(GLES3Device *device, GLES3GPUInputAssemble } } - uint streamOffsets[GFX_MAX_VERTEX_ATTRIBUTES] = {0}; + vector streamOffsets(device->getMaxVertexAttributes(), 0u); gpuInputAssembler->glAttribs.resize(gpuInputAssembler->attributes.size()); for (size_t i = 0; i < gpuInputAssembler->glAttribs.size(); ++i) { @@ -1915,7 +1919,7 @@ void GLES3CmdFuncExecuteCmds(GLES3Device *device, GLES3CmdPackage *cmdPackage) { cache->glVAO = glVAO; } } else { - for (uint a = 0; a < GFX_MAX_VERTEX_ATTRIBUTES; ++a) { + for (uint a = 0; a < cache->glCurrentAttribLocs.size(); ++a) { cache->glCurrentAttribLocs[a] = false; } @@ -1950,7 +1954,7 @@ void GLES3CmdFuncExecuteCmds(GLES3Device *device, GLES3CmdPackage *cmdPackage) { } } - for (uint a = 0; a < GFX_MAX_VERTEX_ATTRIBUTES; ++a) { + for (uint a = 0; a < cache->glCurrentAttribLocs.size(); ++a) { if (cache->glEnabledAttribLocs[a] != cache->glCurrentAttribLocs[a]) { glDisableVertexAttribArray(a); cache->glEnabledAttribLocs[a] = false; diff --git a/native/cocos/renderer/gfx-gles3/GLES3Device.cc b/native/cocos/renderer/gfx-gles3/GLES3Device.cc index d3f4b3faa4..7ebac5c3ce 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Device.cc +++ b/native/cocos/renderer/gfx-gles3/GLES3Device.cc @@ -139,6 +139,8 @@ bool GLES3Device::initialize(const DeviceInfo &info) { glGetIntegerv(GL_DEPTH_BITS, (GLint *)&_depthBits); glGetIntegerv(GL_STENCIL_BITS, (GLint *)&_stencilBits); + stateCache->initialize(_maxTextureUnits, _maxUniformBufferBindings, _maxVertexAttributes); + return true; } diff --git a/native/cocos/renderer/gfx-gles3/GLES3StateCache.h b/native/cocos/renderer/gfx-gles3/GLES3StateCache.h index 5a1795d9f0..52abd1d35d 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3StateCache.h +++ b/native/cocos/renderer/gfx-gles3/GLES3StateCache.h @@ -11,15 +11,15 @@ public: GLuint glArrayBuffer = 0; GLuint glElementArrayBuffer = 0; GLuint glUniformBuffer = 0; - GLuint glBindUBOs[GFX_MAX_BUFFER_BINDINGS] = {0}; - GLuint glBindUBOOffsets[GFX_MAX_BUFFER_BINDINGS] = {0}; + vector glBindUBOs; + vector glBindUBOOffsets; GLuint glVAO = 0; uint texUint = 0; - GLuint glTextures[GFX_MAX_TEXTURE_UNITS] = {0}; - GLuint glSamplers[GFX_MAX_TEXTURE_UNITS] = {0}; + vector glTextures; + vector glSamplers; GLuint glProgram = 0; - bool glEnabledAttribLocs[GFX_MAX_VERTEX_ATTRIBUTES] = {false}; - bool glCurrentAttribLocs[GFX_MAX_VERTEX_ATTRIBUTES] = {false}; + vector glEnabledAttribLocs; + vector glCurrentAttribLocs; GLuint glFramebuffer = 0; GLuint glReadFBO = 0; Viewport viewport; @@ -31,7 +31,14 @@ public: bool isStencilTestEnabled = false; map texUnitCacheMap; - GLES3StateCache() {} + void initialize(size_t texUnits, size_t bufferBindings, size_t vertexAttributes) { + glBindUBOs.resize(bufferBindings, 0u); + glBindUBOOffsets.resize(bufferBindings, 0u); + glTextures.resize(texUnits, 0u); + glSamplers.resize(texUnits, 0u); + glEnabledAttribLocs.resize(vertexAttributes, false); + glCurrentAttribLocs.resize(vertexAttributes, false); + } }; } // namespace gfx diff --git a/native/cocos/renderer/gfx-metal/MTLPipelineState.mm b/native/cocos/renderer/gfx-metal/MTLPipelineState.mm index 441bcf2b6b..9f03cf17dd 100644 --- a/native/cocos/renderer/gfx-metal/MTLPipelineState.mm +++ b/native/cocos/renderer/gfx-metal/MTLPipelineState.mm @@ -144,7 +144,7 @@ void CCMTLPipelineState::setVertexDescriptor(MTLRenderPipelineDescriptor *descri vector> layouts; unordered_map> map; - uint streamOffsets[GFX_MAX_VERTEX_ATTRIBUTES] = {0}; + vector streamOffsets(_device->getMaxVertexAttributes(), 0u); bool attributeFound = false; for (const auto &activeAttribute : activeAttributes) { attributeFound = false; diff --git a/native/templates/template-link/platforms/android/app/build.gradle b/native/templates/template-link/platforms/android/app/build.gradle index f90d3ddf14..ce9b9c76b5 100644 --- a/native/templates/template-link/platforms/android/app/build.gradle +++ b/native/templates/template-link/platforms/android/app/build.gradle @@ -29,6 +29,10 @@ android { jniLibs.srcDirs "../libs", 'libs' manifest.srcFile "AndroidManifest.xml" assets.srcDir "assets" + jniLibs { + // Vulkan validation layer + // srcDir "${android.ndkDirectory}/sources/third_party/vulkan/src/build-android/jniLibs" + } } externalNativeBuild {