blob: 7ff150c332829bdae0e840ab5aeb923b3c3658ca [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{
Geoff Langd93cd6c2017-08-11 16:32:41 -040029void CopyColor(gl::ColorF *color)
30{
31 // No-op
32}
33
34void PremultiplyAlpha(gl::ColorF *color)
35{
36 color->red *= color->alpha;
37 color->green *= color->alpha;
38 color->blue *= color->alpha;
39}
40
41void UnmultiplyAlpha(gl::ColorF *color)
42{
43 if (color->alpha != 0.0f)
44 {
45 float invAlpha = 1.0f / color->alpha;
46 color->red *= invAlpha;
47 color->green *= invAlpha;
48 color->blue *= invAlpha;
49 }
50}
51
52void ClipChannelsR(gl::ColorF *color)
53{
54 color->green = 0.0f;
55 color->blue = 0.0f;
56 color->alpha = 1.0f;
57}
58
59void ClipChannelsRG(gl::ColorF *color)
60{
61 color->blue = 0.0f;
62 color->alpha = 1.0f;
63}
64
65void ClipChannelsRGB(gl::ColorF *color)
66{
67 color->alpha = 1.0f;
68}
69
70void ClipChannelsLuminance(gl::ColorF *color)
71{
72 color->alpha = 1.0f;
73}
74
75void ClipChannelsAlpha(gl::ColorF *color)
76{
77 color->red = 0.0f;
78 color->green = 0.0f;
79 color->blue = 0.0f;
80}
81
Jamie Madillb980c562018-11-27 11:34:27 -050082void ClipChannelsNoOp(gl::ColorF *color) {}
Geoff Langd93cd6c2017-08-11 16:32:41 -040083
84void WriteUintColor(const gl::ColorF &color,
Jamie Madill522095f2018-07-23 14:59:41 -040085 PixelWriteFunction colorWriteFunction,
Geoff Langd93cd6c2017-08-11 16:32:41 -040086 uint8_t *destPixelData)
87{
88 gl::ColorUI destColor(
89 static_cast<unsigned int>(color.red * 255), static_cast<unsigned int>(color.green * 255),
90 static_cast<unsigned int>(color.blue * 255), static_cast<unsigned int>(color.alpha * 255));
91 colorWriteFunction(reinterpret_cast<const uint8_t *>(&destColor), destPixelData);
92}
93
94void WriteFloatColor(const gl::ColorF &color,
Jamie Madill522095f2018-07-23 14:59:41 -040095 PixelWriteFunction colorWriteFunction,
Geoff Langd93cd6c2017-08-11 16:32:41 -040096 uint8_t *destPixelData)
97{
98 colorWriteFunction(reinterpret_cast<const uint8_t *>(&color), destPixelData);
99}
100
Luc Ferron46bcea52018-05-31 09:48:36 -0400101template <typename T, int cols, int rows>
102bool TransposeExpandMatrix(T *target, const GLfloat *value)
103{
104 constexpr int targetWidth = 4;
105 constexpr int targetHeight = rows;
106 constexpr int srcWidth = rows;
107 constexpr int srcHeight = cols;
108
109 constexpr int copyWidth = std::min(targetHeight, srcWidth);
110 constexpr int copyHeight = std::min(targetWidth, srcHeight);
111
112 T staging[targetWidth * targetHeight] = {0};
113
114 for (int x = 0; x < copyWidth; x++)
115 {
116 for (int y = 0; y < copyHeight; y++)
117 {
118 staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
119 }
120 }
121
122 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
123 {
124 return false;
125 }
126
127 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
128 return true;
129}
130
131template <typename T, int cols, int rows>
132bool ExpandMatrix(T *target, const GLfloat *value)
133{
134 constexpr int kTargetWidth = 4;
135 constexpr int kTargetHeight = rows;
136 constexpr int kSrcWidth = cols;
137 constexpr int kSrcHeight = rows;
138
139 constexpr int kCopyWidth = std::min(kTargetWidth, kSrcWidth);
140 constexpr int kCopyHeight = std::min(kTargetHeight, kSrcHeight);
141
142 T staging[kTargetWidth * kTargetHeight] = {0};
143
144 for (int y = 0; y < kCopyHeight; y++)
145 {
146 for (int x = 0; x < kCopyWidth; x++)
147 {
148 staging[y * kTargetWidth + x] = static_cast<T>(value[y * kSrcWidth + x]);
149 }
150 }
151
152 if (memcmp(target, staging, kTargetWidth * kTargetHeight * sizeof(T)) == 0)
153 {
154 return false;
155 }
156
157 memcpy(target, staging, kTargetWidth * kTargetHeight * sizeof(T));
158 return true;
159}
Jamie Madilld2b50a02016-06-09 00:13:35 -0700160} // anonymous namespace
Geoff Lang051dbc72015-01-05 15:48:58 -0500161
Jamie Madilld2b50a02016-06-09 00:13:35 -0700162PackPixelsParams::PackPixelsParams()
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400163 : destFormat(nullptr), outputPitch(0), packBuffer(nullptr), offset(0)
Jamie Madillb980c562018-11-27 11:34:27 -0500164{}
Jamie Madilld2b50a02016-06-09 00:13:35 -0700165
166PackPixelsParams::PackPixelsParams(const gl::Rectangle &areaIn,
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400167 const angle::Format &destFormat,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700168 GLuint outputPitchIn,
Frank Henigman1ffad842018-09-24 23:40:45 -0400169 bool reverseRowOrderIn,
Corentin Wallezcda6af12017-10-30 19:20:37 -0400170 gl::Buffer *packBufferIn,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700171 ptrdiff_t offsetIn)
172 : area(areaIn),
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400173 destFormat(&destFormat),
Jamie Madilld2b50a02016-06-09 00:13:35 -0700174 outputPitch(outputPitchIn),
Corentin Wallezcda6af12017-10-30 19:20:37 -0400175 packBuffer(packBufferIn),
Frank Henigman1ffad842018-09-24 23:40:45 -0400176 reverseRowOrder(reverseRowOrderIn),
Jamie Madilld2b50a02016-06-09 00:13:35 -0700177 offset(offsetIn)
Jamie Madillb980c562018-11-27 11:34:27 -0500178{}
Jamie Madill4928b7c2017-06-20 12:57:39 -0400179
Jamie Madilld2b50a02016-06-09 00:13:35 -0700180void PackPixels(const PackPixelsParams &params,
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400181 const angle::Format &sourceFormat,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700182 int inputPitchIn,
183 const uint8_t *sourceIn,
184 uint8_t *destWithoutOffset)
185{
186 uint8_t *destWithOffset = destWithoutOffset + params.offset;
187
188 const uint8_t *source = sourceIn;
189 int inputPitch = inputPitchIn;
190
Frank Henigman1ffad842018-09-24 23:40:45 -0400191 if (params.reverseRowOrder)
Jamie Madilld2b50a02016-06-09 00:13:35 -0700192 {
193 source += inputPitch * (params.area.height - 1);
194 inputPitch = -inputPitch;
195 }
196
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400197 if (sourceFormat == *params.destFormat)
Jamie Madilld2b50a02016-06-09 00:13:35 -0700198 {
199 // Direct copy possible
200 for (int y = 0; y < params.area.height; ++y)
201 {
202 memcpy(destWithOffset + y * params.outputPitch, source + y * inputPitch,
Luc Ferron339b95e2018-06-12 10:21:52 -0400203 params.area.width * sourceFormat.pixelBytes);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700204 }
205 return;
206 }
207
Jamie Madill522095f2018-07-23 14:59:41 -0400208 PixelCopyFunction fastCopyFunc = sourceFormat.fastCopyFunctions.get(params.destFormat->id);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700209
210 if (fastCopyFunc)
211 {
212 // Fast copy is possible through some special function
213 for (int y = 0; y < params.area.height; ++y)
214 {
215 for (int x = 0; x < params.area.width; ++x)
216 {
217 uint8_t *dest =
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400218 destWithOffset + y * params.outputPitch + x * params.destFormat->pixelBytes;
Luc Ferron339b95e2018-06-12 10:21:52 -0400219 const uint8_t *src = source + y * inputPitch + x * sourceFormat.pixelBytes;
Jamie Madilld2b50a02016-06-09 00:13:35 -0700220
221 fastCopyFunc(src, dest);
222 }
223 }
224 return;
225 }
226
Jamie Madill522095f2018-07-23 14:59:41 -0400227 PixelWriteFunction pixelWriteFunction = params.destFormat->pixelWriteFunction;
228 ASSERT(pixelWriteFunction != nullptr);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700229
230 // Maximum size of any Color<T> type used.
231 uint8_t temp[16];
232 static_assert(sizeof(temp) >= sizeof(gl::ColorF) && sizeof(temp) >= sizeof(gl::ColorUI) &&
Jamie Madillb436aac2018-07-18 17:23:48 -0400233 sizeof(temp) >= sizeof(gl::ColorI) &&
234 sizeof(temp) >= sizeof(angle::DepthStencil),
235 "Unexpected size of pixel struct.");
Jamie Madilld2b50a02016-06-09 00:13:35 -0700236
Jamie Madill522095f2018-07-23 14:59:41 -0400237 PixelReadFunction pixelReadFunction = sourceFormat.pixelReadFunction;
238 ASSERT(pixelReadFunction != nullptr);
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400239
Jamie Madilld2b50a02016-06-09 00:13:35 -0700240 for (int y = 0; y < params.area.height; ++y)
241 {
242 for (int x = 0; x < params.area.width; ++x)
243 {
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400244 uint8_t *dest =
245 destWithOffset + y * params.outputPitch + x * params.destFormat->pixelBytes;
Luc Ferron339b95e2018-06-12 10:21:52 -0400246 const uint8_t *src = source + y * inputPitch + x * sourceFormat.pixelBytes;
Jamie Madilld2b50a02016-06-09 00:13:35 -0700247
248 // readFunc and writeFunc will be using the same type of color, CopyTexImage
249 // will not allow the copy otherwise.
Jamie Madill522095f2018-07-23 14:59:41 -0400250 pixelReadFunction(src, temp);
251 pixelWriteFunction(temp, dest);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700252 }
253 }
254}
255
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400256bool FastCopyFunctionMap::has(angle::FormatID formatID) const
Geoff Lang051dbc72015-01-05 15:48:58 -0500257{
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400258 return (get(formatID) != nullptr);
Geoff Lang051dbc72015-01-05 15:48:58 -0500259}
260
Jamie Madill522095f2018-07-23 14:59:41 -0400261PixelCopyFunction FastCopyFunctionMap::get(angle::FormatID formatID) const
Jamie Madill4f57e5f2016-10-27 17:36:53 -0400262{
263 for (size_t index = 0; index < mSize; ++index)
264 {
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400265 if (mData[index].formatID == formatID)
Jamie Madill4f57e5f2016-10-27 17:36:53 -0400266 {
267 return mData[index].func;
268 }
269 }
270
271 return nullptr;
Geoff Lang051dbc72015-01-05 15:48:58 -0500272}
Jamie Madilld2b50a02016-06-09 00:13:35 -0700273
Jamie Madill222c5172017-07-19 16:15:42 -0400274bool ShouldUseDebugLayers(const egl::AttributeMap &attribs)
275{
276 EGLAttrib debugSetting =
277 attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE);
278
279// Prefer to enable debug layers if compiling in Debug, and disabled in Release.
Geoff Lang58ba6bf2017-11-28 16:40:58 -0500280#if defined(ANGLE_ENABLE_ASSERTS)
Jamie Madill222c5172017-07-19 16:15:42 -0400281 return (debugSetting != EGL_FALSE);
282#else
283 return (debugSetting == EGL_TRUE);
Geoff Lang58ba6bf2017-11-28 16:40:58 -0500284#endif // defined(ANGLE_ENABLE_ASSERTS)
Jamie Madill222c5172017-07-19 16:15:42 -0400285}
286
Geoff Lang24ddc7a2018-06-11 14:56:34 -0400287bool ShouldUseVirtualizedContexts(const egl::AttributeMap &attribs, bool defaultValue)
288{
289 EGLAttrib virtualizedContextRequest =
290 attribs.get(EGL_PLATFORM_ANGLE_CONTEXT_VIRTUALIZATION_ANGLE, EGL_DONT_CARE);
291 if (defaultValue)
292 {
293 return (virtualizedContextRequest != EGL_FALSE);
294 }
295 else
296 {
297 return (virtualizedContextRequest == EGL_TRUE);
298 }
299}
300
Geoff Langd93cd6c2017-08-11 16:32:41 -0400301void CopyImageCHROMIUM(const uint8_t *sourceData,
302 size_t sourceRowPitch,
303 size_t sourcePixelBytes,
Brandon Jones4e6f2ae2018-09-19 11:09:51 -0700304 size_t sourceDepthPitch,
Jamie Madill522095f2018-07-23 14:59:41 -0400305 PixelReadFunction pixelReadFunction,
Geoff Langd93cd6c2017-08-11 16:32:41 -0400306 uint8_t *destData,
307 size_t destRowPitch,
308 size_t destPixelBytes,
Brandon Jones4e6f2ae2018-09-19 11:09:51 -0700309 size_t destDepthPitch,
Jamie Madill522095f2018-07-23 14:59:41 -0400310 PixelWriteFunction pixelWriteFunction,
Geoff Langd93cd6c2017-08-11 16:32:41 -0400311 GLenum destUnsizedFormat,
312 GLenum destComponentType,
313 size_t width,
314 size_t height,
Brandon Jones4e6f2ae2018-09-19 11:09:51 -0700315 size_t depth,
Geoff Langd93cd6c2017-08-11 16:32:41 -0400316 bool unpackFlipY,
317 bool unpackPremultiplyAlpha,
318 bool unpackUnmultiplyAlpha)
319{
320 using ConversionFunction = void (*)(gl::ColorF *);
321 ConversionFunction conversionFunction = CopyColor;
322 if (unpackPremultiplyAlpha != unpackUnmultiplyAlpha)
323 {
324 if (unpackPremultiplyAlpha)
325 {
326 conversionFunction = PremultiplyAlpha;
327 }
328 else
329 {
330 conversionFunction = UnmultiplyAlpha;
331 }
332 }
333
334 auto clipChannelsFunction = ClipChannelsNoOp;
335 switch (destUnsizedFormat)
336 {
337 case GL_RED:
338 clipChannelsFunction = ClipChannelsR;
339 break;
340 case GL_RG:
341 clipChannelsFunction = ClipChannelsRG;
342 break;
343 case GL_RGB:
344 clipChannelsFunction = ClipChannelsRGB;
345 break;
346 case GL_LUMINANCE:
347 clipChannelsFunction = ClipChannelsLuminance;
348 break;
349 case GL_ALPHA:
350 clipChannelsFunction = ClipChannelsAlpha;
351 break;
352 }
353
354 auto writeFunction = (destComponentType == GL_UNSIGNED_INT) ? WriteUintColor : WriteFloatColor;
355
Brandon Jones4e6f2ae2018-09-19 11:09:51 -0700356 for (size_t z = 0; z < depth; z++)
Geoff Langd93cd6c2017-08-11 16:32:41 -0400357 {
Brandon Jones4e6f2ae2018-09-19 11:09:51 -0700358 for (size_t y = 0; y < height; y++)
Geoff Langd93cd6c2017-08-11 16:32:41 -0400359 {
Brandon Jones4e6f2ae2018-09-19 11:09:51 -0700360 for (size_t x = 0; x < width; x++)
Geoff Langd93cd6c2017-08-11 16:32:41 -0400361 {
Brandon Jones4e6f2ae2018-09-19 11:09:51 -0700362 const uint8_t *sourcePixelData =
363 sourceData + y * sourceRowPitch + x * sourcePixelBytes + z * sourceDepthPitch;
Geoff Langd93cd6c2017-08-11 16:32:41 -0400364
Brandon Jones4e6f2ae2018-09-19 11:09:51 -0700365 gl::ColorF sourceColor;
366 pixelReadFunction(sourcePixelData, reinterpret_cast<uint8_t *>(&sourceColor));
367
368 conversionFunction(&sourceColor);
369 clipChannelsFunction(&sourceColor);
370
371 size_t destY = 0;
372 if (unpackFlipY)
373 {
374 destY += (height - 1);
375 destY -= y;
376 }
377 else
378 {
379 destY += y;
380 }
381
382 uint8_t *destPixelData =
383 destData + destY * destRowPitch + x * destPixelBytes + z * destDepthPitch;
384 writeFunction(sourceColor, pixelWriteFunction, destPixelData);
385 }
Geoff Langd93cd6c2017-08-11 16:32:41 -0400386 }
387 }
388}
389
Jamie Madill42975642017-10-12 12:31:51 -0400390// IncompleteTextureSet implementation.
Jamie Madillb980c562018-11-27 11:34:27 -0500391IncompleteTextureSet::IncompleteTextureSet() {}
Jamie Madill42975642017-10-12 12:31:51 -0400392
Jamie Madillb980c562018-11-27 11:34:27 -0500393IncompleteTextureSet::~IncompleteTextureSet() {}
Jamie Madill42975642017-10-12 12:31:51 -0400394
395void IncompleteTextureSet::onDestroy(const gl::Context *context)
396{
397 // Clear incomplete textures.
398 for (auto &incompleteTexture : mIncompleteTextures)
399 {
Corentin Wallez99d492c2018-02-27 15:17:10 -0500400 if (incompleteTexture.get() != nullptr)
401 {
Jamie Madill1c7f08c2018-10-10 16:13:02 -0400402 incompleteTexture->onDestroy(context);
Corentin Wallez99d492c2018-02-27 15:17:10 -0500403 incompleteTexture.set(context, nullptr);
404 }
Jamie Madill42975642017-10-12 12:31:51 -0400405 }
Jamie Madill42975642017-10-12 12:31:51 -0400406}
407
Jamie Madill666818e2018-11-14 09:54:33 -0500408angle::Result IncompleteTextureSet::getIncompleteTexture(
Jamie Madill42975642017-10-12 12:31:51 -0400409 const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -0500410 gl::TextureType type,
Jamie Madill42975642017-10-12 12:31:51 -0400411 MultisampleTextureInitializer *multisampleInitializer,
412 gl::Texture **textureOut)
413{
Corentin Wallez99d492c2018-02-27 15:17:10 -0500414 *textureOut = mIncompleteTextures[type].get();
415 if (*textureOut != nullptr)
Jamie Madill42975642017-10-12 12:31:51 -0400416 {
Jamie Madill666818e2018-11-14 09:54:33 -0500417 return angle::Result::Continue();
Jamie Madill42975642017-10-12 12:31:51 -0400418 }
419
420 ContextImpl *implFactory = context->getImplementation();
421
422 const GLubyte color[] = {0, 0, 0, 255};
423 const gl::Extents colorSize(1, 1, 1);
Corentin Wallezcda6af12017-10-30 19:20:37 -0400424 gl::PixelUnpackState unpack;
425 unpack.alignment = 1;
Jamie Madill42975642017-10-12 12:31:51 -0400426 const gl::Box area(0, 0, 0, 1, 1, 1);
427
428 // If a texture is external use a 2D texture for the incomplete texture
Corentin Wallez99d492c2018-02-27 15:17:10 -0500429 gl::TextureType createType = (type == gl::TextureType::External) ? gl::TextureType::_2D : type;
Jamie Madill42975642017-10-12 12:31:51 -0400430
431 gl::Texture *tex = new gl::Texture(implFactory, std::numeric_limits<GLuint>::max(), createType);
432 angle::UniqueObjectPointer<gl::Texture, gl::Context> t(tex, context);
433
Jamie Madill4f6592f2018-11-27 16:37:45 -0500434 // This is a bit of a kludge but is necessary to consume the error.
435 gl::Context *mutableContext = const_cast<gl::Context *>(context);
436
Corentin Wallez99d492c2018-02-27 15:17:10 -0500437 if (createType == gl::TextureType::_2DMultisample)
Jamie Madill42975642017-10-12 12:31:51 -0400438 {
Jamie Madill4f6592f2018-11-27 16:37:45 -0500439 ANGLE_TRY(
440 t->setStorageMultisample(mutableContext, createType, 1, GL_RGBA8, colorSize, true));
Jamie Madill42975642017-10-12 12:31:51 -0400441 }
442 else
443 {
Jamie Madill4f6592f2018-11-27 16:37:45 -0500444 ANGLE_TRY(t->setStorage(mutableContext, createType, 1, GL_RGBA8, colorSize));
Jamie Madill42975642017-10-12 12:31:51 -0400445 }
446
Corentin Wallez99d492c2018-02-27 15:17:10 -0500447 if (type == gl::TextureType::CubeMap)
Jamie Madill42975642017-10-12 12:31:51 -0400448 {
Corentin Wallez99d492c2018-02-27 15:17:10 -0500449 for (gl::TextureTarget face : gl::AllCubeFaceTextureTargets())
Jamie Madill42975642017-10-12 12:31:51 -0400450 {
Jamie Madill4f6592f2018-11-27 16:37:45 -0500451 ANGLE_TRY(t->setSubImage(mutableContext, unpack, nullptr, face, 0, area, GL_RGBA,
Jamie Madill0d0fb432018-09-07 17:43:32 -0400452 GL_UNSIGNED_BYTE, color));
Jamie Madill42975642017-10-12 12:31:51 -0400453 }
454 }
Corentin Wallez99d492c2018-02-27 15:17:10 -0500455 else if (type == gl::TextureType::_2DMultisample)
Jamie Madill42975642017-10-12 12:31:51 -0400456 {
457 // Call a specialized clear function to init a multisample texture.
458 ANGLE_TRY(multisampleInitializer->initializeMultisampleTextureToBlack(context, t.get()));
459 }
460 else
461 {
Jamie Madill4f6592f2018-11-27 16:37:45 -0500462 ANGLE_TRY(t->setSubImage(mutableContext, unpack, nullptr,
Jamie Madill0d0fb432018-09-07 17:43:32 -0400463 gl::NonCubeTextureTypeToTarget(createType), 0, area, GL_RGBA,
464 GL_UNSIGNED_BYTE, color));
Jamie Madill42975642017-10-12 12:31:51 -0400465 }
466
Luc Ferron4bba74f2018-04-19 14:40:45 -0400467 ANGLE_TRY(t->syncState(context));
Jamie Madill42975642017-10-12 12:31:51 -0400468
469 mIncompleteTextures[type].set(context, t.release());
470 *textureOut = mIncompleteTextures[type].get();
Jamie Madill666818e2018-11-14 09:54:33 -0500471 return angle::Result::Continue();
Jamie Madill42975642017-10-12 12:31:51 -0400472}
473
Luc Ferron46bcea52018-05-31 09:48:36 -0400474#define ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(cols, rows) \
475 template bool SetFloatUniformMatrix<cols, rows>(unsigned int, unsigned int, GLsizei, \
476 GLboolean, const GLfloat *, uint8_t *)
477
478ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 2);
479ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 3);
480ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 4);
481ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 3);
482ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 2);
483ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 4);
484ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 2);
485ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 4);
486ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 3);
487
488#undef ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC
489
490template <int cols, int rows>
491bool SetFloatUniformMatrix(unsigned int arrayElementOffset,
492 unsigned int elementCount,
493 GLsizei countIn,
494 GLboolean transpose,
495 const GLfloat *value,
496 uint8_t *targetData)
497{
498 unsigned int count =
499 std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn));
500
501 const unsigned int targetMatrixStride = (4 * rows);
502 GLfloat *target = reinterpret_cast<GLfloat *>(
503 targetData + arrayElementOffset * sizeof(GLfloat) * targetMatrixStride);
504
505 bool dirty = false;
506
507 for (unsigned int i = 0; i < count; i++)
508 {
509 if (transpose == GL_FALSE)
510 {
511 dirty = ExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
512 }
513 else
514 {
515 dirty = TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
516 }
517 target += targetMatrixStride;
518 value += cols * rows;
519 }
520
521 return dirty;
522}
523
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400524template void GetMatrixUniform<GLint>(GLenum, GLint *, const GLint *, bool);
525template void GetMatrixUniform<GLuint>(GLenum, GLuint *, const GLuint *, bool);
526
527void GetMatrixUniform(GLenum type, GLfloat *dataOut, const GLfloat *source, bool transpose)
528{
529 int columns = gl::VariableColumnCount(type);
530 int rows = gl::VariableRowCount(type);
531 for (GLint col = 0; col < columns; ++col)
532 {
533 for (GLint row = 0; row < rows; ++row)
534 {
535 GLfloat *outptr = dataOut + ((col * rows) + row);
536 const GLfloat *inptr =
537 transpose ? source + ((row * 4) + col) : source + ((col * 4) + row);
538 *outptr = *inptr;
539 }
540 }
541}
542
543template <typename NonFloatT>
544void GetMatrixUniform(GLenum type, NonFloatT *dataOut, const NonFloatT *source, bool transpose)
545{
546 UNREACHABLE();
547}
548
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400549const angle::Format &GetFormatFromFormatType(GLenum format, GLenum type)
550{
551 GLenum sizedInternalFormat = gl::GetInternalFormatInfo(format, type).sizedInternalFormat;
552 angle::FormatID angleFormatID = angle::Format::InternalFormatToID(sizedInternalFormat);
553 return angle::Format::Get(angleFormatID);
554}
Jamie Madillc1fd7372018-10-26 22:48:39 -0400555
556angle::Result ComputeStartVertex(ContextImpl *contextImpl,
557 const gl::IndexRange &indexRange,
558 GLint baseVertex,
559 GLint *firstVertexOut)
560{
561 // The entire index range should be within the limits of a 32-bit uint because the largest
562 // GL index type is GL_UNSIGNED_INT.
563 ASSERT(indexRange.start <= std::numeric_limits<uint32_t>::max() &&
564 indexRange.end <= std::numeric_limits<uint32_t>::max());
565
566 // The base vertex is only used in DrawElementsIndirect. Given the assertion above and the
567 // type of mBaseVertex (GLint), adding them both as 64-bit ints is safe.
568 int64_t startVertexInt64 =
569 static_cast<int64_t>(baseVertex) + static_cast<int64_t>(indexRange.start);
570
571 // OpenGL ES 3.2 spec section 10.5: "Behavior of DrawElementsOneInstance is undefined if the
572 // vertex ID is negative for any element"
573 ANGLE_CHECK_GL_MATH(contextImpl, startVertexInt64 >= 0);
574
575 // OpenGL ES 3.2 spec section 10.5: "If the vertex ID is larger than the maximum value
576 // representable by type, it should behave as if the calculation were upconverted to 32-bit
577 // unsigned integers(with wrapping on overflow conditions)." ANGLE does not fully handle
578 // these rules, an overflow error is returned if the start vertex cannot be stored in a
579 // 32-bit signed integer.
580 ANGLE_CHECK_GL_MATH(contextImpl, startVertexInt64 <= std::numeric_limits<GLint>::max())
581
582 *firstVertexOut = static_cast<GLint>(startVertexInt64);
583 return angle::Result::Continue();
584}
585
586angle::Result GetVertexRangeInfo(const gl::Context *context,
587 GLint firstVertex,
588 GLsizei vertexOrIndexCount,
589 GLenum indexTypeOrNone,
590 const void *indices,
591 GLint baseVertex,
592 GLint *startVertexOut,
593 size_t *vertexCountOut)
594{
595 if (indexTypeOrNone != GL_NONE)
596 {
597 gl::IndexRange indexRange;
598 ANGLE_TRY(context->getGLState().getVertexArray()->getIndexRange(
599 context, indexTypeOrNone, vertexOrIndexCount, indices, &indexRange));
600 ANGLE_TRY(ComputeStartVertex(context->getImplementation(), indexRange, baseVertex,
601 startVertexOut));
602 *vertexCountOut = indexRange.vertexCount();
603 }
604 else
605 {
606 *startVertexOut = firstVertex;
607 *vertexCountOut = vertexOrIndexCount;
608 }
609 return angle::Result::Continue();
610}
Jamie Madilld2b50a02016-06-09 00:13:35 -0700611} // namespace rx