1
0
mirror of https://github.com/doitsujin/dxvk.git synced 2025-03-15 07:29:17 +01:00

[dxbc] Replace computeResourceSlotId by light-weight alternatives

Slightly reduces overhead of D3D11 binding methods.
This commit is contained in:
Philip Rebohle 2019-04-17 22:41:40 +02:00
parent 044e3967e7
commit b44cad4d32
No known key found for this signature in database
GPG Key ID: C8CC613427A31C99
4 changed files with 119 additions and 114 deletions

View File

@ -3116,8 +3116,7 @@ namespace dxvk {
DxbcProgramType ShaderStage, DxbcProgramType ShaderStage,
const D3D11CommonShader* pShaderModule) { const D3D11CommonShader* pShaderModule) {
// Bind the shader and the ICB at once // Bind the shader and the ICB at once
const uint32_t slotId = computeResourceSlotId( uint32_t slotId = computeConstantBufferBinding(ShaderStage,
ShaderStage, DxbcBindingType::ConstantBuffer,
D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT); D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
EmitCs([ EmitCs([
@ -3359,9 +3358,7 @@ namespace dxvk {
UINT StartSlot, UINT StartSlot,
UINT NumBuffers, UINT NumBuffers,
ID3D11Buffer* const* ppConstantBuffers) { ID3D11Buffer* const* ppConstantBuffers) {
const uint32_t slotId = computeResourceSlotId( uint32_t slotId = computeConstantBufferBinding(ShaderStage, StartSlot);
ShaderStage, DxbcBindingType::ConstantBuffer,
StartSlot);
for (uint32_t i = 0; i < NumBuffers; i++) { for (uint32_t i = 0; i < NumBuffers; i++) {
auto newBuffer = static_cast<D3D11Buffer*>(ppConstantBuffers[i]); auto newBuffer = static_cast<D3D11Buffer*>(ppConstantBuffers[i]);
@ -3391,9 +3388,7 @@ namespace dxvk {
ID3D11Buffer* const* ppConstantBuffers, ID3D11Buffer* const* ppConstantBuffers,
const UINT* pFirstConstant, const UINT* pFirstConstant,
const UINT* pNumConstants) { const UINT* pNumConstants) {
const uint32_t slotId = computeResourceSlotId( uint32_t slotId = computeConstantBufferBinding(ShaderStage, StartSlot);
ShaderStage, DxbcBindingType::ConstantBuffer,
StartSlot);
for (uint32_t i = 0; i < NumBuffers; i++) { for (uint32_t i = 0; i < NumBuffers; i++) {
auto newBuffer = static_cast<D3D11Buffer*>(ppConstantBuffers[i]); auto newBuffer = static_cast<D3D11Buffer*>(ppConstantBuffers[i]);
@ -3442,9 +3437,7 @@ namespace dxvk {
UINT StartSlot, UINT StartSlot,
UINT NumSamplers, UINT NumSamplers,
ID3D11SamplerState* const* ppSamplers) { ID3D11SamplerState* const* ppSamplers) {
const uint32_t slotId = computeResourceSlotId( uint32_t slotId = computeSamplerBinding(ShaderStage, StartSlot);
ShaderStage, DxbcBindingType::ImageSampler,
StartSlot);
for (uint32_t i = 0; i < NumSamplers; i++) { for (uint32_t i = 0; i < NumSamplers; i++) {
auto sampler = static_cast<D3D11SamplerState*>(ppSamplers[i]); auto sampler = static_cast<D3D11SamplerState*>(ppSamplers[i]);
@ -3463,9 +3456,7 @@ namespace dxvk {
UINT StartSlot, UINT StartSlot,
UINT NumResources, UINT NumResources,
ID3D11ShaderResourceView* const* ppResources) { ID3D11ShaderResourceView* const* ppResources) {
const uint32_t slotId = computeResourceSlotId( uint32_t slotId = computeSrvBinding(ShaderStage, StartSlot);
ShaderStage, DxbcBindingType::ShaderResource,
StartSlot);
for (uint32_t i = 0; i < NumResources; i++) { for (uint32_t i = 0; i < NumResources; i++) {
auto resView = static_cast<D3D11ShaderResourceView*>(ppResources[i]); auto resView = static_cast<D3D11ShaderResourceView*>(ppResources[i]);
@ -3485,13 +3476,8 @@ namespace dxvk {
UINT NumUAVs, UINT NumUAVs,
ID3D11UnorderedAccessView* const* ppUnorderedAccessViews, ID3D11UnorderedAccessView* const* ppUnorderedAccessViews,
const UINT* pUAVInitialCounts) { const UINT* pUAVInitialCounts) {
const uint32_t uavSlotId = computeResourceSlotId( uint32_t uavSlotId = computeUavBinding (ShaderStage, StartSlot);
ShaderStage, DxbcBindingType::UnorderedAccessView, uint32_t ctrSlotId = computeUavCounterBinding(ShaderStage, StartSlot);
StartSlot);
const uint32_t ctrSlotId = computeResourceSlotId(
ShaderStage, DxbcBindingType::UavCounter,
StartSlot);
for (uint32_t i = 0; i < NumUAVs; i++) { for (uint32_t i = 0; i < NumUAVs; i++) {
auto uav = static_cast<D3D11UnorderedAccessView*>(ppUnorderedAccessViews[i]); auto uav = static_cast<D3D11UnorderedAccessView*>(ppUnorderedAccessViews[i]);
@ -3613,8 +3599,7 @@ namespace dxvk {
void D3D11DeviceContext::RestoreConstantBuffers( void D3D11DeviceContext::RestoreConstantBuffers(
DxbcProgramType Stage, DxbcProgramType Stage,
D3D11ConstantBufferBindings& Bindings) { D3D11ConstantBufferBindings& Bindings) {
const uint32_t slotId = computeResourceSlotId( uint32_t slotId = computeConstantBufferBinding(Stage, 0);
Stage, DxbcBindingType::ConstantBuffer, 0);
for (uint32_t i = 0; i < Bindings.size(); i++) for (uint32_t i = 0; i < Bindings.size(); i++)
BindConstantBuffer(slotId + i, &Bindings[i]); BindConstantBuffer(slotId + i, &Bindings[i]);
@ -3624,8 +3609,7 @@ namespace dxvk {
void D3D11DeviceContext::RestoreSamplers( void D3D11DeviceContext::RestoreSamplers(
DxbcProgramType Stage, DxbcProgramType Stage,
D3D11SamplerBindings& Bindings) { D3D11SamplerBindings& Bindings) {
const uint32_t slotId = computeResourceSlotId( uint32_t slotId = computeSamplerBinding(Stage, 0);
Stage, DxbcBindingType::ImageSampler, 0);
for (uint32_t i = 0; i < Bindings.size(); i++) for (uint32_t i = 0; i < Bindings.size(); i++)
BindSampler(slotId + i, Bindings[i].ptr()); BindSampler(slotId + i, Bindings[i].ptr());
@ -3635,8 +3619,7 @@ namespace dxvk {
void D3D11DeviceContext::RestoreShaderResources( void D3D11DeviceContext::RestoreShaderResources(
DxbcProgramType Stage, DxbcProgramType Stage,
D3D11ShaderResourceBindings& Bindings) { D3D11ShaderResourceBindings& Bindings) {
const uint32_t slotId = computeResourceSlotId( uint32_t slotId = computeSrvBinding(Stage, 0);
Stage, DxbcBindingType::ShaderResource, 0);
for (uint32_t i = 0; i < Bindings.size(); i++) for (uint32_t i = 0; i < Bindings.size(); i++)
BindShaderResource(slotId + i, Bindings[i].ptr()); BindShaderResource(slotId + i, Bindings[i].ptr());
@ -3646,11 +3629,8 @@ namespace dxvk {
void D3D11DeviceContext::RestoreUnorderedAccessViews( void D3D11DeviceContext::RestoreUnorderedAccessViews(
DxbcProgramType Stage, DxbcProgramType Stage,
D3D11UnorderedAccessBindings& Bindings) { D3D11UnorderedAccessBindings& Bindings) {
const uint32_t uavSlotId = computeResourceSlotId( uint32_t uavSlotId = computeUavBinding (Stage, 0);
Stage, DxbcBindingType::UnorderedAccessView, 0); uint32_t ctrSlotId = computeUavCounterBinding(Stage, 0);
const uint32_t ctrSlotId = computeResourceSlotId(
Stage, DxbcBindingType::UavCounter, 0);
for (uint32_t i = 0; i < Bindings.size(); i++) { for (uint32_t i = 0; i < Bindings.size(); i++) {
BindUnorderedAccessView( BindUnorderedAccessView(

View File

@ -777,9 +777,8 @@ namespace dxvk {
// Compute the DXVK binding slot index for the buffer. // Compute the DXVK binding slot index for the buffer.
// D3D11 needs to bind the actual buffers to this slot. // D3D11 needs to bind the actual buffers to this slot.
const uint32_t bindingId = computeResourceSlotId( uint32_t bindingId = computeConstantBufferBinding(
m_programInfo.type(), DxbcBindingType::ConstantBuffer, m_programInfo.type(), regIdx);
regIdx);
m_module.decorateDescriptorSet(varId, 0); m_module.decorateDescriptorSet(varId, 0);
m_module.decorateBinding(varId, bindingId); m_module.decorateBinding(varId, bindingId);
@ -828,8 +827,8 @@ namespace dxvk {
m_samplers.at(samplerId).typeId = samplerType; m_samplers.at(samplerId).typeId = samplerType;
// Compute binding slot index for the sampler // Compute binding slot index for the sampler
const uint32_t bindingId = computeResourceSlotId( uint32_t bindingId = computeSamplerBinding(
m_programInfo.type(), DxbcBindingType::ImageSampler, samplerId); m_programInfo.type(), samplerId);
m_module.decorateDescriptorSet(varId, 0); m_module.decorateDescriptorSet(varId, 0);
m_module.decorateBinding(varId, bindingId); m_module.decorateBinding(varId, bindingId);
@ -959,11 +958,9 @@ namespace dxvk {
// Compute the DXVK binding slot index for the resource. // Compute the DXVK binding slot index for the resource.
// D3D11 needs to bind the actual resource to this slot. // D3D11 needs to bind the actual resource to this slot.
const uint32_t bindingId = computeResourceSlotId( uint32_t bindingId = isUav
m_programInfo.type(), isUav ? computeUavBinding(m_programInfo.type(), registerId)
? DxbcBindingType::UnorderedAccessView : computeSrvBinding(m_programInfo.type(), registerId);
: DxbcBindingType::ShaderResource,
registerId);
m_module.decorateDescriptorSet(varId, 0); m_module.decorateDescriptorSet(varId, 0);
m_module.decorateBinding(varId, bindingId); m_module.decorateBinding(varId, bindingId);
@ -1076,11 +1073,9 @@ namespace dxvk {
: 0; : 0;
// Compute the DXVK binding slot index for the resource. // Compute the DXVK binding slot index for the resource.
uint32_t bindingId = computeResourceSlotId( uint32_t bindingId = isUav
m_programInfo.type(), isUav ? computeUavBinding(m_programInfo.type(), registerId)
? DxbcBindingType::UnorderedAccessView : computeSrvBinding(m_programInfo.type(), registerId);
: DxbcBindingType::ShaderResource,
registerId);
if (m_moduleInfo.options.useRawSsbo) { if (m_moduleInfo.options.useRawSsbo) {
uint32_t elemType = getScalarTypeId(DxbcScalarType::Uint32); uint32_t elemType = getScalarTypeId(DxbcScalarType::Uint32);
@ -1398,9 +1393,8 @@ namespace dxvk {
m_module.setDebugName(varId, m_module.setDebugName(varId,
str::format("u", regId, "_meta").c_str()); str::format("u", regId, "_meta").c_str());
const uint32_t bindingId = computeResourceSlotId( uint32_t bindingId = computeUavCounterBinding(
m_programInfo.type(), DxbcBindingType::UavCounter, m_programInfo.type(), regId);
regId);
m_module.decorateDescriptorSet(varId, 0); m_module.decorateDescriptorSet(varId, 0);
m_module.decorateBinding(varId, bindingId); m_module.decorateBinding(varId, bindingId);

View File

@ -2,48 +2,6 @@
namespace dxvk { namespace dxvk {
uint32_t computeResourceSlotId(
DxbcProgramType shaderStage,
DxbcBindingType bindingType,
uint32_t bindingIndex) {
// First resource slot index for per-stage resources
const uint32_t stageOffset = 128 + 160 * uint32_t(shaderStage);
if (shaderStage == DxbcProgramType::ComputeShader) {
// 0 - 15: Constant buffers
// 16 - 31: Samplers
// 32 - 159: Shader resources
// 160 - 223: Unordered access views
// 224 - 287: UAV counter buffers
switch (bindingType) {
case DxbcBindingType::ConstantBuffer: return bindingIndex + stageOffset + 0;
case DxbcBindingType::ImageSampler: return bindingIndex + stageOffset + 16;
case DxbcBindingType::ShaderResource: return bindingIndex + stageOffset + 32;
case DxbcBindingType::UnorderedAccessView:return bindingIndex + stageOffset + 160;
case DxbcBindingType::UavCounter: return bindingIndex + stageOffset + 224;
default: Logger::err("computeResourceSlotId: Invalid resource type");
}
} else {
// Global resource slots
// 0 - 63: Unordered access views
// 64 - 128: UAV counter buffers
// Per-stage resource slots:
// 0 - 15: Constant buffers
// 16 - 31: Samplers
// 32 - 159: Shader resources
switch (bindingType) {
case DxbcBindingType::UnorderedAccessView:return bindingIndex + 0;
case DxbcBindingType::UavCounter: return bindingIndex + 64;
case DxbcBindingType::ConstantBuffer: return bindingIndex + stageOffset + 0;
case DxbcBindingType::ImageSampler: return bindingIndex + stageOffset + 16;
case DxbcBindingType::ShaderResource: return bindingIndex + stageOffset + 32;
default: Logger::err("computeResourceSlotId: Invalid resource type");
}
}
return 0;
}
uint32_t primitiveVertexCount(DxbcPrimitive primitive) { uint32_t primitiveVertexCount(DxbcPrimitive primitive) {
static const std::array<uint32_t, 8> s_vertexCounts = { static const std::array<uint32_t, 8> s_vertexCounts = {
0, // Undefined 0, // Undefined

View File

@ -6,33 +6,106 @@
namespace dxvk { namespace dxvk {
/** /**
* \brief Resource type * \brief Binding numbers and properties
*
* The type of a shader resource. Used
* to determine the DXVK resource slot.
*/ */
enum DxbcBindingType : uint32_t { enum DxbcBindingProperties : uint32_t {
ConstantBuffer = 0, DxbcConstBufBindingIndex = 0,
ShaderResource = 1, DxbcConstBufBindingCount = 16,
ImageSampler = 2, DxbcSamplerBindingIndex = DxbcConstBufBindingIndex
UnorderedAccessView = 3, + DxbcConstBufBindingCount,
StreamOutputBuffer = 4, DxbcSamplerBindingCount = 16,
UavCounter = 5, DxbcResourceBindingIndex = DxbcSamplerBindingIndex
+ DxbcSamplerBindingCount,
DxbcResourceBindingCount = 128,
DxbcStageBindingCount = DxbcConstBufBindingCount
+ DxbcSamplerBindingCount
+ DxbcResourceBindingCount,
DxbcUavBindingIndex = DxbcStageBindingCount * 6,
DxbcUavBindingCount = 64,
}; };
/** /**
* \brief Computes the DXVK resource slot for a binding * \brief Computes first binding index for a given stage
* *
* \param [in] shaderStage The target shader stage * \param [in] stage The shader stage
* \param [in] bindingType Type of the resource * \returns Index of first binding
* \param [in] bindingIndex Resource binding index
* \returns DXVK resource slot index
*/ */
uint32_t computeResourceSlotId( inline uint32_t computeStageBindingOffset(DxbcProgramType stage) {
DxbcProgramType shaderStage, return DxbcStageBindingCount * uint32_t(stage);
DxbcBindingType bindingType, }
uint32_t bindingIndex);
/**
* \brief Computes first UAV binding index offset for a given stage
*
* \param [in] stage The shader stage
* \returns Index of first UAV binding
*/
inline uint32_t computeStageUavBindingOffset(DxbcProgramType stage) {
return DxbcUavBindingIndex
+ DxbcUavBindingCount * (stage == DxbcProgramType::ComputeShader ? 2 : 0);
}
/**
* \brief Computes constant buffer binding index
*
* \param [in] stage Shader stage
* \param [in] index Constant buffer index
* \returns Binding index
*/
inline uint32_t computeConstantBufferBinding(DxbcProgramType stage, uint32_t index) {
return computeStageBindingOffset(stage) + DxbcConstBufBindingIndex + index;
}
/**
* \brief Computes sampler binding index
*
* \param [in] stage Shader stage
* \param [in] index Sampler index
* \returns Binding index
*/
inline uint32_t computeSamplerBinding(DxbcProgramType stage, uint32_t index) {
return computeStageBindingOffset(stage) + DxbcSamplerBindingIndex + index;
}
/**
* \brief Computes resource binding index
*
* \param [in] stage Shader stage
* \param [in] index Resource index
* \returns Binding index
*/
inline uint32_t computeSrvBinding(DxbcProgramType stage, uint32_t index) {
return computeStageBindingOffset(stage) + DxbcResourceBindingIndex + index;
}
/**
* \brief Computes UAV binding offset
*
* \param [in] stage Shader stage
* \param [in] index UAV index
* \returns Binding index
*/
inline uint32_t computeUavBinding(DxbcProgramType stage, uint32_t index) {
return computeStageUavBindingOffset(stage) + index;
}
/**
* \brief Computes UAV counter binding offset
*
* \param [in] stage Shader stage
* \param [in] index UAV index
* \returns Binding index
*/
inline uint32_t computeUavCounterBinding(DxbcProgramType stage, uint32_t index) {
return computeStageUavBindingOffset(stage) + DxbcUavBindingCount + index;
}
/** /**
* \brief Primitive vertex count * \brief Primitive vertex count