blob: e2e350731c20c5528c616798f9562b8e501cc0eb [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
Geoff Langca271392017-04-05 12:30:00 -0400332 const auto &sourceGLInfo = gl::GetSizedInternalFormatInfo(sourceFormat.glInternalFormat);
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400333
334 if (sourceGLInfo.format == params.format && sourceGLInfo.type == params.type)
Jamie Madilld2b50a02016-06-09 00:13:35 -0700335 {
336 // Direct copy possible
337 for (int y = 0; y < params.area.height; ++y)
338 {
339 memcpy(destWithOffset + y * params.outputPitch, source + y * inputPitch,
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400340 params.area.width * sourceGLInfo.pixelBytes);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700341 }
342 return;
343 }
344
Geoff Langca271392017-04-05 12:30:00 -0400345 ASSERT(sourceGLInfo.sized);
Jamie Madillec0b5802016-07-04 13:11:59 -0400346
Jamie Madilld2b50a02016-06-09 00:13:35 -0700347 gl::FormatType formatType(params.format, params.type);
Jamie Madill30712062016-08-09 11:10:36 -0400348 ColorCopyFunction fastCopyFunc =
349 GetFastCopyFunction(sourceFormat.fastCopyFunctions, formatType);
Geoff Langca271392017-04-05 12:30:00 -0400350 const auto &destFormatInfo = gl::GetInternalFormatInfo(formatType.format, formatType.type);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700351
352 if (fastCopyFunc)
353 {
354 // Fast copy is possible through some special function
355 for (int y = 0; y < params.area.height; ++y)
356 {
357 for (int x = 0; x < params.area.width; ++x)
358 {
359 uint8_t *dest =
360 destWithOffset + y * params.outputPitch + x * destFormatInfo.pixelBytes;
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400361 const uint8_t *src = source + y * inputPitch + x * sourceGLInfo.pixelBytes;
Jamie Madilld2b50a02016-06-09 00:13:35 -0700362
363 fastCopyFunc(src, dest);
364 }
365 }
366 return;
367 }
368
369 ColorWriteFunction colorWriteFunction = GetColorWriteFunction(formatType);
370
371 // Maximum size of any Color<T> type used.
372 uint8_t temp[16];
373 static_assert(sizeof(temp) >= sizeof(gl::ColorF) && sizeof(temp) >= sizeof(gl::ColorUI) &&
374 sizeof(temp) >= sizeof(gl::ColorI),
375 "Unexpected size of gl::Color struct.");
376
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400377 const auto &colorReadFunction = sourceFormat.colorReadFunction;
378
Jamie Madilld2b50a02016-06-09 00:13:35 -0700379 for (int y = 0; y < params.area.height; ++y)
380 {
381 for (int x = 0; x < params.area.width; ++x)
382 {
383 uint8_t *dest = destWithOffset + y * params.outputPitch + x * destFormatInfo.pixelBytes;
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400384 const uint8_t *src = source + y * inputPitch + x * sourceGLInfo.pixelBytes;
Jamie Madilld2b50a02016-06-09 00:13:35 -0700385
386 // readFunc and writeFunc will be using the same type of color, CopyTexImage
387 // will not allow the copy otherwise.
388 colorReadFunction(src, temp);
389 colorWriteFunction(temp, dest);
390 }
391 }
392}
393
394ColorWriteFunction GetColorWriteFunction(const gl::FormatType &formatType)
Geoff Lang051dbc72015-01-05 15:48:58 -0500395{
396 static const FormatWriteFunctionMap formatTypeMap = BuildFormatWriteFunctionMap();
Jamie Madilld2b50a02016-06-09 00:13:35 -0700397 auto iter = formatTypeMap.find(formatType);
Geoff Lang051dbc72015-01-05 15:48:58 -0500398 ASSERT(iter != formatTypeMap.end());
399 if (iter != formatTypeMap.end())
400 {
401 return iter->second;
402 }
403 else
404 {
Jamie Madilld2b50a02016-06-09 00:13:35 -0700405 return nullptr;
Geoff Lang051dbc72015-01-05 15:48:58 -0500406 }
407}
408
Jamie Madilld2b50a02016-06-09 00:13:35 -0700409ColorCopyFunction GetFastCopyFunction(const FastCopyFunctionMap &fastCopyFunctions,
410 const gl::FormatType &formatType)
411{
Jamie Madill4f57e5f2016-10-27 17:36:53 -0400412 return fastCopyFunctions.get(formatType);
413}
414
415bool FastCopyFunctionMap::has(const gl::FormatType &formatType) const
416{
417 return (get(formatType) != nullptr);
418}
419
420ColorCopyFunction FastCopyFunctionMap::get(const gl::FormatType &formatType) const
421{
422 for (size_t index = 0; index < mSize; ++index)
423 {
424 if (mData[index].format == formatType.format && mData[index].type == formatType.type)
425 {
426 return mData[index].func;
427 }
428 }
429
430 return nullptr;
Geoff Lang051dbc72015-01-05 15:48:58 -0500431}
Jamie Madilld2b50a02016-06-09 00:13:35 -0700432
Jamie Madill222c5172017-07-19 16:15:42 -0400433bool ShouldUseDebugLayers(const egl::AttributeMap &attribs)
434{
435 EGLAttrib debugSetting =
436 attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE);
437
438// Prefer to enable debug layers if compiling in Debug, and disabled in Release.
Geoff Lang58ba6bf2017-11-28 16:40:58 -0500439#if defined(ANGLE_ENABLE_ASSERTS)
Jamie Madill222c5172017-07-19 16:15:42 -0400440 return (debugSetting != EGL_FALSE);
441#else
442 return (debugSetting == EGL_TRUE);
Geoff Lang58ba6bf2017-11-28 16:40:58 -0500443#endif // defined(ANGLE_ENABLE_ASSERTS)
Jamie Madill222c5172017-07-19 16:15:42 -0400444}
445
Geoff Langd93cd6c2017-08-11 16:32:41 -0400446void CopyImageCHROMIUM(const uint8_t *sourceData,
447 size_t sourceRowPitch,
448 size_t sourcePixelBytes,
449 ColorReadFunction colorReadFunction,
450 uint8_t *destData,
451 size_t destRowPitch,
452 size_t destPixelBytes,
453 ColorWriteFunction colorWriteFunction,
454 GLenum destUnsizedFormat,
455 GLenum destComponentType,
456 size_t width,
457 size_t height,
458 bool unpackFlipY,
459 bool unpackPremultiplyAlpha,
460 bool unpackUnmultiplyAlpha)
461{
462 using ConversionFunction = void (*)(gl::ColorF *);
463 ConversionFunction conversionFunction = CopyColor;
464 if (unpackPremultiplyAlpha != unpackUnmultiplyAlpha)
465 {
466 if (unpackPremultiplyAlpha)
467 {
468 conversionFunction = PremultiplyAlpha;
469 }
470 else
471 {
472 conversionFunction = UnmultiplyAlpha;
473 }
474 }
475
476 auto clipChannelsFunction = ClipChannelsNoOp;
477 switch (destUnsizedFormat)
478 {
479 case GL_RED:
480 clipChannelsFunction = ClipChannelsR;
481 break;
482 case GL_RG:
483 clipChannelsFunction = ClipChannelsRG;
484 break;
485 case GL_RGB:
486 clipChannelsFunction = ClipChannelsRGB;
487 break;
488 case GL_LUMINANCE:
489 clipChannelsFunction = ClipChannelsLuminance;
490 break;
491 case GL_ALPHA:
492 clipChannelsFunction = ClipChannelsAlpha;
493 break;
494 }
495
496 auto writeFunction = (destComponentType == GL_UNSIGNED_INT) ? WriteUintColor : WriteFloatColor;
497
498 for (size_t y = 0; y < height; y++)
499 {
500 for (size_t x = 0; x < width; x++)
501 {
502 const uint8_t *sourcePixelData = sourceData + y * sourceRowPitch + x * sourcePixelBytes;
503
504 gl::ColorF sourceColor;
505 colorReadFunction(sourcePixelData, reinterpret_cast<uint8_t *>(&sourceColor));
506
507 conversionFunction(&sourceColor);
508 clipChannelsFunction(&sourceColor);
509
510 size_t destY = 0;
511 if (unpackFlipY)
512 {
513 destY += (height - 1);
514 destY -= y;
515 }
516 else
517 {
518 destY += y;
519 }
520
521 uint8_t *destPixelData = destData + destY * destRowPitch + x * destPixelBytes;
522 writeFunction(sourceColor, colorWriteFunction, destPixelData);
523 }
524 }
525}
526
Jamie Madill42975642017-10-12 12:31:51 -0400527// IncompleteTextureSet implementation.
528IncompleteTextureSet::IncompleteTextureSet()
529{
530}
531
532IncompleteTextureSet::~IncompleteTextureSet()
533{
534}
535
536void IncompleteTextureSet::onDestroy(const gl::Context *context)
537{
538 // Clear incomplete textures.
539 for (auto &incompleteTexture : mIncompleteTextures)
540 {
Corentin Wallez99d492c2018-02-27 15:17:10 -0500541 if (incompleteTexture.get() != nullptr)
542 {
543 ANGLE_SWALLOW_ERR(incompleteTexture->onDestroy(context));
544 incompleteTexture.set(context, nullptr);
545 }
Jamie Madill42975642017-10-12 12:31:51 -0400546 }
Jamie Madill42975642017-10-12 12:31:51 -0400547}
548
549gl::Error IncompleteTextureSet::getIncompleteTexture(
550 const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -0500551 gl::TextureType type,
Jamie Madill42975642017-10-12 12:31:51 -0400552 MultisampleTextureInitializer *multisampleInitializer,
553 gl::Texture **textureOut)
554{
Corentin Wallez99d492c2018-02-27 15:17:10 -0500555 *textureOut = mIncompleteTextures[type].get();
556 if (*textureOut != nullptr)
Jamie Madill42975642017-10-12 12:31:51 -0400557 {
Jamie Madill42975642017-10-12 12:31:51 -0400558 return gl::NoError();
559 }
560
561 ContextImpl *implFactory = context->getImplementation();
562
563 const GLubyte color[] = {0, 0, 0, 255};
564 const gl::Extents colorSize(1, 1, 1);
Corentin Wallezcda6af12017-10-30 19:20:37 -0400565 gl::PixelUnpackState unpack;
566 unpack.alignment = 1;
Jamie Madill42975642017-10-12 12:31:51 -0400567 const gl::Box area(0, 0, 0, 1, 1, 1);
568
569 // If a texture is external use a 2D texture for the incomplete texture
Corentin Wallez99d492c2018-02-27 15:17:10 -0500570 gl::TextureType createType = (type == gl::TextureType::External) ? gl::TextureType::_2D : type;
Jamie Madill42975642017-10-12 12:31:51 -0400571
572 gl::Texture *tex = new gl::Texture(implFactory, std::numeric_limits<GLuint>::max(), createType);
573 angle::UniqueObjectPointer<gl::Texture, gl::Context> t(tex, context);
574
Corentin Wallez99d492c2018-02-27 15:17:10 -0500575 if (createType == gl::TextureType::_2DMultisample)
Jamie Madill42975642017-10-12 12:31:51 -0400576 {
577 ANGLE_TRY(t->setStorageMultisample(context, createType, 1, GL_RGBA8, colorSize, true));
578 }
579 else
580 {
581 ANGLE_TRY(t->setStorage(context, createType, 1, GL_RGBA8, colorSize));
582 }
583
Corentin Wallez99d492c2018-02-27 15:17:10 -0500584 if (type == gl::TextureType::CubeMap)
Jamie Madill42975642017-10-12 12:31:51 -0400585 {
Corentin Wallez99d492c2018-02-27 15:17:10 -0500586 for (gl::TextureTarget face : gl::AllCubeFaceTextureTargets())
Jamie Madill42975642017-10-12 12:31:51 -0400587 {
588 ANGLE_TRY(
589 t->setSubImage(context, unpack, face, 0, area, GL_RGBA, GL_UNSIGNED_BYTE, color));
590 }
591 }
Corentin Wallez99d492c2018-02-27 15:17:10 -0500592 else if (type == gl::TextureType::_2DMultisample)
Jamie Madill42975642017-10-12 12:31:51 -0400593 {
594 // Call a specialized clear function to init a multisample texture.
595 ANGLE_TRY(multisampleInitializer->initializeMultisampleTextureToBlack(context, t.get()));
596 }
597 else
598 {
Corentin Wallez99d492c2018-02-27 15:17:10 -0500599 ANGLE_TRY(t->setSubImage(context, unpack, gl::NonCubeTextureTypeToTarget(createType), 0,
600 area, GL_RGBA, GL_UNSIGNED_BYTE, color));
Jamie Madill42975642017-10-12 12:31:51 -0400601 }
602
Luc Ferron4bba74f2018-04-19 14:40:45 -0400603 ANGLE_TRY(t->syncState(context));
Jamie Madill42975642017-10-12 12:31:51 -0400604
605 mIncompleteTextures[type].set(context, t.release());
606 *textureOut = mIncompleteTextures[type].get();
607 return gl::NoError();
608}
609
Luc Ferron46bcea52018-05-31 09:48:36 -0400610#define ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(cols, rows) \
611 template bool SetFloatUniformMatrix<cols, rows>(unsigned int, unsigned int, GLsizei, \
612 GLboolean, const GLfloat *, uint8_t *)
613
614ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 2);
615ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 3);
616ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 4);
617ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 3);
618ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 2);
619ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 4);
620ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 2);
621ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 4);
622ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 3);
623
624#undef ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC
625
626template <int cols, int rows>
627bool SetFloatUniformMatrix(unsigned int arrayElementOffset,
628 unsigned int elementCount,
629 GLsizei countIn,
630 GLboolean transpose,
631 const GLfloat *value,
632 uint8_t *targetData)
633{
634 unsigned int count =
635 std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn));
636
637 const unsigned int targetMatrixStride = (4 * rows);
638 GLfloat *target = reinterpret_cast<GLfloat *>(
639 targetData + arrayElementOffset * sizeof(GLfloat) * targetMatrixStride);
640
641 bool dirty = false;
642
643 for (unsigned int i = 0; i < count; i++)
644 {
645 if (transpose == GL_FALSE)
646 {
647 dirty = ExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
648 }
649 else
650 {
651 dirty = TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
652 }
653 target += targetMatrixStride;
654 value += cols * rows;
655 }
656
657 return dirty;
658}
659
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400660template void GetMatrixUniform<GLint>(GLenum, GLint *, const GLint *, bool);
661template void GetMatrixUniform<GLuint>(GLenum, GLuint *, const GLuint *, bool);
662
663void GetMatrixUniform(GLenum type, GLfloat *dataOut, const GLfloat *source, bool transpose)
664{
665 int columns = gl::VariableColumnCount(type);
666 int rows = gl::VariableRowCount(type);
667 for (GLint col = 0; col < columns; ++col)
668 {
669 for (GLint row = 0; row < rows; ++row)
670 {
671 GLfloat *outptr = dataOut + ((col * rows) + row);
672 const GLfloat *inptr =
673 transpose ? source + ((row * 4) + col) : source + ((col * 4) + row);
674 *outptr = *inptr;
675 }
676 }
677}
678
679template <typename NonFloatT>
680void GetMatrixUniform(GLenum type, NonFloatT *dataOut, const NonFloatT *source, bool transpose)
681{
682 UNREACHABLE();
683}
684
Jamie Madilld2b50a02016-06-09 00:13:35 -0700685} // namespace rx