blob: c21ef6b28bbe8148b3b95c7fe280c6e641e61ccd [file] [log] [blame]
Geoff Lang051dbc72015-01-05 15:48:58 -05001//
Jamie Madilld2b50a02016-06-09 00:13:35 -07002// Copyright 2016 The ANGLE Project Authors. All rights reserved.
Geoff Lang051dbc72015-01-05 15:48:58 -05003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
Jamie Madilld2b50a02016-06-09 00:13:35 -07006// renderer_utils:
7// Helper methods pertaining to most or all back-ends.
8//
Geoff Lang051dbc72015-01-05 15:48:58 -05009
Jamie Madilld2b50a02016-06-09 00:13:35 -070010#include "libANGLE/renderer/renderer_utils.h"
Geoff Lang051dbc72015-01-05 15:48:58 -050011
Geoff Lang6e4cfce2016-06-13 15:06:31 -040012#include "image_util/copyimage.h"
13#include "image_util/imageformats.h"
14
Jamie Madill222c5172017-07-19 16:15:42 -040015#include "libANGLE/AttributeMap.h"
Jamie Madill42975642017-10-12 12:31:51 -040016#include "libANGLE/Context.h"
Jamie Madilld2b50a02016-06-09 00:13:35 -070017#include "libANGLE/formatutils.h"
Jamie Madill42975642017-10-12 12:31:51 -040018#include "libANGLE/renderer/ContextImpl.h"
Jamie Madill86e0b7f2016-08-09 11:10:29 -040019#include "libANGLE/renderer/Format.h"
Geoff Lang6e4cfce2016-06-13 15:06:31 -040020
21#include <string.h>
Geoff Lang051dbc72015-01-05 15:48:58 -050022
23namespace rx
24{
25
Jamie Madilld2b50a02016-06-09 00:13:35 -070026namespace
27{
28typedef std::pair<gl::FormatType, ColorWriteFunction> FormatWriteFunctionPair;
29typedef std::map<gl::FormatType, ColorWriteFunction> FormatWriteFunctionMap;
Geoff Lang051dbc72015-01-05 15:48:58 -050030
Jamie Madilld2b50a02016-06-09 00:13:35 -070031static inline void InsertFormatWriteFunctionMapping(FormatWriteFunctionMap *map,
32 GLenum format,
33 GLenum type,
Geoff Lang051dbc72015-01-05 15:48:58 -050034 ColorWriteFunction writeFunc)
35{
Jamie Madilld2b50a02016-06-09 00:13:35 -070036 map->insert(FormatWriteFunctionPair(gl::FormatType(format, type), writeFunc));
Geoff Lang051dbc72015-01-05 15:48:58 -050037}
38
39static FormatWriteFunctionMap BuildFormatWriteFunctionMap()
40{
Geoff Lang6e4cfce2016-06-13 15:06:31 -040041 using namespace angle; // For image writing functions
42
Geoff Lang051dbc72015-01-05 15:48:58 -050043 FormatWriteFunctionMap map;
44
Jamie Madilld2b50a02016-06-09 00:13:35 -070045 // clang-format off
Geoff Lang051dbc72015-01-05 15:48:58 -050046 // | Format | Type | Color write function |
47 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_BYTE, WriteColor<R8G8B8A8, GLfloat> );
48 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_BYTE, WriteColor<R8G8B8A8S, GLfloat> );
49 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, WriteColor<R4G4B4A4, GLfloat> );
50 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, WriteColor<R5G5B5A1, GLfloat> );
51 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, WriteColor<R10G10B10A2, GLfloat> );
52 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_FLOAT, WriteColor<R32G32B32A32F, GLfloat>);
53 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_HALF_FLOAT, WriteColor<R16G16B16A16F, GLfloat>);
54 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_HALF_FLOAT_OES, WriteColor<R16G16B16A16F, GLfloat>);
Vincent Lang25ab4512016-05-13 18:13:59 +020055 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_SHORT,
56 WriteColor<R16G16B16A16, GLfloat>);
57 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_SHORT, WriteColor<R16G16B16A16S, GLfloat>);
Geoff Lang051dbc72015-01-05 15:48:58 -050058
59 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8G8B8A8, GLuint> );
60 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_BYTE, WriteColor<R8G8B8A8S, GLint> );
61 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16G16B16A16, GLuint> );
62 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_SHORT, WriteColor<R16G16B16A16S, GLint> );
63 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_INT, WriteColor<R32G32B32A32, GLuint> );
64 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_INT, WriteColor<R32G32B32A32S, GLint> );
65 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV, WriteColor<R10G10B10A2, GLuint> );
66
67 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_BYTE, WriteColor<R8G8B8, GLfloat> );
68 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_BYTE, WriteColor<R8G8B8S, GLfloat> );
69 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, WriteColor<R5G6B5, GLfloat> );
70 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, WriteColor<R11G11B10F, GLfloat> );
71 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV, WriteColor<R9G9B9E5, GLfloat> );
72 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_FLOAT, WriteColor<R32G32B32F, GLfloat> );
73 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_HALF_FLOAT, WriteColor<R16G16B16F, GLfloat> );
74 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_HALF_FLOAT_OES, WriteColor<R16G16B16F, GLfloat> );
Vincent Lang25ab4512016-05-13 18:13:59 +020075 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_SHORT,
76 WriteColor<R16G16B16, GLfloat>);
77 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_SHORT, WriteColor<R16G16B16S, GLfloat>);
Geoff Lang051dbc72015-01-05 15:48:58 -050078
79 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8G8B8, GLuint> );
80 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_BYTE, WriteColor<R8G8B8S, GLint> );
81 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16G16B16, GLuint> );
82 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_SHORT, WriteColor<R16G16B16S, GLint> );
83 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_UNSIGNED_INT, WriteColor<R32G32B32, GLuint> );
84 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_INT, WriteColor<R32G32B32S, GLint> );
85
86 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_UNSIGNED_BYTE, WriteColor<R8G8, GLfloat> );
87 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_BYTE, WriteColor<R8G8S, GLfloat> );
88 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_FLOAT, WriteColor<R32G32F, GLfloat> );
89 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_HALF_FLOAT, WriteColor<R16G16F, GLfloat> );
90 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_HALF_FLOAT_OES, WriteColor<R16G16F, GLfloat> );
Vincent Lang25ab4512016-05-13 18:13:59 +020091 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_UNSIGNED_SHORT, WriteColor<R16G16, GLfloat>);
92 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_SHORT, WriteColor<R16G16S, GLfloat>);
Geoff Lang051dbc72015-01-05 15:48:58 -050093
94 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8G8, GLuint> );
95 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_BYTE, WriteColor<R8G8S, GLint> );
96 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16G16, GLuint> );
97 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_SHORT, WriteColor<R16G16S, GLint> );
98 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_UNSIGNED_INT, WriteColor<R32G32, GLuint> );
99 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_INT, WriteColor<R32G32S, GLint> );
100
101 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_UNSIGNED_BYTE, WriteColor<R8, GLfloat> );
102 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_BYTE, WriteColor<R8S, GLfloat> );
103 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_FLOAT, WriteColor<R32F, GLfloat> );
104 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_HALF_FLOAT, WriteColor<R16F, GLfloat> );
105 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_HALF_FLOAT_OES, WriteColor<R16F, GLfloat> );
Vincent Lang25ab4512016-05-13 18:13:59 +0200106 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_UNSIGNED_SHORT, WriteColor<R16, GLfloat>);
107 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_SHORT, WriteColor<R16S, GLfloat>);
Geoff Lang051dbc72015-01-05 15:48:58 -0500108
109 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8, GLuint> );
110 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_BYTE, WriteColor<R8S, GLint> );
111 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16, GLuint> );
112 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_SHORT, WriteColor<R16S, GLint> );
113 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_UNSIGNED_INT, WriteColor<R32, GLuint> );
114 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_INT, WriteColor<R32S, GLint> );
115
116 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, WriteColor<L8A8, GLfloat> );
117 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_UNSIGNED_BYTE, WriteColor<L8, GLfloat> );
118 InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_UNSIGNED_BYTE, WriteColor<A8, GLfloat> );
119 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_FLOAT, WriteColor<L32A32F, GLfloat> );
120 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_FLOAT, WriteColor<L32F, GLfloat> );
121 InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_FLOAT, WriteColor<A32F, GLfloat> );
122 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT, WriteColor<L16A16F, GLfloat> );
123 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES, WriteColor<L16A16F, GLfloat> );
124 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_HALF_FLOAT, WriteColor<L16F, GLfloat> );
125 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_HALF_FLOAT_OES, WriteColor<L16F, GLfloat> );
126 InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_HALF_FLOAT, WriteColor<A16F, GLfloat> );
127 InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_HALF_FLOAT_OES, WriteColor<A16F, GLfloat> );
128
129 InsertFormatWriteFunctionMapping(&map, GL_BGRA_EXT, GL_UNSIGNED_BYTE, WriteColor<B8G8R8A8, GLfloat> );
Austin Kinross5cf0f982015-08-12 09:35:10 -0700130 InsertFormatWriteFunctionMapping(&map, GL_BGRA_EXT, GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT, WriteColor<A4R4G4B4, GLfloat> );
131 InsertFormatWriteFunctionMapping(&map, GL_BGRA_EXT, GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT, WriteColor<A1R5G5B5, GLfloat> );
Geoff Lang051dbc72015-01-05 15:48:58 -0500132
133 InsertFormatWriteFunctionMapping(&map, GL_SRGB_EXT, GL_UNSIGNED_BYTE, WriteColor<R8G8B8, GLfloat> );
134 InsertFormatWriteFunctionMapping(&map, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE, WriteColor<R8G8B8A8, GLfloat> );
135
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800136 InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE, nullptr );
137 InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE, nullptr );
138 InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, GL_UNSIGNED_BYTE, nullptr );
139 InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, GL_UNSIGNED_BYTE, nullptr );
Geoff Lang051dbc72015-01-05 15:48:58 -0500140
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800141 InsertFormatWriteFunctionMapping(&map, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, nullptr );
142 InsertFormatWriteFunctionMapping(&map, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr );
143 InsertFormatWriteFunctionMapping(&map, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr );
Geoff Lang051dbc72015-01-05 15:48:58 -0500144
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800145 InsertFormatWriteFunctionMapping(&map, GL_STENCIL, GL_UNSIGNED_BYTE, nullptr );
Geoff Lang051dbc72015-01-05 15:48:58 -0500146
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800147 InsertFormatWriteFunctionMapping(&map, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr );
148 InsertFormatWriteFunctionMapping(&map, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, nullptr );
Jamie Madilld2b50a02016-06-09 00:13:35 -0700149 // clang-format on
Geoff Lang051dbc72015-01-05 15:48:58 -0500150
151 return map;
152}
Geoff Langd93cd6c2017-08-11 16:32:41 -0400153
154void CopyColor(gl::ColorF *color)
155{
156 // No-op
157}
158
159void PremultiplyAlpha(gl::ColorF *color)
160{
161 color->red *= color->alpha;
162 color->green *= color->alpha;
163 color->blue *= color->alpha;
164}
165
166void UnmultiplyAlpha(gl::ColorF *color)
167{
168 if (color->alpha != 0.0f)
169 {
170 float invAlpha = 1.0f / color->alpha;
171 color->red *= invAlpha;
172 color->green *= invAlpha;
173 color->blue *= invAlpha;
174 }
175}
176
177void ClipChannelsR(gl::ColorF *color)
178{
179 color->green = 0.0f;
180 color->blue = 0.0f;
181 color->alpha = 1.0f;
182}
183
184void ClipChannelsRG(gl::ColorF *color)
185{
186 color->blue = 0.0f;
187 color->alpha = 1.0f;
188}
189
190void ClipChannelsRGB(gl::ColorF *color)
191{
192 color->alpha = 1.0f;
193}
194
195void ClipChannelsLuminance(gl::ColorF *color)
196{
197 color->alpha = 1.0f;
198}
199
200void ClipChannelsAlpha(gl::ColorF *color)
201{
202 color->red = 0.0f;
203 color->green = 0.0f;
204 color->blue = 0.0f;
205}
206
207void ClipChannelsNoOp(gl::ColorF *color)
208{
209}
210
211void WriteUintColor(const gl::ColorF &color,
212 ColorWriteFunction colorWriteFunction,
213 uint8_t *destPixelData)
214{
215 gl::ColorUI destColor(
216 static_cast<unsigned int>(color.red * 255), static_cast<unsigned int>(color.green * 255),
217 static_cast<unsigned int>(color.blue * 255), static_cast<unsigned int>(color.alpha * 255));
218 colorWriteFunction(reinterpret_cast<const uint8_t *>(&destColor), destPixelData);
219}
220
221void WriteFloatColor(const gl::ColorF &color,
222 ColorWriteFunction colorWriteFunction,
223 uint8_t *destPixelData)
224{
225 colorWriteFunction(reinterpret_cast<const uint8_t *>(&color), destPixelData);
226}
227
Luc Ferron46bcea52018-05-31 09:48:36 -0400228template <typename T, int cols, int rows>
229bool TransposeExpandMatrix(T *target, const GLfloat *value)
230{
231 constexpr int targetWidth = 4;
232 constexpr int targetHeight = rows;
233 constexpr int srcWidth = rows;
234 constexpr int srcHeight = cols;
235
236 constexpr int copyWidth = std::min(targetHeight, srcWidth);
237 constexpr int copyHeight = std::min(targetWidth, srcHeight);
238
239 T staging[targetWidth * targetHeight] = {0};
240
241 for (int x = 0; x < copyWidth; x++)
242 {
243 for (int y = 0; y < copyHeight; y++)
244 {
245 staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
246 }
247 }
248
249 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
250 {
251 return false;
252 }
253
254 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
255 return true;
256}
257
258template <typename T, int cols, int rows>
259bool ExpandMatrix(T *target, const GLfloat *value)
260{
261 constexpr int kTargetWidth = 4;
262 constexpr int kTargetHeight = rows;
263 constexpr int kSrcWidth = cols;
264 constexpr int kSrcHeight = rows;
265
266 constexpr int kCopyWidth = std::min(kTargetWidth, kSrcWidth);
267 constexpr int kCopyHeight = std::min(kTargetHeight, kSrcHeight);
268
269 T staging[kTargetWidth * kTargetHeight] = {0};
270
271 for (int y = 0; y < kCopyHeight; y++)
272 {
273 for (int x = 0; x < kCopyWidth; x++)
274 {
275 staging[y * kTargetWidth + x] = static_cast<T>(value[y * kSrcWidth + x]);
276 }
277 }
278
279 if (memcmp(target, staging, kTargetWidth * kTargetHeight * sizeof(T)) == 0)
280 {
281 return false;
282 }
283
284 memcpy(target, staging, kTargetWidth * kTargetHeight * sizeof(T));
285 return true;
286}
287
Jamie Madilld2b50a02016-06-09 00:13:35 -0700288} // anonymous namespace
Geoff Lang051dbc72015-01-05 15:48:58 -0500289
Jamie Madilld2b50a02016-06-09 00:13:35 -0700290PackPixelsParams::PackPixelsParams()
291 : format(GL_NONE), type(GL_NONE), outputPitch(0), packBuffer(nullptr), offset(0)
292{
293}
294
295PackPixelsParams::PackPixelsParams(const gl::Rectangle &areaIn,
296 GLenum formatIn,
297 GLenum typeIn,
298 GLuint outputPitchIn,
299 const gl::PixelPackState &packIn,
Corentin Wallezcda6af12017-10-30 19:20:37 -0400300 gl::Buffer *packBufferIn,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700301 ptrdiff_t offsetIn)
302 : area(areaIn),
303 format(formatIn),
304 type(typeIn),
305 outputPitch(outputPitchIn),
Corentin Wallezcda6af12017-10-30 19:20:37 -0400306 packBuffer(packBufferIn),
307 pack(),
Jamie Madilld2b50a02016-06-09 00:13:35 -0700308 offset(offsetIn)
309{
Corentin Wallezcda6af12017-10-30 19:20:37 -0400310 pack.alignment = packIn.alignment;
311 pack.reverseRowOrder = packIn.reverseRowOrder;
Jamie Madill4928b7c2017-06-20 12:57:39 -0400312}
313
Jamie Madilld2b50a02016-06-09 00:13:35 -0700314void PackPixels(const PackPixelsParams &params,
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400315 const angle::Format &sourceFormat,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700316 int inputPitchIn,
317 const uint8_t *sourceIn,
318 uint8_t *destWithoutOffset)
319{
320 uint8_t *destWithOffset = destWithoutOffset + params.offset;
321
322 const uint8_t *source = sourceIn;
323 int inputPitch = inputPitchIn;
324
325 if (params.pack.reverseRowOrder)
326 {
327 source += inputPitch * (params.area.height - 1);
328 inputPitch = -inputPitch;
329 }
330
Geoff Langca271392017-04-05 12:30:00 -0400331 const auto &sourceGLInfo = gl::GetSizedInternalFormatInfo(sourceFormat.glInternalFormat);
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400332
333 if (sourceGLInfo.format == params.format && sourceGLInfo.type == params.type)
Jamie Madilld2b50a02016-06-09 00:13:35 -0700334 {
335 // Direct copy possible
336 for (int y = 0; y < params.area.height; ++y)
337 {
338 memcpy(destWithOffset + y * params.outputPitch, source + y * inputPitch,
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400339 params.area.width * sourceGLInfo.pixelBytes);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700340 }
341 return;
342 }
343
Geoff Langca271392017-04-05 12:30:00 -0400344 ASSERT(sourceGLInfo.sized);
Jamie Madillec0b5802016-07-04 13:11:59 -0400345
Jamie Madilld2b50a02016-06-09 00:13:35 -0700346 gl::FormatType formatType(params.format, params.type);
Jamie Madill30712062016-08-09 11:10:36 -0400347 ColorCopyFunction fastCopyFunc =
348 GetFastCopyFunction(sourceFormat.fastCopyFunctions, formatType);
Geoff Langca271392017-04-05 12:30:00 -0400349 const auto &destFormatInfo = gl::GetInternalFormatInfo(formatType.format, formatType.type);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700350
351 if (fastCopyFunc)
352 {
353 // Fast copy is possible through some special function
354 for (int y = 0; y < params.area.height; ++y)
355 {
356 for (int x = 0; x < params.area.width; ++x)
357 {
358 uint8_t *dest =
359 destWithOffset + y * params.outputPitch + x * destFormatInfo.pixelBytes;
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400360 const uint8_t *src = source + y * inputPitch + x * sourceGLInfo.pixelBytes;
Jamie Madilld2b50a02016-06-09 00:13:35 -0700361
362 fastCopyFunc(src, dest);
363 }
364 }
365 return;
366 }
367
368 ColorWriteFunction colorWriteFunction = GetColorWriteFunction(formatType);
369
370 // Maximum size of any Color<T> type used.
371 uint8_t temp[16];
372 static_assert(sizeof(temp) >= sizeof(gl::ColorF) && sizeof(temp) >= sizeof(gl::ColorUI) &&
373 sizeof(temp) >= sizeof(gl::ColorI),
374 "Unexpected size of gl::Color struct.");
375
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400376 const auto &colorReadFunction = sourceFormat.colorReadFunction;
377
Jamie Madilld2b50a02016-06-09 00:13:35 -0700378 for (int y = 0; y < params.area.height; ++y)
379 {
380 for (int x = 0; x < params.area.width; ++x)
381 {
382 uint8_t *dest = destWithOffset + y * params.outputPitch + x * destFormatInfo.pixelBytes;
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400383 const uint8_t *src = source + y * inputPitch + x * sourceGLInfo.pixelBytes;
Jamie Madilld2b50a02016-06-09 00:13:35 -0700384
385 // readFunc and writeFunc will be using the same type of color, CopyTexImage
386 // will not allow the copy otherwise.
387 colorReadFunction(src, temp);
388 colorWriteFunction(temp, dest);
389 }
390 }
391}
392
393ColorWriteFunction GetColorWriteFunction(const gl::FormatType &formatType)
Geoff Lang051dbc72015-01-05 15:48:58 -0500394{
395 static const FormatWriteFunctionMap formatTypeMap = BuildFormatWriteFunctionMap();
Jamie Madilld2b50a02016-06-09 00:13:35 -0700396 auto iter = formatTypeMap.find(formatType);
Geoff Lang051dbc72015-01-05 15:48:58 -0500397 ASSERT(iter != formatTypeMap.end());
398 if (iter != formatTypeMap.end())
399 {
400 return iter->second;
401 }
402 else
403 {
Jamie Madilld2b50a02016-06-09 00:13:35 -0700404 return nullptr;
Geoff Lang051dbc72015-01-05 15:48:58 -0500405 }
406}
407
Jamie Madilld2b50a02016-06-09 00:13:35 -0700408ColorCopyFunction GetFastCopyFunction(const FastCopyFunctionMap &fastCopyFunctions,
409 const gl::FormatType &formatType)
410{
Jamie Madill4f57e5f2016-10-27 17:36:53 -0400411 return fastCopyFunctions.get(formatType);
412}
413
414bool FastCopyFunctionMap::has(const gl::FormatType &formatType) const
415{
416 return (get(formatType) != nullptr);
417}
418
419ColorCopyFunction FastCopyFunctionMap::get(const gl::FormatType &formatType) const
420{
421 for (size_t index = 0; index < mSize; ++index)
422 {
423 if (mData[index].format == formatType.format && mData[index].type == formatType.type)
424 {
425 return mData[index].func;
426 }
427 }
428
429 return nullptr;
Geoff Lang051dbc72015-01-05 15:48:58 -0500430}
Jamie Madilld2b50a02016-06-09 00:13:35 -0700431
Jamie Madill222c5172017-07-19 16:15:42 -0400432bool ShouldUseDebugLayers(const egl::AttributeMap &attribs)
433{
434 EGLAttrib debugSetting =
435 attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE);
436
437// Prefer to enable debug layers if compiling in Debug, and disabled in Release.
Geoff Lang58ba6bf2017-11-28 16:40:58 -0500438#if defined(ANGLE_ENABLE_ASSERTS)
Jamie Madill222c5172017-07-19 16:15:42 -0400439 return (debugSetting != EGL_FALSE);
440#else
441 return (debugSetting == EGL_TRUE);
Geoff Lang58ba6bf2017-11-28 16:40:58 -0500442#endif // defined(ANGLE_ENABLE_ASSERTS)
Jamie Madill222c5172017-07-19 16:15:42 -0400443}
444
Geoff Langd93cd6c2017-08-11 16:32:41 -0400445void CopyImageCHROMIUM(const uint8_t *sourceData,
446 size_t sourceRowPitch,
447 size_t sourcePixelBytes,
448 ColorReadFunction colorReadFunction,
449 uint8_t *destData,
450 size_t destRowPitch,
451 size_t destPixelBytes,
452 ColorWriteFunction colorWriteFunction,
453 GLenum destUnsizedFormat,
454 GLenum destComponentType,
455 size_t width,
456 size_t height,
457 bool unpackFlipY,
458 bool unpackPremultiplyAlpha,
459 bool unpackUnmultiplyAlpha)
460{
461 using ConversionFunction = void (*)(gl::ColorF *);
462 ConversionFunction conversionFunction = CopyColor;
463 if (unpackPremultiplyAlpha != unpackUnmultiplyAlpha)
464 {
465 if (unpackPremultiplyAlpha)
466 {
467 conversionFunction = PremultiplyAlpha;
468 }
469 else
470 {
471 conversionFunction = UnmultiplyAlpha;
472 }
473 }
474
475 auto clipChannelsFunction = ClipChannelsNoOp;
476 switch (destUnsizedFormat)
477 {
478 case GL_RED:
479 clipChannelsFunction = ClipChannelsR;
480 break;
481 case GL_RG:
482 clipChannelsFunction = ClipChannelsRG;
483 break;
484 case GL_RGB:
485 clipChannelsFunction = ClipChannelsRGB;
486 break;
487 case GL_LUMINANCE:
488 clipChannelsFunction = ClipChannelsLuminance;
489 break;
490 case GL_ALPHA:
491 clipChannelsFunction = ClipChannelsAlpha;
492 break;
493 }
494
495 auto writeFunction = (destComponentType == GL_UNSIGNED_INT) ? WriteUintColor : WriteFloatColor;
496
497 for (size_t y = 0; y < height; y++)
498 {
499 for (size_t x = 0; x < width; x++)
500 {
501 const uint8_t *sourcePixelData = sourceData + y * sourceRowPitch + x * sourcePixelBytes;
502
503 gl::ColorF sourceColor;
504 colorReadFunction(sourcePixelData, reinterpret_cast<uint8_t *>(&sourceColor));
505
506 conversionFunction(&sourceColor);
507 clipChannelsFunction(&sourceColor);
508
509 size_t destY = 0;
510 if (unpackFlipY)
511 {
512 destY += (height - 1);
513 destY -= y;
514 }
515 else
516 {
517 destY += y;
518 }
519
520 uint8_t *destPixelData = destData + destY * destRowPitch + x * destPixelBytes;
521 writeFunction(sourceColor, colorWriteFunction, destPixelData);
522 }
523 }
524}
525
Jamie Madill42975642017-10-12 12:31:51 -0400526// IncompleteTextureSet implementation.
527IncompleteTextureSet::IncompleteTextureSet()
528{
529}
530
531IncompleteTextureSet::~IncompleteTextureSet()
532{
533}
534
535void IncompleteTextureSet::onDestroy(const gl::Context *context)
536{
537 // Clear incomplete textures.
538 for (auto &incompleteTexture : mIncompleteTextures)
539 {
Corentin Wallez99d492c2018-02-27 15:17:10 -0500540 if (incompleteTexture.get() != nullptr)
541 {
542 ANGLE_SWALLOW_ERR(incompleteTexture->onDestroy(context));
543 incompleteTexture.set(context, nullptr);
544 }
Jamie Madill42975642017-10-12 12:31:51 -0400545 }
Jamie Madill42975642017-10-12 12:31:51 -0400546}
547
548gl::Error IncompleteTextureSet::getIncompleteTexture(
549 const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -0500550 gl::TextureType type,
Jamie Madill42975642017-10-12 12:31:51 -0400551 MultisampleTextureInitializer *multisampleInitializer,
552 gl::Texture **textureOut)
553{
Corentin Wallez99d492c2018-02-27 15:17:10 -0500554 *textureOut = mIncompleteTextures[type].get();
555 if (*textureOut != nullptr)
Jamie Madill42975642017-10-12 12:31:51 -0400556 {
Jamie Madill42975642017-10-12 12:31:51 -0400557 return gl::NoError();
558 }
559
560 ContextImpl *implFactory = context->getImplementation();
561
562 const GLubyte color[] = {0, 0, 0, 255};
563 const gl::Extents colorSize(1, 1, 1);
Corentin Wallezcda6af12017-10-30 19:20:37 -0400564 gl::PixelUnpackState unpack;
565 unpack.alignment = 1;
Jamie Madill42975642017-10-12 12:31:51 -0400566 const gl::Box area(0, 0, 0, 1, 1, 1);
567
568 // If a texture is external use a 2D texture for the incomplete texture
Corentin Wallez99d492c2018-02-27 15:17:10 -0500569 gl::TextureType createType = (type == gl::TextureType::External) ? gl::TextureType::_2D : type;
Jamie Madill42975642017-10-12 12:31:51 -0400570
571 gl::Texture *tex = new gl::Texture(implFactory, std::numeric_limits<GLuint>::max(), createType);
572 angle::UniqueObjectPointer<gl::Texture, gl::Context> t(tex, context);
573
Corentin Wallez99d492c2018-02-27 15:17:10 -0500574 if (createType == gl::TextureType::_2DMultisample)
Jamie Madill42975642017-10-12 12:31:51 -0400575 {
576 ANGLE_TRY(t->setStorageMultisample(context, createType, 1, GL_RGBA8, colorSize, true));
577 }
578 else
579 {
580 ANGLE_TRY(t->setStorage(context, createType, 1, GL_RGBA8, colorSize));
581 }
582
Corentin Wallez99d492c2018-02-27 15:17:10 -0500583 if (type == gl::TextureType::CubeMap)
Jamie Madill42975642017-10-12 12:31:51 -0400584 {
Corentin Wallez99d492c2018-02-27 15:17:10 -0500585 for (gl::TextureTarget face : gl::AllCubeFaceTextureTargets())
Jamie Madill42975642017-10-12 12:31:51 -0400586 {
587 ANGLE_TRY(
588 t->setSubImage(context, unpack, face, 0, area, GL_RGBA, GL_UNSIGNED_BYTE, color));
589 }
590 }
Corentin Wallez99d492c2018-02-27 15:17:10 -0500591 else if (type == gl::TextureType::_2DMultisample)
Jamie Madill42975642017-10-12 12:31:51 -0400592 {
593 // Call a specialized clear function to init a multisample texture.
594 ANGLE_TRY(multisampleInitializer->initializeMultisampleTextureToBlack(context, t.get()));
595 }
596 else
597 {
Corentin Wallez99d492c2018-02-27 15:17:10 -0500598 ANGLE_TRY(t->setSubImage(context, unpack, gl::NonCubeTextureTypeToTarget(createType), 0,
599 area, GL_RGBA, GL_UNSIGNED_BYTE, color));
Jamie Madill42975642017-10-12 12:31:51 -0400600 }
601
Luc Ferron4bba74f2018-04-19 14:40:45 -0400602 ANGLE_TRY(t->syncState(context));
Jamie Madill42975642017-10-12 12:31:51 -0400603
604 mIncompleteTextures[type].set(context, t.release());
605 *textureOut = mIncompleteTextures[type].get();
606 return gl::NoError();
607}
608
Luc Ferron46bcea52018-05-31 09:48:36 -0400609#define ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(cols, rows) \
610 template bool SetFloatUniformMatrix<cols, rows>(unsigned int, unsigned int, GLsizei, \
611 GLboolean, const GLfloat *, uint8_t *)
612
613ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 2);
614ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 3);
615ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 4);
616ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 3);
617ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 2);
618ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 4);
619ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 2);
620ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 4);
621ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 3);
622
623#undef ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC
624
625template <int cols, int rows>
626bool SetFloatUniformMatrix(unsigned int arrayElementOffset,
627 unsigned int elementCount,
628 GLsizei countIn,
629 GLboolean transpose,
630 const GLfloat *value,
631 uint8_t *targetData)
632{
633 unsigned int count =
634 std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn));
635
636 const unsigned int targetMatrixStride = (4 * rows);
637 GLfloat *target = reinterpret_cast<GLfloat *>(
638 targetData + arrayElementOffset * sizeof(GLfloat) * targetMatrixStride);
639
640 bool dirty = false;
641
642 for (unsigned int i = 0; i < count; i++)
643 {
644 if (transpose == GL_FALSE)
645 {
646 dirty = ExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
647 }
648 else
649 {
650 dirty = TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
651 }
652 target += targetMatrixStride;
653 value += cols * rows;
654 }
655
656 return dirty;
657}
658
Jamie Madilld2b50a02016-06-09 00:13:35 -0700659} // namespace rx