Skip to content

Commit

Permalink
D3D: Fix validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
godlikepanos committed Jun 23, 2024
1 parent 7d9397b commit 7087b31
Show file tree
Hide file tree
Showing 58 changed files with 637 additions and 378 deletions.
2 changes: 1 addition & 1 deletion AnKi/Gr/BackendCommon/Format.def.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ ANKI_FORMAT_DEF(A2R10G10B10_Uscaled_Pack32, 60, 1041, 4, 4, 0, 0,
ANKI_FORMAT_DEF(A2R10G10B10_Sscaled_Pack32, 61, 1042, 4, 4, 0, 0, 0, 2, None)
ANKI_FORMAT_DEF(A2R10G10B10_Uint_Pack32, 62, 1043, 4, 4, 0, 0, 0, 1, None)
ANKI_FORMAT_DEF(A2R10G10B10_Sint_Pack32, 63, 1044, 4, 4, 0, 0, 0, 2, None)
ANKI_FORMAT_DEF(A2B10G10R10_Unorm_Pack32, 64, 1045, 4, 4, 0, 0, 0, 0, None)
ANKI_FORMAT_DEF(A2B10G10R10_Unorm_Pack32, 64, 24, 4, 4, 0, 0, 0, 0, None)
ANKI_FORMAT_DEF(A2B10G10R10_Snorm_Pack32, 65, 1046, 4, 4, 0, 0, 0, 0, None)
ANKI_FORMAT_DEF(A2B10G10R10_Uscaled_Pack32, 66, 1047, 4, 4, 0, 0, 0, 1, None)
ANKI_FORMAT_DEF(A2B10G10R10_Sscaled_Pack32, 67, 1048, 4, 4, 0, 0, 0, 2, None)
Expand Down
4 changes: 2 additions & 2 deletions AnKi/Gr/BackendCommon/GraphicsStateTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ class GraphicsStateTracker
m_hashes.m_vert = 0;
}

#if ANKI_GR_BACKEND_VULKAN
if(!!(prog->getShaderTypes() & ShaderTypeBit::kVertex) && refl.m_vertex.m_vertexAttributeMask.getSetBitCount())
{
if(m_staticState.m_shaderProg)
Expand All @@ -334,13 +335,12 @@ class GraphicsStateTracker
}
}

#if ANKI_GR_BACKEND_VULKAN
for(VertexAttributeSemantic s : EnumIterable<VertexAttributeSemantic>())
{
m_staticState.m_vert.m_attribs[s].m_semanticToVertexAttributeLocation = refl.m_vertex.m_vkVertexAttributeLocations[s];
}
#endif
}
#endif

if(m_staticState.m_misc.m_colorRtMask != refl.m_fragment.m_colorAttachmentWritemask)
{
Expand Down
6 changes: 1 addition & 5 deletions AnKi/Gr/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,7 @@ enum class Format : U32
{
kNone = 0,

#if ANKI_GR_BACKEND_VULKAN
# define ANKI_FORMAT_DEF(type, vk, d3d, componentCount, texelSize, blockWidth, blockHeight, blockSize, shaderType, depthStencil) k##type = vk,
#else
# define ANKI_FORMAT_DEF(type, vk, d3d, componentCount, texelSize, blockWidth, blockHeight, blockSize, shaderType, depthStencil) k##type = d3d,
#endif
#define ANKI_FORMAT_DEF(type, vk, d3d, componentCount, texelSize, blockWidth, blockHeight, blockSize, shaderType, depthStencil) k##type = vk,
#include <AnKi/Gr/BackendCommon/Format.def.h>
#undef ANKI_FORMAT_DEF
};
Expand Down
11 changes: 8 additions & 3 deletions AnKi/Gr/D3D/D3DBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ Error BufferImpl::init(const BufferInitInfo& inf)
const D3D12_RESOURCE_STATES initialState = D3D12_RESOURCE_STATE_COMMON;
ANKI_D3D_CHECK(getDevice().CreateCommittedResource(&heapProperties, heapFlags, &resourceDesc, initialState, nullptr, IID_PPV_ARGS(&m_resource)));

ANKI_D3D_CHECK(m_resource->SetName(s2ws(inf.getName().cstr()).c_str()));
GrDynamicArray<WChar> wstr;
wstr.resize(getName().getLength() + 1);
getName().toWideChars(wstr.getBegin(), wstr.getSize());
ANKI_D3D_CHECK(m_resource->SetName(wstr.getBegin()));

m_gpuAddress = m_resource->GetGPUVirtualAddress();

return Error::kNone;
}
Expand Down Expand Up @@ -195,9 +200,9 @@ D3D12_BARRIER_SYNC BufferImpl::computeSync(BufferUsageBit usage) const
sync |= D3D12_BARRIER_SYNC_EXECUTE_INDIRECT;
}

