Debian Codegraph @

Sources/nCine/Graphics/RHI/RSX/RsxRenderTarget.cpp

raw
1 #if defined(WITH_RHI_RSX)
2
3 #include "RsxRenderTarget.h"
4 #include "RsxTexture.h"
5 #include "RsxDevice.h"
6
7 #include "../../../../Main.h"
8
9 #include <cstring>
10
11 namespace nCine::RHI::RSX
12 {
13 RsxRenderTarget::RsxRenderTarget()
14 : _numDrawBuffers(1), _surfaceTexture(nullptr), _surfaceData(nullptr), _surfaceOffset(0),
15 _surfacePitch(0), _surfaceWidth(0), _surfaceHeight(0), _surfaceValid(false)
16 {
17 for (std::uint32_t i = 0; i < MaxColorAttachments; i++) {
18 _colorTextures[i] = nullptr;
19 }
20 }
21
22 RsxRenderTarget::~RsxRenderTarget()
23 {
24 // The device tracks the current target by pointer, so it has to forget this one before it goes
25 RsxDevice::UnbindRenderTarget(this);
26
27 // A texture that was a colour attachment goes back to being an ordinary sampled texture, so its
28 // contents become the host store's business again
29 for (std::uint32_t i = 0; i < MaxColorAttachments; i++) {
30 if (_colorTextures[i] != nullptr) {
31 _colorTextures[i]->SetRenderTarget(false);
32 _colorTextures[i] = nullptr;
33 }
34 }
35 }
36
37 void RsxRenderTarget::AttachColorTexture(RsxTexture& texture, std::uint32_t index)
38 {
39 if (index >= MaxColorAttachments) {
40 return;
41 }
42 if (_colorTextures[index] == &texture) {
43 return;
44 }
45 if (_colorTextures[index] != nullptr) {
46 _colorTextures[index]->SetRenderTarget(false);
47 }
48 _colorTextures[index] = &texture;
49 // From now on the GPU writes these texels, so the texture stops uploading its host store over them
50 texture.SetRenderTarget(true);
51 _surfaceValid = false;
52 }
53
54 void RsxRenderTarget::DetachColorTexture(std::uint32_t index)
55 {
56 if (index >= MaxColorAttachments || _colorTextures[index] == nullptr) {
57 return;
58 }
59 _colorTextures[index]->SetRenderTarget(false);
60 _colorTextures[index] = nullptr;
61 _surfaceValid = false;
62 }
63
64 void RsxRenderTarget::AttachDepthStencil(DepthStencilFormat format, std::int32_t width, std::int32_t height)
65 {
66 // Every target shares the device's one depth surface (see the class documentation), so there is
67 // nothing to allocate - only the geometry is worth checking, because a target larger than the shared
68 // surface would have depth testing read outside it
69 static_cast<void>(format);
70 if (width > RsxDevice::GetDisplayWidth() || height > RsxDevice::GetDisplayHeight()) {
71 LOGW("A {}x{} render target is larger than the shared depth surface ({}x{}), so depth testing "
72 "is disabled for it", width, height, RsxDevice::GetDisplayWidth(), RsxDevice::GetDisplayHeight());
73 }
74 }
75
76 void RsxRenderTarget::DetachDepthStencil(DepthStencilFormat format)
77 {
78 static_cast<void>(format);
79 }
80
81 void RsxRenderTarget::BindDraw()
82 {
83 RsxDevice::SetRenderTarget(this);
84 }
85
86 void RsxRenderTarget::UnbindDraw()
87 {
88 RsxDevice::SetRenderTarget(nullptr);
89 }
90
91 bool RsxRenderTarget::SetDrawBuffers(std::uint32_t numColorAttachments)
92 {
93 if (numColorAttachments > MaxColorAttachments) {
94 return false;
95 }
96 _numDrawBuffers = numColorAttachments;
97 return true;
98 }
99
100 bool RsxRenderTarget::IsStatusComplete()
101 {
102 return (_colorTextures[0] != nullptr && _colorTextures[0]->GetSurfaceData() != nullptr);
103 }
104
105 void RsxRenderTarget::InvalidateDepthStencil(DepthStencilFormat format)
106 {
107 static_cast<void>(format);
108 }
109
110 void RsxRenderTarget::SetObjectLabel(StringView label)
111 {
112 static_cast<void>(label);
113 }
114
115 bool RsxRenderTarget::GetDrawSurface(gcmSurface& surface, std::int32_t& width, std::int32_t& height)
116 {
117 RsxTexture* texture = _colorTextures[0];
118 if (texture == nullptr) {
119 return false;
120 }
121
122 // Materializes the texture's GPU storage if this is the first use, and gives the address the surface
123 // is described by. A texture that reallocated (an upload changed its size) hands back a different
124 // one, which is exactly what invalidates the cached description below.
125 void* surfaceData = texture->GetSurfaceData();
126 if (surfaceData == nullptr) {
127 return false;
128 }
129
130 if (!_surfaceValid || _surfaceTexture != texture || _surfaceData != surfaceData ||
131 _surfaceWidth != texture->GetWidth() || _surfaceHeight != texture->GetHeight()) {
132 _surfaceTexture = texture;
133 _surfaceData = surfaceData;
134 _surfacePitch = texture->GetSurfaceStride();
135 _surfaceWidth = texture->GetWidth();
136 _surfaceHeight = texture->GetHeight();
137
138 // The offset is what the hardware addresses the surface by, and it comes from the same block the
139 // texture samples - a render target and the texture reading it back are one allocation here,
140 // which is why there is no resolve or copy step between a pass and the pass that samples it
141 if (const gcmTexture* gcmTex = texture->GetGcmTexture()) {
142 _surfaceOffset = gcmTex->offset;
143 _surfaceValid = true;
144 } else {
145 _surfaceValid = false;
146 return false;
147 }
148 }
149
150 std::memset(&surface, 0, sizeof(surface));
151 surface.colorFormat = GCM_SURFACE_A8R8G8B8;
152 surface.colorTarget = GCM_SURFACE_TARGET_0;
153 surface.colorLocation[0] = GCM_LOCATION_RSX;
154 surface.colorOffset[0] = _surfaceOffset;
155 surface.colorPitch[0] = _surfacePitch;
156 // The three unused colour targets still need a legal pitch: the hardware validates all four pitch
157 // fields whether or not the target is enabled, and rejects a zero
158 for (std::uint32_t i = 1; i < 4; i++) {
159 surface.colorLocation[i] = GCM_LOCATION_RSX;
160 surface.colorOffset[i] = 0;
161 surface.colorPitch[i] = 64;
162 }
163 surface.type = GCM_SURFACE_TYPE_LINEAR;
164 surface.antiAlias = GCM_SURFACE_CENTER_1;
165 surface.width = std::uint32_t(_surfaceWidth);
166 surface.height = std::uint32_t(_surfaceHeight);
167 surface.x = 0;
168 surface.y = 0;
169
170 width = _surfaceWidth;
171 height = _surfaceHeight;
172 return true;
173 }
174 }
175
176 #endif