Skip to content

Commit

Permalink
feat(d3d11): support ClearView on rtv when it is a full-sized clear
Browse files Browse the repository at this point in the history
  • Loading branch information
3Shain committed Jan 22, 2025
1 parent 8b0cd3c commit af1380f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
26 changes: 21 additions & 5 deletions src/d3d11/d3d11_context_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,27 @@ template <typename ContextInternalState> class MTLD3D11DeviceContextImplBase : p

void
ClearView(ID3D11View *pView, const FLOAT Color[4], const D3D11_RECT *pRect, UINT NumRects) override {
if (NumRects && !pRect)
return;

while (auto expected = com_cast<ID3D11RenderTargetView>(pView)) {
auto rtv = static_cast<IMTLD3D11RenderTargetView *>(expected.ptr());
if (NumRects > 1)
break;
if (NumRects) {
if (pRect[0].top != 0 || pRect[0].left != 0) {
break;
}
uint32_t rect_width = pRect[0].right - pRect[0].left;
uint32_t rect_height = pRect[0].bottom - pRect[0].top;
if (rect_width != rtv->GetAttachmentDesc().Width)
break;
if (rect_height != rtv->GetAttachmentDesc().Height)
break;
}
return ClearRenderTargetView(rtv, Color);
}

IMPLEMENT_ME
}

