Added ability to specify sampler flags for samplers used in compute shader.

This commit is contained in:
Branimir Karadžić 2018-11-14 17:04:24 -08:00
parent 6a11acc270
commit a50b76a9a3
7 changed files with 87 additions and 85 deletions

View File

@ -1479,24 +1479,12 @@ namespace bgfx
Count
};
uint32_t m_samplerFlags;
uint16_t m_idx;
uint8_t m_type;
union
{
struct
{
uint32_t m_textureFlags;
} m_draw;
struct
{
uint8_t m_format;
uint8_t m_access;
uint8_t m_mip;
} m_compute;
} m_un;
uint8_t m_format;
uint8_t m_access;
uint8_t m_mip;
};
struct Stream
@ -1522,7 +1510,7 @@ namespace bgfx
Binding& bind = m_bind[ii];
bind.m_idx = kInvalidHandle;
bind.m_type = 0;
bind.m_un.m_draw.m_textureFlags = 0;
bind.m_samplerFlags = 0;
}
};
@ -2326,7 +2314,7 @@ namespace bgfx
Binding& bind = m_bind.m_bind[_stage];
bind.m_idx = _handle.idx;
bind.m_type = uint8_t(Binding::Texture);
bind.m_un.m_draw.m_textureFlags = (_flags&BGFX_SAMPLER_INTERNAL_DEFAULT)
bind.m_samplerFlags = (_flags&BGFX_SAMPLER_INTERNAL_DEFAULT)
? BGFX_SAMPLER_INTERNAL_DEFAULT
: _flags
;
@ -2343,9 +2331,9 @@ namespace bgfx
Binding& bind = m_bind.m_bind[_stage];
bind.m_idx = _handle.idx;
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;
bind.m_format = 0;
bind.m_access = uint8_t(_access);
bind.m_mip = 0;
}
void setBuffer(uint8_t _stage, VertexBufferHandle _handle, Access::Enum _access)
@ -2353,9 +2341,9 @@ namespace bgfx
Binding& bind = m_bind.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;
bind.m_format = 0;
bind.m_access = uint8_t(_access);
bind.m_mip = 0;
}
void setImage(uint8_t _stage, TextureHandle _handle, uint8_t _mip, Access::Enum _access, TextureFormat::Enum _format)
@ -2363,9 +2351,9 @@ namespace bgfx
Binding& bind = m_bind.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;
bind.m_format = uint8_t(_format);
bind.m_access = uint8_t(_access);
bind.m_mip = _mip;
}
void discard()

View File

