Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 1 | // |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 2 | // Copyright 2016 The ANGLE Project Authors. All rights reserved. |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 6 | // renderer_utils: |
| 7 | // Helper methods pertaining to most or all back-ends. |
| 8 | // |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 9 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 10 | #include "libANGLE/renderer/renderer_utils.h" |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 11 | |
Geoff Lang | 6e4cfce | 2016-06-13 15:06:31 -0400 | [diff] [blame] | 12 | #include "image_util/copyimage.h" |
| 13 | #include "image_util/imageformats.h" |
| 14 | |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 15 | #include "libANGLE/AttributeMap.h" |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 16 | #include "libANGLE/Context.h" |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 17 | #include "libANGLE/formatutils.h" |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 18 | #include "libANGLE/renderer/ContextImpl.h" |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 19 | #include "libANGLE/renderer/Format.h" |
Geoff Lang | 6e4cfce | 2016-06-13 15:06:31 -0400 | [diff] [blame] | 20 | |
| 21 | #include <string.h> |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 22 | #include "common/utilities.h" |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 23 | |
| 24 | namespace rx |
| 25 | { |
| 26 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 27 | namespace |
| 28 | { |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 29 | void CopyColor(gl::ColorF *color) |
| 30 | { |
| 31 | // No-op |
| 32 | } |
| 33 | |
| 34 | void PremultiplyAlpha(gl::ColorF *color) |
| 35 | { |
| 36 | color->red *= color->alpha; |
| 37 | color->green *= color->alpha; |
| 38 | color->blue *= color->alpha; |
| 39 | } |
| 40 | |
| 41 | void 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 | |
| 52 | void ClipChannelsR(gl::ColorF *color) |
| 53 | { |
| 54 | color->green = 0.0f; |
| 55 | color->blue = 0.0f; |
| 56 | color->alpha = 1.0f; |
| 57 | } |
| 58 | |
| 59 | void ClipChannelsRG(gl::ColorF *color) |
| 60 | { |
| 61 | color->blue = 0.0f; |
| 62 | color->alpha = 1.0f; |
| 63 | } |
| 64 | |
| 65 | void ClipChannelsRGB(gl::ColorF *color) |
| 66 | { |
| 67 | color->alpha = 1.0f; |
| 68 | } |
| 69 | |
| 70 | void ClipChannelsLuminance(gl::ColorF *color) |
| 71 | { |
| 72 | color->alpha = 1.0f; |
| 73 | } |
| 74 | |
| 75 | void ClipChannelsAlpha(gl::ColorF *color) |
| 76 | { |
| 77 | color->red = 0.0f; |
| 78 | color->green = 0.0f; |
| 79 | color->blue = 0.0f; |
| 80 | } |
| 81 | |
| 82 | void ClipChannelsNoOp(gl::ColorF *color) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | void WriteUintColor(const gl::ColorF &color, |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 87 | PixelWriteFunction colorWriteFunction, |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 88 | uint8_t *destPixelData) |
| 89 | { |
| 90 | gl::ColorUI destColor( |
| 91 | static_cast<unsigned int>(color.red * 255), static_cast<unsigned int>(color.green * 255), |
| 92 | static_cast<unsigned int>(color.blue * 255), static_cast<unsigned int>(color.alpha * 255)); |
| 93 | colorWriteFunction(reinterpret_cast<const uint8_t *>(&destColor), destPixelData); |
| 94 | } |
| 95 | |
| 96 | void WriteFloatColor(const gl::ColorF &color, |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 97 | PixelWriteFunction colorWriteFunction, |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 98 | uint8_t *destPixelData) |
| 99 | { |
| 100 | colorWriteFunction(reinterpret_cast<const uint8_t *>(&color), destPixelData); |
| 101 | } |
| 102 | |
Luc Ferron | 46bcea5 | 2018-05-31 09:48:36 -0400 | [diff] [blame] | 103 | template <typename T, int cols, int rows> |
| 104 | bool TransposeExpandMatrix(T *target, const GLfloat *value) |
| 105 | { |
| 106 | constexpr int targetWidth = 4; |
| 107 | constexpr int targetHeight = rows; |
| 108 | constexpr int srcWidth = rows; |
| 109 | constexpr int srcHeight = cols; |
| 110 | |
| 111 | constexpr int copyWidth = std::min(targetHeight, srcWidth); |
| 112 | constexpr int copyHeight = std::min(targetWidth, srcHeight); |
| 113 | |
| 114 | T staging[targetWidth * targetHeight] = {0}; |
| 115 | |
| 116 | for (int x = 0; x < copyWidth; x++) |
| 117 | { |
| 118 | for (int y = 0; y < copyHeight; y++) |
| 119 | { |
| 120 | staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0) |
| 125 | { |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | memcpy(target, staging, targetWidth * targetHeight * sizeof(T)); |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | template <typename T, int cols, int rows> |
| 134 | bool ExpandMatrix(T *target, const GLfloat *value) |
| 135 | { |
| 136 | constexpr int kTargetWidth = 4; |
| 137 | constexpr int kTargetHeight = rows; |
| 138 | constexpr int kSrcWidth = cols; |
| 139 | constexpr int kSrcHeight = rows; |
| 140 | |
| 141 | constexpr int kCopyWidth = std::min(kTargetWidth, kSrcWidth); |
| 142 | constexpr int kCopyHeight = std::min(kTargetHeight, kSrcHeight); |
| 143 | |
| 144 | T staging[kTargetWidth * kTargetHeight] = {0}; |
| 145 | |
| 146 | for (int y = 0; y < kCopyHeight; y++) |
| 147 | { |
| 148 | for (int x = 0; x < kCopyWidth; x++) |
| 149 | { |
| 150 | staging[y * kTargetWidth + x] = static_cast<T>(value[y * kSrcWidth + x]); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (memcmp(target, staging, kTargetWidth * kTargetHeight * sizeof(T)) == 0) |
| 155 | { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | memcpy(target, staging, kTargetWidth * kTargetHeight * sizeof(T)); |
| 160 | return true; |
| 161 | } |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 162 | } // anonymous namespace |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 163 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 164 | PackPixelsParams::PackPixelsParams() |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 165 | : destFormat(nullptr), outputPitch(0), packBuffer(nullptr), offset(0) |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 166 | { |
| 167 | } |
| 168 | |
| 169 | PackPixelsParams::PackPixelsParams(const gl::Rectangle &areaIn, |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 170 | const angle::Format &destFormat, |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 171 | GLuint outputPitchIn, |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 172 | bool reverseRowOrderIn, |
Corentin Wallez | cda6af1 | 2017-10-30 19:20:37 -0400 | [diff] [blame] | 173 | gl::Buffer *packBufferIn, |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 174 | ptrdiff_t offsetIn) |
| 175 | : area(areaIn), |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 176 | destFormat(&destFormat), |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 177 | outputPitch(outputPitchIn), |
Corentin Wallez | cda6af1 | 2017-10-30 19:20:37 -0400 | [diff] [blame] | 178 | packBuffer(packBufferIn), |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 179 | reverseRowOrder(reverseRowOrderIn), |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 180 | offset(offsetIn) |
| 181 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 182 | } |
| 183 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 184 | void PackPixels(const PackPixelsParams ¶ms, |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 185 | const angle::Format &sourceFormat, |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 186 | int inputPitchIn, |
| 187 | const uint8_t *sourceIn, |
| 188 | uint8_t *destWithoutOffset) |
| 189 | { |
| 190 | uint8_t *destWithOffset = destWithoutOffset + params.offset; |
| 191 | |
| 192 | const uint8_t *source = sourceIn; |
| 193 | int inputPitch = inputPitchIn; |
| 194 | |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 195 | if (params.reverseRowOrder) |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 196 | { |
| 197 | source += inputPitch * (params.area.height - 1); |
| 198 | inputPitch = -inputPitch; |
| 199 | } |
| 200 | |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 201 | if (sourceFormat == *params.destFormat) |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 202 | { |
| 203 | // Direct copy possible |
| 204 | for (int y = 0; y < params.area.height; ++y) |
| 205 | { |
| 206 | memcpy(destWithOffset + y * params.outputPitch, source + y * inputPitch, |
Luc Ferron | 339b95e | 2018-06-12 10:21:52 -0400 | [diff] [blame] | 207 | params.area.width * sourceFormat.pixelBytes); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 208 | } |
| 209 | return; |
| 210 | } |
| 211 | |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 212 | PixelCopyFunction fastCopyFunc = sourceFormat.fastCopyFunctions.get(params.destFormat->id); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 213 | |
| 214 | if (fastCopyFunc) |
| 215 | { |
| 216 | // Fast copy is possible through some special function |
| 217 | for (int y = 0; y < params.area.height; ++y) |
| 218 | { |
| 219 | for (int x = 0; x < params.area.width; ++x) |
| 220 | { |
| 221 | uint8_t *dest = |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 222 | destWithOffset + y * params.outputPitch + x * params.destFormat->pixelBytes; |
Luc Ferron | 339b95e | 2018-06-12 10:21:52 -0400 | [diff] [blame] | 223 | const uint8_t *src = source + y * inputPitch + x * sourceFormat.pixelBytes; |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 224 | |
| 225 | fastCopyFunc(src, dest); |
| 226 | } |
| 227 | } |
| 228 | return; |
| 229 | } |
| 230 | |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 231 | PixelWriteFunction pixelWriteFunction = params.destFormat->pixelWriteFunction; |
| 232 | ASSERT(pixelWriteFunction != nullptr); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 233 | |
| 234 | // Maximum size of any Color<T> type used. |
| 235 | uint8_t temp[16]; |
| 236 | static_assert(sizeof(temp) >= sizeof(gl::ColorF) && sizeof(temp) >= sizeof(gl::ColorUI) && |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 237 | sizeof(temp) >= sizeof(gl::ColorI) && |
| 238 | sizeof(temp) >= sizeof(angle::DepthStencil), |
| 239 | "Unexpected size of pixel struct."); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 240 | |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 241 | PixelReadFunction pixelReadFunction = sourceFormat.pixelReadFunction; |
| 242 | ASSERT(pixelReadFunction != nullptr); |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 243 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 244 | for (int y = 0; y < params.area.height; ++y) |
| 245 | { |
| 246 | for (int x = 0; x < params.area.width; ++x) |
| 247 | { |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 248 | uint8_t *dest = |
| 249 | destWithOffset + y * params.outputPitch + x * params.destFormat->pixelBytes; |
Luc Ferron | 339b95e | 2018-06-12 10:21:52 -0400 | [diff] [blame] | 250 | const uint8_t *src = source + y * inputPitch + x * sourceFormat.pixelBytes; |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 251 | |
| 252 | // readFunc and writeFunc will be using the same type of color, CopyTexImage |
| 253 | // will not allow the copy otherwise. |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 254 | pixelReadFunction(src, temp); |
| 255 | pixelWriteFunction(temp, dest); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 260 | bool FastCopyFunctionMap::has(angle::FormatID formatID) const |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 261 | { |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 262 | return (get(formatID) != nullptr); |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 263 | } |
| 264 | |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 265 | PixelCopyFunction FastCopyFunctionMap::get(angle::FormatID formatID) const |
Jamie Madill | 4f57e5f | 2016-10-27 17:36:53 -0400 | [diff] [blame] | 266 | { |
| 267 | for (size_t index = 0; index < mSize; ++index) |
| 268 | { |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 269 | if (mData[index].formatID == formatID) |
Jamie Madill | 4f57e5f | 2016-10-27 17:36:53 -0400 | [diff] [blame] | 270 | { |
| 271 | return mData[index].func; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | return nullptr; |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 276 | } |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 277 | |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 278 | bool ShouldUseDebugLayers(const egl::AttributeMap &attribs) |
| 279 | { |
| 280 | EGLAttrib debugSetting = |
| 281 | attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE); |
| 282 | |
| 283 | // Prefer to enable debug layers if compiling in Debug, and disabled in Release. |
Geoff Lang | 58ba6bf | 2017-11-28 16:40:58 -0500 | [diff] [blame] | 284 | #if defined(ANGLE_ENABLE_ASSERTS) |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 285 | return (debugSetting != EGL_FALSE); |
| 286 | #else |
| 287 | return (debugSetting == EGL_TRUE); |
Geoff Lang | 58ba6bf | 2017-11-28 16:40:58 -0500 | [diff] [blame] | 288 | #endif // defined(ANGLE_ENABLE_ASSERTS) |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 289 | } |
| 290 | |
Geoff Lang | 24ddc7a | 2018-06-11 14:56:34 -0400 | [diff] [blame] | 291 | bool ShouldUseVirtualizedContexts(const egl::AttributeMap &attribs, bool defaultValue) |
| 292 | { |
| 293 | EGLAttrib virtualizedContextRequest = |
| 294 | attribs.get(EGL_PLATFORM_ANGLE_CONTEXT_VIRTUALIZATION_ANGLE, EGL_DONT_CARE); |
| 295 | if (defaultValue) |
| 296 | { |
| 297 | return (virtualizedContextRequest != EGL_FALSE); |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | return (virtualizedContextRequest == EGL_TRUE); |
| 302 | } |
| 303 | } |
| 304 | |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 305 | void CopyImageCHROMIUM(const uint8_t *sourceData, |
| 306 | size_t sourceRowPitch, |
| 307 | size_t sourcePixelBytes, |
Brandon Jones | 4e6f2ae | 2018-09-19 11:09:51 -0700 | [diff] [blame] | 308 | size_t sourceDepthPitch, |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 309 | PixelReadFunction pixelReadFunction, |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 310 | uint8_t *destData, |
| 311 | size_t destRowPitch, |
| 312 | size_t destPixelBytes, |
Brandon Jones | 4e6f2ae | 2018-09-19 11:09:51 -0700 | [diff] [blame] | 313 | size_t destDepthPitch, |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 314 | PixelWriteFunction pixelWriteFunction, |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 315 | GLenum destUnsizedFormat, |
| 316 | GLenum destComponentType, |
| 317 | size_t width, |
| 318 | size_t height, |
Brandon Jones | 4e6f2ae | 2018-09-19 11:09:51 -0700 | [diff] [blame] | 319 | size_t depth, |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 320 | bool unpackFlipY, |
| 321 | bool unpackPremultiplyAlpha, |
| 322 | bool unpackUnmultiplyAlpha) |
| 323 | { |
| 324 | using ConversionFunction = void (*)(gl::ColorF *); |
| 325 | ConversionFunction conversionFunction = CopyColor; |
| 326 | if (unpackPremultiplyAlpha != unpackUnmultiplyAlpha) |
| 327 | { |
| 328 | if (unpackPremultiplyAlpha) |
| 329 | { |
| 330 | conversionFunction = PremultiplyAlpha; |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | conversionFunction = UnmultiplyAlpha; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | auto clipChannelsFunction = ClipChannelsNoOp; |
| 339 | switch (destUnsizedFormat) |
| 340 | { |
| 341 | case GL_RED: |
| 342 | clipChannelsFunction = ClipChannelsR; |
| 343 | break; |
| 344 | case GL_RG: |
| 345 | clipChannelsFunction = ClipChannelsRG; |
| 346 | break; |
| 347 | case GL_RGB: |
| 348 | clipChannelsFunction = ClipChannelsRGB; |
| 349 | break; |
| 350 | case GL_LUMINANCE: |
| 351 | clipChannelsFunction = ClipChannelsLuminance; |
| 352 | break; |
| 353 | case GL_ALPHA: |
| 354 | clipChannelsFunction = ClipChannelsAlpha; |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | auto writeFunction = (destComponentType == GL_UNSIGNED_INT) ? WriteUintColor : WriteFloatColor; |
| 359 | |
Brandon Jones | 4e6f2ae | 2018-09-19 11:09:51 -0700 | [diff] [blame] | 360 | for (size_t z = 0; z < depth; z++) |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 361 | { |
Brandon Jones | 4e6f2ae | 2018-09-19 11:09:51 -0700 | [diff] [blame] | 362 | for (size_t y = 0; y < height; y++) |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 363 | { |
Brandon Jones | 4e6f2ae | 2018-09-19 11:09:51 -0700 | [diff] [blame] | 364 | for (size_t x = 0; x < width; x++) |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 365 | { |
Brandon Jones | 4e6f2ae | 2018-09-19 11:09:51 -0700 | [diff] [blame] | 366 | const uint8_t *sourcePixelData = |
| 367 | sourceData + y * sourceRowPitch + x * sourcePixelBytes + z * sourceDepthPitch; |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 368 | |
Brandon Jones | 4e6f2ae | 2018-09-19 11:09:51 -0700 | [diff] [blame] | 369 | gl::ColorF sourceColor; |
| 370 | pixelReadFunction(sourcePixelData, reinterpret_cast<uint8_t *>(&sourceColor)); |
| 371 | |
| 372 | conversionFunction(&sourceColor); |
| 373 | clipChannelsFunction(&sourceColor); |
| 374 | |
| 375 | size_t destY = 0; |
| 376 | if (unpackFlipY) |
| 377 | { |
| 378 | destY += (height - 1); |
| 379 | destY -= y; |
| 380 | } |
| 381 | else |
| 382 | { |
| 383 | destY += y; |
| 384 | } |
| 385 | |
| 386 | uint8_t *destPixelData = |
| 387 | destData + destY * destRowPitch + x * destPixelBytes + z * destDepthPitch; |
| 388 | writeFunction(sourceColor, pixelWriteFunction, destPixelData); |
| 389 | } |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 394 | // IncompleteTextureSet implementation. |
| 395 | IncompleteTextureSet::IncompleteTextureSet() |
| 396 | { |
| 397 | } |
| 398 | |
| 399 | IncompleteTextureSet::~IncompleteTextureSet() |
| 400 | { |
| 401 | } |
| 402 | |
| 403 | void IncompleteTextureSet::onDestroy(const gl::Context *context) |
| 404 | { |
| 405 | // Clear incomplete textures. |
| 406 | for (auto &incompleteTexture : mIncompleteTextures) |
| 407 | { |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 408 | if (incompleteTexture.get() != nullptr) |
| 409 | { |
Jamie Madill | 1c7f08c | 2018-10-10 16:13:02 -0400 | [diff] [blame] | 410 | incompleteTexture->onDestroy(context); |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 411 | incompleteTexture.set(context, nullptr); |
| 412 | } |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 413 | } |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | gl::Error IncompleteTextureSet::getIncompleteTexture( |
| 417 | const gl::Context *context, |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 418 | gl::TextureType type, |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 419 | MultisampleTextureInitializer *multisampleInitializer, |
| 420 | gl::Texture **textureOut) |
| 421 | { |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 422 | *textureOut = mIncompleteTextures[type].get(); |
| 423 | if (*textureOut != nullptr) |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 424 | { |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 425 | return gl::NoError(); |
| 426 | } |
| 427 | |
| 428 | ContextImpl *implFactory = context->getImplementation(); |
| 429 | |
| 430 | const GLubyte color[] = {0, 0, 0, 255}; |
| 431 | const gl::Extents colorSize(1, 1, 1); |
Corentin Wallez | cda6af1 | 2017-10-30 19:20:37 -0400 | [diff] [blame] | 432 | gl::PixelUnpackState unpack; |
| 433 | unpack.alignment = 1; |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 434 | const gl::Box area(0, 0, 0, 1, 1, 1); |
| 435 | |
| 436 | // If a texture is external use a 2D texture for the incomplete texture |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 437 | gl::TextureType createType = (type == gl::TextureType::External) ? gl::TextureType::_2D : type; |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 438 | |
| 439 | gl::Texture *tex = new gl::Texture(implFactory, std::numeric_limits<GLuint>::max(), createType); |
| 440 | angle::UniqueObjectPointer<gl::Texture, gl::Context> t(tex, context); |
| 441 | |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 442 | if (createType == gl::TextureType::_2DMultisample) |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 443 | { |
| 444 | ANGLE_TRY(t->setStorageMultisample(context, createType, 1, GL_RGBA8, colorSize, true)); |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | ANGLE_TRY(t->setStorage(context, createType, 1, GL_RGBA8, colorSize)); |
| 449 | } |
| 450 | |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 451 | if (type == gl::TextureType::CubeMap) |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 452 | { |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 453 | for (gl::TextureTarget face : gl::AllCubeFaceTextureTargets()) |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 454 | { |
Jamie Madill | 0d0fb43 | 2018-09-07 17:43:32 -0400 | [diff] [blame] | 455 | ANGLE_TRY(t->setSubImage(context, unpack, nullptr, face, 0, area, GL_RGBA, |
| 456 | GL_UNSIGNED_BYTE, color)); |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 457 | } |
| 458 | } |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 459 | else if (type == gl::TextureType::_2DMultisample) |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 460 | { |
| 461 | // Call a specialized clear function to init a multisample texture. |
| 462 | ANGLE_TRY(multisampleInitializer->initializeMultisampleTextureToBlack(context, t.get())); |
| 463 | } |
| 464 | else |
| 465 | { |
Jamie Madill | 0d0fb43 | 2018-09-07 17:43:32 -0400 | [diff] [blame] | 466 | ANGLE_TRY(t->setSubImage(context, unpack, nullptr, |
| 467 | gl::NonCubeTextureTypeToTarget(createType), 0, area, GL_RGBA, |
| 468 | GL_UNSIGNED_BYTE, color)); |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 469 | } |
| 470 | |
Luc Ferron | 4bba74f | 2018-04-19 14:40:45 -0400 | [diff] [blame] | 471 | ANGLE_TRY(t->syncState(context)); |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 472 | |
| 473 | mIncompleteTextures[type].set(context, t.release()); |
| 474 | *textureOut = mIncompleteTextures[type].get(); |
| 475 | return gl::NoError(); |
| 476 | } |
| 477 | |
Luc Ferron | 46bcea5 | 2018-05-31 09:48:36 -0400 | [diff] [blame] | 478 | #define ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(cols, rows) \ |
| 479 | template bool SetFloatUniformMatrix<cols, rows>(unsigned int, unsigned int, GLsizei, \ |
| 480 | GLboolean, const GLfloat *, uint8_t *) |
| 481 | |
| 482 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 2); |
| 483 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 3); |
| 484 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 4); |
| 485 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 3); |
| 486 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 2); |
| 487 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 4); |
| 488 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 2); |
| 489 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 4); |
| 490 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 3); |
| 491 | |
| 492 | #undef ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC |
| 493 | |
| 494 | template <int cols, int rows> |
| 495 | bool SetFloatUniformMatrix(unsigned int arrayElementOffset, |
| 496 | unsigned int elementCount, |
| 497 | GLsizei countIn, |
| 498 | GLboolean transpose, |
| 499 | const GLfloat *value, |
| 500 | uint8_t *targetData) |
| 501 | { |
| 502 | unsigned int count = |
| 503 | std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn)); |
| 504 | |
| 505 | const unsigned int targetMatrixStride = (4 * rows); |
| 506 | GLfloat *target = reinterpret_cast<GLfloat *>( |
| 507 | targetData + arrayElementOffset * sizeof(GLfloat) * targetMatrixStride); |
| 508 | |
| 509 | bool dirty = false; |
| 510 | |
| 511 | for (unsigned int i = 0; i < count; i++) |
| 512 | { |
| 513 | if (transpose == GL_FALSE) |
| 514 | { |
| 515 | dirty = ExpandMatrix<GLfloat, cols, rows>(target, value) || dirty; |
| 516 | } |
| 517 | else |
| 518 | { |
| 519 | dirty = TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || dirty; |
| 520 | } |
| 521 | target += targetMatrixStride; |
| 522 | value += cols * rows; |
| 523 | } |
| 524 | |
| 525 | return dirty; |
| 526 | } |
| 527 | |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 528 | template void GetMatrixUniform<GLint>(GLenum, GLint *, const GLint *, bool); |
| 529 | template void GetMatrixUniform<GLuint>(GLenum, GLuint *, const GLuint *, bool); |
| 530 | |
| 531 | void GetMatrixUniform(GLenum type, GLfloat *dataOut, const GLfloat *source, bool transpose) |
| 532 | { |
| 533 | int columns = gl::VariableColumnCount(type); |
| 534 | int rows = gl::VariableRowCount(type); |
| 535 | for (GLint col = 0; col < columns; ++col) |
| 536 | { |
| 537 | for (GLint row = 0; row < rows; ++row) |
| 538 | { |
| 539 | GLfloat *outptr = dataOut + ((col * rows) + row); |
| 540 | const GLfloat *inptr = |
| 541 | transpose ? source + ((row * 4) + col) : source + ((col * 4) + row); |
| 542 | *outptr = *inptr; |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | template <typename NonFloatT> |
| 548 | void GetMatrixUniform(GLenum type, NonFloatT *dataOut, const NonFloatT *source, bool transpose) |
| 549 | { |
| 550 | UNREACHABLE(); |
| 551 | } |
| 552 | |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 553 | const angle::Format &GetFormatFromFormatType(GLenum format, GLenum type) |
| 554 | { |
| 555 | GLenum sizedInternalFormat = gl::GetInternalFormatInfo(format, type).sizedInternalFormat; |
| 556 | angle::FormatID angleFormatID = angle::Format::InternalFormatToID(sizedInternalFormat); |
| 557 | return angle::Format::Get(angleFormatID); |
| 558 | } |
Jamie Madill | c1fd737 | 2018-10-26 22:48:39 -0400 | [diff] [blame] | 559 | |
| 560 | angle::Result ComputeStartVertex(ContextImpl *contextImpl, |
| 561 | const gl::IndexRange &indexRange, |
| 562 | GLint baseVertex, |
| 563 | GLint *firstVertexOut) |
| 564 | { |
| 565 | // The entire index range should be within the limits of a 32-bit uint because the largest |
| 566 | // GL index type is GL_UNSIGNED_INT. |
| 567 | ASSERT(indexRange.start <= std::numeric_limits<uint32_t>::max() && |
| 568 | indexRange.end <= std::numeric_limits<uint32_t>::max()); |
| 569 | |
| 570 | // The base vertex is only used in DrawElementsIndirect. Given the assertion above and the |
| 571 | // type of mBaseVertex (GLint), adding them both as 64-bit ints is safe. |
| 572 | int64_t startVertexInt64 = |
| 573 | static_cast<int64_t>(baseVertex) + static_cast<int64_t>(indexRange.start); |
| 574 | |
| 575 | // OpenGL ES 3.2 spec section 10.5: "Behavior of DrawElementsOneInstance is undefined if the |
| 576 | // vertex ID is negative for any element" |
| 577 | ANGLE_CHECK_GL_MATH(contextImpl, startVertexInt64 >= 0); |
| 578 | |
| 579 | // OpenGL ES 3.2 spec section 10.5: "If the vertex ID is larger than the maximum value |
| 580 | // representable by type, it should behave as if the calculation were upconverted to 32-bit |
| 581 | // unsigned integers(with wrapping on overflow conditions)." ANGLE does not fully handle |
| 582 | // these rules, an overflow error is returned if the start vertex cannot be stored in a |
| 583 | // 32-bit signed integer. |
| 584 | ANGLE_CHECK_GL_MATH(contextImpl, startVertexInt64 <= std::numeric_limits<GLint>::max()) |
| 585 | |
| 586 | *firstVertexOut = static_cast<GLint>(startVertexInt64); |
| 587 | return angle::Result::Continue(); |
| 588 | } |
| 589 | |
| 590 | angle::Result GetVertexRangeInfo(const gl::Context *context, |
| 591 | GLint firstVertex, |
| 592 | GLsizei vertexOrIndexCount, |
| 593 | GLenum indexTypeOrNone, |
| 594 | const void *indices, |
| 595 | GLint baseVertex, |
| 596 | GLint *startVertexOut, |
| 597 | size_t *vertexCountOut) |
| 598 | { |
| 599 | if (indexTypeOrNone != GL_NONE) |
| 600 | { |
| 601 | gl::IndexRange indexRange; |
| 602 | ANGLE_TRY(context->getGLState().getVertexArray()->getIndexRange( |
| 603 | context, indexTypeOrNone, vertexOrIndexCount, indices, &indexRange)); |
| 604 | ANGLE_TRY(ComputeStartVertex(context->getImplementation(), indexRange, baseVertex, |
| 605 | startVertexOut)); |
| 606 | *vertexCountOut = indexRange.vertexCount(); |
| 607 | } |
| 608 | else |
| 609 | { |
| 610 | *startVertexOut = firstVertex; |
| 611 | *vertexCountOut = vertexOrIndexCount; |
| 612 | } |
| 613 | return angle::Result::Continue(); |
| 614 | } |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 615 | } // namespace rx |