Skip to content

Commit

Permalink
[d3d9] WIP: only copy smaller used float constants
Browse files Browse the repository at this point in the history
  • Loading branch information
K0bin committed Sep 9, 2021
1 parent 4759c6f commit 3c8d284
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
41 changes: 36 additions & 5 deletions src/d3d9/d3d9_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3209,6 +3209,8 @@ namespace dxvk {
UINT Vector4fCount) {
D3D9DeviceLock lock = LockDevice();

m_psFloatConstsCount = std::max(m_psFloatConstsCount, StartRegister + Vector4fCount);

return SetShaderConstants <
DxsoProgramTypes::PixelShader,
D3D9ConstantType::Float>(
Expand Down Expand Up @@ -4836,17 +4838,21 @@ namespace dxvk {


void D3D9DeviceEx::CreateConstantBuffers() {
auto& robustness2 = m_dxvkDevice->adapter()->devicePropertiesExt().extRobustness2;
const uint32_t ssboSizeAlignment = robustness2.robustStorageBufferAccessSizeAlignment;
const uint32_t uboSizeAlignment = robustness2.robustUniformBufferAccessSizeAlignment;

if (!m_isSWVP) {
m_consts[DxsoProgramTypes::VertexShader].buffer =
CreateConstantBuffer(m_dxsoOptions.vertexConstantBufferAsSSBO,
m_vsLayout.totalSize(),
align(m_vsLayout.totalSize(), m_dxsoOptions.vertexConstantBufferAsSSBO ? ssboSizeAlignment : uboSizeAlignment),
DxsoProgramType::VertexShader,
DxsoConstantBuffers::VSConstantBuffer);
}
// SWVP constant buffers are created late based on the amount of constants set by the application
m_consts[DxsoProgramTypes::PixelShader].buffer =
CreateConstantBuffer(false,
m_psLayout.totalSize(),
align(m_psLayout.totalSize(), uboSizeAlignment),
DxsoProgramType::PixelShader,
DxsoConstantBuffers::PSConstantBuffer);

Expand Down Expand Up @@ -4981,6 +4987,31 @@ namespace dxvk {

constSet.dirty = false;

const uint32_t floatCount = ShaderStage == DxsoProgramType::VertexShader ? m_definedVSFloatConstsCount : m_psFloatConstsCount;

const uint32_t intRange = caps::MaxOtherConstants * sizeof(Vector4i);
const uint32_t intDataSize = std::min(constSet.meta.maxConstIndexI + 1u, caps::MaxOtherConstants) * sizeof(Vector4i);
uint32_t floatDataSize = std::min(constSet.meta.maxConstIndexF + 1u, floatCount) * sizeof(Vector4);

auto& robustness2 = m_dxvkDevice->adapter()->devicePropertiesExt().extRobustness2;
uint32_t alignment = robustness2.robustUniformBufferAccessSizeAlignment;
alignment = std::max(alignment, 64u); // Make sure we do not recreate the buffer because the new one has to be a tiny bit larger

const uint32_t bufferSize = std::max(align(floatDataSize + intRange, alignment), alignment);
floatDataSize = bufferSize - intRange; // Read additional floats for padding so we don't end up with garbage data

if (m_boundConstantsBufferSize != bufferSize) {
constexpr uint32_t slotId = computeResourceSlotId(ShaderStage, DxsoBindingType::ConstantBuffer, 0);
EmitCs([
cBuffer = constSet.buffer,
cSlotId = slotId,
cSize = bufferSize
] (DxvkContext* ctx) {
ctx->bindResourceBuffer(cSlotId,
DxvkBufferSlice(cBuffer, 0, cSize));
});
}

DxvkBufferSliceHandle slice = constSet.buffer->allocSlice();

EmitCs([
Expand All @@ -4992,10 +5023,10 @@ namespace dxvk {

auto* dst = reinterpret_cast<HardwareLayoutType*>(slice.mapPtr);

if (constSet.meta.maxConstIndexF)
std::memcpy(dst->fConsts, Src.fConsts, constSet.meta.maxConstIndexF * sizeof(Vector4));
if (constSet.meta.maxConstIndexI)
std::memcpy(dst->iConsts, Src.iConsts, constSet.meta.maxConstIndexI * sizeof(Vector4i));
std::memcpy(dst->iConsts, Src.iConsts, intDataSize);
if (constSet.meta.maxConstIndexF)
std::memcpy(dst->fConsts, Src.fConsts, floatDataSize);

if (constSet.meta.needsConstantCopies) {
Vector4* data = reinterpret_cast<Vector4*>(dst->fConsts);
Expand Down
3 changes: 3 additions & 0 deletions src/d3d9/d3d9_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,9 @@ namespace dxvk {
uint32_t m_definedVSIntConstsCount = 0;
uint32_t m_definedVSBoolConstsCount = 0;

uint32_t m_psFloatConstsCount = 0;
uint32_t m_boundConstantsBufferSize = 0;

D3D9ConstantLayout m_vsLayout;
D3D9ConstantLayout m_psLayout;
D3D9ConstantSets m_consts[DxsoProgramTypes::Count];
Expand Down

0 comments on commit 3c8d284

Please sign in to comment.