internal/graphicsdriver/directx: bug fix: leave mapped regions for constant buffers

Mapping for a constant buffer every frame might sometimes fails. We
are not sure the reason, but in the official examples, leaving mapped
regoins seems the way to go.

Updates #2084
Closes #2157
This commit is contained in:
Hajime Hoshi 2022-06-23 17:09:44 +09:00
parent 506d534370
commit 712940cb02

View File

@ -275,6 +275,7 @@ type pipelineStates struct {
samplerDescriptorHeap *_ID3D12DescriptorHeap samplerDescriptorHeap *_ID3D12DescriptorHeap
constantBuffers [frameCount][]*_ID3D12Resource constantBuffers [frameCount][]*_ID3D12Resource
constantBufferMaps [frameCount][]uintptr
} }
const numConstantBufferAndSourceTextures = 1 + graphics.ShaderImageNum const numConstantBufferAndSourceTextures = 1 + graphics.ShaderImageNum
@ -366,8 +367,10 @@ func (p *pipelineStates) useGraphicsPipelineState(device *_ID3D12Device, command
if cap(p.constantBuffers[frameIndex]) > idx { if cap(p.constantBuffers[frameIndex]) > idx {
p.constantBuffers[frameIndex] = p.constantBuffers[frameIndex][:idx+1] p.constantBuffers[frameIndex] = p.constantBuffers[frameIndex][:idx+1]
p.constantBufferMaps[frameIndex] = p.constantBufferMaps[frameIndex][:idx+1]
} else { } else {
p.constantBuffers[frameIndex] = append(p.constantBuffers[frameIndex], nil) p.constantBuffers[frameIndex] = append(p.constantBuffers[frameIndex], nil)
p.constantBufferMaps[frameIndex] = append(p.constantBufferMaps[frameIndex], 0)
} }
const bufferSizeAlignement = 256 const bufferSizeAlignement = 256
@ -377,10 +380,13 @@ func (p *pipelineStates) useGraphicsPipelineState(device *_ID3D12Device, command
} }
cb := p.constantBuffers[frameIndex][idx] cb := p.constantBuffers[frameIndex][idx]
m := p.constantBufferMaps[frameIndex][idx]
if cb != nil { if cb != nil {
if uint32(cb.GetDesc().Width) < bufferSize { if uint32(cb.GetDesc().Width) < bufferSize {
p.constantBuffers[frameIndex][idx].Unmap(0, nil)
p.constantBuffers[frameIndex][idx].Release() p.constantBuffers[frameIndex][idx].Release()
p.constantBuffers[frameIndex][idx] = nil p.constantBuffers[frameIndex][idx] = nil
p.constantBufferMaps[frameIndex][idx] = 0
cb = nil cb = nil
} }
} }
@ -401,6 +407,15 @@ func (p *pipelineStates) useGraphicsPipelineState(device *_ID3D12Device, command
BufferLocation: cb.GetGPUVirtualAddress(), BufferLocation: cb.GetGPUVirtualAddress(),
SizeInBytes: bufferSize, SizeInBytes: bufferSize,
}, h) }, h)
m, err = cb.Map(0, &_D3D12_RANGE{0, 0})
if err != nil {
return err
}
p.constantBufferMaps[frameIndex][idx] = m
}
if m == 0 {
return fmt.Errorf("directx: ID3D12Resource::Map failed")
} }
h, err := p.shaderDescriptorHeap.GetCPUDescriptorHandleForHeapStart() h, err := p.shaderDescriptorHeap.GetCPUDescriptorHandleForHeapStart()
@ -424,14 +439,8 @@ func (p *pipelineStates) useGraphicsPipelineState(device *_ID3D12Device, command
} }
// Update the constant buffer. // Update the constant buffer.
m, err := cb.Map(0, &_D3D12_RANGE{0, 0})
if err != nil {
return err
}
copyFloat32s(m, uniforms) copyFloat32s(m, uniforms)
cb.Unmap(0, nil)
commandList.SetPipelineState(pipelineState) commandList.SetPipelineState(pipelineState)
rs, err := p.ensureRootSignature(device) rs, err := p.ensureRootSignature(device)