| 1 |
#include "D3D11RenderTarget.h" |
| 2 |
#include "D3D11Device.h" |
| 3 |
#include "D3D11Texture.h" |
| 4 |
|
| 5 |
#include <d3d11.h> |
| 6 |
|
| 7 |
namespace nCine::RHI::D3D11 |
| 8 |
{ |
| 9 |
D3D11RenderTarget::D3D11RenderTarget() |
| 10 |
: _numDrawBuffers(1) |
| 11 |
{ |
| 12 |
for (std::uint32_t i = 0; i < MaxColorAttachments; i++) { |
| 13 |
_colorTextures[i] = nullptr; |
| 14 |
_rtvs[i] = nullptr; |
| 15 |
_rtvTextures[i] = nullptr; |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
D3D11RenderTarget::~D3D11RenderTarget() |
| 20 |
{ |
| 21 |
// Clear from the device first so a destroyed render target can't dangle as _currentRenderTarget |
| 22 |
// (this also scrubs every released RTV from the device's last-bound shadow) |
| 23 |
D3D11Device::UnbindRenderTarget(this); |
| 24 |
for (std::uint32_t i = 0; i < MaxColorAttachments; i++) { |
| 25 |
ReleaseRTV(i); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
void D3D11RenderTarget::ReleaseRTV(std::uint32_t index) |
| 30 |
{ |
| 31 |
if (_rtvs[index] != nullptr) { |
| 32 |
// Scrub from the device's last-bound shadow first: the view is released right after this call and a |
| 33 |
// new RTV recycling the same pointer would otherwise be mistaken for still bound and its bind skipped |
| 34 |
D3D11Device::OnRtvReleased(_rtvs[index]); |
| 35 |
_rtvs[index]->Release(); |
| 36 |
_rtvs[index] = nullptr; |
| 37 |
} |
| 38 |
_rtvTextures[index] = nullptr; |
| 39 |
} |
| 40 |
|
| 41 |
ID3D11RenderTargetView* D3D11RenderTarget::GetRTV(std::uint32_t index) |
| 42 |
{ |
| 43 |
if (index >= MaxColorAttachments) { |
| 44 |
return nullptr; |
| 45 |
} |
| 46 |
D3D11Texture* color = _colorTextures[index]; |
| 47 |
if (color == nullptr) { |
| 48 |
return nullptr; |
| 49 |
} |
| 50 |
// Rebuild if the attachment changed or the view has not been created yet |
| 51 |
if (_rtvs[index] != nullptr && _rtvTextures[index] == color) { |
| 52 |
return _rtvs[index]; |
| 53 |
} |
| 54 |
ReleaseRTV(index); |
| 55 |
|
| 56 |
ID3D11Device* device = D3D11Device::GetD3DDevice(); |
| 57 |
ID3D11Texture2D* tex = color->GetOrCreateTexture2D(); |
| 58 |
if (device == nullptr || tex == nullptr) { |
| 59 |
return nullptr; |
| 60 |
} |
| 61 |
if (FAILED(device->CreateRenderTargetView(tex, nullptr, &_rtvs[index]))) { |
| 62 |
_rtvs[index] = nullptr; |
| 63 |
return nullptr; |
| 64 |
} |
| 65 |
_rtvTextures[index] = color; |
| 66 |
return _rtvs[index]; |
| 67 |
} |
| 68 |
|
| 69 |
std::uint32_t D3D11RenderTarget::GetAttachedCount() const |
| 70 |
{ |
| 71 |
std::uint32_t count = 0; |
| 72 |
while (count < MaxColorAttachments && _colorTextures[count] != nullptr) { |
| 73 |
count++; |
| 74 |
} |
| 75 |
return count; |
| 76 |
} |
| 77 |
|
| 78 |
std::uint32_t D3D11RenderTarget::GetRTVs(ID3D11RenderTargetView* outRtvs[MaxColorAttachments]) |
| 79 |
{ |
| 80 |
// The bound set is the contiguous attached run, limited by the draw-buffer count (mirroring |
| 81 |
// glDrawBuffers) and truncated at the first attachment whose view cannot be created |
| 82 |
std::uint32_t count = GetAttachedCount(); |
| 83 |
if (_numDrawBuffers > 0 && _numDrawBuffers < count) { |
| 84 |
count = _numDrawBuffers; |
| 85 |
} |
| 86 |
for (std::uint32_t i = 0; i < count; i++) { |
| 87 |
outRtvs[i] = GetRTV(i); |
| 88 |
if (outRtvs[i] == nullptr) { |
| 89 |
return i; |
| 90 |
} |
| 91 |
} |
| 92 |
return count; |
| 93 |
} |
| 94 |
|
| 95 |
void D3D11RenderTarget::AttachColorTexture(D3D11Texture& texture, std::uint32_t index) |
| 96 |
{ |
| 97 |
if (index < MaxColorAttachments) { |
| 98 |
_colorTextures[index] = &texture; |
| 99 |
texture.SetRenderTarget(true); |
| 100 |
ReleaseRTV(index); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
void D3D11RenderTarget::DetachColorTexture(std::uint32_t index) |
| 105 |
{ |
| 106 |
if (index < MaxColorAttachments) { |
| 107 |
_colorTextures[index] = nullptr; |
| 108 |
ReleaseRTV(index); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
void D3D11RenderTarget::AttachDepthStencil(DepthStencilFormat format, std::int32_t width, std::int32_t height) |
| 113 |
{ |
| 114 |
static_cast<void>(format); |
| 115 |
static_cast<void>(width); |
| 116 |
static_cast<void>(height); |
| 117 |
} |
| 118 |
|
| 119 |
void D3D11RenderTarget::DetachDepthStencil(DepthStencilFormat format) |
| 120 |
{ |
| 121 |
static_cast<void>(format); |
| 122 |
} |
| 123 |
|
| 124 |
void D3D11RenderTarget::BindDraw() |
| 125 |
{ |
| 126 |
D3D11Device::SetRenderTarget(this); |
| 127 |
} |
| 128 |
|
| 129 |
void D3D11RenderTarget::UnbindDraw() |
| 130 |
{ |
| 131 |
D3D11Device::SetRenderTarget(nullptr); |
| 132 |
} |
| 133 |
|
| 134 |
bool D3D11RenderTarget::SetDrawBuffers(std::uint32_t numColorAttachments) |
| 135 |
{ |
| 136 |
_numDrawBuffers = numColorAttachments; |
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
bool D3D11RenderTarget::IsStatusComplete() |
| 141 |
{ |
| 142 |
return (_colorTextures[0] != nullptr); |
| 143 |
} |
| 144 |
|
| 145 |
void D3D11RenderTarget::InvalidateDepthStencil(DepthStencilFormat format) |
| 146 |
{ |
| 147 |
static_cast<void>(format); |
| 148 |
} |
| 149 |
|
| 150 |
void D3D11RenderTarget::SetObjectLabel(StringView label) |
| 151 |
{ |
| 152 |
static_cast<void>(label); |
| 153 |
} |
| 154 |
} |