Debian Codegraph @

Sources/nCine/Graphics/RHI/GXM/GxmRenderTarget.cpp

raw
1 #include "GxmRenderTarget.h"
2 #include "GxmTexture.h"
3 #include "GxmDevice.h"
4
5 #include "../../../../Main.h"
6
7 #include <cstring>
8
9 namespace nCine::RHI::GXM
10 {
11 GxmRenderTarget::GxmRenderTarget()
12 : _numDrawBuffers(MaxColorAttachments), _gxmRenderTarget(nullptr), _syncObject(nullptr), _surfaceTexture(nullptr),
13 _surfaceData(nullptr), _surfaceWidth(0), _surfaceHeight(0), _surfaceValid(false)
14 {
15 for (std::uint32_t i = 0; i < MaxColorAttachments; i++) {
16 _colorTextures[i] = nullptr;
17 }
18 std::memset(&_colorSurface, 0, sizeof(_colorSurface));
19 }
20
21 GxmRenderTarget::~GxmRenderTarget()
22 {
23 // Drop the device's pointer to this target before its sceGxm objects go away, and make sure no scene
24 // still recording into it is left open
25 GxmDevice::UnbindRenderTarget(this);
26 ReleaseSceneTarget();
27
28 for (std::uint32_t i = 0; i < MaxColorAttachments; i++) {
29 if (_colorTextures[i] != nullptr) {
30 _colorTextures[i]->SetRenderTarget(false);
31 _colorTextures[i] = nullptr;
32 }
33 }
34 }
35
36 void GxmRenderTarget::ReleaseSceneTarget()
37 {
38 if (_gxmRenderTarget != nullptr) {
39 // The GPU must not still be binning into this target when its data structures are destroyed
40 GxmDevice::FinishScene();
41 if (SceGxmContext* context = GxmDevice::GetContext()) {
42 sceGxmFinish(context);
43 }
44 sceGxmDestroyRenderTarget(_gxmRenderTarget);
45 _gxmRenderTarget = nullptr;
46 }
47 if (_syncObject != nullptr) {
48 sceGxmSyncObjectDestroy(_syncObject);
49 _syncObject = nullptr;
50 }
51 _surfaceValid = false;
52 _surfaceTexture = nullptr;
53 _surfaceData = nullptr;
54 _surfaceWidth = 0;
55 _surfaceHeight = 0;
56 }
57
58 void GxmRenderTarget::AttachColorTexture(GxmTexture& texture, std::uint32_t index)
59 {
60 if (index >= MaxColorAttachments) {
61 LOGW("sceGxm binds one colour surface per scene, so colour attachment {} is not available", index);
62 return;
63 }
64 if (_colorTextures[index] == &texture) {
65 return;
66 }
67
68 if (_colorTextures[index] != nullptr) {
69 _colorTextures[index]->SetRenderTarget(false);
70 }
71 _colorTextures[index] = &texture;
72 // The texture is written by the GPU from now on, which changes how its GPU-side copy is allocated
73 texture.SetRenderTarget(true);
74 ReleaseSceneTarget();
75 }
76
77 void GxmRenderTarget::DetachColorTexture(std::uint32_t index)
78 {
79 if (index >= MaxColorAttachments || _colorTextures[index] == nullptr) {
80 return;
81 }
82 _colorTextures[index]->SetRenderTarget(false);
83 _colorTextures[index] = nullptr;
84 ReleaseSceneTarget();
85 }
86
87 void GxmRenderTarget::AttachDepthStencil(DepthStencilFormat format, std::int32_t width, std::int32_t height)
88 {
89 // Every scene shares the device's depth/stencil surface, so there is nothing to allocate here
90 static_cast<void>(format);
91 static_cast<void>(width);
92 static_cast<void>(height);
93 }
94
95 void GxmRenderTarget::DetachDepthStencil(DepthStencilFormat format)
96 {
97 static_cast<void>(format);
98 }
99
100 void GxmRenderTarget::BindDraw()
101 {
102 GxmDevice::SetRenderTarget(this);
103 }
104
105 void GxmRenderTarget::UnbindDraw()
106 {
107 GxmDevice::SetRenderTarget(nullptr);
108 }
109
110 bool GxmRenderTarget::SetDrawBuffers(std::uint32_t numColorAttachments)
111 {
112 if (numColorAttachments > MaxColorAttachments) {
113 return false;
114 }
115 _numDrawBuffers = numColorAttachments;
116 return true;
117 }
118
119 bool GxmRenderTarget::IsStatusComplete()
120 {
121 SceGxmRenderTarget* renderTarget;
122 SceGxmColorSurface* colorSurface;
123 SceGxmSyncObject* syncObject;
124 std::int32_t width, height;
125 return GetSceneTarget(renderTarget, colorSurface, syncObject, width, height);
126 }
127
128 void GxmRenderTarget::InvalidateDepthStencil(DepthStencilFormat format)
129 {
130 static_cast<void>(format);
131 }
132
133 void GxmRenderTarget::SetObjectLabel(StringView label)
134 {
135 static_cast<void>(label);
136 }
137
138 bool GxmRenderTarget::GetSceneTarget(SceGxmRenderTarget*& renderTarget, SceGxmColorSurface*& colorSurface,
139 SceGxmSyncObject*& syncObject, std::int32_t& width, std::int32_t& height)
140 {
141 GxmTexture* texture = _colorTextures[0];
142 if (texture == nullptr || _numDrawBuffers == 0) {
143 return false;
144 }
145
146 void* surfaceData = texture->GetSurfaceData();
147 if (surfaceData == nullptr) {
148 return false;
149 }
150
151 // Rebuild when the attachment, its size or its GPU allocation changed under us (an upload that
152 // resizes the texture reallocates the block the surface points at)
153 if (_surfaceValid && (_surfaceTexture != texture || _surfaceData != surfaceData ||
154 _surfaceWidth != texture->GetWidth() || _surfaceHeight != texture->GetHeight())) {
155 ReleaseSceneTarget();
156 surfaceData = texture->GetSurfaceData();
157 if (surfaceData == nullptr) {
158 return false;
159 }
160 }
161
162 if (!_surfaceValid) {
163 const std::int32_t targetWidth = texture->GetWidth();
164 const std::int32_t targetHeight = texture->GetHeight();
165 if (targetWidth <= 0 || targetHeight <= 0) {
166 return false;
167 }
168
169 SceGxmRenderTargetParams params = {};
170 params.flags = 0;
171 params.width = std::uint16_t(targetWidth);
172 params.height = std::uint16_t(targetHeight);
173 // The pipeline can render into the same target more than once in a frame (a blur chain ping-pongs
174 // between its levels); 8 is the largest sceGxm accepts and only sizes the driver's own bookkeeping
175 params.scenesPerFrame = 8;
176 params.multisampleMode = SCE_GXM_MULTISAMPLE_NONE;
177 params.multisampleLocations = 0;
178 params.driverMemBlock = GxmMemory::InvalidUid;
179
180 std::int32_t result = sceGxmSyncObjectCreate(&_syncObject);
181 if (result < 0) {
182 LOGE("sceGxmSyncObjectCreate() for a {}x{} render target failed with 0x{:.8x}",
183 targetWidth, targetHeight, std::uint32_t(result));
184 _syncObject = nullptr;
185 return false;
186 }
187
188 result = sceGxmCreateRenderTarget(&params, &_gxmRenderTarget);
189 if (result < 0) {
190 LOGE("sceGxmCreateRenderTarget({}x{}) failed with 0x{:.8x}", targetWidth, targetHeight, std::uint32_t(result));
191 _gxmRenderTarget = nullptr;
192 return false;
193 }
194
195 // The colour surface's stride is the texture's own row pitch, so the GPU renders straight into the
196 // texels the shaders will sample afterwards - no resolve step in between
197 // Tiled, to match the layout the attachment's texture describes (see GxmTexture::EnsureGpuTexture()):
198 // a linear texture cannot sample outside [0, 1], and every one of these targets is later read by a
199 // pass that does. The stride is the tile-padded width the texture allocated.
200 result = sceGxmColorSurfaceInit(&_colorSurface, SCE_GXM_COLOR_FORMAT_U8U8U8U8_ABGR,
201 SCE_GXM_COLOR_SURFACE_TILED, SCE_GXM_COLOR_SURFACE_SCALE_NONE, SCE_GXM_OUTPUT_REGISTER_SIZE_32BIT,
202 std::uint32_t(targetWidth), std::uint32_t(targetHeight), texture->GetSurfaceStride() / 4u, surfaceData);
203 if (result < 0) {
204 LOGE("sceGxmColorSurfaceInit({}x{}) failed with 0x{:.8x}", targetWidth, targetHeight, std::uint32_t(result));
205 sceGxmDestroyRenderTarget(_gxmRenderTarget);
206 _gxmRenderTarget = nullptr;
207 return false;
208 }
209
210 _surfaceTexture = texture;
211 _surfaceData = surfaceData;
212 _surfaceWidth = targetWidth;
213 _surfaceHeight = targetHeight;
214 _surfaceValid = true;
215 }
216
217 renderTarget = _gxmRenderTarget;
218 colorSurface = &_colorSurface;
219 syncObject = _syncObject;
220 width = _surfaceWidth;
221 height = _surfaceHeight;
222 return true;
223 }
224 }