if(!!(usage & (BufferUsageBit::kIndex | BufferUsageBit::kVertex)))
if(!!(usage & BufferUsageBit::kIndex))
{
sync |= D3D12_BARRIER_SYNC_INDEX_INPUT | D3D12_BARRIER_SYNC_VERTEX_SHADING;
sync |= D3D12_BARRIER_SYNC_INDEX_INPUT;
}

if(!!(usage & BufferUsageBit::kAllGeometry))
Expand Down
83 changes: 61 additions & 22 deletions AnKi/Gr/D3D/D3DCommandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ void CommandBuffer::endRecording()
const QueryInfo qinfo = TimestampQueryFactory::getSingleton().getQueryInfo(handle);

self.m_cmdList->ResolveQueryData(qinfo.m_queryHeap, D3D12_QUERY_TYPE_TIMESTAMP, qinfo.m_indexInHeap, 1, qinfo.m_resultsBuffer,
qinfo.m_resultsBufferOffset / sizeof(U64));
qinfo.m_resultsBufferOffset);
}

for(QueryHandle handle : self.m_pipelineQueries)
{
const QueryInfo qinfo = PrimitivesPassedClippingFactory::getSingleton().getQueryInfo(handle);

self.m_cmdList->ResolveQueryData(qinfo.m_queryHeap, D3D12_QUERY_TYPE_PIPELINE_STATISTICS, qinfo.m_indexInHeap, 1, qinfo.m_resultsBuffer,
qinfo.m_resultsBufferOffset / sizeof(U64));
qinfo.m_resultsBufferOffset);
}

self.m_cmdList->Close();
Expand Down Expand Up @@ -275,12 +275,12 @@ void CommandBuffer::bindTexelBuffer(Register reg, const BufferView& buff, Format
const BufferImpl& impl = static_cast<const BufferImpl&>(buff.getBuffer());
if(reg.m_resourceType == HlslResourceType::kUav)
{
self.m_descriptors.bindUav(reg.m_space, reg.m_bindPoint, &impl.getD3DResource(), buff.getOffset(), buff.getRange(), DXGI_FORMAT(fmt));
self.m_descriptors.bindUav(reg.m_space, reg.m_bindPoint, &impl.getD3DResource(), buff.getOffset(), buff.getRange(), fmt);
}
else
{
ANKI_ASSERT(reg.m_resourceType == HlslResourceType::kSrv);
self.m_descriptors.bindSrv(reg.m_space, reg.m_bindPoint, &impl.getD3DResource(), buff.getOffset(), buff.getRange(), DXGI_FORMAT(fmt));
self.m_descriptors.bindSrv(reg.m_space, reg.m_bindPoint, &impl.getD3DResource(), buff.getOffset(), buff.getRange(), fmt);
}
}

