diff --git a/include/bgfx/bgfx.h b/include/bgfx/bgfx.h index 047ab10b7..3430764e8 100644 --- a/include/bgfx/bgfx.h +++ b/include/bgfx/bgfx.h @@ -866,6 +866,9 @@ namespace bgfx /// struct Attachment { + void init(TextureHandle _handle, Access::Enum _access = Access::Write, uint16_t _layer = 0, uint16_t _mip = 0, uint8_t _resolve = BGFX_RESOLVE_AUTO_GEN_MIPS); + + Access::Enum access; //!< TextureHandle handle; //!< Texture handle. uint16_t mip; //!< Mip level. uint16_t layer; //!< Cubemap side or depth layer/slice. diff --git a/include/bgfx/c99/bgfx.h b/include/bgfx/c99/bgfx.h index df4e6554a..58a583ff1 100644 --- a/include/bgfx/c99/bgfx.h +++ b/include/bgfx/c99/bgfx.h @@ -483,6 +483,7 @@ typedef struct bgfx_uniform_info_s /**/ typedef struct bgfx_attachment_s { + bgfx_access_t access; bgfx_texture_handle_t handle; uint16_t mip; uint16_t layer; diff --git a/include/bgfx/defines.h b/include/bgfx/defines.h index a043b0abe..d6419e47a 100644 --- a/include/bgfx/defines.h +++ b/include/bgfx/defines.h @@ -6,7 +6,7 @@ #ifndef BGFX_DEFINES_H_HEADER_GUARD #define BGFX_DEFINES_H_HEADER_GUARD -#define BGFX_API_VERSION UINT32_C(89) +#define BGFX_API_VERSION UINT32_C(90) /// Color RGB/alpha/depth write. When it's not specified write will be disabled. #define BGFX_STATE_WRITE_R UINT64_C(0x0000000000000001) //!< Enable R write. diff --git a/src/bgfx.cpp b/src/bgfx.cpp index a632b8c4d..c3df9abe2 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -3104,6 +3104,15 @@ namespace bgfx limits.transientIbSize = BGFX_CONFIG_TRANSIENT_INDEX_BUFFER_SIZE; } + void Attachment::init(TextureHandle _handle, Access::Enum _access, uint16_t _layer, uint16_t _mip, uint8_t _resolve) + { + access = _access; + handle = _handle; + mip = _mip; + layer = _layer; + resolve = _resolve; + } + bool init(const Init& _init) { if (NULL != s_ctx) @@ -4288,10 +4297,7 @@ namespace bgfx for (uint8_t ii = 0; ii < _num; ++ii) { Attachment& at = attachment[ii]; - at.handle = _handles[ii]; - at.mip = 0; - at.layer = 0; - at.resolve = BGFX_RESOLVE_AUTO_GEN_MIPS; + at.init(_handles[ii], Access::Write, 0, 0, BGFX_RESOLVE_AUTO_GEN_MIPS); } return createFrameBuffer(_num, attachment, _destroyTextures); } diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index 8d1d3b12f..42374f490 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -4673,10 +4673,11 @@ namespace bgfx { namespace d3d11 m_num = 0; for (uint32_t ii = 0; ii < m_numTh; ++ii) { - TextureHandle handle = m_attachment[ii].handle; - if (isValid(handle) ) + const Attachment& at = m_attachment[ii]; + + if (isValid(at.handle) ) { - const TextureD3D11& texture = s_renderD3D11->m_textures[handle.idx]; + const TextureD3D11& texture = s_renderD3D11->m_textures[at.handle.idx]; if (0 == m_width) { @@ -4722,7 +4723,7 @@ namespace bgfx { namespace d3d11 : D3D11_DSV_DIMENSION_TEXTURE2D ; dsvDesc.Flags = 0; - dsvDesc.Texture2D.MipSlice = m_attachment[ii].mip; + dsvDesc.Texture2D.MipSlice = at.mip; DX_CHECK(s_renderD3D11->m_device->CreateDepthStencilView( NULL == texture.m_rt ? texture.m_ptr : texture.m_rt , &dsvDesc @@ -4739,14 +4740,14 @@ namespace bgfx { namespace d3d11 { dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY; dsvDesc.Texture2DMSArray.ArraySize = 1; - dsvDesc.Texture2DMSArray.FirstArraySlice = m_attachment[ii].layer; + dsvDesc.Texture2DMSArray.FirstArraySlice = at.layer; } else { dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY; dsvDesc.Texture2DArray.ArraySize = 1; - dsvDesc.Texture2DArray.FirstArraySlice = m_attachment[ii].layer; - dsvDesc.Texture2DArray.MipSlice = m_attachment[ii].mip; + dsvDesc.Texture2DArray.FirstArraySlice = at.layer; + dsvDesc.Texture2DArray.MipSlice = at.mip; } dsvDesc.Flags = 0; DX_CHECK(s_renderD3D11->m_device->CreateDepthStencilView(texture.m_ptr, &dsvDesc, &m_dsv) ); @@ -4754,7 +4755,7 @@ namespace bgfx { namespace d3d11 break; } } - else + else if (Access::Write == at.access) { D3D11_RENDER_TARGET_VIEW_DESC desc; desc.Format = texture.getSrvFormat(); @@ -4767,7 +4768,7 @@ namespace bgfx { namespace d3d11 if (1 < texture.m_depth) { desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY; - desc.Texture2DMSArray.FirstArraySlice = m_attachment[ii].layer; + desc.Texture2DMSArray.FirstArraySlice = at.layer; desc.Texture2DMSArray.ArraySize = 1; } else @@ -4780,14 +4781,14 @@ namespace bgfx { namespace d3d11 if (1 < texture.m_depth) { desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY; - desc.Texture2DArray.FirstArraySlice = m_attachment[ii].layer; + desc.Texture2DArray.FirstArraySlice = at.layer; desc.Texture2DArray.ArraySize = 1; - desc.Texture2DArray.MipSlice = m_attachment[ii].mip; + desc.Texture2DArray.MipSlice = at.mip; } else { desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; - desc.Texture2D.MipSlice = m_attachment[ii].mip; + desc.Texture2D.MipSlice = at.mip; } } @@ -4803,23 +4804,23 @@ namespace bgfx { namespace d3d11 { desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY; desc.Texture2DMSArray.ArraySize = 1; - desc.Texture2DMSArray.FirstArraySlice = m_attachment[ii].layer; + desc.Texture2DMSArray.FirstArraySlice = at.layer; } else { desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY; desc.Texture2DArray.ArraySize = 1; - desc.Texture2DArray.FirstArraySlice = m_attachment[ii].layer; - desc.Texture2DArray.MipSlice = m_attachment[ii].mip; + desc.Texture2DArray.FirstArraySlice = at.layer; + desc.Texture2DArray.MipSlice = at.mip; } DX_CHECK(s_renderD3D11->m_device->CreateRenderTargetView(texture.m_ptr, &desc, &m_rtv[m_num]) ); break; case TextureD3D11::Texture3D: desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE3D; - desc.Texture3D.MipSlice = m_attachment[ii].mip; + desc.Texture3D.MipSlice = at.mip; desc.Texture3D.WSize = 1; - desc.Texture3D.FirstWSlice = m_attachment[ii].layer; + desc.Texture3D.FirstWSlice = at.layer; DX_CHECK(s_renderD3D11->m_device->CreateRenderTargetView(texture.m_ptr, &desc, &m_rtv[m_num]) ); break; } @@ -4827,6 +4828,10 @@ namespace bgfx { namespace d3d11 DX_CHECK(s_renderD3D11->m_device->CreateShaderResourceView(texture.m_ptr, NULL, &m_srv[m_num]) ); m_num++; } + else + { + BX_CHECK(false, ""); + } } } } @@ -4839,6 +4844,7 @@ namespace bgfx { namespace d3d11 for (uint32_t ii = 0; ii < m_numTh; ++ii) { const Attachment& at = m_attachment[ii]; + if (isValid(at.handle) ) { const TextureD3D11& texture = s_renderD3D11->m_textures[at.handle.idx]; diff --git a/src/renderer_d3d12.cpp b/src/renderer_d3d12.cpp index d6dc1cd67..d577e63b6 100644 --- a/src/renderer_d3d12.cpp +++ b/src/renderer_d3d12.cpp @@ -5070,10 +5070,11 @@ namespace bgfx { namespace d3d12 m_num = 0; for (uint32_t ii = 0; ii < m_numTh; ++ii) { - TextureHandle handle = m_attachment[ii].handle; - if (isValid(handle) ) + const Attachment& at = m_attachment[ii]; + + if (isValid(at.handle) ) { - const TextureD3D12& texture = s_renderD3D12->m_textures[handle.idx]; + const TextureD3D12& texture = s_renderD3D12->m_textures[at.handle.idx]; if (0 == m_width) { @@ -5085,7 +5086,7 @@ namespace bgfx { namespace d3d12 if (bimg::isDepth(bimg::TextureFormat::Enum(texture.m_textureFormat) ) ) { BX_CHECK(!isValid(m_depth), ""); - m_depth = handle; + m_depth = at.handle; D3D12_CPU_DESCRIPTOR_HANDLE dsvDescriptor = getCPUHandleHeapStart(s_renderD3D12->m_dsvDescriptorHeap); uint32_t dsvDescriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV); dsvDescriptor.ptr += (1 + fbhIdx) * dsvDescriptorSize; @@ -5116,9 +5117,9 @@ namespace bgfx { namespace d3d12 , NULL ); } - else + else if (Access::Write == at.access) { - m_texture[m_num] = handle; + m_texture[m_num] = at.handle; D3D12_CPU_DESCRIPTOR_HANDLE rtv = { rtvDescriptor.ptr + m_num * rtvDescriptorSize }; D3D12_RENDER_TARGET_VIEW_DESC desc; @@ -5135,7 +5136,7 @@ namespace bgfx { namespace d3d12 // else { desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D; - desc.Texture2D.MipSlice = m_attachment[ii].mip; + desc.Texture2D.MipSlice = at.mip; desc.Texture2D.PlaneSlice = 0; } break; @@ -5145,23 +5146,23 @@ namespace bgfx { namespace d3d12 // { // desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DMSARRAY; // desc.Texture2DMSArray.ArraySize = 1; -// desc.Texture2DMSArray.FirstArraySlice = m_attachment[ii].layer; +// desc.Texture2DMSArray.FirstArraySlice = at.layer; // } // else { desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DARRAY; desc.Texture2DArray.ArraySize = 1; - desc.Texture2DArray.FirstArraySlice = m_attachment[ii].layer; - desc.Texture2DArray.MipSlice = m_attachment[ii].mip; + desc.Texture2DArray.FirstArraySlice = at.layer; + desc.Texture2DArray.MipSlice = at.mip; desc.Texture2DArray.PlaneSlice = 0; } break; case TextureD3D12::Texture3D: desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE3D; - desc.Texture3D.MipSlice = m_attachment[ii].mip; + desc.Texture3D.MipSlice = at.mip; desc.Texture3D.WSize = 1; - desc.Texture3D.FirstWSlice = m_attachment[ii].layer; + desc.Texture3D.FirstWSlice = at.layer; break; } @@ -5180,6 +5181,10 @@ namespace bgfx { namespace d3d12 m_num++; } + else + { + BX_CHECK(false, ""); + } } } } @@ -5192,6 +5197,7 @@ namespace bgfx { namespace d3d12 for (uint32_t ii = 0; ii < m_numTh; ++ii) { const Attachment& at = m_attachment[ii]; + if (isValid(at.handle) ) { const TextureD3D12& texture = s_renderD3D12->m_textures[at.handle.idx]; diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp index 18c4adcb6..21dbef68f 100644 --- a/src/renderer_d3d9.cpp +++ b/src/renderer_d3d9.cpp @@ -3197,10 +3197,11 @@ namespace bgfx { namespace d3d9 for (uint32_t ii = 0; ii < _num; ++ii) { - TextureHandle handle = m_attachment[ii].handle; - if (isValid(handle) ) + const Attachment& at = m_attachment[ii]; + + if (isValid(at.handle) ) { - const TextureD3D9& texture = s_renderD3D9->m_textures[handle.idx]; + const TextureD3D9& texture = s_renderD3D9->m_textures[at.handle.idx]; if (NULL != texture.m_surface) { @@ -3209,7 +3210,7 @@ namespace bgfx { namespace d3d9 } else { - m_surface[ii] = texture.getSurface(uint8_t(m_attachment[ii].layer), uint8_t(m_attachment[ii].mip) ); + m_surface[ii] = texture.getSurface(uint8_t(at.layer), uint8_t(at.mip) ); } if (0 == m_num) @@ -3386,19 +3387,24 @@ namespace bgfx { namespace d3d9 { for (uint32_t ii = 0, num = m_numTh; ii < num; ++ii) { - TextureHandle th = m_attachment[ii].handle; + const Attachment& at = m_attachment[ii]; - if (isValid(th) ) + if (isValid(at.handle) ) { - TextureD3D9& texture = s_renderD3D9->m_textures[th.idx]; + TextureD3D9& texture = s_renderD3D9->m_textures[at.handle.idx]; + if (NULL != texture.m_surface) { m_surface[ii] = texture.m_surface; m_surface[ii]->AddRef(); } + else if (Access::Write == at.access) + { + m_surface[ii] = texture.getSurface(uint8_t(at.layer), uint8_t(at.mip) ); + } else { - m_surface[ii] = texture.getSurface(uint8_t(m_attachment[ii].layer), uint8_t(m_attachment[ii].mip) ); + BX_CHECK(false, ""); } } } diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index 46239764f..1bfa5d490 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -5875,15 +5875,16 @@ BX_TRACE("%d, %d, %d, %s", _array, _srgb, _mipAutogen, getName(_format) ); uint32_t colorIdx = 0; for (uint32_t ii = 0; ii < m_numTh; ++ii) { - TextureHandle handle = m_attachment[ii].handle; - if (isValid(handle) ) + const Attachment& at = m_attachment[ii]; + + if (isValid(at.handle) ) { - const TextureGL& texture = s_renderGL->m_textures[handle.idx]; + const TextureGL& texture = s_renderGL->m_textures[at.handle.idx]; if (0 == colorIdx) { - m_width = bx::uint32_max(texture.m_width >> m_attachment[ii].mip, 1); - m_height = bx::uint32_max(texture.m_height >> m_attachment[ii].mip, 1); + m_width = bx::uint32_max(texture.m_width >> at.mip, 1); + m_height = bx::uint32_max(texture.m_height >> at.mip, 1); } GLenum attachment = GL_COLOR_ATTACHMENT0 + colorIdx; @@ -5936,7 +5937,7 @@ BX_TRACE("%d, %d, %d, %s", _array, _srgb, _mipAutogen, getName(_format) ); ) ); } } - else + else if (Access::Write == at.access) { if (1 < texture.m_depth && !texture.isCubeMap()) @@ -5944,14 +5945,14 @@ BX_TRACE("%d, %d, %d, %s", _array, _srgb, _mipAutogen, getName(_format) ); GL_CHECK(glFramebufferTextureLayer(GL_FRAMEBUFFER , attachment , texture.m_id - , m_attachment[ii].mip - , m_attachment[ii].layer + , at.mip + , at.layer ) ); } else { GLenum target = texture.isCubeMap() - ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + m_attachment[ii].layer + ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + at.layer : texture.m_target ; @@ -5959,10 +5960,14 @@ BX_TRACE("%d, %d, %d, %s", _array, _srgb, _mipAutogen, getName(_format) ); , attachment , target , texture.m_id - , m_attachment[ii].mip + , at.mip ) ); } } + else + { + BX_CHECK(false, ""); + } needResolve |= (0 != texture.m_rbo) && (0 != texture.m_id); } @@ -6000,10 +6005,11 @@ BX_TRACE("%d, %d, %d, %s", _array, _srgb, _mipAutogen, getName(_format) ); colorIdx = 0; for (uint32_t ii = 0; ii < m_numTh; ++ii) { - TextureHandle handle = m_attachment[ii].handle; - if (isValid(handle) ) + const Attachment& at = m_attachment[ii]; + + if (isValid(at.handle) ) { - const TextureGL& texture = s_renderGL->m_textures[handle.idx]; + const TextureGL& texture = s_renderGL->m_textures[at.handle.idx]; if (0 != texture.m_id) { @@ -6013,7 +6019,7 @@ BX_TRACE("%d, %d, %d, %s", _array, _srgb, _mipAutogen, getName(_format) ); ++colorIdx; GLenum target = texture.isCubeMap() - ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + m_attachment[ii].layer + ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + at.layer : texture.m_target ; @@ -6021,7 +6027,7 @@ BX_TRACE("%d, %d, %d, %s", _array, _srgb, _mipAutogen, getName(_format) ); , attachment , target , texture.m_id - , m_attachment[ii].mip + , at.mip ) ); } } @@ -6076,10 +6082,11 @@ BX_TRACE("%d, %d, %d, %s", _array, _srgb, _mipAutogen, getName(_format) ); uint32_t colorIdx = 0; for (uint32_t ii = 0; ii < m_numTh; ++ii) { - TextureHandle handle = m_attachment[ii].handle; - if (isValid(handle) ) + const Attachment& at = m_attachment[ii]; + + if (isValid(at.handle) ) { - const TextureGL& texture = s_renderGL->m_textures[handle.idx]; + const TextureGL& texture = s_renderGL->m_textures[at.handle.idx]; bimg::TextureFormat::Enum format = bimg::TextureFormat::Enum(texture.m_textureFormat); if (!bimg::isDepth(format) ) @@ -6112,6 +6119,7 @@ BX_TRACE("%d, %d, %d, %s", _array, _srgb, _mipAutogen, getName(_format) ); for (uint32_t ii = 0; ii < m_numTh; ++ii) { const Attachment& at = m_attachment[ii]; + if (isValid(at.handle) ) { const TextureGL& texture = s_renderGL->m_textures[at.handle.idx];