blob: 07a68a18f2ac79fb2b9899cb8edb77c17243ef24 [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>
Luc Ferron48cdc2e2018-05-31 09:58:34 -040022#include "common/utilities.h"
Geoff Lang051dbc72015-01-05 15:48:58 -050023
24namespace rx
25{
26
Jamie Madilld2b50a02016-06-09 00:13:35 -070027namespace
28{
29typedef std::pair<gl::FormatType, ColorWriteFunction> FormatWriteFunctionPair;
30typedef std::map<gl::FormatType, ColorWriteFunction> FormatWriteFunctionMap;
Geoff Lang051dbc72015-01-05 15:48:58 -050031
Jamie Madilld2b50a02016-06-09 00:13:35 -070032static inline void InsertFormatWriteFunctionMapping(FormatWriteFunctionMap *map,
33 GLenum format,
34 GLenum type,
Geoff Lang051dbc72015-01-05 15:48:58 -050035 ColorWriteFunction writeFunc)
36{
Jamie Madilld2b50a02016-06-09 00:13:35 -070037 map->insert(FormatWriteFunctionPair(gl::FormatType(format, type), writeFunc));
Geoff Lang051dbc72015-01-05 15:48:58 -050038}
39
40static FormatWriteFunctionMap BuildFormatWriteFunctionMap()
41{
Geoff Lang6e4cfce2016-06-13 15:06:31 -040042 using namespace angle; // For image writing functions
43
Geoff Lang051dbc72015-01-05 15:48:58 -050044 FormatWriteFunctionMap map;
45
Jamie Madilld2b50a02016-06-09 00:13:35 -070046 // clang-format off
Geoff Lang051dbc72015-01-05 15:48:58 -050047 // | Format | Type | Color write function |
48 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_BYTE, WriteColor<R8G8B8A8, GLfloat> );
49 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_BYTE, WriteColor<R8G8B8A8S, GLfloat> );
50 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, WriteColor<R4G4B4A4, GLfloat> );
51 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, WriteColor<R5G5B5A1, GLfloat> );
52 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, WriteColor<R10G10B10A2, GLfloat> );
53 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_FLOAT, WriteColor<R32G32B32A32F, GLfloat>);
54 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_HALF_FLOAT, WriteColor<R16G16B16A16F, GLfloat>);
55 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_HALF_FLOAT_OES, WriteColor<R16G16B16A16F, GLfloat>);
Vincent Lang25ab4512016-05-13 18:13:59 +020056 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_SHORT,
57 WriteColor<R16G16B16A16, GLfloat>);
58 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_SHORT, WriteColor<R16G16B16A16S, GLfloat>);
Geoff Lang051dbc72015-01-05 15:48:58 -050059
60 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8G8B8A8, GLuint> );
61 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_BYTE, WriteColor<R8G8B8A8S, GLint> );
62 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16G16B16A16, GLuint> );
63 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_SHORT, WriteColor<R16G16B16A16S, GLint> );
64 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_INT, WriteColor<R32G32B32A32, GLuint> );
65 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_INT, WriteColor<R32G32B32A32S, GLint> );
66 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV, WriteColor<R10G10B10A2, GLuint> );
67
68 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_BYTE, WriteColor<R8G8B8, GLfloat> );
69 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_BYTE, WriteColor<R8G8B8S, GLfloat> );
70 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, WriteColor<R5G6B5, GLfloat> );
71 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, WriteColor<R11G11B10F, GLfloat> );
72 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV, WriteColor<R9G9B9E5, GLfloat> );
73 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_FLOAT, WriteColor<R32G32B32F, GLfloat> );
74 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_HALF_FLOAT, WriteColor<R16G16B16F, GLfloat> );
75 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_HALF_FLOAT_OES, WriteColor<R16G16B16F, GLfloat> );
Vincent Lang25ab4512016-05-13 18:13:59 +020076 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_SHORT,
77 WriteColor<R16G16B16, GLfloat>);
78 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_SHORT, WriteColor<R16G16B16S, GLfloat>);
Geoff Lang051dbc72015-01-05 15:48:58 -050079
80 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8G8B8, GLuint> );
81 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_BYTE, WriteColor<R8G8B8S, GLint> );
82 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16G16B16, GLuint> );
83 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_SHORT, WriteColor<R16G16B16S, GLint> );
84 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_UNSIGNED_INT, WriteColor<R32G32B32, GLuint> );
85 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_INT, WriteColor<R32G32B32S, GLint> );
86
87 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_UNSIGNED_BYTE, WriteColor<R8G8, GLfloat> );
88 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_BYTE, WriteColor<R8G8S, GLfloat> );
89 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_FLOAT, WriteColor<R32G32F, GLfloat> );
90 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_HALF_FLOAT, WriteColor<R16G16F, GLfloat> );
91 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_HALF_FLOAT_OES, WriteColor<R16G16F, GLfloat> );
Vincent Lang25ab4512016-05-13 18:13:59 +020092 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_UNSIGNED_SHORT, WriteColor<R16G16, GLfloat>);
93 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_SHORT, WriteColor<R16G16S, GLfloat>);
Geoff Lang051dbc72015-01-05 15:48:58 -050094
95 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8G8, GLuint> );
96 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_BYTE, WriteColor<R8G8S, GLint> );
97 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16G16, GLuint> );
98 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_SHORT, WriteColor<R16G16S, GLint> );
99 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_UNSIGNED_INT, WriteColor<R32G32, GLuint> );
100 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_INT, WriteColor<R32G32S, GLint> );
101
102 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_UNSIGNED_BYTE, WriteColor<R8, GLfloat> );
103 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_BYTE, WriteColor<R8S, GLfloat> );
104 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_FLOAT, WriteColor<R32F, GLfloat> );
105 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_HALF_FLOAT, WriteColor<R16F, GLfloat> );
106 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_HALF_FLOAT_OES, WriteColor<R16F, GLfloat> );
Vincent Lang25ab4512016-05-13 18:13:59 +0200107 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_UNSIGNED_SHORT, WriteColor<R16, GLfloat>);
108 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_SHORT, WriteColor<R16S, GLfloat>);
Geoff Lang051dbc72015-01-05 15:48:58 -0500109
110 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8, GLuint> );
111 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_BYTE, WriteColor<R8S, GLint> );
112 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16, GLuint> );
113 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_SHORT, WriteColor<R16S, GLint> );
114 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_UNSIGNED_INT, WriteColor<R32, GLuint> );
115 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_INT, WriteColor<R32S, GLint> );
116
117 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, WriteColor<L8A8, GLfloat> );
118 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_UNSIGNED_BYTE, WriteColor<L8, GLfloat> );
119 InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_UNSIGNED_BYTE, WriteColor<A8, GLfloat> );
120 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_FLOAT, WriteColor<L32A32F, GLfloat> );
121 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_FLOAT, WriteColor<L32F, GLfloat> );
122 InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_FLOAT, WriteColor<A32F, GLfloat> );
123 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT, WriteColor<L16A16F, GLfloat> );
124 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES, WriteColor<L16A16F, GLfloat> );
125 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_HALF_FLOAT, WriteColor<L16F, GLfloat> );
126 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_HALF_FLOAT_OES, WriteColor<L16F, GLfloat> );
127 InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_HALF_FLOAT, WriteColor<A16F, GLfloat> );
128 InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_HALF_FLOAT_OES, WriteColor<A16F, GLfloat> );
129
130 InsertFormatWriteFunctionMapping(&map, GL_BGRA_EXT, GL_UNSIGNED_BYTE, WriteColor<B8G8R8A8, GLfloat> );
Austin Kinross5cf0f982015-08-12 09:35:10 -0700131 InsertFormatWriteFunctionMapping(&map, GL_BGRA_EXT, GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT, WriteColor<A4R4G4B4, GLfloat> );
132 InsertFormatWriteFunctionMapping(&map, GL_BGRA_EXT, GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT, WriteColor<A1R5G5B5, GLfloat> );
Geoff Lang051dbc72015-01-05 15:48:58 -0500133
134 InsertFormatWriteFunctionMapping(&map, GL_SRGB_EXT, GL_UNSIGNED_BYTE, WriteColor<R8G8B8, GLfloat> );
135 InsertFormatWriteFunctionMapping(&map, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE, WriteColor<R8G8B8A8, GLfloat> );
136
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800137 InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE, nullptr );
138 InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE, nullptr );
139 InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, GL_UNSIGNED_BYTE, nullptr );
140 InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, GL_UNSIGNED_BYTE, nullptr );
Geoff Lang051dbc72015-01-05 15:48:58 -0500141
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800142 InsertFormatWriteFunctionMapping(&map, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, nullptr );
143 InsertFormatWriteFunctionMapping(&map, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr );
144 InsertFormatWriteFunctionMapping(&map, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr );
Geoff Lang051dbc72015-01-05 15:48:58 -0500145
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800146 InsertFormatWriteFunctionMapping(&map, GL_STENCIL, GL_UNSIGNED_BYTE, nullptr );
Geoff Lang051dbc72015-01-05 15:48:58 -0500147
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800148 InsertFormatWriteFunctionMapping(&map, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr );
149 InsertFormatWriteFunctionMapping(&map, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, nullptr );
Jamie Madilld2b50a02016-06-09 00:13:35 -0700150 // clang-format on
Geoff Lang051dbc72015-01-05 15:48:58 -0500151
152 return map;
153}
Geoff Langd93cd6c2017-08-11 16:32:41 -0400154
155void CopyColor(gl::ColorF *color)
156{
157 // No-op
158}
159
160void PremultiplyAlpha(gl::ColorF *color)
161{
162 color->red *= color->alpha;
163 color->green *= color->alpha;
164 color->blue *= color->alpha;
165}
166
167void UnmultiplyAlpha(gl::ColorF *color)
168{
169 if (color->alpha != 0.0f)
170 {
171 float invAlpha = 1.0f / color->alpha;
172 color->red *= invAlpha;
173 color->green *= invAlpha;
174 color->blue *= invAlpha;
175 }
176}
177
178void ClipChannelsR(gl::ColorF *color)
179{
180 color->green = 0.0f;
181 color->blue = 0.0f;
182 color->alpha = 1.0f;
183}
184
185void ClipChannelsRG(gl::ColorF *color)
186{
187 color->blue = 0.0f;
188 color->alpha = 1.0f;
189}
190
191void ClipChannelsRGB(gl::ColorF *color)
192{
193 color->alpha = 1.0f;
194}
195
196void ClipChannelsLuminance(gl::ColorF *color)
197{
198 color->alpha = 1.0f;
199}
200
201void ClipChannelsAlpha(gl::ColorF *color)
202{
203 color->red = 0.0f;
204 color->green = 0.0f;
205 color->blue = 0.0f;
206}
207
208void ClipChannelsNoOp(gl::ColorF *color)
209{
210}
211
212void WriteUintColor(const gl::ColorF &color,
213 ColorWriteFunction colorWriteFunction,
214 uint8_t *destPixelData)
215{
216 gl::ColorUI destColor(
217 static_cast<unsigned int>(color.red * 255), static_cast<unsigned int>(color.green * 255),
218 static_cast<unsigned int>(color.blue * 255), static_cast<unsigned int>(color.alpha * 255));
219 colorWriteFunction(reinterpret_cast<const uint8_t *>(&destColor), destPixelData);
220}
221
222void WriteFloatColor(const gl::ColorF &color,
223 ColorWriteFunction colorWriteFunction,
224 uint8_t *destPixelData)
225{
226 colorWriteFunction(reinterpret_cast<const uint8_t *>(&color), destPixelData);
227}
228
Luc Ferron46bcea52018-05-31 09:48:36 -0400229template <typename T, int cols, int rows>
230bool TransposeExpandMatrix(T *target, const GLfloat *value)
231{
232 constexpr int targetWidth = 4;
233 constexpr int targetHeight = rows;
234 constexpr int srcWidth = rows;
235 constexpr int srcHeight = cols;
236
237 constexpr int copyWidth = std::min(targetHeight, srcWidth);
238 constexpr int copyHeight = std::min(targetWidth, srcHeight);
239
240 T staging[targetWidth * targetHeight] = {0};
241
242 for (int x = 0; x < copyWidth; x++)
243 {
244 for (int y = 0; y < copyHeight; y++)
245 {
246 staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
247 }
248 }
249
250 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
251 {
252 return false;
253 }
254
255 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
256 return true;
257}
258
259template <typename T, int cols, int rows>
260bool ExpandMatrix(T *target, const GLfloat *value)
261{
262 constexpr int kTargetWidth = 4;
263 constexpr int kTargetHeight = rows;
264 constexpr int kSrcWidth = cols;
265 constexpr int kSrcHeight = rows;
266
267 constexpr int kCopyWidth = std::min(kTargetWidth, kSrcWidth);
268 constexpr int kCopyHeight = std::min(kTargetHeight, kSrcHeight);
269
270 T staging[kTargetWidth * kTargetHeight] = {0};
271
272 for (int y = 0; y < kCopyHeight; y++)
273 {
274 for (int x = 0; x < kCopyWidth; x++)
275 {
276 staging[y * kTargetWidth + x] = static_cast<T>(value[y * kSrcWidth + x]);
277 }
278 }
279
280 if (memcmp(target, staging, kTargetWidth * kTargetHeight * sizeof(T)) == 0)
281 {
282 return false;
283 }
284
285 memcpy(target, staging, kTargetWidth * kTargetHeight * sizeof(T));
286 return true;
287}
288
Jamie Madilld2b50a02016-06-09 00:13:35 -0700289} // anonymous namespace
Geoff Lang051dbc72015-01-05 15:48:58 -0500290
Jamie Madilld2b50a02016-06-09 00:13:35 -0700291PackPixelsParams::PackPixelsParams()
292 : format(GL_NONE), type(GL_NONE), outputPitch(0), packBuffer(nullptr), offset(0)
293{
294}
295
296PackPixelsParams::PackPixelsParams(const gl::Rectangle &areaIn,
297 GLenum formatIn,
298 GLenum typeIn,
299 GLuint outputPitchIn,
300 const gl::PixelPackState &packIn,
Corentin Wallezcda6af12017-10-30 19:20:37 -0400301 gl::Buffer *packBufferIn,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700302 ptrdiff_t offsetIn)
303 : area(areaIn),
304 format(formatIn),
305 type(typeIn),
306 outputPitch(outputPitchIn),
Corentin Wallezcda6af12017-10-30 19:20:37 -0400307 packBuffer(packBufferIn),
308 pack(),
Jamie Madilld2b50a02016-06-09 00:13:35 -0700309 offset(offsetIn)
310{
Corentin Wallezcda6af12017-10-30 19:20:37 -0400311 pack.alignment = packIn.alignment;
312 pack.reverseRowOrder = packIn.reverseRowOrder;
Jamie Madill4928b7c2017-06-20 12:57:39 -0400313}
314
Jamie Madilld2b50a02016-06-09 00:13:35 -0700315void PackPixels(const PackPixelsParams &params,
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400316 const angle::Format &sourceFormat,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700317 int inputPitchIn,
318 const uint8_t *sourceIn,
319 uint8_t *destWithoutOffset)
320{
321 uint8_t *destWithOffset = destWithoutOffset + params.offset;
322
323 const uint8_t *source = sourceIn;
324 int inputPitch = inputPitchIn;
325
326 if (params.pack.reverseRowOrder)
327 {
328 source += inputPitch * (params.area.height - 1);
329 inputPitch = -inputPitch;
330 }
331
Luc Ferron339b95e2018-06-12 10:21:52 -0400332 const gl::InternalFormat &internalFormat =
333 gl::GetInternalFormatInfo(params.format, params.type);
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400334
Luc Ferron339b95e2018-06-12 10:21:52 -0400335 if (sourceFormat.glInternalFormat == internalFormat.sizedInternalFormat)
Jamie Madilld2b50a02016-06-09 00:13:35 -0700336 {
337 // Direct copy possible
338 for (int y = 0; y < params.area.height; ++y)
339 {
340 memcpy(destWithOffset + y * params.outputPitch, source + y * inputPitch,
Luc Ferron339b95e2018-06-12 10:21:52 -0400341 params.area.width * sourceFormat.pixelBytes);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700342 }
343 return;
344 }
345
346 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;
Luc Ferron339b95e2018-06-12 10:21:52 -0400360 const uint8_t *src = source + y * inputPitch + x * sourceFormat.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;
Luc Ferron339b95e2018-06-12 10:21:52 -0400383 const uint8_t *src = source + y * inputPitch + x * sourceFormat.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 Lang24ddc7a2018-06-11 14:56:34 -0400445bool ShouldUseVirtualizedContexts(const egl::AttributeMap &attribs, bool defaultValue)
446{
447 EGLAttrib virtualizedContextRequest =
448 attribs.get(EGL_PLATFORM_ANGLE_CONTEXT_VIRTUALIZATION_ANGLE, EGL_DONT_CARE);
449 if (defaultValue)
450 {
451 return (virtualizedContextRequest != EGL_FALSE);
452 }
453 else
454 {
455 return (virtualizedContextRequest == EGL_TRUE);
456 }
457}
458
Geoff Langd93cd6c2017-08-11 16:32:41 -0400459void CopyImageCHROMIUM(const uint8_t *sourceData,
460 size_t sourceRowPitch,
461 size_t sourcePixelBytes,
462 ColorReadFunction colorReadFunction,
463 uint8_t *destData,
464 size_t destRowPitch,
465 size_t destPixelBytes,
466 ColorWriteFunction colorWriteFunction,
467 GLenum destUnsizedFormat,
468 GLenum destComponentType,
469 size_t width,
470 size_t height,
471 bool unpackFlipY,
472 bool unpackPremultiplyAlpha,
473 bool unpackUnmultiplyAlpha)
474{
475 using ConversionFunction = void (*)(gl::ColorF *);
476 ConversionFunction conversionFunction = CopyColor;
477 if (unpackPremultiplyAlpha != unpackUnmultiplyAlpha)
478 {
479 if (unpackPremultiplyAlpha)
480 {
481 conversionFunction = PremultiplyAlpha;
482 }
483 else
484 {
485 conversionFunction = UnmultiplyAlpha;
486 }
487 }
488
489 auto clipChannelsFunction = ClipChannelsNoOp;
490 switch (destUnsizedFormat)
491 {
492 case GL_RED:
493 clipChannelsFunction = ClipChannelsR;
494 break;
495 case GL_RG:
496 clipChannelsFunction = ClipChannelsRG;
497 break;
498 case GL_RGB:
499 clipChannelsFunction = ClipChannelsRGB;
500 break;
501 case GL_LUMINANCE:
502 clipChannelsFunction = ClipChannelsLuminance;
503 break;
504 case GL_ALPHA:
505 clipChannelsFunction = ClipChannelsAlpha;
506 break;
507 }
508
509 auto writeFunction = (destComponentType == GL_UNSIGNED_INT) ? WriteUintColor : WriteFloatColor;
510
511 for (size_t y = 0; y < height; y++)
512 {
513 for (size_t x = 0; x < width; x++)
514 {
515 const uint8_t *sourcePixelData = sourceData + y * sourceRowPitch + x * sourcePixelBytes;
516
517 gl::ColorF sourceColor;
518 colorReadFunction(sourcePixelData, reinterpret_cast<uint8_t *>(&sourceColor));
519
520 conversionFunction(&sourceColor);
521 clipChannelsFunction(&sourceColor);
522
523 size_t destY = 0;
524 if (unpackFlipY)
525 {
526 destY += (height - 1);
527 destY -= y;
528 }
529 else
530 {
531 destY += y;
532 }
533
534 uint8_t *destPixelData = destData + destY * destRowPitch + x * destPixelBytes;
535 writeFunction(sourceColor, colorWriteFunction, destPixelData);
536 }
537 }
538}
539
Jamie Madill42975642017-10-12 12:31:51 -0400540// IncompleteTextureSet implementation.
541IncompleteTextureSet::IncompleteTextureSet()
542{
543}
544
545IncompleteTextureSet::~IncompleteTextureSet()
546{
547}
548
549void IncompleteTextureSet::onDestroy(const gl::Context *context)
550{
551 // Clear incomplete textures.
552 for (auto &incompleteTexture : mIncompleteTextures)
553 {
Corentin Wallez99d492c2018-02-27 15:17:10 -0500554 if (incompleteTexture.get() != nullptr)
555 {
556 ANGLE_SWALLOW_ERR(incompleteTexture->onDestroy(context));
557 incompleteTexture.set(context, nullptr);
558 }
Jamie Madill42975642017-10-12 12:31:51 -0400559 }
Jamie Madill42975642017-10-12 12:31:51 -0400560}
561
562gl::Error IncompleteTextureSet::getIncompleteTexture(
563 const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -0500564 gl::TextureType type,
Jamie Madill42975642017-10-12 12:31:51 -0400565 MultisampleTextureInitializer *multisampleInitializer,
566 gl::Texture **textureOut)
567{
Corentin Wallez99d492c2018-02-27 15:17:10 -0500568 *textureOut = mIncompleteTextures[type].get();
569 if (*textureOut != nullptr)
Jamie Madill42975642017-10-12 12:31:51 -0400570 {
Jamie Madill42975642017-10-12 12:31:51 -0400571 return gl::NoError();
572 }
573
574 ContextImpl *implFactory = context->getImplementation();
575
576 const GLubyte color[] = {0, 0, 0, 255};
577 const gl::Extents colorSize(1, 1, 1);
Corentin Wallezcda6af12017-10-30 19:20:37 -0400578 gl::PixelUnpackState unpack;
579 unpack.alignment = 1;
Jamie Madill42975642017-10-12 12:31:51 -0400580 const gl::Box area(0, 0, 0, 1, 1, 1);
581
582 // If a texture is external use a 2D texture for the incomplete texture
Corentin Wallez99d492c2018-02-27 15:17:10 -0500583 gl::TextureType createType = (type == gl::TextureType::External) ? gl::TextureType::_2D : type;
Jamie Madill42975642017-10-12 12:31:51 -0400584
585 gl::Texture *tex = new gl::Texture(implFactory, std::numeric_limits<GLuint>::max(), createType);
586 angle::UniqueObjectPointer<gl::Texture, gl::Context> t(tex, context);
587
Corentin Wallez99d492c2018-02-27 15:17:10 -0500588 if (createType == gl::TextureType::_2DMultisample)
Jamie Madill42975642017-10-12 12:31:51 -0400589 {
590 ANGLE_TRY(t->setStorageMultisample(context, createType, 1, GL_RGBA8, colorSize, true));
591 }
592 else
593 {
594 ANGLE_TRY(t->setStorage(context, createType, 1, GL_RGBA8, colorSize));
595 }
596
Corentin Wallez99d492c2018-02-27 15:17:10 -0500597 if (type == gl::TextureType::CubeMap)
Jamie Madill42975642017-10-12 12:31:51 -0400598 {
Corentin Wallez99d492c2018-02-27 15:17:10 -0500599 for (gl::TextureTarget face : gl::AllCubeFaceTextureTargets())
Jamie Madill42975642017-10-12 12:31:51 -0400600 {
601 ANGLE_TRY(
602 t->setSubImage(context, unpack, face, 0, area, GL_RGBA, GL_UNSIGNED_BYTE, color));
603 }
604 }
Corentin Wallez99d492c2018-02-27 15:17:10 -0500605 else if (type == gl::TextureType::_2DMultisample)
Jamie Madill42975642017-10-12 12:31:51 -0400606 {
607 // Call a specialized clear function to init a multisample texture.
608 ANGLE_TRY(multisampleInitializer->initializeMultisampleTextureToBlack(context, t.get()));
609 }
610 else
611 {
Corentin Wallez99d492c2018-02-27 15:17:10 -0500612 ANGLE_TRY(t->setSubImage(context, unpack, gl::NonCubeTextureTypeToTarget(createType), 0,
613 area, GL_RGBA, GL_UNSIGNED_BYTE, color));
Jamie Madill42975642017-10-12 12:31:51 -0400614 }
615
Luc Ferron4bba74f2018-04-19 14:40:45 -0400616 ANGLE_TRY(t->syncState(context));
Jamie Madill42975642017-10-12 12:31:51 -0400617
618 mIncompleteTextures[type].set(context, t.release());
619 *textureOut = mIncompleteTextures[type].get();
620 return gl::NoError();
621}
622
Luc Ferron46bcea52018-05-31 09:48:36 -0400623#define ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(cols, rows) \
624 template bool SetFloatUniformMatrix<cols, rows>(unsigned int, unsigned int, GLsizei, \
625 GLboolean, const GLfloat *, uint8_t *)
626
627ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 2);
628ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 3);
629ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 4);
630ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 3);
631ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 2);
632ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 4);
633ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 2);
634ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 4);
635ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 3);
636
637#undef ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC
638
639template <int cols, int rows>
640bool SetFloatUniformMatrix(unsigned int arrayElementOffset,
641 unsigned int elementCount,
642 GLsizei countIn,
643 GLboolean transpose,
644 const GLfloat *value,
645 uint8_t *targetData)
646{
647 unsigned int count =
648 std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn));
649
650 const unsigned int targetMatrixStride = (4 * rows);
651 GLfloat *target = reinterpret_cast<GLfloat *>(
652 targetData + arrayElementOffset * sizeof(GLfloat) * targetMatrixStride);
653
654 bool dirty = false;
655
656 for (unsigned int i = 0; i < count; i++)
657 {
658 if (transpose == GL_FALSE)
659 {
660 dirty = ExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
661 }
662 else
663 {
664 dirty = TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
665 }
666 target += targetMatrixStride;
667 value += cols * rows;
668 }
669
670 return dirty;
671}
672
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400673template void GetMatrixUniform<GLint>(GLenum, GLint *, const GLint *, bool);
674template void GetMatrixUniform<GLuint>(GLenum, GLuint *, const GLuint *, bool);
675
676void GetMatrixUniform(GLenum type, GLfloat *dataOut, const GLfloat *source, bool transpose)
677{
678 int columns = gl::VariableColumnCount(type);
679 int rows = gl::VariableRowCount(type);
680 for (GLint col = 0; col < columns; ++col)
681 {
682 for (GLint row = 0; row < rows; ++row)
683 {
684 GLfloat *outptr = dataOut + ((col * rows) + row);
685 const GLfloat *inptr =
686 transpose ? source + ((row * 4) + col) : source + ((col * 4) + row);
687 *outptr = *inptr;
688 }
689 }
690}
691
692template <typename NonFloatT>
693void GetMatrixUniform(GLenum type, NonFloatT *dataOut, const NonFloatT *source, bool transpose)
694{
695 UNREACHABLE();
696}
697
Jamie Madilld2b50a02016-06-09 00:13:35 -0700698} // namespace rx