blob: 062b0d727902a5626816bbfd129bef2e1e948b02 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef sw_Surface_hpp
16#define sw_Surface_hpp
17
18#include "Color.hpp"
19#include "Main/Config.hpp"
20#include "Common/Resource.hpp"
21
22namespace sw
23{
24 class Resource;
25
Alexis Hetu10c74a62017-11-29 14:00:32 -050026 template <typename T> struct RectT
Nicolas Capens0bac2852016-05-07 06:09:58 -040027 {
Alexis Hetu10c74a62017-11-29 14:00:32 -050028 RectT() {}
29 RectT(T x0i, T y0i, T x1i, T y1i) : x0(x0i), y0(y0i), x1(x1i), y1(y1i) {}
Nicolas Capens0bac2852016-05-07 06:09:58 -040030
Alexis Hetu10c74a62017-11-29 14:00:32 -050031 void clip(T minX, T minY, T maxX, T maxY)
32 {
33 x0 = clamp(x0, minX, maxX);
34 y0 = clamp(y0, minY, maxY);
35 x1 = clamp(x1, minX, maxX);
36 y1 = clamp(y1, minY, maxY);
37 }
Nicolas Capens0bac2852016-05-07 06:09:58 -040038
Alexis Hetu10c74a62017-11-29 14:00:32 -050039 T width() const { return x1 - x0; }
40 T height() const { return y1 - y0; }
Nicolas Capens0bac2852016-05-07 06:09:58 -040041
Alexis Hetu10c74a62017-11-29 14:00:32 -050042 T x0; // Inclusive
43 T y0; // Inclusive
44 T x1; // Exclusive
45 T y1; // Exclusive
Nicolas Capens0bac2852016-05-07 06:09:58 -040046 };
47
Alexis Hetu10c74a62017-11-29 14:00:32 -050048 typedef RectT<int> Rect;
49 typedef RectT<float> RectF;
50
Nicolas Capensbfa23b32017-12-11 10:06:37 -050051 template<typename T> struct SliceRectT : public RectT<T>
Nicolas Capens0bac2852016-05-07 06:09:58 -040052 {
Alexis Hetu10c74a62017-11-29 14:00:32 -050053 SliceRectT() : slice(0) {}
54 SliceRectT(const RectT<T>& rect) : RectT<T>(rect), slice(0) {}
55 SliceRectT(const RectT<T>& rect, int s) : RectT<T>(rect), slice(s) {}
56 SliceRectT(T x0, T y0, T x1, T y1, int s) : RectT<T>(x0, y0, x1, y1), slice(s) {}
Nicolas Capens0bac2852016-05-07 06:09:58 -040057 int slice;
58 };
59
Alexis Hetu10c74a62017-11-29 14:00:32 -050060 typedef SliceRectT<int> SliceRect;
61 typedef SliceRectT<float> SliceRectF;
62
Nicolas Capens0bac2852016-05-07 06:09:58 -040063 enum Format : unsigned char
64 {
65 FORMAT_NULL,
66
67 FORMAT_A8,
68 FORMAT_R8I,
69 FORMAT_R8UI,
Nicolas Capens975adb72017-12-19 15:34:20 -050070 FORMAT_R8_SNORM,
71 FORMAT_R8,
Nicolas Capens0bac2852016-05-07 06:09:58 -040072 FORMAT_R16I,
73 FORMAT_R16UI,
74 FORMAT_R32I,
75 FORMAT_R32UI,
76 FORMAT_R3G3B2,
77 FORMAT_A8R3G3B2,
78 FORMAT_X4R4G4B4,
79 FORMAT_A4R4G4B4,
80 FORMAT_R4G4B4A4,
81 FORMAT_R5G6B5,
82 FORMAT_R8G8B8,
83 FORMAT_B8G8R8,
84 FORMAT_X8R8G8B8,
85 FORMAT_A8R8G8B8,
86 FORMAT_X8B8G8R8I,
87 FORMAT_X8B8G8R8UI,
Nicolas Capens975adb72017-12-19 15:34:20 -050088 FORMAT_X8B8G8R8_SNORM,
89 FORMAT_X8B8G8R8,
Nicolas Capens0bac2852016-05-07 06:09:58 -040090 FORMAT_A8B8G8R8I,
91 FORMAT_A8B8G8R8UI,
Nicolas Capens975adb72017-12-19 15:34:20 -050092 FORMAT_A8B8G8R8_SNORM,
93 FORMAT_A8B8G8R8,
Nicolas Capens0bac2852016-05-07 06:09:58 -040094 FORMAT_SRGB8_X8,
95 FORMAT_SRGB8_A8,
96 FORMAT_X1R5G5B5,
97 FORMAT_A1R5G5B5,
98 FORMAT_R5G5B5A1,
99 FORMAT_G8R8I,
100 FORMAT_G8R8UI,
Nicolas Capens975adb72017-12-19 15:34:20 -0500101 FORMAT_G8R8_SNORM,
102 FORMAT_G8R8,
103 FORMAT_G16R16,
Nicolas Capens0bac2852016-05-07 06:09:58 -0400104 FORMAT_G16R16I,
105 FORMAT_G16R16UI,
106 FORMAT_G32R32I,
107 FORMAT_G32R32UI,
108 FORMAT_A2R10G10B10,
109 FORMAT_A2B10G10R10,
Nicolas Capens5555af42017-12-14 13:14:03 -0500110 FORMAT_A2B10G10R10UI,
Nicolas Capens975adb72017-12-19 15:34:20 -0500111 FORMAT_A16B16G16R16,
Nicolas Capens0bac2852016-05-07 06:09:58 -0400112 FORMAT_X16B16G16R16I,
113 FORMAT_X16B16G16R16UI,
114 FORMAT_A16B16G16R16I,
115 FORMAT_A16B16G16R16UI,
116 FORMAT_X32B32G32R32I,
117 FORMAT_X32B32G32R32UI,
118 FORMAT_A32B32G32R32I,
119 FORMAT_A32B32G32R32UI,
120 // Paletted formats
121 FORMAT_P8,
122 FORMAT_A8P8,
123 // Compressed formats
124 FORMAT_DXT1,
125 FORMAT_DXT3,
126 FORMAT_DXT5,
127 FORMAT_ATI1,
128 FORMAT_ATI2,
129 FORMAT_ETC1,
130 FORMAT_R11_EAC,
131 FORMAT_SIGNED_R11_EAC,
132 FORMAT_RG11_EAC,
133 FORMAT_SIGNED_RG11_EAC,
134 FORMAT_RGB8_ETC2,
135 FORMAT_SRGB8_ETC2,
136 FORMAT_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
137 FORMAT_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
138 FORMAT_RGBA8_ETC2_EAC,
139 FORMAT_SRGB8_ALPHA8_ETC2_EAC,
140 FORMAT_RGBA_ASTC_4x4_KHR,
141 FORMAT_RGBA_ASTC_5x4_KHR,
142 FORMAT_RGBA_ASTC_5x5_KHR,
143 FORMAT_RGBA_ASTC_6x5_KHR,
144 FORMAT_RGBA_ASTC_6x6_KHR,
145 FORMAT_RGBA_ASTC_8x5_KHR,
146 FORMAT_RGBA_ASTC_8x6_KHR,
147 FORMAT_RGBA_ASTC_8x8_KHR,
148 FORMAT_RGBA_ASTC_10x5_KHR,
149 FORMAT_RGBA_ASTC_10x6_KHR,
150 FORMAT_RGBA_ASTC_10x8_KHR,
151 FORMAT_RGBA_ASTC_10x10_KHR,
152 FORMAT_RGBA_ASTC_12x10_KHR,
153 FORMAT_RGBA_ASTC_12x12_KHR,
154 FORMAT_SRGB8_ALPHA8_ASTC_4x4_KHR,
155 FORMAT_SRGB8_ALPHA8_ASTC_5x4_KHR,
156 FORMAT_SRGB8_ALPHA8_ASTC_5x5_KHR,
157 FORMAT_SRGB8_ALPHA8_ASTC_6x5_KHR,
158 FORMAT_SRGB8_ALPHA8_ASTC_6x6_KHR,
159 FORMAT_SRGB8_ALPHA8_ASTC_8x5_KHR,
160 FORMAT_SRGB8_ALPHA8_ASTC_8x6_KHR,
161 FORMAT_SRGB8_ALPHA8_ASTC_8x8_KHR,
162 FORMAT_SRGB8_ALPHA8_ASTC_10x5_KHR,
163 FORMAT_SRGB8_ALPHA8_ASTC_10x6_KHR,
164 FORMAT_SRGB8_ALPHA8_ASTC_10x8_KHR,
165 FORMAT_SRGB8_ALPHA8_ASTC_10x10_KHR,
166 FORMAT_SRGB8_ALPHA8_ASTC_12x10_KHR,
167 FORMAT_SRGB8_ALPHA8_ASTC_12x12_KHR,
168 // Floating-point formats
169 FORMAT_A16F,
170 FORMAT_R16F,
171 FORMAT_G16R16F,
172 FORMAT_B16G16R16F,
Nicolas Capensa6bc61d2017-12-20 11:07:45 -0500173 FORMAT_X16B16G16R16F,
Nicolas Capens0bac2852016-05-07 06:09:58 -0400174 FORMAT_A16B16G16R16F,
Nicolas Capens67fdd832017-12-21 11:20:54 -0500175 FORMAT_X16B16G16R16F_UNSIGNED,
Nicolas Capens0bac2852016-05-07 06:09:58 -0400176 FORMAT_A32F,
177 FORMAT_R32F,
178 FORMAT_G32R32F,
179 FORMAT_B32G32R32F,
180 FORMAT_X32B32G32R32F,
181 FORMAT_A32B32G32R32F,
Nicolas Capens67fdd832017-12-21 11:20:54 -0500182 FORMAT_X32B32G32R32F_UNSIGNED,
Nicolas Capens0bac2852016-05-07 06:09:58 -0400183 // Bump map formats
184 FORMAT_V8U8,
185 FORMAT_L6V5U5,
186 FORMAT_Q8W8V8U8,
187 FORMAT_X8L8V8U8,
188 FORMAT_A2W10V10U10,
189 FORMAT_V16U16,
190 FORMAT_A16W16V16U16,
191 FORMAT_Q16W16V16U16,
192 // Luminance formats
193 FORMAT_L8,
194 FORMAT_A4L4,
195 FORMAT_L16,
196 FORMAT_A8L8,
197 FORMAT_L16F,
198 FORMAT_A16L16F,
199 FORMAT_L32F,
200 FORMAT_A32L32F,
201 // Depth/stencil formats
202 FORMAT_D16,
203 FORMAT_D32,
204 FORMAT_D24X8,
205 FORMAT_D24S8,
206 FORMAT_D24FS8,
207 FORMAT_D32F, // Quad layout
Nicolas Capens57e7cea2017-12-13 22:25:04 -0500208 FORMAT_D32FS8, // Quad layout
Nicolas Capens0bac2852016-05-07 06:09:58 -0400209 FORMAT_D32F_COMPLEMENTARY, // Quad layout, 1 - z
Nicolas Capens57e7cea2017-12-13 22:25:04 -0500210 FORMAT_D32FS8_COMPLEMENTARY, // Quad layout, 1 - z
Nicolas Capens0bac2852016-05-07 06:09:58 -0400211 FORMAT_D32F_LOCKABLE, // Linear layout
212 FORMAT_D32FS8_TEXTURE, // Linear layout, no PCF
Nicolas Capens57e7cea2017-12-13 22:25:04 -0500213 FORMAT_D32F_SHADOW, // Linear layout, PCF
Nicolas Capens0bac2852016-05-07 06:09:58 -0400214 FORMAT_D32FS8_SHADOW, // Linear layout, PCF
215 FORMAT_DF24S8,
216 FORMAT_DF16S8,
217 FORMAT_INTZ,
218 FORMAT_S8,
219 // Quad layout framebuffer
220 FORMAT_X8G8R8B8Q,
221 FORMAT_A8G8R8B8Q,
222 // YUV formats
223 FORMAT_YV12_BT601,
224 FORMAT_YV12_BT709,
225 FORMAT_YV12_JFIF, // Full-swing BT.601
226
227 FORMAT_LAST = FORMAT_YV12_JFIF
228 };
229
230 enum Lock
231 {
232 LOCK_UNLOCKED,
233 LOCK_READONLY,
234 LOCK_WRITEONLY,
235 LOCK_READWRITE,
236 LOCK_DISCARD
237 };
238
Nicolas Capens9ed48ba2017-05-11 11:25:00 -0400239 class [[clang::lto_visibility_public]] Surface
Nicolas Capens0bac2852016-05-07 06:09:58 -0400240 {
241 private:
242 struct Buffer
243 {
Nicolas Capensbfa23b32017-12-11 10:06:37 -0500244 friend Surface;
245
246 private:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400247 void write(int x, int y, int z, const Color<float> &color);
248 void write(int x, int y, const Color<float> &color);
249 void write(void *element, const Color<float> &color);
250 Color<float> read(int x, int y, int z) const;
251 Color<float> read(int x, int y) const;
252 Color<float> read(void *element) const;
253 Color<float> sample(float x, float y, float z) const;
Nicolas Capensbfa23b32017-12-11 10:06:37 -0500254 Color<float> sample(float x, float y, int layer) const;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400255
256 void *lockRect(int x, int y, int z, Lock lock);
257 void unlockRect();
258
259 void *buffer;
260 int width;
261 int height;
262 int depth;
Nicolas Capensbfa23b32017-12-11 10:06:37 -0500263 short border;
264 short samples;
265
Nicolas Capens0bac2852016-05-07 06:09:58 -0400266 int bytes;
267 int pitchB;
268 int pitchP;
269 int sliceB;
270 int sliceP;
Nicolas Capensbfa23b32017-12-11 10:06:37 -0500271
Nicolas Capens0bac2852016-05-07 06:09:58 -0400272 Format format;
Alexis Hetu6b164c32017-09-20 11:24:52 -0400273 AtomicInt lock;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400274
Nicolas Capens73e18c12017-11-28 13:31:35 -0500275 bool dirty; // Sibling internal/external buffer doesn't match.
Nicolas Capens0bac2852016-05-07 06:09:58 -0400276 };
277
Nicolas Capensf41f0332017-05-30 15:25:50 -0400278 protected:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400279 Surface(int width, int height, int depth, Format format, void *pixels, int pitch, int slice);
Nicolas Capensbfa23b32017-12-11 10:06:37 -0500280 Surface(Resource *texture, int width, int height, int depth, int border, int samples, Format format, bool lockable, bool renderTarget, int pitchP = 0);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400281
Nicolas Capensf41f0332017-05-30 15:25:50 -0400282 public:
283 static Surface *create(int width, int height, int depth, Format format, void *pixels, int pitch, int slice);
Nicolas Capensbfa23b32017-12-11 10:06:37 -0500284 static Surface *create(Resource *texture, int width, int height, int depth, int border, int samples, Format format, bool lockable, bool renderTarget, int pitchP = 0);
Nicolas Capensf41f0332017-05-30 15:25:50 -0400285
286 virtual ~Surface() = 0;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400287
288 inline void *lock(int x, int y, int z, Lock lock, Accessor client, bool internal = false);
289 inline void unlock(bool internal = false);
290 inline int getWidth() const;
291 inline int getHeight() const;
292 inline int getDepth() const;
Alexis Hetu9c6d5222016-11-29 17:02:14 -0500293 inline int getBorder() const;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400294 inline Format getFormat(bool internal = false) const;
295 inline int getPitchB(bool internal = false) const;
296 inline int getPitchP(bool internal = false) const;
297 inline int getSliceB(bool internal = false) const;
298 inline int getSliceP(bool internal = false) const;
299
300 void *lockExternal(int x, int y, int z, Lock lock, Accessor client);
301 void unlockExternal();
302 inline Format getExternalFormat() const;
303 inline int getExternalPitchB() const;
304 inline int getExternalPitchP() const;
305 inline int getExternalSliceB() const;
306 inline int getExternalSliceP() const;
307
Nicolas Capensf41f0332017-05-30 15:25:50 -0400308 virtual void *lockInternal(int x, int y, int z, Lock lock, Accessor client) = 0;
309 virtual void unlockInternal() = 0;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400310 inline Format getInternalFormat() const;
311 inline int getInternalPitchB() const;
312 inline int getInternalPitchP() const;
313 inline int getInternalSliceB() const;
314 inline int getInternalSliceP() const;
315
Alexis Hetua52dfbd2016-10-05 17:03:30 -0400316 void *lockStencil(int x, int y, int front, Accessor client);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400317 void unlockStencil();
Alexis Hetua52dfbd2016-10-05 17:03:30 -0400318 inline Format getStencilFormat() const;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400319 inline int getStencilPitchB() const;
320 inline int getStencilSliceB() const;
321
Antoine Labourfc2b84d2017-06-09 18:14:05 -0700322 void sync(); // Wait for lock(s) to be released.
323 inline bool isUnlocked() const; // Only reliable after sync().
Nicolas Capensbf7a8142017-05-19 10:57:28 -0400324
Nicolas Capensbfa23b32017-12-11 10:06:37 -0500325 inline int getSamples() const;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400326 inline int getMultiSampleCount() const;
327 inline int getSuperSampleCount() const;
328
Nicolas Capens426cb5e2017-07-20 14:14:09 -0400329 bool isEntire(const Rect& rect) const;
330 Rect getRect() const;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400331 void clearDepth(float depth, int x0, int y0, int width, int height);
332 void clearStencil(unsigned char stencil, unsigned char mask, int x0, int y0, int width, int height);
333 void fill(const Color<float> &color, int x0, int y0, int width, int height);
334
335 Color<float> readExternal(int x, int y, int z) const;
336 Color<float> readExternal(int x, int y) const;
337 Color<float> sampleExternal(float x, float y, float z) const;
338 Color<float> sampleExternal(float x, float y) const;
339 void writeExternal(int x, int y, int z, const Color<float> &color);
340 void writeExternal(int x, int y, const Color<float> &color);
341
342 void copyInternal(const Surface* src, int x, int y, float srcX, float srcY, bool filter);
343 void copyInternal(const Surface* src, int x, int y, int z, float srcX, float srcY, float srcZ, bool filter);
344
Alexis Hetua76a1bf2016-11-29 17:17:26 -0500345 enum Edge { TOP, BOTTOM, RIGHT, LEFT };
346 void copyCubeEdge(Edge dstEdge, Surface *src, Edge srcEdge);
347 void computeCubeCorner(int x0, int y0, int x1, int y1);
348
Nicolas Capens0bac2852016-05-07 06:09:58 -0400349 bool hasStencil() const;
350 bool hasDepth() const;
351 bool hasPalette() const;
352 bool isRenderTarget() const;
353
Nicolas Capens73e18c12017-11-28 13:31:35 -0500354 bool hasDirtyContents() const;
355 void markContentsClean();
Nicolas Capens0bac2852016-05-07 06:09:58 -0400356 inline bool isExternalDirty() const;
357 Resource *getResource();
358
359 static int bytes(Format format);
Alexis Hetu9c6d5222016-11-29 17:02:14 -0500360 static int pitchB(int width, int border, Format format, bool target);
361 static int pitchP(int width, int border, Format format, bool target);
362 static int sliceB(int width, int height, int border, Format format, bool target);
363 static int sliceP(int width, int height, int border, Format format, bool target);
Nicolas Capensbfa23b32017-12-11 10:06:37 -0500364 static unsigned int size(int width, int height, int depth, int border, int samples, Format format); // FIXME: slice * depth
Nicolas Capens0bac2852016-05-07 06:09:58 -0400365
366 static bool isStencil(Format format);
367 static bool isDepth(Format format);
Alexis Hetub9dda642016-10-06 11:25:32 -0400368 static bool hasQuadLayout(Format format);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400369 static bool isPalette(Format format);
370
371 static bool isFloatFormat(Format format);
372 static bool isUnsignedComponent(Format format, int component);
373 static bool isSRGBreadable(Format format);
374 static bool isSRGBwritable(Format format);
Nicolas Capens5555af42017-12-14 13:14:03 -0500375 static bool isSRGBformat(Format format);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400376 static bool isCompressed(Format format);
Nicolas Capens492887a2017-03-27 14:50:51 -0400377 static bool isSignedNonNormalizedInteger(Format format);
378 static bool isUnsignedNonNormalizedInteger(Format format);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400379 static bool isNonNormalizedInteger(Format format);
Nicolas Capens492887a2017-03-27 14:50:51 -0400380 static bool isNormalizedInteger(Format format);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400381 static int componentCount(Format format);
382
383 static void setTexturePalette(unsigned int *palette);
384
Nicolas Capensbf7a8142017-05-19 10:57:28 -0400385 private:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400386 sw::Resource *resource;
387
Nicolas Capens0bac2852016-05-07 06:09:58 -0400388 typedef unsigned char byte;
389 typedef unsigned short word;
390 typedef unsigned int dword;
391 typedef uint64_t qword;
392
393 #if S3TC_SUPPORT
394 struct DXT1
395 {
396 word c0;
397 word c1;
398 dword lut;
399 };
400
401 struct DXT3
402 {
403 qword a;
404
405 word c0;
406 word c1;
407 dword lut;
408 };
409
410 struct DXT5
411 {
412 union
413 {
414 struct
415 {
416 byte a0;
417 byte a1;
418 };
419
420 qword alut; // Skip first 16 bit
421 };
422
423 word c0;
424 word c1;
425 dword clut;
426 };
427 #endif
428
429 struct ATI2
430 {
431 union
432 {
433 struct
434 {
435 byte y0;
436 byte y1;
437 };
438
439 qword ylut; // Skip first 16 bit
440 };
441
442 union
443 {
444 struct
445 {
446 byte x0;
447 byte x1;
448 };
449
450 qword xlut; // Skip first 16 bit
451 };
452 };
453
454 struct ATI1
455 {
456 union
457 {
458 struct
459 {
460 byte r0;
461 byte r1;
462 };
463
464 qword rlut; // Skip first 16 bit
465 };
466 };
467
Alexis Hetu9c6d5222016-11-29 17:02:14 -0500468 static void decodeR8G8B8(Buffer &destination, Buffer &source);
469 static void decodeX1R5G5B5(Buffer &destination, Buffer &source);
470 static void decodeA1R5G5B5(Buffer &destination, Buffer &source);
471 static void decodeX4R4G4B4(Buffer &destination, Buffer &source);
472 static void decodeA4R4G4B4(Buffer &destination, Buffer &source);
473 static void decodeP8(Buffer &destination, Buffer &source);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400474
475 #if S3TC_SUPPORT
Alexis Hetu9c6d5222016-11-29 17:02:14 -0500476 static void decodeDXT1(Buffer &internal, Buffer &external);
477 static void decodeDXT3(Buffer &internal, Buffer &external);
478 static void decodeDXT5(Buffer &internal, Buffer &external);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400479 #endif
Alexis Hetu9c6d5222016-11-29 17:02:14 -0500480 static void decodeATI1(Buffer &internal, Buffer &external);
481 static void decodeATI2(Buffer &internal, Buffer &external);
482 static void decodeEAC(Buffer &internal, Buffer &external, int nbChannels, bool isSigned);
483 static void decodeETC2(Buffer &internal, Buffer &external, int nbAlphaBits, bool isSRGB);
484 static void decodeASTC(Buffer &internal, Buffer &external, int xSize, int ySize, int zSize, bool isSRGB);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400485
486 static void update(Buffer &destination, Buffer &source);
487 static void genericUpdate(Buffer &destination, Buffer &source);
Nicolas Capensbfa23b32017-12-11 10:06:37 -0500488 static void *allocateBuffer(int width, int height, int depth, int border, int samples, Format format);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400489 static void memfill4(void *buffer, int pattern, int bytes);
490
491 bool identicalFormats() const;
492 Format selectInternalFormat(Format format) const;
493
494 void resolve();
495
496 Buffer external;
497 Buffer internal;
498 Buffer stencil;
499
500 const bool lockable;
501 const bool renderTarget;
502
Nicolas Capens73e18c12017-11-28 13:31:35 -0500503 bool dirtyContents; // Sibling surfaces need updating (mipmaps / cube borders).
Nicolas Capens0bac2852016-05-07 06:09:58 -0400504 unsigned int paletteUsed;
505
506 static unsigned int *palette; // FIXME: Not multi-device safe
507 static unsigned int paletteID;
508
509 bool hasParent;
510 bool ownExternal;
511 };
512}
513
514#undef min
515#undef max
516
517namespace sw
518{
519 void *Surface::lock(int x, int y, int z, Lock lock, Accessor client, bool internal)
520 {
521 return internal ? lockInternal(x, y, z, lock, client) : lockExternal(x, y, z, lock, client);
522 }
523
524 void Surface::unlock(bool internal)
525 {
526 return internal ? unlockInternal() : unlockExternal();
527 }
528
529 int Surface::getWidth() const
530 {
531 return external.width;
532 }
533
534 int Surface::getHeight() const
535 {
536 return external.height;
537 }
538
539 int Surface::getDepth() const
540 {
541 return external.depth;
542 }
543
Alexis Hetu9c6d5222016-11-29 17:02:14 -0500544 int Surface::getBorder() const
545 {
546 return internal.border;
547 }
548
Nicolas Capens0bac2852016-05-07 06:09:58 -0400549 Format Surface::getFormat(bool internal) const
550 {
551 return internal ? getInternalFormat() : getExternalFormat();
552 }
553
554 int Surface::getPitchB(bool internal) const
555 {
556 return internal ? getInternalPitchB() : getExternalPitchB();
557 }
558
559 int Surface::getPitchP(bool internal) const
560 {
Nicolas Capens26b41162017-02-14 22:42:58 -0500561 return internal ? getInternalPitchP() : getExternalPitchP();
Nicolas Capens0bac2852016-05-07 06:09:58 -0400562 }
563
564 int Surface::getSliceB(bool internal) const
565 {
566 return internal ? getInternalSliceB() : getExternalSliceB();
567 }
568
569 int Surface::getSliceP(bool internal) const
570 {
Nicolas Capens26b41162017-02-14 22:42:58 -0500571 return internal ? getInternalSliceP() : getExternalSliceP();
Nicolas Capens0bac2852016-05-07 06:09:58 -0400572 }
573
574 Format Surface::getExternalFormat() const
575 {
576 return external.format;
577 }
578
579 int Surface::getExternalPitchB() const
580 {
581 return external.pitchB;
582 }
583
584 int Surface::getExternalPitchP() const
585 {
586 return external.pitchP;
587 }
588
589 int Surface::getExternalSliceB() const
590 {
591 return external.sliceB;
592 }
593
594 int Surface::getExternalSliceP() const
595 {
596 return external.sliceP;
597 }
598
599 Format Surface::getInternalFormat() const
600 {
601 return internal.format;
602 }
603
604 int Surface::getInternalPitchB() const
605 {
606 return internal.pitchB;
607 }
608
609 int Surface::getInternalPitchP() const
610 {
611 return internal.pitchP;
612 }
613
614 int Surface::getInternalSliceB() const
615 {
616 return internal.sliceB;
617 }
618
619 int Surface::getInternalSliceP() const
620 {
621 return internal.sliceP;
622 }
623
Alexis Hetua52dfbd2016-10-05 17:03:30 -0400624 Format Surface::getStencilFormat() const
625 {
626 return stencil.format;
627 }
628
Nicolas Capens0bac2852016-05-07 06:09:58 -0400629 int Surface::getStencilPitchB() const
630 {
631 return stencil.pitchB;
632 }
633
634 int Surface::getStencilSliceB() const
635 {
636 return stencil.sliceB;
637 }
638
Nicolas Capensbfa23b32017-12-11 10:06:37 -0500639 int Surface::getSamples() const
640 {
641 return internal.samples;
642 }
643
Nicolas Capens0bac2852016-05-07 06:09:58 -0400644 int Surface::getMultiSampleCount() const
645 {
Nicolas Capensbfa23b32017-12-11 10:06:37 -0500646 return sw::min((int)internal.samples, 4);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400647 }
648
649 int Surface::getSuperSampleCount() const
650 {
Nicolas Capensbfa23b32017-12-11 10:06:37 -0500651 return internal.samples > 4 ? internal.samples / 4 : 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400652 }
653
Antoine Labourfc2b84d2017-06-09 18:14:05 -0700654 bool Surface::isUnlocked() const
655 {
656 return external.lock == LOCK_UNLOCKED &&
657 internal.lock == LOCK_UNLOCKED &&
658 stencil.lock == LOCK_UNLOCKED;
659 }
660
Nicolas Capens0bac2852016-05-07 06:09:58 -0400661 bool Surface::isExternalDirty() const
662 {
663 return external.buffer && external.buffer != internal.buffer && external.dirty;
664 }
665}
666
667#endif // sw_Surface_hpp