1 //
2 // Copyright 2016 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // renderer_utils:
7 // Helper methods pertaining to most or all back-ends.
8 //
9
10 #include "libANGLE/renderer/renderer_utils.h"
11
12 #include "common/string_utils.h"
13 #include "common/system_utils.h"
14 #include "common/utilities.h"
15 #include "image_util/copyimage.h"
16 #include "image_util/imageformats.h"
17 #include "libANGLE/AttributeMap.h"
18 #include "libANGLE/Context.h"
19 #include "libANGLE/Display.h"
20 #include "libANGLE/formatutils.h"
21 #include "libANGLE/renderer/ContextImpl.h"
22 #include "libANGLE/renderer/Format.h"
23 #include "platform/Feature.h"
24
25 #include <string.h>
26
27 namespace rx
28 {
29
30 namespace
31 {
32 // Both D3D and Vulkan support the same set of standard sample positions for 1, 2, 4, 8, and 16
33 // samples. See:
34 //
35 // - https://msdn.microsoft.com/en-us/library/windows/desktop/ff476218.aspx
36 //
37 // -
38 // https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#primsrast-multisampling
39 using SamplePositionsArray = std::array<float, 32>;
40 constexpr std::array<SamplePositionsArray, 5> kSamplePositions = {
41 {{{0.5f, 0.5f}},
42 {{0.75f, 0.75f, 0.25f, 0.25f}},
43 {{0.375f, 0.125f, 0.875f, 0.375f, 0.125f, 0.625f, 0.625f, 0.875f}},
44 {{0.5625f, 0.3125f, 0.4375f, 0.6875f, 0.8125f, 0.5625f, 0.3125f, 0.1875f, 0.1875f, 0.8125f,
45 0.0625f, 0.4375f, 0.6875f, 0.9375f, 0.9375f, 0.0625f}},
46 {{0.5625f, 0.5625f, 0.4375f, 0.3125f, 0.3125f, 0.625f, 0.75f, 0.4375f,
47 0.1875f, 0.375f, 0.625f, 0.8125f, 0.8125f, 0.6875f, 0.6875f, 0.1875f,
48 0.375f, 0.875f, 0.5f, 0.0625f, 0.25f, 0.125f, 0.125f, 0.75f,
49 0.0f, 0.5f, 0.9375f, 0.25f, 0.875f, 0.9375f, 0.0625f, 0.0f}}}};
50
CopyColor(gl::ColorF * color)51 void CopyColor(gl::ColorF *color)
52 {
53 // No-op
54 }
55
PremultiplyAlpha(gl::ColorF * color)56 void PremultiplyAlpha(gl::ColorF *color)
57 {
58 color->red *= color->alpha;
59 color->green *= color->alpha;
60 color->blue *= color->alpha;
61 }
62
UnmultiplyAlpha(gl::ColorF * color)63 void UnmultiplyAlpha(gl::ColorF *color)
64 {
65 if (color->alpha != 0.0f)
66 {
67 float invAlpha = 1.0f / color->alpha;
68 color->red *= invAlpha;
69 color->green *= invAlpha;
70 color->blue *= invAlpha;
71 }
72 }
73
ClipChannelsR(gl::ColorF * color)74 void ClipChannelsR(gl::ColorF *color)
75 {
76 color->green = 0.0f;
77 color->blue = 0.0f;
78 color->alpha = 1.0f;
79 }
80
ClipChannelsRG(gl::ColorF * color)81 void ClipChannelsRG(gl::ColorF *color)
82 {
83 color->blue = 0.0f;
84 color->alpha = 1.0f;
85 }
86
ClipChannelsRGB(gl::ColorF * color)87 void ClipChannelsRGB(gl::ColorF *color)
88 {
89 color->alpha = 1.0f;
90 }
91
ClipChannelsLuminance(gl::ColorF * color)92 void ClipChannelsLuminance(gl::ColorF *color)
93 {
94 color->alpha = 1.0f;
95 }
96
ClipChannelsAlpha(gl::ColorF * color)97 void ClipChannelsAlpha(gl::ColorF *color)
98 {
99 color->red = 0.0f;
100 color->green = 0.0f;
101 color->blue = 0.0f;
102 }
103
ClipChannelsNoOp(gl::ColorF * color)104 void ClipChannelsNoOp(gl::ColorF *color) {}
105
WriteUintColor(const gl::ColorF & color,PixelWriteFunction colorWriteFunction,uint8_t * destPixelData)106 void WriteUintColor(const gl::ColorF &color,
107 PixelWriteFunction colorWriteFunction,
108 uint8_t *destPixelData)
109 {
110 gl::ColorUI destColor(
111 static_cast<unsigned int>(color.red * 255), static_cast<unsigned int>(color.green * 255),
112 static_cast<unsigned int>(color.blue * 255), static_cast<unsigned int>(color.alpha * 255));
113 colorWriteFunction(reinterpret_cast<const uint8_t *>(&destColor), destPixelData);
114 }
115
WriteFloatColor(const gl::ColorF & color,PixelWriteFunction colorWriteFunction,uint8_t * destPixelData)116 void WriteFloatColor(const gl::ColorF &color,
117 PixelWriteFunction colorWriteFunction,
118 uint8_t *destPixelData)
119 {
120 colorWriteFunction(reinterpret_cast<const uint8_t *>(&color), destPixelData);
121 }
122
123 template <int cols, int rows, bool IsColumnMajor>
GetFlattenedIndex(int col,int row)124 inline int GetFlattenedIndex(int col, int row)
125 {
126 if (IsColumnMajor)
127 {
128 return col * rows + row;
129 }
130 else
131 {
132 return row * cols + col;
133 }
134 }
135
136 template <typename T,
137 bool IsSrcColumnMajor,
138 int colsSrc,
139 int rowsSrc,
140 bool IsDstColumnMajor,
141 int colsDst,
142 int rowsDst>
ExpandMatrix(T * target,const GLfloat * value)143 void ExpandMatrix(T *target, const GLfloat *value)
144 {
145 static_assert(colsSrc <= colsDst && rowsSrc <= rowsDst, "Can only expand!");
146
147 constexpr int kDstFlatSize = colsDst * rowsDst;
148 T staging[kDstFlatSize] = {0};
149
150 for (int r = 0; r < rowsSrc; r++)
151 {
152 for (int c = 0; c < colsSrc; c++)
153 {
154 int srcIndex = GetFlattenedIndex<colsSrc, rowsSrc, IsSrcColumnMajor>(c, r);
155 int dstIndex = GetFlattenedIndex<colsDst, rowsDst, IsDstColumnMajor>(c, r);
156
157 staging[dstIndex] = static_cast<T>(value[srcIndex]);
158 }
159 }
160
161 memcpy(target, staging, kDstFlatSize * sizeof(T));
162 }
163
164 template <bool IsSrcColumMajor,
165 int colsSrc,
166 int rowsSrc,
167 bool IsDstColumnMajor,
168 int colsDst,
169 int rowsDst>
SetFloatUniformMatrix(unsigned int arrayElementOffset,unsigned int elementCount,GLsizei countIn,const GLfloat * value,uint8_t * targetData)170 void SetFloatUniformMatrix(unsigned int arrayElementOffset,
171 unsigned int elementCount,
172 GLsizei countIn,
173 const GLfloat *value,
174 uint8_t *targetData)
175 {
176 unsigned int count =
177 std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn));
178
179 const unsigned int targetMatrixStride = colsDst * rowsDst;
180 GLfloat *target = reinterpret_cast<GLfloat *>(
181 targetData + arrayElementOffset * sizeof(GLfloat) * targetMatrixStride);
182
183 for (unsigned int i = 0; i < count; i++)
184 {
185 ExpandMatrix<GLfloat, IsSrcColumMajor, colsSrc, rowsSrc, IsDstColumnMajor, colsDst,
186 rowsDst>(target, value);
187
188 target += targetMatrixStride;
189 value += colsSrc * rowsSrc;
190 }
191 }
192
SetFloatUniformMatrixFast(unsigned int arrayElementOffset,unsigned int elementCount,GLsizei countIn,size_t matrixSize,const GLfloat * value,uint8_t * targetData)193 void SetFloatUniformMatrixFast(unsigned int arrayElementOffset,
194 unsigned int elementCount,
195 GLsizei countIn,
196 size_t matrixSize,
197 const GLfloat *value,
198 uint8_t *targetData)
199 {
200 const unsigned int count =
201 std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn));
202
203 const uint8_t *valueData = reinterpret_cast<const uint8_t *>(value);
204 targetData = targetData + arrayElementOffset * matrixSize;
205
206 memcpy(targetData, valueData, matrixSize * count);
207 }
208
209 } // anonymous namespace
210
PackPixelsParams()211 PackPixelsParams::PackPixelsParams()
212 : destFormat(nullptr), outputPitch(0), packBuffer(nullptr), offset(0)
213 {}
214
PackPixelsParams(const gl::Rectangle & areaIn,const angle::Format & destFormat,GLuint outputPitchIn,bool reverseRowOrderIn,gl::Buffer * packBufferIn,ptrdiff_t offsetIn)215 PackPixelsParams::PackPixelsParams(const gl::Rectangle &areaIn,
216 const angle::Format &destFormat,
217 GLuint outputPitchIn,
218 bool reverseRowOrderIn,
219 gl::Buffer *packBufferIn,
220 ptrdiff_t offsetIn)
221 : area(areaIn),
222 destFormat(&destFormat),
223 outputPitch(outputPitchIn),
224 packBuffer(packBufferIn),
225 reverseRowOrder(reverseRowOrderIn),
226 offset(offsetIn)
227 {}
228
PackPixels(const PackPixelsParams & params,const angle::Format & sourceFormat,int inputPitchIn,const uint8_t * sourceIn,uint8_t * destWithoutOffset)229 void PackPixels(const PackPixelsParams ¶ms,
230 const angle::Format &sourceFormat,
231 int inputPitchIn,
232 const uint8_t *sourceIn,
233 uint8_t *destWithoutOffset)
234 {
235 uint8_t *destWithOffset = destWithoutOffset + params.offset;
236
237 const uint8_t *source = sourceIn;
238 int inputPitch = inputPitchIn;
239
240 if (params.reverseRowOrder)
241 {
242 source += inputPitch * (params.area.height - 1);
243 inputPitch = -inputPitch;
244 }
245
246 if (sourceFormat == *params.destFormat)
247 {
248 // Direct copy possible
249 for (int y = 0; y < params.area.height; ++y)
250 {
251 memcpy(destWithOffset + y * params.outputPitch, source + y * inputPitch,
252 params.area.width * sourceFormat.pixelBytes);
253 }
254 return;
255 }
256
257 PixelCopyFunction fastCopyFunc = sourceFormat.fastCopyFunctions.get(params.destFormat->id);
258
259 if (fastCopyFunc)
260 {
261 // Fast copy is possible through some special function
262 for (int y = 0; y < params.area.height; ++y)
263 {
264 for (int x = 0; x < params.area.width; ++x)
265 {
266 uint8_t *dest =
267 destWithOffset + y * params.outputPitch + x * params.destFormat->pixelBytes;
268 const uint8_t *src = source + y * inputPitch + x * sourceFormat.pixelBytes;
269
270 fastCopyFunc(src, dest);
271 }
272 }
273 return;
274 }
275
276 PixelWriteFunction pixelWriteFunction = params.destFormat->pixelWriteFunction;
277 ASSERT(pixelWriteFunction != nullptr);
278
279 // Maximum size of any Color<T> type used.
280 uint8_t temp[16];
281 static_assert(sizeof(temp) >= sizeof(gl::ColorF) && sizeof(temp) >= sizeof(gl::ColorUI) &&
282 sizeof(temp) >= sizeof(gl::ColorI) &&
283 sizeof(temp) >= sizeof(angle::DepthStencil),
284 "Unexpected size of pixel struct.");
285
286 PixelReadFunction pixelReadFunction = sourceFormat.pixelReadFunction;
287 ASSERT(pixelReadFunction != nullptr);
288
289 for (int y = 0; y < params.area.height; ++y)
290 {
291 for (int x = 0; x < params.area.width; ++x)
292 {
293 uint8_t *dest =
294 destWithOffset + y * params.outputPitch + x * params.destFormat->pixelBytes;
295 const uint8_t *src = source + y * inputPitch + x * sourceFormat.pixelBytes;
296
297 // readFunc and writeFunc will be using the same type of color, CopyTexImage
298 // will not allow the copy otherwise.
299 pixelReadFunction(src, temp);
300 pixelWriteFunction(temp, dest);
301 }
302 }
303 }
304
has(angle::FormatID formatID) const305 bool FastCopyFunctionMap::has(angle::FormatID formatID) const
306 {
307 return (get(formatID) != nullptr);
308 }
309
get(angle::FormatID formatID) const310 PixelCopyFunction FastCopyFunctionMap::get(angle::FormatID formatID) const
311 {
312 for (size_t index = 0; index < mSize; ++index)
313 {
314 if (mData[index].formatID == formatID)
315 {
316 return mData[index].func;
317 }
318 }
319
320 return nullptr;
321 }
322
ShouldUseDebugLayers(const egl::AttributeMap & attribs)323 bool ShouldUseDebugLayers(const egl::AttributeMap &attribs)
324 {
325 EGLAttrib debugSetting =
326 attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE);
327
328 // Prefer to enable debug layers when available.
329 #if defined(ANGLE_ENABLE_ASSERTS)
330 return (debugSetting != EGL_FALSE);
331 #else
332 return (debugSetting == EGL_TRUE);
333 #endif // defined(ANGLE_ENABLE_ASSERTS)
334 }
335
ShouldUseVirtualizedContexts(const egl::AttributeMap & attribs,bool defaultValue)336 bool ShouldUseVirtualizedContexts(const egl::AttributeMap &attribs, bool defaultValue)
337 {
338 EGLAttrib virtualizedContextRequest =
339 attribs.get(EGL_PLATFORM_ANGLE_CONTEXT_VIRTUALIZATION_ANGLE, EGL_DONT_CARE);
340 if (defaultValue)
341 {
342 return (virtualizedContextRequest != EGL_FALSE);
343 }
344 else
345 {
346 return (virtualizedContextRequest == EGL_TRUE);
347 }
348 }
349
CopyImageCHROMIUM(const uint8_t * sourceData,size_t sourceRowPitch,size_t sourcePixelBytes,size_t sourceDepthPitch,PixelReadFunction pixelReadFunction,uint8_t * destData,size_t destRowPitch,size_t destPixelBytes,size_t destDepthPitch,PixelWriteFunction pixelWriteFunction,GLenum destUnsizedFormat,GLenum destComponentType,size_t width,size_t height,size_t depth,bool unpackFlipY,bool unpackPremultiplyAlpha,bool unpackUnmultiplyAlpha)350 void CopyImageCHROMIUM(const uint8_t *sourceData,
351 size_t sourceRowPitch,
352 size_t sourcePixelBytes,
353 size_t sourceDepthPitch,
354 PixelReadFunction pixelReadFunction,
355 uint8_t *destData,
356 size_t destRowPitch,
357 size_t destPixelBytes,
358 size_t destDepthPitch,
359 PixelWriteFunction pixelWriteFunction,
360 GLenum destUnsizedFormat,
361 GLenum destComponentType,
362 size_t width,
363 size_t height,
364 size_t depth,
365 bool unpackFlipY,
366 bool unpackPremultiplyAlpha,
367 bool unpackUnmultiplyAlpha)
368 {
369 using ConversionFunction = void (*)(gl::ColorF *);
370 ConversionFunction conversionFunction = CopyColor;
371 if (unpackPremultiplyAlpha != unpackUnmultiplyAlpha)
372 {
373 if (unpackPremultiplyAlpha)
374 {
375 conversionFunction = PremultiplyAlpha;
376 }
377 else
378 {
379 conversionFunction = UnmultiplyAlpha;
380 }
381 }
382
383 auto clipChannelsFunction = ClipChannelsNoOp;
384 switch (destUnsizedFormat)
385 {
386 case GL_RED:
387 clipChannelsFunction = ClipChannelsR;
388 break;
389 case GL_RG:
390 clipChannelsFunction = ClipChannelsRG;
391 break;
392 case GL_RGB:
393 clipChannelsFunction = ClipChannelsRGB;
394 break;
395 case GL_LUMINANCE:
396 clipChannelsFunction = ClipChannelsLuminance;
397 break;
398 case GL_ALPHA:
399 clipChannelsFunction = ClipChannelsAlpha;
400 break;
401 }
402
403 auto writeFunction = (destComponentType == GL_UNSIGNED_INT) ? WriteUintColor : WriteFloatColor;
404
405 for (size_t z = 0; z < depth; z++)
406 {
407 for (size_t y = 0; y < height; y++)
408 {
409 for (size_t x = 0; x < width; x++)
410 {
411 const uint8_t *sourcePixelData =
412 sourceData + y * sourceRowPitch + x * sourcePixelBytes + z * sourceDepthPitch;
413
414 gl::ColorF sourceColor;
415 pixelReadFunction(sourcePixelData, reinterpret_cast<uint8_t *>(&sourceColor));
416
417 conversionFunction(&sourceColor);
418 clipChannelsFunction(&sourceColor);
419
420 size_t destY = 0;
421 if (unpackFlipY)
422 {
423 destY += (height - 1);
424 destY -= y;
425 }
426 else
427 {
428 destY += y;
429 }
430
431 uint8_t *destPixelData =
432 destData + destY * destRowPitch + x * destPixelBytes + z * destDepthPitch;
433 writeFunction(sourceColor, pixelWriteFunction, destPixelData);
434 }
435 }
436 }
437 }
438
439 // IncompleteTextureSet implementation.
IncompleteTextureSet()440 IncompleteTextureSet::IncompleteTextureSet() {}
441
~IncompleteTextureSet()442 IncompleteTextureSet::~IncompleteTextureSet() {}
443
onDestroy(const gl::Context * context)444 void IncompleteTextureSet::onDestroy(const gl::Context *context)
445 {
446 // Clear incomplete textures.
447 for (auto &incompleteTexture : mIncompleteTextures)
448 {
449 if (incompleteTexture.get() != nullptr)
450 {
451 incompleteTexture->onDestroy(context);
452 incompleteTexture.set(context, nullptr);
453 }
454 }
455 }
456
getIncompleteTexture(const gl::Context * context,gl::TextureType type,MultisampleTextureInitializer * multisampleInitializer,gl::Texture ** textureOut)457 angle::Result IncompleteTextureSet::getIncompleteTexture(
458 const gl::Context *context,
459 gl::TextureType type,
460 MultisampleTextureInitializer *multisampleInitializer,
461 gl::Texture **textureOut)
462 {
463 *textureOut = mIncompleteTextures[type].get();
464 if (*textureOut != nullptr)
465 {
466 return angle::Result::Continue;
467 }
468
469 ContextImpl *implFactory = context->getImplementation();
470
471 const GLubyte color[] = {0, 0, 0, 255};
472 const gl::Extents colorSize(1, 1, 1);
473 gl::PixelUnpackState unpack;
474 unpack.alignment = 1;
475 const gl::Box area(0, 0, 0, 1, 1, 1);
476
477 // If a texture is external use a 2D texture for the incomplete texture
478 gl::TextureType createType = (type == gl::TextureType::External) ? gl::TextureType::_2D : type;
479
480 gl::Texture *tex =
481 new gl::Texture(implFactory, {std::numeric_limits<GLuint>::max()}, createType);
482 angle::UniqueObjectPointer<gl::Texture, gl::Context> t(tex, context);
483
484 // This is a bit of a kludge but is necessary to consume the error.
485 gl::Context *mutableContext = const_cast<gl::Context *>(context);
486
487 if (createType == gl::TextureType::_2DMultisample)
488 {
489 ANGLE_TRY(
490 t->setStorageMultisample(mutableContext, createType, 1, GL_RGBA8, colorSize, true));
491 }
492 else
493 {
494 ANGLE_TRY(t->setStorage(mutableContext, createType, 1, GL_RGBA8, colorSize));
495 }
496
497 if (type == gl::TextureType::CubeMap)
498 {
499 for (gl::TextureTarget face : gl::AllCubeFaceTextureTargets())
500 {
501 ANGLE_TRY(t->setSubImage(mutableContext, unpack, nullptr, face, 0, area, GL_RGBA,
502 GL_UNSIGNED_BYTE, color));
503 }
504 }
505 else if (type == gl::TextureType::_2DMultisample)
506 {
507 // Call a specialized clear function to init a multisample texture.
508 ANGLE_TRY(multisampleInitializer->initializeMultisampleTextureToBlack(context, t.get()));
509 }
510 else
511 {
512 ANGLE_TRY(t->setSubImage(mutableContext, unpack, nullptr,
513 gl::NonCubeTextureTypeToTarget(createType), 0, area, GL_RGBA,
514 GL_UNSIGNED_BYTE, color));
515 }
516
517 ANGLE_TRY(t->syncState(context));
518
519 mIncompleteTextures[type].set(context, t.release());
520 *textureOut = mIncompleteTextures[type].get();
521 return angle::Result::Continue;
522 }
523
524 #define ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(api, cols, rows) \
525 template void SetFloatUniformMatrix##api<cols, rows>::Run( \
526 unsigned int, unsigned int, GLsizei, GLboolean, const GLfloat *, uint8_t *)
527
528 ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(GLSL, 2, 2);
529 ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(GLSL, 3, 3);
530 ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(GLSL, 2, 3);
531 ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(GLSL, 3, 2);
532 ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(GLSL, 4, 2);
533 ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(GLSL, 4, 3);
534
535 ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(HLSL, 2, 2);
536 ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(HLSL, 3, 3);
537 ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(HLSL, 2, 3);
538 ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(HLSL, 3, 2);
539 ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(HLSL, 2, 4);
540 ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(HLSL, 3, 4);
541
542 #undef ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC
543
544 #define ANGLE_SPECIALIZATION_ROWS_SET_UNIFORM_MATRIX_FUNC(api, cols, rows) \
545 template void SetFloatUniformMatrix##api<cols, 4>::Run(unsigned int, unsigned int, GLsizei, \
546 GLboolean, const GLfloat *, uint8_t *)
547
548 template <int cols>
549 struct SetFloatUniformMatrixGLSL<cols, 4>
550 {
551 static void Run(unsigned int arrayElementOffset,
552 unsigned int elementCount,
553 GLsizei countIn,
554 GLboolean transpose,
555 const GLfloat *value,
556 uint8_t *targetData);
557 };
558
559 ANGLE_SPECIALIZATION_ROWS_SET_UNIFORM_MATRIX_FUNC(GLSL, 2, 4);
560 ANGLE_SPECIALIZATION_ROWS_SET_UNIFORM_MATRIX_FUNC(GLSL, 3, 4);
561 ANGLE_SPECIALIZATION_ROWS_SET_UNIFORM_MATRIX_FUNC(GLSL, 4, 4);
562
563 #undef ANGLE_SPECIALIZATION_ROWS_SET_UNIFORM_MATRIX_FUNC
564
565 #define ANGLE_SPECIALIZATION_COLS_SET_UNIFORM_MATRIX_FUNC(api, cols, rows) \
566 template void SetFloatUniformMatrix##api<4, rows>::Run(unsigned int, unsigned int, GLsizei, \
567 GLboolean, const GLfloat *, uint8_t *)
568
569 template <int rows>
570 struct SetFloatUniformMatrixHLSL<4, rows>
571 {
572 static void Run(unsigned int arrayElementOffset,
573 unsigned int elementCount,
574 GLsizei countIn,
575 GLboolean transpose,
576 const GLfloat *value,
577 uint8_t *targetData);
578 };
579
580 ANGLE_SPECIALIZATION_COLS_SET_UNIFORM_MATRIX_FUNC(HLSL, 4, 2);
581 ANGLE_SPECIALIZATION_COLS_SET_UNIFORM_MATRIX_FUNC(HLSL, 4, 3);
582 ANGLE_SPECIALIZATION_COLS_SET_UNIFORM_MATRIX_FUNC(HLSL, 4, 4);
583
584 #undef ANGLE_SPECIALIZATION_COLS_SET_UNIFORM_MATRIX_FUNC
585
586 template <int cols>
Run(unsigned int arrayElementOffset,unsigned int elementCount,GLsizei countIn,GLboolean transpose,const GLfloat * value,uint8_t * targetData)587 void SetFloatUniformMatrixGLSL<cols, 4>::Run(unsigned int arrayElementOffset,
588 unsigned int elementCount,
589 GLsizei countIn,
590 GLboolean transpose,
591 const GLfloat *value,
592 uint8_t *targetData)
593 {
594 const bool isSrcColumnMajor = !transpose;
595 if (isSrcColumnMajor)
596 {
597 // Both src and dst matrixs are has same layout,
598 // a single memcpy updates all the matrices
599 constexpr size_t srcMatrixSize = sizeof(GLfloat) * cols * 4;
600 SetFloatUniformMatrixFast(arrayElementOffset, elementCount, countIn, srcMatrixSize, value,
601 targetData);
602 }
603 else
604 {
605 // fallback to general cases
606 SetFloatUniformMatrix<false, cols, 4, true, cols, 4>(arrayElementOffset, elementCount,
607 countIn, value, targetData);
608 }
609 }
610
611 template <int cols, int rows>
Run(unsigned int arrayElementOffset,unsigned int elementCount,GLsizei countIn,GLboolean transpose,const GLfloat * value,uint8_t * targetData)612 void SetFloatUniformMatrixGLSL<cols, rows>::Run(unsigned int arrayElementOffset,
613 unsigned int elementCount,
614 GLsizei countIn,
615 GLboolean transpose,
616 const GLfloat *value,
617 uint8_t *targetData)
618 {
619 const bool isSrcColumnMajor = !transpose;
620 // GLSL expects matrix uniforms to be column-major, and each column is padded to 4 rows.
621 if (isSrcColumnMajor)
622 {
623 SetFloatUniformMatrix<true, cols, rows, true, cols, 4>(arrayElementOffset, elementCount,
624 countIn, value, targetData);
625 }
626 else
627 {
628 SetFloatUniformMatrix<false, cols, rows, true, cols, 4>(arrayElementOffset, elementCount,
629 countIn, value, targetData);
630 }
631 }
632
633 template <int rows>
Run(unsigned int arrayElementOffset,unsigned int elementCount,GLsizei countIn,GLboolean transpose,const GLfloat * value,uint8_t * targetData)634 void SetFloatUniformMatrixHLSL<4, rows>::Run(unsigned int arrayElementOffset,
635 unsigned int elementCount,
636 GLsizei countIn,
637 GLboolean transpose,
638 const GLfloat *value,
639 uint8_t *targetData)
640 {
641 const bool isSrcColumnMajor = !transpose;
642 if (!isSrcColumnMajor)
643 {
644 // Both src and dst matrixs are has same layout,
645 // a single memcpy updates all the matrices
646 constexpr size_t srcMatrixSize = sizeof(GLfloat) * 4 * rows;
647 SetFloatUniformMatrixFast(arrayElementOffset, elementCount, countIn, srcMatrixSize, value,
648 targetData);
649 }
650 else
651 {
652 // fallback to general cases
653 SetFloatUniformMatrix<true, 4, rows, false, 4, rows>(arrayElementOffset, elementCount,
654 countIn, value, targetData);
655 }
656 }
657
658 template <int cols, int rows>
Run(unsigned int arrayElementOffset,unsigned int elementCount,GLsizei countIn,GLboolean transpose,const GLfloat * value,uint8_t * targetData)659 void SetFloatUniformMatrixHLSL<cols, rows>::Run(unsigned int arrayElementOffset,
660 unsigned int elementCount,
661 GLsizei countIn,
662 GLboolean transpose,
663 const GLfloat *value,
664 uint8_t *targetData)
665 {
666 const bool isSrcColumnMajor = !transpose;
667 // Internally store matrices as row-major to accomodate HLSL matrix indexing. Each row is
668 // padded to 4 columns.
669 if (!isSrcColumnMajor)
670 {
671 SetFloatUniformMatrix<false, cols, rows, false, 4, rows>(arrayElementOffset, elementCount,
672 countIn, value, targetData);
673 }
674 else
675 {
676 SetFloatUniformMatrix<true, cols, rows, false, 4, rows>(arrayElementOffset, elementCount,
677 countIn, value, targetData);
678 }
679 }
680
681 template void GetMatrixUniform<GLint>(GLenum, GLint *, const GLint *, bool);
682 template void GetMatrixUniform<GLuint>(GLenum, GLuint *, const GLuint *, bool);
683
GetMatrixUniform(GLenum type,GLfloat * dataOut,const GLfloat * source,bool transpose)684 void GetMatrixUniform(GLenum type, GLfloat *dataOut, const GLfloat *source, bool transpose)
685 {
686 int columns = gl::VariableColumnCount(type);
687 int rows = gl::VariableRowCount(type);
688 for (GLint col = 0; col < columns; ++col)
689 {
690 for (GLint row = 0; row < rows; ++row)
691 {
692 GLfloat *outptr = dataOut + ((col * rows) + row);
693 const GLfloat *inptr =
694 transpose ? source + ((row * 4) + col) : source + ((col * 4) + row);
695 *outptr = *inptr;
696 }
697 }
698 }
699
700 template <typename NonFloatT>
GetMatrixUniform(GLenum type,NonFloatT * dataOut,const NonFloatT * source,bool transpose)701 void GetMatrixUniform(GLenum type, NonFloatT *dataOut, const NonFloatT *source, bool transpose)
702 {
703 UNREACHABLE();
704 }
705
GetFormatFromFormatType(GLenum format,GLenum type)706 const angle::Format &GetFormatFromFormatType(GLenum format, GLenum type)
707 {
708 GLenum sizedInternalFormat = gl::GetInternalFormatInfo(format, type).sizedInternalFormat;
709 angle::FormatID angleFormatID = angle::Format::InternalFormatToID(sizedInternalFormat);
710 return angle::Format::Get(angleFormatID);
711 }
712
ComputeStartVertex(ContextImpl * contextImpl,const gl::IndexRange & indexRange,GLint baseVertex,GLint * firstVertexOut)713 angle::Result ComputeStartVertex(ContextImpl *contextImpl,
714 const gl::IndexRange &indexRange,
715 GLint baseVertex,
716 GLint *firstVertexOut)
717 {
718 // The entire index range should be within the limits of a 32-bit uint because the largest
719 // GL index type is GL_UNSIGNED_INT.
720 ASSERT(indexRange.start <= std::numeric_limits<uint32_t>::max() &&
721 indexRange.end <= std::numeric_limits<uint32_t>::max());
722
723 // The base vertex is only used in DrawElementsIndirect. Given the assertion above and the
724 // type of mBaseVertex (GLint), adding them both as 64-bit ints is safe.
725 int64_t startVertexInt64 =
726 static_cast<int64_t>(baseVertex) + static_cast<int64_t>(indexRange.start);
727
728 // OpenGL ES 3.2 spec section 10.5: "Behavior of DrawElementsOneInstance is undefined if the
729 // vertex ID is negative for any element"
730 ANGLE_CHECK_GL_MATH(contextImpl, startVertexInt64 >= 0);
731
732 // OpenGL ES 3.2 spec section 10.5: "If the vertex ID is larger than the maximum value
733 // representable by type, it should behave as if the calculation were upconverted to 32-bit
734 // unsigned integers(with wrapping on overflow conditions)." ANGLE does not fully handle
735 // these rules, an overflow error is returned if the start vertex cannot be stored in a
736 // 32-bit signed integer.
737 ANGLE_CHECK_GL_MATH(contextImpl, startVertexInt64 <= std::numeric_limits<GLint>::max());
738
739 *firstVertexOut = static_cast<GLint>(startVertexInt64);
740 return angle::Result::Continue;
741 }
742
GetVertexRangeInfo(const gl::Context * context,GLint firstVertex,GLsizei vertexOrIndexCount,gl::DrawElementsType indexTypeOrInvalid,const void * indices,GLint baseVertex,GLint * startVertexOut,size_t * vertexCountOut)743 angle::Result GetVertexRangeInfo(const gl::Context *context,
744 GLint firstVertex,
745 GLsizei vertexOrIndexCount,
746 gl::DrawElementsType indexTypeOrInvalid,
747 const void *indices,
748 GLint baseVertex,
749 GLint *startVertexOut,
750 size_t *vertexCountOut)
751 {
752 if (indexTypeOrInvalid != gl::DrawElementsType::InvalidEnum)
753 {
754 gl::IndexRange indexRange;
755 ANGLE_TRY(context->getState().getVertexArray()->getIndexRange(
756 context, indexTypeOrInvalid, vertexOrIndexCount, indices, &indexRange));
757 ANGLE_TRY(ComputeStartVertex(context->getImplementation(), indexRange, baseVertex,
758 startVertexOut));
759 *vertexCountOut = indexRange.vertexCount();
760 }
761 else
762 {
763 *startVertexOut = firstVertex;
764 *vertexCountOut = vertexOrIndexCount;
765 }
766 return angle::Result::Continue;
767 }
768
ClipRectToScissor(const gl::State & glState,const gl::Rectangle & rect,bool invertY)769 gl::Rectangle ClipRectToScissor(const gl::State &glState, const gl::Rectangle &rect, bool invertY)
770 {
771 if (glState.isScissorTestEnabled())
772 {
773 gl::Rectangle clippedRect;
774 if (!gl::ClipRectangle(glState.getScissor(), rect, &clippedRect))
775 {
776 return gl::Rectangle();
777 }
778
779 if (invertY)
780 {
781 clippedRect.y = rect.height - clippedRect.y - clippedRect.height;
782 }
783
784 return clippedRect;
785 }
786
787 // If the scissor test isn't enabled, assume it has infinite size. Its intersection with the
788 // rect would be the rect itself.
789 //
790 // Note that on Vulkan, returning this (as opposed to a fixed max-int-sized rect) could lead to
791 // unnecessary pipeline creations if two otherwise identical pipelines are used on framebuffers
792 // with different sizes. If such usage is observed in an application, we should investigate
793 // possible optimizations.
794 return rect;
795 }
796
ApplyFeatureOverrides(angle::FeatureSetBase * features,const egl::DisplayState & state)797 void ApplyFeatureOverrides(angle::FeatureSetBase *features, const egl::DisplayState &state)
798 {
799 features->overrideFeatures(state.featureOverridesEnabled, true);
800 features->overrideFeatures(state.featureOverridesDisabled, false);
801
802 // Override with environment as well.
803 std::vector<std::string> overridesEnabled =
804 angle::GetStringsFromEnvironmentVar("ANGLE_FEATURE_OVERRIDES_ENABLED", ":");
805 std::vector<std::string> overridesDisabled =
806 angle::GetStringsFromEnvironmentVar("ANGLE_FEATURE_OVERRIDES_DISABLED", ":");
807 features->overrideFeatures(overridesEnabled, true);
808 features->overrideFeatures(overridesDisabled, false);
809 }
810
GetSamplePosition(GLsizei sampleCount,size_t index,GLfloat * xy)811 void GetSamplePosition(GLsizei sampleCount, size_t index, GLfloat *xy)
812 {
813 ASSERT(gl::isPow2(sampleCount));
814 if (sampleCount > 16)
815 {
816 // Vulkan (and D3D11) doesn't have standard sample positions for 32 and 64 samples (and no
817 // drivers are known to support that many samples)
818 xy[0] = 0.5f;
819 xy[1] = 0.5f;
820 }
821 else
822 {
823 size_t indexKey = static_cast<size_t>(gl::log2(sampleCount));
824 ASSERT(indexKey < kSamplePositions.size() &&
825 (2 * index + 1) < kSamplePositions[indexKey].size());
826
827 xy[0] = kSamplePositions[indexKey][2 * index];
828 xy[1] = kSamplePositions[indexKey][2 * index + 1];
829 }
830 }
831 } // namespace rx
832