Debian Codegraph @

Sources/nCine/Graphics/RHI/Software/SwTexture.cpp

raw
1 #include "SwTexture.h"
2 #include "SwDevice.h"
3 #include "SwRaster.h"
4
5 #include <cstring>
6
7 namespace nCine::RHI::Software
8 {
9 namespace
10 {
11 // Copies one packed row into the store, expanding a narrower source (RGB8) to a wider store
12 // (RGBA8) by filling the extra channel with 255 (opaque). A same-width copy is a plain memcpy.
13 void CopyExpandRow(std::uint8_t* dst, std::int32_t dstBpp, const std::uint8_t* src, std::int32_t srcBpp, std::int32_t width)
14 {
15 if (srcBpp == dstBpp) {
16 std::memcpy(dst, src, std::size_t(width) * std::size_t(dstBpp));
17 return;
18 }
19 const std::int32_t shared = (srcBpp < dstBpp ? srcBpp : dstBpp);
20 for (std::int32_t x = 0; x < width; x++) {
21 std::int32_t c = 0;
22 for (; c < shared; c++) {
23 dst[x * dstBpp + c] = src[x * srcBpp + c];
24 }
25 for (; c < dstBpp; c++) {
26 dst[x * dstBpp + c] = 255;
27 }
28 }
29 }
30 }
31
32 std::uint32_t SwTexture::_nextHandle = 1;
33 std::uint32_t SwTexture::_nextContentVersion = 0;
34
35 SwTexture::SwTexture(TextureTarget target)
36 : _handle(_nextHandle++), _contentVersion(0), _target(target), _format(PixelFormat::Unknown), _uploadFormat(PixelFormat::Unknown),
37 _width(0), _height(0), _strideBytes(0), _bytesPerPixel(0),
38 _minFilter(nCine::SamplerFilter::Nearest), _magFilter(nCine::SamplerFilter::Nearest), _wrap(SamplerWrapping::ClampToEdge),
39 _textureUnit(0), _isRenderTarget(false)
40 {
41 _swizzle[0] = SwizzleChannel::Red;
42 _swizzle[1] = SwizzleChannel::Green;
43 _swizzle[2] = SwizzleChannel::Blue;
44 _swizzle[3] = SwizzleChannel::Alpha;
45 }
46
47 SwTexture::~SwTexture()
48 {
49 // Clear from the device so a destroyed texture can't dangle in _boundTextures (a later deferred draw would
50 // dereference freed memory in Dispatch)
51 SwDevice::UnbindTexture(this);
52 }
53
54 std::int32_t SwTexture::BytesPerPixel(PixelFormat format)
55 {
56 switch (format) {
57 case PixelFormat::R8: return 1;
58 case PixelFormat::RG8: return 2;
59 case PixelFormat::RGB8: return 3;
60 case PixelFormat::RGBA8: return 4;
61 default: return 0;
62 }
63 }
64
65 void SwTexture::Allocate(PixelFormat format, std::int32_t width, std::int32_t height)
66 {
67 // R8 / RG8 index textures are stored NATIVELY (1 / 2 bytes per texel): the samplers expand each
68 // fetched texel to the 4-byte RGBA working form on the fly, filling the missing channels with 255 -
69 // so an R8 index samples as [index,255,255,255] and an RG8 one as [index,alpha,255,255], the exact
70 // bytes the former promoted-to-RGBA8 store held. That makes indexed sprite atlases and tilesets
71 // (the bulk of this game's texel data) 4x / 2x smaller in memory and in gather bandwidth. Only two
72 // cases still widen to an RGBA8 store: RGB8 (3-byte texels are unaligned, and it is the render-
73 // target format of the shader path), and any texture attached as a render target (the rasterizer
74 // composites 4 bytes per pixel - see SetRenderTarget, which re-widens an already-native store).
75 // The requested format is remembered in _uploadFormat so the palette effect can still distinguish
76 // an R8 index texture (alpha from the palette entry) from an RG8 one (alpha from the texture's G)
77 // even after such a widening.
78 _uploadFormat = format;
79 _format = (format == PixelFormat::RGB8 || (_isRenderTarget && (format == PixelFormat::R8 || format == PixelFormat::RG8))
80 ? PixelFormat::RGBA8 : format);
81 _width = width;
82 _height = height;
83 _bytesPerPixel = BytesPerPixel(_format);
84 _strideBytes = width * _bytesPerPixel;
85 // A deferred tile-renderer command may still reference this texture's current store (the prepared
86 // command snapshots the level-0 pixel pointer at submit); drain the queue before the buffer can be
87 // reallocated so no worker rasterizes from freed memory. A no-op when nothing is queued.
88 if (!_pixels.empty()) {
89 SwRaster::Flush();
90 }
91 _pixels.assign(std::size_t(_strideBytes) * std::size_t(height > 0 ? height : 0), std::uint8_t(0));
92 // The store content changed; the counter is process-global so a stamp is never repeated, even by
93 // a different texture object reusing this one's address
94 _contentVersion = ++_nextContentVersion;
95 }
96
97 void SwTexture::SetRenderTarget(bool isRenderTarget)
98 {
99 _isRenderTarget = isRenderTarget;
100 if (isRenderTarget && _format != PixelFormat::Unknown && _bytesPerPixel != 4) {
101 // Widen a native R8/RG8 store to RGBA8 (the rasterizer composites 4 bytes per pixel). The texels
102 // are discarded - a render target is always fully drawn or cleared before it is read - and
103 // Allocate keeps _uploadFormat at the originally requested format and re-widens because
104 // _isRenderTarget is already set.
105 Allocate(_uploadFormat, _width, _height);
106 }
107 }
108
109 bool SwTexture::Bind(std::uint32_t textureUnit) const
110 {
111 _textureUnit = textureUnit;
112 SwDevice::BindTexture(textureUnit, this);
113 return true;
114 }
115
116 bool SwTexture::Unbind() const
117 {
118 SwDevice::BindTexture(_textureUnit, nullptr);
119 return true;
120 }
121
122 bool SwTexture::Unbind(std::uint32_t textureUnit)
123 {
124 SwDevice::BindTexture(textureUnit, nullptr);
125 return true;
126 }
127
128 void SwTexture::TexImage2D(std::int32_t level, PixelFormat format, bool bgr, std::int32_t width, std::int32_t height, const void* data)
129 {
130 static_cast<void>(bgr);
131 if (level != 0) {
132 // The fast path samples level 0 only; higher levels are accepted but not stored
133 return;
134 }
135 Allocate(format, width, height);
136 if (data != nullptr && !_pixels.empty()) {
137 // `_format` may be wider than the upload (RGB8, or a render-target-widened R8/RG8), so copy
138 // against the source's own bpp and let CopyExpandRow widen each row when the two differ; for
139 // the native R8/RG8 stores the two match and the copy is a plain memcpy
140 const std::int32_t srcBpp = BytesPerPixel(format);
141 const std::int32_t dstBpp = BytesPerPixel(_format);
142 if (srcBpp == dstBpp) {
143 std::memcpy(_pixels.data(), data, _pixels.size());
144 } else {
145 const std::uint8_t* src = static_cast<const std::uint8_t*>(data);
146 for (std::int32_t y = 0; y < _height; y++) {
147 CopyExpandRow(_pixels.data() + std::size_t(y) * _strideBytes,
148 dstBpp, src + std::size_t(y) * std::size_t(_width) * srcBpp, srcBpp, _width);
149 }
150 }
151 _contentVersion = ++_nextContentVersion;
152 }
153 }
154
155 void SwTexture::TexSubImage2D(std::int32_t level, std::int32_t xoffset, std::int32_t yoffset, std::int32_t width, std::int32_t height, PixelFormat format, bool bgr, const void* data)
156 {
157 static_cast<void>(bgr);
158 if (level != 0 || data == nullptr || _pixels.empty()) {
159 return;
160 }
161 const std::int32_t srcBpp = BytesPerPixel(format);
162 const std::int32_t dstBpp = BytesPerPixel(_format);
163 for (std::int32_t y = 0; y < height; y++) {
164 const std::int32_t dstY = yoffset + y;
165 if (dstY < 0 || dstY >= _height) {
166 continue;
167 }
168 // Clamp the destination span to the texture so a sub-rect running past an edge can never write
169 // past the row (and past the store on the last row)
170 std::int32_t dstX = xoffset;
171 std::int32_t copyW = width;
172 std::int32_t srcX0 = 0;
173 if (dstX < 0) {
174 srcX0 = -dstX;
175 copyW += dstX;
176 dstX = 0;
177 }
178 if (dstX + copyW > _width) {
179 copyW = _width - dstX;
180 }
181 if (copyW <= 0) {
182 continue;
183 }
184 const std::uint8_t* srcRow = static_cast<const std::uint8_t*>(data) + std::size_t(y) * std::size_t(width) * srcBpp + std::size_t(srcX0) * srcBpp;
185 std::uint8_t* dstRow = _pixels.data() + std::size_t(dstY) * _strideBytes + std::size_t(dstX) * dstBpp;
186 CopyExpandRow(dstRow, dstBpp, srcRow, srcBpp, copyW);
187 }
188 _contentVersion = ++_nextContentVersion;
189 }
190
191 void SwTexture::TexStorage2D(std::int32_t levels, PixelFormat format, std::int32_t width, std::int32_t height)
192 {
193 static_cast<void>(levels);
194 Allocate(format, width, height);
195 }
196
197 void SwTexture::CompressedTexImage2D(std::int32_t level, PixelFormat format, std::int32_t width, std::int32_t height, std::int32_t imageSize, const void* data)
198 {
199 static_cast<void>(level);
200 static_cast<void>(format);
201 static_cast<void>(width);
202 static_cast<void>(height);
203 static_cast<void>(imageSize);
204 static_cast<void>(data);
205 }
206
207 void SwTexture::CompressedTexSubImage2D(std::int32_t level, std::int32_t xoffset, std::int32_t yoffset, std::int32_t width, std::int32_t height, PixelFormat format, std::int32_t imageSize, const void* data)
208 {
209 static_cast<void>(level);
210 static_cast<void>(xoffset);
211 static_cast<void>(yoffset);
212 static_cast<void>(width);
213 static_cast<void>(height);
214 static_cast<void>(format);
215 static_cast<void>(imageSize);
216 static_cast<void>(data);
217 }
218
219 void SwTexture::GetTexImage(std::int32_t level, PixelFormat format, bool bgr, void* pixels)
220 {
221 static_cast<void>(level);
222 static_cast<void>(bgr);
223 if (pixels == nullptr || _pixels.empty()) {
224 return;
225 }
226 const std::int32_t dstBpp = BytesPerPixel(format);
227 if (dstBpp <= 0 || dstBpp == _bytesPerPixel) {
228 // The store already matches the requested layout (the common case - native R8/RG8 reads back
229 // exactly the bytes that were uploaded, which the promoted store never did)
230 std::memcpy(pixels, _pixels.data(), _pixels.size());
231 return;
232 }
233 // A widened store read back at the original format (RGB8 upload, or a render-target-widened
234 // R8/RG8): narrow each texel to the requested channel count (or widen with 255, matching the
235 // samplers' expansion, if the caller asks for more channels than are stored)
236 const std::size_t count = std::size_t(_width) * std::size_t(_height > 0 ? _height : 0);
237 const std::uint8_t* src = _pixels.data();
238 std::uint8_t* dst = static_cast<std::uint8_t*>(pixels);
239 const std::int32_t shared = (_bytesPerPixel < dstBpp ? _bytesPerPixel : dstBpp);
240 for (std::size_t i = 0; i < count; i++) {
241 std::int32_t c = 0;
242 for (; c < shared; c++) {
243 dst[c] = src[c];
244 }
245 for (; c < dstBpp; c++) {
246 dst[c] = 255;
247 }
248 src += _bytesPerPixel;
249 dst += dstBpp;
250 }
251 }
252
253 void SwTexture::SetMinFiltering(nCine::SamplerFilter filter)
254 {
255 _minFilter = filter;
256 }
257
258 void SwTexture::SetMagFiltering(nCine::SamplerFilter filter)
259 {
260 _magFilter = filter;
261 }
262
263 void SwTexture::SetWrap(SamplerWrapping wrap)
264 {
265 _wrap = wrap;
266 }
267
268 void SwTexture::SetSwizzle(SwizzleChannel r, SwizzleChannel g, SwizzleChannel b, SwizzleChannel a)
269 {
270 _swizzle[0] = r;
271 _swizzle[1] = g;
272 _swizzle[2] = b;
273 _swizzle[3] = a;
274 }
275
276 void SwTexture::SetMaxLevel(std::int32_t maxLevel)
277 {
278 static_cast<void>(maxLevel);
279 }
280
281 void SwTexture::SetUnpackAlignment(std::int32_t alignment)
282 {
283 static_cast<void>(alignment);
284 }
285
286 void SwTexture::SetObjectLabel(StringView label)
287 {
288 static_cast<void>(label);
289 }
290 }