From 069de61bdb4f899839b859b234cd84a3e46be13e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Tue, 13 Jan 2015 22:34:48 -0800 Subject: [PATCH] Combined sampler and compute binding. --- src/bgfx_p.h | 100 +++++++++++++++++++++-------------------- src/renderer_d3d11.cpp | 24 +++++----- src/renderer_d3d9.cpp | 10 ++--- src/renderer_gl.cpp | 27 ++++++----- 4 files changed, 86 insertions(+), 75 deletions(-) diff --git a/src/bgfx_p.h b/src/bgfx_p.h index ea0b4b6e6..0b1f2b8dc 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -1026,15 +1026,36 @@ namespace bgfx return info; } - private: - typedef stl::unordered_map UniformHashMap; - UniformHashMap m_uniforms; - }; + private: + typedef stl::unordered_map UniformHashMap; + UniformHashMap m_uniforms; + }; - struct Sampler + struct Binding { - uint32_t m_flags; + enum Enum + { + Image, + IndexBuffer, + VertexBuffer, + Texture, + + Count + }; + uint16_t m_idx; + uint8_t m_type; + + union + { + uint32_t m_flags; + struct + { + uint8_t m_format; + uint8_t m_access; + uint8_t m_mip; + } m_compute; + } m_un; }; struct RenderDraw @@ -1064,8 +1085,9 @@ namespace bgfx for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_TEXTURE_SAMPLERS; ++ii) { - m_sampler[ii].m_idx = invalidHandle; - m_sampler[ii].m_flags = 0; + m_bind[ii].m_idx = invalidHandle; + m_bind[ii].m_type = uint8_t(Binding::Texture); + m_bind[ii].m_un.m_flags = 0; } } @@ -1090,25 +1112,7 @@ namespace bgfx VertexDeclHandle m_vertexDecl; IndexBufferHandle m_indexBuffer; VertexBufferHandle m_instanceDataBuffer; - Sampler m_sampler[BGFX_CONFIG_MAX_TEXTURE_SAMPLERS]; - }; - - struct ComputeBinding - { - enum Enum - { - Image, - VertexBuffer, - IndexBuffer, - - Count - }; - - uint16_t m_idx; - uint8_t m_format; - uint8_t m_access; - uint8_t m_mip; - uint8_t m_type; + Binding m_bind[BGFX_CONFIG_MAX_TEXTURE_SAMPLERS]; }; struct RenderCompute @@ -1140,7 +1144,7 @@ namespace bgfx uint16_t m_num; uint8_t m_submitFlags; - ComputeBinding m_bind[BGFX_MAX_COMPUTE_BINDINGS]; + Binding m_bind[BGFX_MAX_COMPUTE_BINDINGS]; }; union RenderItem @@ -1382,9 +1386,9 @@ namespace bgfx void setTexture(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle, uint32_t _flags) { - Sampler& sampler = m_draw.m_sampler[_stage]; - sampler.m_idx = _handle.idx; - sampler.m_flags = (_flags&BGFX_SAMPLER_DEFAULT_FLAGS) ? BGFX_SAMPLER_DEFAULT_FLAGS : _flags; + Binding& sampler = m_draw.m_bind[_stage]; + sampler.m_idx = _handle.idx; + sampler.m_un.m_flags = (_flags&BGFX_SAMPLER_DEFAULT_FLAGS) ? BGFX_SAMPLER_DEFAULT_FLAGS : _flags; if (isValid(_sampler) && (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES) ) ) @@ -1396,32 +1400,32 @@ namespace bgfx void setBuffer(uint8_t _stage, IndexBufferHandle _handle, Access::Enum _access) { - ComputeBinding& bind = m_compute.m_bind[_stage]; + Binding& bind = m_compute.m_bind[_stage]; bind.m_idx = _handle.idx; - bind.m_format = 0; - bind.m_access = uint8_t(_access); - bind.m_mip = 0; - bind.m_type = uint8_t(ComputeBinding::IndexBuffer); + bind.m_type = uint8_t(Binding::IndexBuffer); + bind.m_un.m_compute.m_format = 0; + bind.m_un.m_compute.m_access = uint8_t(_access); + bind.m_un.m_compute.m_mip = 0; } void setBuffer(uint8_t _stage, VertexBufferHandle _handle, Access::Enum _access) { - ComputeBinding& bind = m_compute.m_bind[_stage]; - bind.m_idx = _handle.idx; - bind.m_format = 0; - bind.m_access = uint8_t(_access); - bind.m_mip = 0; - bind.m_type = uint8_t(ComputeBinding::VertexBuffer); + Binding& bind = m_compute.m_bind[_stage]; + bind.m_idx = _handle.idx; + bind.m_type = uint8_t(Binding::VertexBuffer); + bind.m_un.m_compute.m_format = 0; + bind.m_un.m_compute.m_access = uint8_t(_access); + bind.m_un.m_compute.m_mip = 0; } void setImage(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle, uint8_t _mip, TextureFormat::Enum _format, Access::Enum _access) { - ComputeBinding& bind = m_compute.m_bind[_stage]; - bind.m_idx = _handle.idx; - bind.m_format = uint8_t(_format); - bind.m_access = uint8_t(_access); - bind.m_mip = _mip; - bind.m_type = uint8_t(ComputeBinding::Image); + Binding& bind = m_compute.m_bind[_stage]; + bind.m_idx = _handle.idx; + bind.m_type = uint8_t(Binding::Image); + bind.m_un.m_compute.m_format = uint8_t(_format); + bind.m_un.m_compute.m_access = uint8_t(_access); + bind.m_un.m_compute.m_mip = _mip; if (isValid(_sampler) && (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES) ) ) diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index 20abdfb8a..6caebb1ce 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -3200,15 +3200,15 @@ namespace bgfx for (uint32_t ii = 0; ii < BGFX_MAX_COMPUTE_BINDINGS; ++ii) { - const ComputeBinding& bind = compute.m_bind[ii]; + const Binding& bind = compute.m_bind[ii]; if (invalidHandle != bind.m_idx) { switch (bind.m_type) { - case ComputeBinding::Image: + case Binding::Image: { const TextureD3D11& texture = m_textures[bind.m_idx]; - if (Access::Read != bind.m_access) + if (Access::Read != bind.m_un.m_compute.m_access) { uav[ii] = texture.m_uav; } @@ -3220,14 +3220,14 @@ namespace bgfx } break; - case ComputeBinding::IndexBuffer: - case ComputeBinding::VertexBuffer: + case Binding::IndexBuffer: + case Binding::VertexBuffer: { - const BufferD3D11& buffer = ComputeBinding::IndexBuffer == bind.m_type + const BufferD3D11& buffer = Binding::IndexBuffer == bind.m_type ? m_indexBuffers[bind.m_idx] : m_vertexBuffers[bind.m_idx] ; - if (Access::Read != bind.m_access) + if (Access::Read != bind.m_un.m_compute.m_access) { uav[ii] = buffer.m_uav; } @@ -3443,16 +3443,16 @@ namespace bgfx uint32_t changes = 0; for (uint32_t stage = 0; stage < BGFX_CONFIG_MAX_TEXTURE_SAMPLERS; ++stage) { - const Sampler& sampler = draw.m_sampler[stage]; - Sampler& current = currentState.m_sampler[stage]; - if (current.m_idx != sampler.m_idx - || current.m_flags != sampler.m_flags + const Binding& sampler = draw.m_bind[stage]; + Binding& current = currentState.m_bind[stage]; + if (current.m_idx != sampler.m_idx + || current.m_un.m_flags != sampler.m_un.m_flags || programChanged) { if (invalidHandle != sampler.m_idx) { TextureD3D11& texture = m_textures[sampler.m_idx]; - texture.commit(stage, sampler.m_flags); + texture.commit(stage, sampler.m_un.m_flags); } else { diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp index a594c76c1..82e4a3d0b 100644 --- a/src/renderer_d3d9.cpp +++ b/src/renderer_d3d9.cpp @@ -3168,15 +3168,15 @@ namespace bgfx { for (uint32_t stage = 0; stage < BGFX_CONFIG_MAX_TEXTURE_SAMPLERS; ++stage) { - const Sampler& sampler = draw.m_sampler[stage]; - Sampler& current = currentState.m_sampler[stage]; - if (current.m_idx != sampler.m_idx - || current.m_flags != sampler.m_flags + const Binding& sampler = draw.m_bind[stage]; + Binding& current = currentState.m_bind[stage]; + if (current.m_idx != sampler.m_idx + || current.m_un.m_flags != sampler.m_un.m_flags || programChanged) { if (invalidHandle != sampler.m_idx) { - m_textures[sampler.m_idx].commit(stage, sampler.m_flags); + m_textures[sampler.m_idx].commit(stage, sampler.m_un.m_flags); } else { diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index a6d67894f..edb45c657 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -4373,20 +4373,27 @@ namespace bgfx GLbitfield barrier = 0; for (uint32_t ii = 0; ii < BGFX_MAX_COMPUTE_BINDINGS; ++ii) { - const ComputeBinding& bind = compute.m_bind[ii]; + const Binding& bind = compute.m_bind[ii]; if (invalidHandle != bind.m_idx) { switch (bind.m_type) { - case ComputeBinding::Image: + case Binding::Image: { const TextureGL& texture = m_textures[bind.m_idx]; - GL_CHECK(glBindImageTexture(ii, texture.m_id, bind.m_mip, GL_FALSE, 0, s_access[bind.m_access], s_imageFormat[bind.m_format]) ); + GL_CHECK(glBindImageTexture(ii + , texture.m_id + , bind.m_un.m_compute.m_mip + , GL_FALSE + , 0 + , s_access[bind.m_un.m_compute.m_access] + , s_imageFormat[bind.m_un.m_compute.m_format]) + ); barrier |= GL_SHADER_IMAGE_ACCESS_BARRIER_BIT; } break; - case ComputeBinding::IndexBuffer: + case Binding::IndexBuffer: { const IndexBufferGL& buffer = m_indexBuffers[bind.m_idx]; GL_CHECK(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, ii, buffer.m_id)); @@ -4394,7 +4401,7 @@ namespace bgfx } break; - case ComputeBinding::VertexBuffer: + case Binding::VertexBuffer: { const VertexBufferGL& buffer = m_vertexBuffers[bind.m_idx]; GL_CHECK(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, ii, buffer.m_id)); @@ -4747,16 +4754,16 @@ namespace bgfx { for (uint32_t stage = 0; stage < BGFX_CONFIG_MAX_TEXTURE_SAMPLERS; ++stage) { - const Sampler& sampler = draw.m_sampler[stage]; - Sampler& current = currentState.m_sampler[stage]; - if (current.m_idx != sampler.m_idx - || current.m_flags != sampler.m_flags + const Binding& sampler = draw.m_bind[stage]; + Binding& current = currentState.m_bind[stage]; + if (current.m_idx != sampler.m_idx + || current.m_un.m_flags != sampler.m_un.m_flags || programChanged) { if (invalidHandle != sampler.m_idx) { TextureGL& texture = m_textures[sampler.m_idx]; - texture.commit(stage, sampler.m_flags); + texture.commit(stage, sampler.m_un.m_flags); } }