blob: 8fa4a7786df4ac218424885d60f32d289703b8fa [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
Jamie Madilld2b50a02016-06-09 00:13:35 -0700228} // anonymous namespace
Geoff Lang051dbc72015-01-05 15:48:58 -0500229
Jamie Madilld2b50a02016-06-09 00:13:35 -0700230PackPixelsParams::PackPixelsParams()
231 : format(GL_NONE), type(GL_NONE), outputPitch(0), packBuffer(nullptr), offset(0)
232{
233}
234
235PackPixelsParams::PackPixelsParams(const gl::Rectangle &areaIn,
236 GLenum formatIn,
237 GLenum typeIn,
238 GLuint outputPitchIn,
239 const gl::PixelPackState &packIn,
Corentin Wallezcda6af12017-10-30 19:20:37 -0400240 gl::Buffer *packBufferIn,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700241 ptrdiff_t offsetIn)
242 : area(areaIn),
243 format(formatIn),
244 type(typeIn),
245 outputPitch(outputPitchIn),
Corentin Wallezcda6af12017-10-30 19:20:37 -0400246 packBuffer(packBufferIn),
247 pack(),
Jamie Madilld2b50a02016-06-09 00:13:35 -0700248 offset(offsetIn)
249{
Corentin Wallezcda6af12017-10-30 19:20:37 -0400250 pack.alignment = packIn.alignment;
251 pack.reverseRowOrder = packIn.reverseRowOrder;
Jamie Madill4928b7c2017-06-20 12:57:39 -0400252}
253
Jamie Madilld2b50a02016-06-09 00:13:35 -0700254void PackPixels(const PackPixelsParams &params,
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400255 const angle::Format &sourceFormat,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700256 int inputPitchIn,
257 const uint8_t *sourceIn,
258 uint8_t *destWithoutOffset)
259{
260 uint8_t *destWithOffset = destWithoutOffset + params.offset;
261
262 const uint8_t *source = sourceIn;
263 int inputPitch = inputPitchIn;
264
265 if (params.pack.reverseRowOrder)
266 {
267 source += inputPitch * (params.area.height - 1);
268 inputPitch = -inputPitch;
269 }
270
Geoff Langca271392017-04-05 12:30:00 -0400271 const auto &sourceGLInfo = gl::GetSizedInternalFormatInfo(sourceFormat.glInternalFormat);
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400272
273 if (sourceGLInfo.format == params.format && sourceGLInfo.type == params.type)
Jamie Madilld2b50a02016-06-09 00:13:35 -0700274 {
275 // Direct copy possible
276 for (int y = 0; y < params.area.height; ++y)
277 {
278 memcpy(destWithOffset + y * params.outputPitch, source + y * inputPitch,
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400279 params.area.width * sourceGLInfo.pixelBytes);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700280 }
281 return;
282 }
283
Geoff Langca271392017-04-05 12:30:00 -0400284 ASSERT(sourceGLInfo.sized);
Jamie Madillec0b5802016-07-04 13:11:59 -0400285
Jamie Madilld2b50a02016-06-09 00:13:35 -0700286 gl::FormatType formatType(params.format, params.type);
Jamie Madill30712062016-08-09 11:10:36 -0400287 ColorCopyFunction fastCopyFunc =
288 GetFastCopyFunction(sourceFormat.fastCopyFunctions, formatType);
Geoff Langca271392017-04-05 12:30:00 -0400289 const auto &destFormatInfo = gl::GetInternalFormatInfo(formatType.format, formatType.type);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700290
291 if (fastCopyFunc)
292 {
293 // Fast copy is possible through some special function
294 for (int y = 0; y < params.area.height; ++y)
295 {
296 for (int x = 0; x < params.area.width; ++x)
297 {
298 uint8_t *dest =
299 destWithOffset + y * params.outputPitch + x * destFormatInfo.pixelBytes;
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400300 const uint8_t *src = source + y * inputPitch + x * sourceGLInfo.pixelBytes;
Jamie Madilld2b50a02016-06-09 00:13:35 -0700301
302 fastCopyFunc(src, dest);
303 }
304 }
305 return;
306 }
307
308 ColorWriteFunction colorWriteFunction = GetColorWriteFunction(formatType);
309
310 // Maximum size of any Color<T> type used.
311 uint8_t temp[16];
312 static_assert(sizeof(temp) >= sizeof(gl::ColorF) && sizeof(temp) >= sizeof(gl::ColorUI) &&
313 sizeof(temp) >= sizeof(gl::ColorI),
314 "Unexpected size of gl::Color struct.");
315
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400316 const auto &colorReadFunction = sourceFormat.colorReadFunction;
317
Jamie Madilld2b50a02016-06-09 00:13:35 -0700318 for (int y = 0; y < params.area.height; ++y)
319 {
320 for (int x = 0; x < params.area.width; ++x)
321 {
322 uint8_t *dest = destWithOffset + y * params.outputPitch + x * destFormatInfo.pixelBytes;
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400323 const uint8_t *src = source + y * inputPitch + x * sourceGLInfo.pixelBytes;
Jamie Madilld2b50a02016-06-09 00:13:35 -0700324
325 // readFunc and writeFunc will be using the same type of color, CopyTexImage
326 // will not allow the copy otherwise.
327 colorReadFunction(src, temp);
328 colorWriteFunction(temp, dest);
329 }
330 }
331}
332
333ColorWriteFunction GetColorWriteFunction(const gl::FormatType &formatType)
Geoff Lang051dbc72015-01-05 15:48:58 -0500334{
335 static const FormatWriteFunctionMap formatTypeMap = BuildFormatWriteFunctionMap();
Jamie Madilld2b50a02016-06-09 00:13:35 -0700336 auto iter = formatTypeMap.find(formatType);
Geoff Lang051dbc72015-01-05 15:48:58 -0500337 ASSERT(iter != formatTypeMap.end());
338 if (iter != formatTypeMap.end())
339 {
340 return iter->second;
341 }
342 else
343 {
Jamie Madilld2b50a02016-06-09 00:13:35 -0700344 return nullptr;
Geoff Lang051dbc72015-01-05 15:48:58 -0500345 }
346}
347
Jamie Madilld2b50a02016-06-09 00:13:35 -0700348ColorCopyFunction GetFastCopyFunction(const FastCopyFunctionMap &fastCopyFunctions,
349 const gl::FormatType &formatType)
350{
Jamie Madill4f57e5f2016-10-27 17:36:53 -0400351 return fastCopyFunctions.get(formatType);
352}
353
354bool FastCopyFunctionMap::has(const gl::FormatType &formatType) const
355{
356 return (get(formatType) != nullptr);
357}
358
359ColorCopyFunction FastCopyFunctionMap::get(const gl::FormatType &formatType) const
360{
361 for (size_t index = 0; index < mSize; ++index)
362 {
363 if (mData[index].format == formatType.format && mData[index].type == formatType.type)
364 {
365 return mData[index].func;
366 }
367 }
368
369 return nullptr;
Geoff Lang051dbc72015-01-05 15:48:58 -0500370}
Jamie Madilld2b50a02016-06-09 00:13:35 -0700371
Jamie Madill222c5172017-07-19 16:15:42 -0400372bool ShouldUseDebugLayers(const egl::AttributeMap &attribs)
373{
374 EGLAttrib debugSetting =
375 attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE);
376
377// Prefer to enable debug layers if compiling in Debug, and disabled in Release.
Geoff Lang58ba6bf2017-11-28 16:40:58 -0500378#if defined(ANGLE_ENABLE_ASSERTS)
Jamie Madill222c5172017-07-19 16:15:42 -0400379 return (debugSetting != EGL_FALSE);
380#else
381 return (debugSetting == EGL_TRUE);
Geoff Lang58ba6bf2017-11-28 16:40:58 -0500382#endif // defined(ANGLE_ENABLE_ASSERTS)
Jamie Madill222c5172017-07-19 16:15:42 -0400383}
384
Geoff Langd93cd6c2017-08-11 16:32:41 -0400385void CopyImageCHROMIUM(const uint8_t *sourceData,
386 size_t sourceRowPitch,
387 size_t sourcePixelBytes,
388 ColorReadFunction colorReadFunction,
389 uint8_t *destData,
390 size_t destRowPitch,
391 size_t destPixelBytes,
392 ColorWriteFunction colorWriteFunction,
393 GLenum destUnsizedFormat,
394 GLenum destComponentType,
395 size_t width,
396 size_t height,
397 bool unpackFlipY,
398 bool unpackPremultiplyAlpha,
399 bool unpackUnmultiplyAlpha)
400{
401 using ConversionFunction = void (*)(gl::ColorF *);
402 ConversionFunction conversionFunction = CopyColor;
403 if (unpackPremultiplyAlpha != unpackUnmultiplyAlpha)
404 {
405 if (unpackPremultiplyAlpha)
406 {
407 conversionFunction = PremultiplyAlpha;
408 }
409 else
410 {
411 conversionFunction = UnmultiplyAlpha;
412 }
413 }
414
415 auto clipChannelsFunction = ClipChannelsNoOp;
416 switch (destUnsizedFormat)
417 {
418 case GL_RED:
419 clipChannelsFunction = ClipChannelsR;
420 break;
421 case GL_RG:
422 clipChannelsFunction = ClipChannelsRG;
423 break;
424 case GL_RGB:
425 clipChannelsFunction = ClipChannelsRGB;
426 break;
427 case GL_LUMINANCE:
428 clipChannelsFunction = ClipChannelsLuminance;
429 break;
430 case GL_ALPHA:
431 clipChannelsFunction = ClipChannelsAlpha;
432 break;
433 }
434
435 auto writeFunction = (destComponentType == GL_UNSIGNED_INT) ? WriteUintColor : WriteFloatColor;
436
437 for (size_t y = 0; y < height; y++)
438 {
439 for (size_t x = 0; x < width; x++)
440 {
441 const uint8_t *sourcePixelData = sourceData + y * sourceRowPitch + x * sourcePixelBytes;
442
443 gl::ColorF sourceColor;
444 colorReadFunction(sourcePixelData, reinterpret_cast<uint8_t *>(&sourceColor));
445
446 conversionFunction(&sourceColor);
447 clipChannelsFunction(&sourceColor);
448
449 size_t destY = 0;
450 if (unpackFlipY)
451 {
452 destY += (height - 1);
453 destY -= y;
454 }
455 else
456 {
457 destY += y;
458 }
459
460 uint8_t *destPixelData = destData + destY * destRowPitch + x * destPixelBytes;
461 writeFunction(sourceColor, colorWriteFunction, destPixelData);
462 }
463 }
464}
465
Jamie Madill42975642017-10-12 12:31:51 -0400466// IncompleteTextureSet implementation.
467IncompleteTextureSet::IncompleteTextureSet()
468{
469}
470
471IncompleteTextureSet::~IncompleteTextureSet()
472{
473}
474
475void IncompleteTextureSet::onDestroy(const gl::Context *context)
476{
477 // Clear incomplete textures.
478 for (auto &incompleteTexture : mIncompleteTextures)
479 {
Corentin Wallez99d492c2018-02-27 15:17:10 -0500480 if (incompleteTexture.get() != nullptr)
481 {
482 ANGLE_SWALLOW_ERR(incompleteTexture->onDestroy(context));
483 incompleteTexture.set(context, nullptr);
484 }
Jamie Madill42975642017-10-12 12:31:51 -0400485 }
Jamie Madill42975642017-10-12 12:31:51 -0400486}
487
488gl::Error IncompleteTextureSet::getIncompleteTexture(
489 const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -0500490 gl::TextureType type,
Jamie Madill42975642017-10-12 12:31:51 -0400491 MultisampleTextureInitializer *multisampleInitializer,
492 gl::Texture **textureOut)
493{
Corentin Wallez99d492c2018-02-27 15:17:10 -0500494 *textureOut = mIncompleteTextures[type].get();
495 if (*textureOut != nullptr)
Jamie Madill42975642017-10-12 12:31:51 -0400496 {
Jamie Madill42975642017-10-12 12:31:51 -0400497 return gl::NoError();
498 }
499
500 ContextImpl *implFactory = context->getImplementation();
501
502 const GLubyte color[] = {0, 0, 0, 255};
503 const gl::Extents colorSize(1, 1, 1);
Corentin Wallezcda6af12017-10-30 19:20:37 -0400504 gl::PixelUnpackState unpack;
505 unpack.alignment = 1;
Jamie Madill42975642017-10-12 12:31:51 -0400506 const gl::Box area(0, 0, 0, 1, 1, 1);
507
508 // If a texture is external use a 2D texture for the incomplete texture
Corentin Wallez99d492c2018-02-27 15:17:10 -0500509 gl::TextureType createType = (type == gl::TextureType::External) ? gl::TextureType::_2D : type;
Jamie Madill42975642017-10-12 12:31:51 -0400510
511 gl::Texture *tex = new gl::Texture(implFactory, std::numeric_limits<GLuint>::max(), createType);
512 angle::UniqueObjectPointer<gl::Texture, gl::Context> t(tex, context);
513
Corentin Wallez99d492c2018-02-27 15:17:10 -0500514 if (createType == gl::TextureType::_2DMultisample)
Jamie Madill42975642017-10-12 12:31:51 -0400515 {
516 ANGLE_TRY(t->setStorageMultisample(context, createType, 1, GL_RGBA8, colorSize, true));
517 }
518 else
519 {
520 ANGLE_TRY(t->setStorage(context, createType, 1, GL_RGBA8, colorSize));
521 }
522
Corentin Wallez99d492c2018-02-27 15:17:10 -0500523 if (type == gl::TextureType::CubeMap)
Jamie Madill42975642017-10-12 12:31:51 -0400524 {
Corentin Wallez99d492c2018-02-27 15:17:10 -0500525 for (gl::TextureTarget face : gl::AllCubeFaceTextureTargets())
Jamie Madill42975642017-10-12 12:31:51 -0400526 {
527 ANGLE_TRY(
528 t->setSubImage(context, unpack, face, 0, area, GL_RGBA, GL_UNSIGNED_BYTE, color));
529 }
530 }
Corentin Wallez99d492c2018-02-27 15:17:10 -0500531 else if (type == gl::TextureType::_2DMultisample)
Jamie Madill42975642017-10-12 12:31:51 -0400532 {
533 // Call a specialized clear function to init a multisample texture.
534 ANGLE_TRY(multisampleInitializer->initializeMultisampleTextureToBlack(context, t.get()));
535 }
536 else
537 {
Corentin Wallez99d492c2018-02-27 15:17:10 -0500538 ANGLE_TRY(t->setSubImage(context, unpack, gl::NonCubeTextureTypeToTarget(createType), 0,
539 area, GL_RGBA, GL_UNSIGNED_BYTE, color));
Jamie Madill42975642017-10-12 12:31:51 -0400540 }
541
Luc Ferron4bba74f2018-04-19 14:40:45 -0400542 ANGLE_TRY(t->syncState(context));
Jamie Madill42975642017-10-12 12:31:51 -0400543
544 mIncompleteTextures[type].set(context, t.release());
545 *textureOut = mIncompleteTextures[type].get();
546 return gl::NoError();
547}
548
Jamie Madilld2b50a02016-06-09 00:13:35 -0700549} // namespace rx