Expand Down Expand Up @@ -3954,11 +3975,6 @@ class MTLD3D11CommandList : public ManagedDeviceChild<ID3D11CommandList> {

#pragma region ImmediateContext-related

bool
Noop() {
return cpu_arugment_heap_offset == 0;
};

void Execute(ArgumentEncodingContext& ctx) {
list.execute(ctx);
};
Expand Down
2 changes: 2 additions & 0 deletions src/d3d11/d3d11_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ struct MTL_RENDER_PASS_ATTACHMENT_DESC {
uint32_t RenderTargetArrayLength;
uint32_t SampleCount;
uint32_t DepthPlane;
uint32_t Width;
uint32_t Height;
};

DEFINE_COM_INTERFACE("f1d21087-fbde-44b3-bc2c-b69be540a0ad",
Expand Down
20 changes: 20 additions & 0 deletions src/d3d11/mtld11_resource_view_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ InitializeAndNormalizeViewDescriptor(
return E_FAIL;
}

inline void
GetRenderTargetSize(Texture *pTexture, uint32_t level, MTL_RENDER_PASS_ATTACHMENT_DESC &AttachmentDesc) {
AttachmentDesc.Width = std::max(1u, pTexture->width() >> level);
AttachmentDesc.Height = std::max(1u, pTexture->height() >> level);
}

template <>
HRESULT InitializeAndNormalizeViewDescriptor(
MTLD3D11Device *pDevice, unsigned MiplevelCount, unsigned ArraySize, Texture* pTexture,
Expand Down Expand Up @@ -306,6 +312,7 @@ HRESULT InitializeAndNormalizeViewDescriptor(
Descriptor.miplevelCount = 1;
Descriptor.firstArraySlice = 0;
Descriptor.arraySize = 1;
GetRenderTargetSize(pTexture, Descriptor.firstMiplevel, AttachmentDesc);
return S_OK;
}
break;
Expand All @@ -321,6 +328,7 @@ HRESULT InitializeAndNormalizeViewDescriptor(
Descriptor.firstArraySlice = ViewDesc.Texture1DArray.FirstArraySlice;
Descriptor.arraySize = ViewDesc.Texture1DArray.ArraySize;
AttachmentDesc.RenderTargetArrayLength = Descriptor.arraySize;
GetRenderTargetSize(pTexture, Descriptor.firstMiplevel, AttachmentDesc);
return S_OK;
}
break;
Expand All @@ -334,6 +342,7 @@ HRESULT InitializeAndNormalizeViewDescriptor(
Descriptor.miplevelCount = 1;
Descriptor.firstArraySlice = 0;
Descriptor.arraySize = 1;
GetRenderTargetSize(pTexture, Descriptor.firstMiplevel, AttachmentDesc);
return S_OK;
}
break;
Expand All @@ -350,6 +359,7 @@ HRESULT InitializeAndNormalizeViewDescriptor(
Descriptor.firstArraySlice = ViewDesc.Texture2DArray.FirstArraySlice;
Descriptor.arraySize = ViewDesc.Texture2DArray.ArraySize;
AttachmentDesc.RenderTargetArrayLength = Descriptor.arraySize;
GetRenderTargetSize(pTexture, Descriptor.firstMiplevel, AttachmentDesc);
return S_OK;
}
break;
Expand All @@ -362,6 +372,7 @@ HRESULT InitializeAndNormalizeViewDescriptor(
Descriptor.miplevelCount = 1;
Descriptor.firstArraySlice = 0;
Descriptor.arraySize = 1;
GetRenderTargetSize(pTexture, Descriptor.firstMiplevel, AttachmentDesc);
return S_OK;
}
break;
Expand All @@ -377,6 +388,7 @@ HRESULT InitializeAndNormalizeViewDescriptor(
Descriptor.firstArraySlice = 0; // FIXME: we only handle 2dms here!
Descriptor.arraySize = 1;
AttachmentDesc.RenderTargetArrayLength = 1;
GetRenderTargetSize(pTexture, Descriptor.firstMiplevel, AttachmentDesc);
return S_OK;
}
break;
Expand All @@ -393,6 +405,7 @@ HRESULT InitializeAndNormalizeViewDescriptor(
Descriptor.firstArraySlice = 0;
Descriptor.arraySize = 1;
AttachmentDesc.RenderTargetArrayLength = ArraySize;
GetRenderTargetSize(pTexture, Descriptor.firstMiplevel, AttachmentDesc);
return S_OK;
}
if (ViewDesc.Texture3D.WSize == 1) {
Expand All @@ -403,6 +416,7 @@ HRESULT InitializeAndNormalizeViewDescriptor(
Descriptor.firstArraySlice = 0;
Descriptor.arraySize = 1;
AttachmentDesc.DepthPlane = ViewDesc.Texture3D.FirstWSlice;
GetRenderTargetSize(pTexture, Descriptor.firstMiplevel, AttachmentDesc);
return S_OK;
}
ERR("tex3d rtv creation not properly handled: ", ViewDesc.Texture3D.FirstWSlice, ":", ViewDesc.Texture3D.WSize,
Expand Down Expand Up @@ -446,6 +460,7 @@ InitializeAndNormalizeViewDescriptor(
Descriptor.miplevelCount = 1;
Descriptor.firstArraySlice = 0;
Descriptor.arraySize = 1;
GetRenderTargetSize(pTexture, Descriptor.firstMiplevel, AttachmentDesc);
return S_OK;
}
break;
Expand All @@ -461,6 +476,7 @@ InitializeAndNormalizeViewDescriptor(
Descriptor.firstArraySlice = ViewDesc.Texture1DArray.FirstArraySlice;
Descriptor.arraySize = ViewDesc.Texture1DArray.ArraySize;
AttachmentDesc.RenderTargetArrayLength = Descriptor.arraySize;
GetRenderTargetSize(pTexture, Descriptor.firstMiplevel, AttachmentDesc);
return S_OK;
}
break;
Expand All @@ -473,6 +489,7 @@ InitializeAndNormalizeViewDescriptor(
Descriptor.miplevelCount = 1;
Descriptor.firstArraySlice = 0;
Descriptor.arraySize = 1;
GetRenderTargetSize(pTexture, Descriptor.firstMiplevel, AttachmentDesc);
return S_OK;
}
break;
Expand All @@ -489,6 +506,7 @@ InitializeAndNormalizeViewDescriptor(
Descriptor.firstArraySlice = ViewDesc.Texture2DArray.FirstArraySlice;
Descriptor.arraySize = ViewDesc.Texture2DArray.ArraySize;
AttachmentDesc.RenderTargetArrayLength = Descriptor.arraySize;
GetRenderTargetSize(pTexture, Descriptor.firstMiplevel, AttachmentDesc);
return S_OK;
}
break;
Expand All @@ -501,6 +519,7 @@ InitializeAndNormalizeViewDescriptor(
Descriptor.miplevelCount = 1;
Descriptor.firstArraySlice = 0;
Descriptor.arraySize = 1;
GetRenderTargetSize(pTexture, Descriptor.firstMiplevel, AttachmentDesc);
return S_OK;
}
if (texture_type == MTL::TextureType2D) {
Expand All @@ -511,6 +530,7 @@ InitializeAndNormalizeViewDescriptor(
Descriptor.miplevelCount = 1;
Descriptor.firstArraySlice = 0;
Descriptor.arraySize = 1;
GetRenderTargetSize(pTexture, Descriptor.firstMiplevel, AttachmentDesc);
return S_OK;
}
break;
Expand Down
14 changes: 11 additions & 3 deletions src/dxmt/dxmt_texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,26 @@ class Texture {
return current_.ptr();
}

MTL::TextureType textureType() {
constexpr MTL::TextureType textureType() {
return descriptor_->textureType();
}

MTL::PixelFormat pixelFormat() {
constexpr MTL::PixelFormat pixelFormat() {
return descriptor_->pixelFormat();
}

unsigned sampleCount() {
constexpr unsigned sampleCount() {
return descriptor_->sampleCount();
}

constexpr unsigned width() {
return descriptor_->width();
}

constexpr unsigned height() {
return descriptor_->height();
}

Rc<TextureAllocation> allocate(Flags<TextureAllocationFlag> flags);

MTL::Texture *view(TextureViewKey key);
Expand Down

0 comments on commit af1380f

Please sign in to comment.