@ -4514,9 +4514,7 @@ namespace bgfx { namespace d3d11
: uint32_t(m_flags)
;
uint32_t index = (flags & BGFX_SAMPLER_BORDER_COLOR_MASK) >> BGFX_SAMPLER_BORDER_COLOR_SHIFT;
ts.m_sampler[_stage] = s_renderD3D11->getSamplerState(flags
, _palette[index])
;
ts.m_sampler[_stage] = s_renderD3D11->getSamplerState(flags, _palette[index]);
}
void TextureD3D11::resolve(uint8_t _resolve) const
@ -5487,35 +5485,39 @@ namespace bgfx { namespace d3d11
}
BX_UNUSED(programChanged);
ID3D11UnorderedAccessView* uav[BGFX_MAX_COMPUTE_BINDINGS] = {};
ID3D11ShaderResourceView* srv[BGFX_MAX_COMPUTE_BINDINGS] = {};
ID3D11SamplerState* sampler[BGFX_MAX_COMPUTE_BINDINGS] = {};
for (uint32_t ii = 0; ii < maxComputeBindings; ++ii)
for (uint8_t stage = 0; stage < maxComputeBindings; ++stage)
{
const Binding& bind = renderBind.m_bind[ii];
const Binding& bind = renderBind.m_bind[stage];
if (kInvalidHandle != bind.m_idx)
{
switch (bind.m_type)
{
case Binding::Image:
case Binding::Texture:
{
TextureD3D11& texture = m_textures[bind.m_idx];
if (Access::Read != bind.m_un.m_compute.m_access)
if (Access::Read != bind.m_access)
{
uav[ii] = 0 == bind.m_un.m_compute.m_mip
uav[stage] = 0 == bind.m_mip
? texture.m_uav
: s_renderD3D11->getCachedUav(texture.getHandle(), bind.m_un.m_compute.m_mip)
: s_renderD3D11->getCachedUav(texture.getHandle(), bind.m_mip)
;
}
else
{
srv[ii] = s_renderD3D11->getCachedSrv(texture.getHandle(), bind.m_un.m_compute.m_mip, true);
sampler[ii] = s_renderD3D11->getSamplerState(uint32_t(texture.m_flags), NULL);
m_textureStage.m_srv[stage] = s_renderD3D11->getCachedSrv(texture.getHandle(), bind.m_mip, true);
m_textureStage.m_sampler[stage] = s_renderD3D11->getSamplerState(uint32_t(texture.m_flags), NULL);
}
}
break;
case Binding::Texture:
{
TextureD3D11& texture = m_textures[bind.m_idx];
texture.commit(stage, bind.m_samplerFlags, _render->m_colorPalette);
}
break;
case Binding::IndexBuffer:
case Binding::VertexBuffer:
{
@ -5523,13 +5525,13 @@ namespace bgfx { namespace d3d11
? m_indexBuffers[bind.m_idx]
: m_vertexBuffers[bind.m_idx]
;
if (Access::Read != bind.m_un.m_compute.m_access)
if (Access::Read != bind.m_access)
{
uav[ii] = buffer.m_uav;
uav[stage] = buffer.m_uav;
}
else
{
srv[ii] = buffer.m_srv;
m_textureStage.m_srv[stage] = buffer.m_srv;
}
}
break;
@ -5544,8 +5546,8 @@ namespace bgfx { namespace d3d11
}
deviceCtx->CSSetUnorderedAccessViews(0, maxComputeBindings, uav, NULL);
deviceCtx->CSSetShaderResources(0, maxTextureSamplers, srv);
deviceCtx->CSSetSamplers(0, maxTextureSamplers, sampler);
deviceCtx->CSSetShaderResources(0, maxTextureSamplers, m_textureStage.m_srv);
deviceCtx->CSSetSamplers(0, maxTextureSamplers, m_textureStage.m_sampler);
if (isValid(compute.m_indirectBuffer) )
{
@ -5809,9 +5811,9 @@ namespace bgfx { namespace d3d11
{
const Binding& bind = renderBind.m_bind[stage];
Binding& current = currentBind.m_bind[stage];
if (current.m_idx != bind.m_idx
|| current.m_type != bind.m_type
|| current.m_un.m_draw.m_textureFlags != bind.m_un.m_draw.m_textureFlags
if (current.m_idx != bind.m_idx
|| current.m_type != bind.m_type
|| current.m_samplerFlags != bind.m_samplerFlags
|| programChanged)
{
if (kInvalidHandle != bind.m_idx)
@ -5821,7 +5823,7 @@ namespace bgfx { namespace d3d11
case Binding::Texture:
{
TextureD3D11& texture = m_textures[bind.m_idx];
texture.commit(stage, bind.m_un.m_draw.m_textureFlags, _render->m_colorPalette);
texture.commit(stage, bind.m_samplerFlags, _render->m_colorPalette);
}
break;

View File

@ -5877,32 +5877,44 @@ namespace bgfx { namespace d3d12
D3D12_GPU_DESCRIPTOR_HANDLE srvHandle[BGFX_MAX_COMPUTE_BINDINGS] = {};
uint32_t samplerFlags[BGFX_MAX_COMPUTE_BINDINGS] = {};
for (uint32_t ii = 0; ii < maxComputeBindings; ++ii)
for (uint8_t stage = 0; stage < maxComputeBindings; ++stage)
{
const Binding& bind = renderBind.m_bind[ii];
const Binding& bind = renderBind.m_bind[stage];
if (kInvalidHandle != bind.m_idx)
{
switch (bind.m_type)
{
case Binding::Image:
case Binding::Texture:
{
TextureD3D12& texture = m_textures[bind.m_idx];
if (Access::Read != bind.m_un.m_compute.m_access)
if (Access::Read != bind.m_access)
{
texture.setState(m_commandList, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
scratchBuffer.allocUav(srvHandle[ii], texture, bind.m_un.m_compute.m_mip);
scratchBuffer.allocUav(srvHandle[stage], texture, bind.m_mip);
}
else
{
texture.setState(m_commandList, D3D12_RESOURCE_STATE_GENERIC_READ);
scratchBuffer.allocSrv(srvHandle[ii], texture, bind.m_un.m_compute.m_mip);
samplerFlags[ii] = uint32_t(texture.m_flags);
scratchBuffer.allocSrv(srvHandle[stage], texture, bind.m_mip);
samplerFlags[stage] = uint32_t(texture.m_flags);
}
}
break;
case Binding::Texture:
{
TextureD3D12& texture = m_textures[bind.m_idx];
texture.setState(m_commandList, D3D12_RESOURCE_STATE_GENERIC_READ);
scratchBuffer.allocSrv(srvHandle[stage], texture);
samplerFlags[stage] = (0 == (BGFX_SAMPLER_INTERNAL_DEFAULT & bind.m_samplerFlags)
? bind.m_samplerFlags
: texture.m_flags
) & (BGFX_SAMPLER_BITS_MASK | BGFX_SAMPLER_BORDER_COLOR_MASK | BGFX_SAMPLER_COMPARE_MASK)
;
}
break;
case Binding::IndexBuffer:
case Binding::VertexBuffer:
{
@ -5911,15 +5923,15 @@ namespace bgfx { namespace d3d12
: m_vertexBuffers[bind.m_idx]
;
if (Access::Read != bind.m_un.m_compute.m_access)
if (Access::Read != bind.m_access)
{
buffer.setState(m_commandList, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
scratchBuffer.allocUav(srvHandle[ii], buffer);
scratchBuffer.allocUav(srvHandle[stage], buffer);
}
else
{
buffer.setState(m_commandList, D3D12_RESOURCE_STATE_GENERIC_READ);
scratchBuffer.allocSrv(srvHandle[ii], buffer);
scratchBuffer.allocSrv(srvHandle[stage], buffer);
}
}
break;
@ -5927,8 +5939,8 @@ namespace bgfx { namespace d3d12
}
else
{
samplerFlags[ii] = 0;
scratchBuffer.allocEmpty(srvHandle[ii]);
samplerFlags[stage] = 0;
scratchBuffer.allocEmpty(srvHandle[stage]);
}
}
@ -6199,8 +6211,8 @@ namespace bgfx { namespace d3d12
TextureD3D12& texture = m_textures[bind.m_idx];
texture.setState(m_commandList, D3D12_RESOURCE_STATE_GENERIC_READ);
scratchBuffer.allocSrv(srvHandle[stage], texture);
samplerFlags[stage] = (0 == (BGFX_SAMPLER_INTERNAL_DEFAULT & bind.m_un.m_draw.m_textureFlags)
? bind.m_un.m_draw.m_textureFlags
samplerFlags[stage] = (0 == (BGFX_SAMPLER_INTERNAL_DEFAULT & bind.m_samplerFlags)
? bind.m_samplerFlags
: texture.m_flags
) & (BGFX_SAMPLER_BITS_MASK | BGFX_SAMPLER_BORDER_COLOR_MASK | BGFX_SAMPLER_COMPARE_MASK)
;
@ -6219,7 +6231,7 @@ namespace bgfx { namespace d3d12
: m_vertexBuffers[bind.m_idx]
;
if (Access::Read != bind.m_un.m_compute.m_access)
if (Access::Read != bind.m_access)
{
buffer.setState(m_commandList, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
scratchBuffer.allocUav(srvHandle[stage], buffer);

View File

@ -4162,13 +4162,13 @@ namespace bgfx { namespace d3d9
const Binding& bind = renderBind.m_bind[stage];
Binding& current = currentBind.m_bind[stage];
if (current.m_idx != bind.m_idx
|| current.m_un.m_draw.m_textureFlags != bind.m_un.m_draw.m_textureFlags
if (current.m_idx != bind.m_idx
|| current.m_samplerFlags != bind.m_samplerFlags
|| programChanged)
{
if (kInvalidHandle != bind.m_idx)
{
m_textures[bind.m_idx].commit(stage, bind.m_un.m_draw.m_textureFlags, _render->m_colorPalette);
m_textures[bind.m_idx].commit(stage, bind.m_samplerFlags, _render->m_colorPalette);
}
else
{

View File

@ -6552,13 +6552,13 @@ BX_TRACE("%d, %d, %d, %s", _array, _srgb, _mipAutogen, getName(_format) );
case Binding::Texture:
{
TextureGL& texture = m_textures[bind.m_idx];
texture.commit(ii, bind.m_un.m_draw.m_textureFlags, _render->m_colorPalette);
texture.commit(ii, bind.m_samplerFlags, _render->m_colorPalette);
}
break;
case Binding::Image:
{
if (Access::Read == bind.m_un.m_compute.m_access)
if (Access::Read == bind.m_access)
{
TextureGL& texture = m_textures[bind.m_idx];
texture.commit(ii, uint32_t(texture.m_flags), _render->m_colorPalette);
@ -6568,11 +6568,11 @@ BX_TRACE("%d, %d, %d, %s", _array, _srgb, _mipAutogen, getName(_format) );
const TextureGL& texture = m_textures[bind.m_idx];
GL_CHECK(glBindImageTexture(ii
, texture.m_id
, bind.m_un.m_compute.m_mip
, bind.m_mip
, texture.isCubeMap() ? GL_TRUE : GL_FALSE
, 0
, s_access[bind.m_un.m_compute.m_access]
, s_imageFormat[bind.m_un.m_compute.m_format])
, s_access[bind.m_access]
, s_imageFormat[bind.m_format])
);
barrier |= GL_SHADER_IMAGE_ACCESS_BARRIER_BIT;
}
@ -7061,9 +7061,9 @@ BX_TRACE("%d, %d, %d, %s", _array, _srgb, _mipAutogen, getName(_format) );
{
const Binding& bind = renderBind.m_bind[stage];
Binding& current = currentBind.m_bind[stage];
if (current.m_idx != bind.m_idx
|| current.m_type != bind.m_type
|| current.m_un.m_draw.m_textureFlags != bind.m_un.m_draw.m_textureFlags
if (current.m_idx != bind.m_idx
|| current.m_type != bind.m_type
|| current.m_samplerFlags != bind.m_samplerFlags
|| programChanged)
{
if (kInvalidHandle != bind.m_idx)
@ -7073,7 +7073,7 @@ BX_TRACE("%d, %d, %d, %s", _array, _srgb, _mipAutogen, getName(_format) );
case Binding::Texture:
{
TextureGL& texture = m_textures[bind.m_idx];
texture.commit(stage, bind.m_un.m_draw.m_textureFlags, _render->m_colorPalette);
texture.commit(stage, bind.m_samplerFlags, _render->m_colorPalette);
}
break;

View File

@ -3951,8 +3951,8 @@ namespace bgfx { namespace mtl
const Binding& bind = renderBind.m_bind[stage];
Binding& current = currentBind.m_bind[stage];
if (current.m_idx != bind.m_idx
|| current.m_un.m_draw.m_textureFlags != bind.m_un.m_draw.m_textureFlags
if (current.m_idx != bind.m_idx
|| current.m_samplerFlags != bind.m_samplerFlags
|| programChanged)
{
if (kInvalidHandle != bind.m_idx)
@ -3961,7 +3961,7 @@ namespace bgfx { namespace mtl
texture.commit(samplerInfo.m_index
, !samplerInfo.m_fragment
, samplerInfo.m_fragment
, bind.m_un.m_draw.m_textureFlags
, bind.m_samplerFlags
);
}
}

View File

@ -3908,15 +3908,15 @@ BX_UNUSED(currentSamplerStateIdx);
// {
// TextureD3D12& texture = m_textures[bind.m_idx];
//
// if (Access::Read != bind.m_un.m_compute.m_access)
// if (Access::Read != bind.m_access)
// {
// texture.setState(m_commandList, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
// scratchBuffer.allocUav(srvHandle[ii], texture, bind.m_un.m_compute.m_mip);
// scratchBuffer.allocUav(srvHandle[ii], texture, bind.m_mip);
// }
// else
// {
// texture.setState(m_commandList, D3D12_RESOURCE_STATE_GENERIC_READ);
// scratchBuffer.allocSrv(srvHandle[ii], texture, bind.m_un.m_compute.m_mip);
// scratchBuffer.allocSrv(srvHandle[ii], texture, bind.m_mip);
// samplerFlags[ii] = texture.m_flags;
// }
// }
@ -3930,7 +3930,7 @@ BX_UNUSED(currentSamplerStateIdx);
// : m_vertexBuffers[bind.m_idx]
// ;
//
// if (Access::Read != bind.m_un.m_compute.m_access)
// if (Access::Read != bind.m_access)
// {
// buffer.setState(m_commandList, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
// scratchBuffer.allocUav(srvHandle[ii], buffer);
@ -4158,8 +4158,8 @@ BX_UNUSED(currentSamplerStateIdx);
// TextureD3D12& texture = m_textures[bind.m_idx];
// texture.setState(m_commandList, D3D12_RESOURCE_STATE_GENERIC_READ);
// scratchBuffer.allocSrv(srvHandle[stage], texture);
// samplerFlags[stage] = (0 == (BGFX_TEXTURE_INTERNAL_DEFAULT_SAMPLER & bind.m_un.m_draw.m_textureFlags)
// ? bind.m_un.m_draw.m_textureFlags
// samplerFlags[stage] = (0 == (BGFX_TEXTURE_INTERNAL_DEFAULT_SAMPLER & bind.m_textureFlags)
// ? bind.m_textureFlags
// : texture.m_flags
// ) & (BGFX_TEXTURE_SAMPLER_BITS_MASK|BGFX_TEXTURE_BORDER_COLOR_MASK)
// ;