Expand Down Expand Up @@ -331,6 +331,7 @@ void CommandBuffer::beginRenderPass(ConstWeakArray<RenderTarget> colorRts, Rende

U32 rtWidth = 0;
U32 rtHeight = 0;
D3D12_RENDER_PASS_FLAGS flags = D3D12_RENDER_PASS_FLAG_ALLOW_UAV_WRITES;

Array<D3D12_RENDER_PASS_RENDER_TARGET_DESC, kMaxColorRenderTargets> colorRtDescs;
Array<Format, kMaxColorRenderTargets> colorRtFormats;
Expand Down Expand Up @@ -359,7 +360,9 @@ void CommandBuffer::beginRenderPass(ConstWeakArray<RenderTarget> colorRts, Rende
const TextureImpl& tex = static_cast<const TextureImpl&>(depthStencilRt->m_textureView.getTexture());

dsDesc = {};
dsDesc.cpuDescriptor = tex.getOrCreateDsv(depthStencilRt->m_textureView.getSubresource(), depthStencilRt->m_usage).getCpuOffset();
dsDesc.cpuDescriptor =
tex.getOrCreateDsv(depthStencilRt->m_textureView.getSubresource(), !(depthStencilRt->m_usage & TextureUsageBit::kFramebufferWrite))
.getCpuOffset();

dsDesc.DepthBeginningAccess.Type = convertLoadOp(depthStencilRt->m_loadOperation);
dsDesc.DepthBeginningAccess.Clear.ClearValue.DepthStencil.Depth, depthStencilRt->m_clearValue.m_depthStencil.m_depth;
Expand All @@ -373,12 +376,22 @@ void CommandBuffer::beginRenderPass(ConstWeakArray<RenderTarget> colorRts, Rende

rtWidth = tex.getWidth() >> depthStencilRt->m_textureView.getFirstMipmap();
rtHeight = tex.getHeight() >> depthStencilRt->m_textureView.getFirstMipmap();

if(!(depthStencilRt->m_usage & TextureUsageBit::kFramebufferWrite))
{
flags |= !!(depthStencilRt->m_textureView.getDepthStencilAspect() & DepthStencilAspectBit::kDepth)
? D3D12_RENDER_PASS_FLAG_BIND_READ_ONLY_DEPTH
: D3D12_RENDER_PASS_FLAG_NONE;

flags |= !!(depthStencilRt->m_textureView.getDepthStencilAspect() & DepthStencilAspectBit::kStencil)
? D3D12_RENDER_PASS_FLAG_BIND_READ_ONLY_STENCIL
: D3D12_RENDER_PASS_FLAG_NONE;
}
}

self.m_graphicsState.beginRenderPass(ConstWeakArray(colorRtFormats.getBegin(), colorRts.getSize()), dsFormat, UVec2(rtWidth, rtHeight));

self.m_cmdList->BeginRenderPass(colorRts.getSize(), colorRtDescs.getBegin(), (depthStencilRt) ? &dsDesc : nullptr,
D3D12_RENDER_PASS_FLAG_ALLOW_UAV_WRITES);
self.m_cmdList->BeginRenderPass(colorRts.getSize(), colorRtDescs.getBegin(), (depthStencilRt) ? &dsDesc : nullptr, flags);
}

void CommandBuffer::endRenderPass()
Expand Down Expand Up @@ -589,6 +602,8 @@ void CommandBuffer::copyBufferToTexture(const BufferView& buff, const TextureVie
const U32 depth = (texImpl.getTextureType() == TextureType::k3D) ? (texImpl.getDepth() >> texView.getFirstMipmap()) : 1u;
ANKI_ASSERT(width && height && depth);

const FormatInfo& formatInfo = getFormatInfo(texImpl.getFormat());

D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
srcLocation.pResource = &buffImpl.getD3DResource();
srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
Expand All @@ -597,7 +612,8 @@ void CommandBuffer::copyBufferToTexture(const BufferView& buff, const TextureVie
srcLocation.PlacedFootprint.Footprint.Width = width;
srcLocation.PlacedFootprint.Footprint.Height = height;
srcLocation.PlacedFootprint.Footprint.Depth = depth;
srcLocation.PlacedFootprint.Footprint.RowPitch = width * getFormatInfo(texImpl.getFormat()).m_texelSize;
srcLocation.PlacedFootprint.Footprint.RowPitch = (formatInfo.isCompressed()) ? (width / formatInfo.m_blockWidth * formatInfo.m_blockSize)
: (width * getFormatInfo(texImpl.getFormat()).m_texelSize);

D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
dstLocation.pResource = &texImpl.getD3DResource();
Expand Down Expand Up @@ -688,8 +704,32 @@ void CommandBuffer::setPipelineBarrier(ConstWeakArray<TextureBarrierInfo> textur
for(const BufferBarrierInfo& barrier : buffers)
{
const BufferImpl& impl = static_cast<const BufferImpl&>(barrier.m_bufferView.getBuffer());
D3D12_BUFFER_BARRIER& d3dBarrier = *bufferBarriers.emplaceBack();
d3dBarrier = impl.computeBarrier(barrier.m_previousUsage, barrier.m_nextUsage);
D3D12_BUFFER_BARRIER b = impl.computeBarrier(barrier.m_previousUsage, barrier.m_nextUsage);

if(bufferBarriers.getSize() && bufferBarriers.getBack().pResource == b.pResource)
{
// Merge barriers

if(bufferBarriers.getBack().AccessBefore == D3D12_BARRIER_ACCESS_NO_ACCESS && b.AccessBefore != D3D12_BARRIER_ACCESS_NO_ACCESS)
{
bufferBarriers.getBack().AccessBefore = D3D12_BARRIER_ACCESS(0);
}
else if(bufferBarriers.getBack().AccessBefore != D3D12_BARRIER_ACCESS_NO_ACCESS && b.AccessBefore == D3D12_BARRIER_ACCESS_NO_ACCESS)
{
b.AccessBefore = D3D12_BARRIER_ACCESS(0);
}

bufferBarriers.getBack().AccessBefore |= b.AccessBefore;
bufferBarriers.getBack().AccessAfter |= b.AccessAfter;
bufferBarriers.getBack().SyncBefore |= b.SyncBefore;
bufferBarriers.getBack().SyncAfter |= b.SyncAfter;
}
else
{
// New barrier
D3D12_BUFFER_BARRIER& d3dBarrier = *bufferBarriers.emplaceBack();
d3dBarrier = b;
}
}

ANKI_ASSERT(accelerationStructures.getSize() == 0 && "TODO");
Expand Down Expand Up @@ -768,16 +808,6 @@ void CommandBuffer::writeTimestamp(TimestampQuery* query)
self.m_timestampQueries.emplaceBack(impl.m_handle);

const QueryInfo qinfo = TimestampQueryFactory::getSingleton().getQueryInfo(impl.m_handle);

// Make sure all the work has finished (mesa's dozen does that)
const D3D12_GLOBAL_BARRIER barrier = {.SyncBefore = D3D12_BARRIER_SYNC_ALL,
.SyncAfter = D3D12_BARRIER_SYNC_NONE,
.AccessBefore = D3D12_BARRIER_ACCESS_COMMON,
.AccessAfter = D3D12_BARRIER_ACCESS_NO_ACCESS};

const D3D12_BARRIER_GROUP barrierGroup = {.Type = D3D12_BARRIER_TYPE_GLOBAL, .NumBarriers = 1, .pGlobalBarriers = &barrier};
self.m_cmdList->Barrier(1, &barrierGroup);

self.m_cmdList->EndQuery(qinfo.m_queryHeap, D3D12_QUERY_TYPE_TIMESTAMP, qinfo.m_indexInHeap);
}

Expand Down Expand Up @@ -809,8 +839,8 @@ void CommandBuffer::pushDebugMarker(CString name, Vec3 color)

if(self.m_debugMarkersEnabled)
{
const UVec3 coloru(color * 255.0f);
const U64 val = (U64(coloru.x()) << 48) | (U64(coloru.x()) << 32) | (U64(coloru.x()) << 16);
const U8Vec3 coloru(color * 255.0f);
const U32 val = PIX_COLOR(coloru.x(), coloru.y(), coloru.z());

PIXBeginEvent(self.m_cmdList, val, "%s", name.cstr());
}
Expand All @@ -835,12 +865,21 @@ Error CommandBufferImpl::init(const CommandBufferInitInfo& init)
ANKI_CHECK(CommandBufferFactory::getSingleton().newCommandBuffer(init.m_flags, m_mcmdb));

m_cmdList = &m_mcmdb->getCmdList();

GrDynamicArray<WChar> wstr;
wstr.resize(getName().getLength() + 1);
getName().toWideChars(wstr.getBegin(), wstr.getSize());
m_cmdList->SetName(wstr.getBegin());

m_fastPool = &m_mcmdb->getFastMemoryPool();

m_descriptors.init(m_fastPool);

m_debugMarkersEnabled = g_debugMarkersCVar.get();

m_timestampQueries = {m_fastPool};
m_pipelineQueries = {m_fastPool};

return Error::kNone;
}

Expand Down
30 changes: 30 additions & 0 deletions AnKi/Gr/D3D/D3DCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,34 @@ D3D12_FILTER convertFilter(SamplingFilter minMagFilter, SamplingFilter mipFilter
return out;
}

void invokeDred()
{
getGrManagerImpl().invokeDred();
}

DXGI_FORMAT convertFormat(Format fmt)
{
DXGI_FORMAT out = DXGI_FORMAT_UNKNOWN;

switch(fmt)
{
case Format::kNone:
out = DXGI_FORMAT_UNKNOWN;
break;

#define ANKI_FORMAT_DEF(type, vk, d3d, componentCount, texelSize, blockWidth, blockHeight, blockSize, shaderType, depthStencil) \
case Format::k##type: \
out = DXGI_FORMAT(d3d); \
break;
#include <AnKi/Gr/BackendCommon/Format.def.h>
#undef ANKI_FORMAT_DEF

default:
ANKI_ASSERT(0);
}

ANKI_ASSERT(out < DXGI_FORMAT(1000));
return out;
}

} // end namespace anki
16 changes: 14 additions & 2 deletions AnKi/Gr/D3D/D3DCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ namespace anki {
#define ANKI_D3D_SELF(class_) class_& self = *static_cast<class_*>(this)
#define ANKI_D3D_SELF_CONST(class_) const class_& self = *static_cast<const class_*>(this)

void invokeDred();

#define ANKI_D3D_CHECKF(x) \
do \
{ \
HRESULT rez; \
if((rez = (x)) < 0) [[unlikely]] \
{ \
ANKI_D3D_LOGF("D3D function failed (HRESULT: %d message: %s): %s", rez, errorMessageToString(GetLastError()).cstr(), #x); \
if(rez == DXGI_ERROR_DEVICE_REMOVED) \
{ \
invokeDred(); \
} \
ANKI_D3D_LOGF("D3D function failed (HRESULT: 0x%X message: %s): %s", rez, errorMessageToString(GetLastError()).cstr(), #x); \
} \
} while(0)

Expand All @@ -60,7 +66,11 @@ namespace anki {
HRESULT rez; \
if((rez = (x)) < 0) [[unlikely]] \
{ \
ANKI_D3D_LOGE("D3D function failed (HRESULT: %d message: %s): %s", rez, errorMessageToString(GetLastError()).cstr(), #x); \
ANKI_D3D_LOGE("D3D function failed (HRESULT: 0x%X message: %s): %s", rez, errorMessageToString(GetLastError()).cstr(), #x); \
if(rez == DXGI_ERROR_DEVICE_REMOVED) \
{ \
invokeDred(); \
} \
return Error::kFunctionFailed; \
} \
} while(0)
Expand Down Expand Up @@ -484,6 +494,8 @@ inline [[nodiscard]] D3D12_CULL_MODE convertCullMode(FaceSelectionBit c)

return out;
}

[[nodiscard]] DXGI_FORMAT convertFormat(Format fmt);
/// @}

} // end namespace anki
Loading

0 comments on commit 7087b31

Please sign in to comment.