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 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 82 | void ClipChannelsNoOp(gl::ColorF *color) {} |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 83 | |
| 84 | void WriteUintColor(const gl::ColorF &color, |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 85 | PixelWriteFunction colorWriteFunction, |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 86 | 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 | |
| 94 | void WriteFloatColor(const gl::ColorF &color, |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 95 | PixelWriteFunction colorWriteFunction, |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 96 | uint8_t *destPixelData) |
| 97 | { |
| 98 | colorWriteFunction(reinterpret_cast<const uint8_t *>(&color), destPixelData); |
| 99 | } |
| 100 | |
Luc Ferron | 46bcea5 | 2018-05-31 09:48:36 -0400 | [diff] [blame] | 101 | template <typename T, int cols, int rows> |
| 102 | bool 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 | |
| 131 | template <typename T, int cols, int rows> |
| 132 | bool 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 Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 160 | } // anonymous namespace |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 161 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 162 | PackPixelsParams::PackPixelsParams() |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 163 | : destFormat(nullptr), outputPitch(0), packBuffer(nullptr), offset(0) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 164 | {} |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 165 | |
| 166 | PackPixelsParams::PackPixelsParams(const gl::Rectangle &areaIn, |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 167 | const angle::Format &destFormat, |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 168 | GLuint outputPitchIn, |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 169 | bool reverseRowOrderIn, |
Corentin Wallez | cda6af1 | 2017-10-30 19:20:37 -0400 | [diff] [blame] | 170 | gl::Buffer *packBufferIn, |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 171 | ptrdiff_t offsetIn) |
| 172 | : area(areaIn), |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 173 | destFormat(&destFormat), |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 174 | outputPitch(outputPitchIn), |
Corentin Wallez | cda6af1 | 2017-10-30 19:20:37 -0400 | [diff] [blame] | 175 | packBuffer(packBufferIn), |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 176 | reverseRowOrder(reverseRowOrderIn), |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 177 | offset(offsetIn) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 178 | {} |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 179 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 180 | void PackPixels(const PackPixelsParams ¶ms, |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 181 | const angle::Format &sourceFormat, |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 182 | 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 Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 191 | if (params.reverseRowOrder) |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 192 | { |
| 193 | source += inputPitch * (params.area.height - 1); |
| 194 | inputPitch = -inputPitch; |
| 195 | } |
| 196 | |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 197 | if (sourceFormat == *params.destFormat) |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 198 | { |
| 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 Ferron | 339b95e | 2018-06-12 10:21:52 -0400 | [diff] [blame] | 203 | params.area.width * sourceFormat.pixelBytes); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 204 | } |
| 205 | return; |
| 206 | } |
| 207 | |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 208 | PixelCopyFunction fastCopyFunc = sourceFormat.fastCopyFunctions.get(params.destFormat->id); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 209 | |
| 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 Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 218 | destWithOffset + y * params.outputPitch + x * params.destFormat->pixelBytes; |
Luc Ferron | 339b95e | 2018-06-12 10:21:52 -0400 | [diff] [blame] | 219 | const uint8_t *src = source + y * inputPitch + x * sourceFormat.pixelBytes; |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 220 | |
| 221 | fastCopyFunc(src, dest); |
| 222 | } |
| 223 | } |
| 224 | return; |
| 225 | } |
| 226 | |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 227 | PixelWriteFunction pixelWriteFunction = params.destFormat->pixelWriteFunction; |
| 228 | ASSERT(pixelWriteFunction != nullptr); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 229 | |
| 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 Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 233 | sizeof(temp) >= sizeof(gl::ColorI) && |
| 234 | sizeof(temp) >= sizeof(angle::DepthStencil), |
| 235 | "Unexpected size of pixel struct."); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 236 | |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 237 | PixelReadFunction pixelReadFunction = sourceFormat.pixelReadFunction; |
| 238 | ASSERT(pixelReadFunction != nullptr); |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 239 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 240 | for (int y = 0; y < params.area.height; ++y) |
| 241 | { |
| 242 | for (int x = 0; x < params.area.width; ++x) |
| 243 | { |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 244 | uint8_t *dest = |
| 245 | destWithOffset + y * params.outputPitch + x * params.destFormat->pixelBytes; |
Luc Ferron | 339b95e | 2018-06-12 10:21:52 -0400 | [diff] [blame] | 246 | const uint8_t *src = source + y * inputPitch + x * sourceFormat.pixelBytes; |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 247 | |
| 248 | // readFunc and writeFunc will be using the same type of color, CopyTexImage |
| 249 | // will not allow the copy otherwise. |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 250 | pixelReadFunction(src, temp); |
| 251 | pixelWriteFunction(temp, dest); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 256 | bool FastCopyFunctionMap::has(angle::FormatID formatID) const |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 257 | { |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 258 | return (get(formatID) != nullptr); |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 259 | } |
| 260 | |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 261 | PixelCopyFunction FastCopyFunctionMap::get(angle::FormatID formatID) const |
Jamie Madill | 4f57e5f | 2016-10-27 17:36:53 -0400 | [diff] [blame] | 262 | { |
| 263 | for (size_t index = 0; index < mSize; ++index) |
| 264 | { |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 265 | if (mData[index].formatID == formatID) |
Jamie Madill | 4f57e5f | 2016-10-27 17:36:53 -0400 | [diff] [blame] | 266 | { |
| 267 | return mData[index].func; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | return nullptr; |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 272 | } |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 273 | |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 274 | bool 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 Lang | 58ba6bf | 2017-11-28 16:40:58 -0500 | [diff] [blame] | 280 | #if defined(ANGLE_ENABLE_ASSERTS) |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 281 | return (debugSetting != EGL_FALSE); |
| 282 | #else |
| 283 | return (debugSetting == EGL_TRUE); |
Geoff Lang | 58ba6bf | 2017-11-28 16:40:58 -0500 | [diff] [blame] | 284 | #endif // defined(ANGLE_ENABLE_ASSERTS) |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 285 | } |
| 286 | |
Geoff Lang | 24ddc7a | 2018-06-11 14:56:34 -0400 | [diff] [blame] | 287 | bool 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 Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 301 | void CopyImageCHROMIUM(const uint8_t *sourceData, |
| 302 | size_t sourceRowPitch, |
| 303 | size_t sourcePixelBytes, |
Brandon Jones | 4e6f2ae | 2018-09-19 11:09:51 -0700 | [diff] [blame] | 304 | size_t sourceDepthPitch, |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 305 | PixelReadFunction pixelReadFunction, |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 306 | uint8_t *destData, |
| 307 | size_t destRowPitch, |
| 308 | size_t destPixelBytes, |
Brandon Jones | 4e6f2ae | 2018-09-19 11:09:51 -0700 | [diff] [blame] | 309 | size_t destDepthPitch, |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 310 | PixelWriteFunction pixelWriteFunction, |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 311 | GLenum destUnsizedFormat, |
| 312 | GLenum destComponentType, |
| 313 | size_t width, |
| 314 | size_t height, |
Brandon Jones | 4e6f2ae | 2018-09-19 11:09:51 -0700 | [diff] [blame] | 315 | size_t depth, |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 316 | 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 Jones | 4e6f2ae | 2018-09-19 11:09:51 -0700 | [diff] [blame] | 356 | for (size_t z = 0; z < depth; z++) |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 357 | { |
Brandon Jones | 4e6f2ae | 2018-09-19 11:09:51 -0700 | [diff] [blame] | 358 | for (size_t y = 0; y < height; y++) |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 359 | { |
Brandon Jones | 4e6f2ae | 2018-09-19 11:09:51 -0700 | [diff] [blame] | 360 | for (size_t x = 0; x < width; x++) |
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 | const uint8_t *sourcePixelData = |
| 363 | sourceData + y * sourceRowPitch + x * sourcePixelBytes + z * sourceDepthPitch; |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 364 | |
Brandon Jones | 4e6f2ae | 2018-09-19 11:09:51 -0700 | [diff] [blame] | 365 | 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 Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 390 | // IncompleteTextureSet implementation. |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 391 | IncompleteTextureSet::IncompleteTextureSet() {} |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 392 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 393 | IncompleteTextureSet::~IncompleteTextureSet() {} |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 394 | |
| 395 | void IncompleteTextureSet::onDestroy(const gl::Context *context) |
| 396 | { |
| 397 | // Clear incomplete textures. |
| 398 | for (auto &incompleteTexture : mIncompleteTextures) |
| 399 | { |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 400 | if (incompleteTexture.get() != nullptr) |
| 401 | { |
Jamie Madill | 1c7f08c | 2018-10-10 16:13:02 -0400 | [diff] [blame] | 402 | incompleteTexture->onDestroy(context); |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 403 | incompleteTexture.set(context, nullptr); |
| 404 | } |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 405 | } |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 406 | } |
| 407 | |
Jamie Madill | 666818e | 2018-11-14 09:54:33 -0500 | [diff] [blame] | 408 | angle::Result IncompleteTextureSet::getIncompleteTexture( |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 409 | const gl::Context *context, |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 410 | gl::TextureType type, |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 411 | MultisampleTextureInitializer *multisampleInitializer, |
| 412 | gl::Texture **textureOut) |
| 413 | { |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 414 | *textureOut = mIncompleteTextures[type].get(); |
| 415 | if (*textureOut != nullptr) |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 416 | { |
Jamie Madill | 666818e | 2018-11-14 09:54:33 -0500 | [diff] [blame] | 417 | return angle::Result::Continue(); |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | ContextImpl *implFactory = context->getImplementation(); |
| 421 | |
| 422 | const GLubyte color[] = {0, 0, 0, 255}; |
| 423 | const gl::Extents colorSize(1, 1, 1); |
Corentin Wallez | cda6af1 | 2017-10-30 19:20:37 -0400 | [diff] [blame] | 424 | gl::PixelUnpackState unpack; |
| 425 | unpack.alignment = 1; |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 426 | 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 Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 429 | gl::TextureType createType = (type == gl::TextureType::External) ? gl::TextureType::_2D : type; |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 430 | |
| 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 Madill | 4f6592f | 2018-11-27 16:37:45 -0500 | [diff] [blame] | 434 | // 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 Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 437 | if (createType == gl::TextureType::_2DMultisample) |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 438 | { |
Jamie Madill | 4f6592f | 2018-11-27 16:37:45 -0500 | [diff] [blame] | 439 | ANGLE_TRY( |
| 440 | t->setStorageMultisample(mutableContext, createType, 1, GL_RGBA8, colorSize, true)); |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 441 | } |
| 442 | else |
| 443 | { |
Jamie Madill | 4f6592f | 2018-11-27 16:37:45 -0500 | [diff] [blame] | 444 | ANGLE_TRY(t->setStorage(mutableContext, createType, 1, GL_RGBA8, colorSize)); |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 445 | } |
| 446 | |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 447 | if (type == gl::TextureType::CubeMap) |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 448 | { |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 449 | for (gl::TextureTarget face : gl::AllCubeFaceTextureTargets()) |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 450 | { |
Jamie Madill | 4f6592f | 2018-11-27 16:37:45 -0500 | [diff] [blame] | 451 | ANGLE_TRY(t->setSubImage(mutableContext, unpack, nullptr, face, 0, area, GL_RGBA, |
Jamie Madill | 0d0fb43 | 2018-09-07 17:43:32 -0400 | [diff] [blame] | 452 | GL_UNSIGNED_BYTE, color)); |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 453 | } |
| 454 | } |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 455 | else if (type == gl::TextureType::_2DMultisample) |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 456 | { |
| 457 | // Call a specialized clear function to init a multisample texture. |
| 458 | ANGLE_TRY(multisampleInitializer->initializeMultisampleTextureToBlack(context, t.get())); |
| 459 | } |
| 460 | else |
| 461 | { |
Jamie Madill | 4f6592f | 2018-11-27 16:37:45 -0500 | [diff] [blame] | 462 | ANGLE_TRY(t->setSubImage(mutableContext, unpack, nullptr, |
Jamie Madill | 0d0fb43 | 2018-09-07 17:43:32 -0400 | [diff] [blame] | 463 | gl::NonCubeTextureTypeToTarget(createType), 0, area, GL_RGBA, |
| 464 | GL_UNSIGNED_BYTE, color)); |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 465 | } |
| 466 | |
Luc Ferron | 4bba74f | 2018-04-19 14:40:45 -0400 | [diff] [blame] | 467 | ANGLE_TRY(t->syncState(context)); |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 468 | |
| 469 | mIncompleteTextures[type].set(context, t.release()); |
| 470 | *textureOut = mIncompleteTextures[type].get(); |
Jamie Madill | 666818e | 2018-11-14 09:54:33 -0500 | [diff] [blame] | 471 | return angle::Result::Continue(); |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 472 | } |
| 473 | |
Luc Ferron | 46bcea5 | 2018-05-31 09:48:36 -0400 | [diff] [blame] | 474 | #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 | |
| 478 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 2); |
| 479 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 3); |
| 480 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 4); |
| 481 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 3); |
| 482 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 2); |
| 483 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 4); |
| 484 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 2); |
| 485 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 4); |
| 486 | ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 3); |
| 487 | |
| 488 | #undef ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC |
| 489 | |
| 490 | template <int cols, int rows> |
| 491 | bool 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 Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 524 | template void GetMatrixUniform<GLint>(GLenum, GLint *, const GLint *, bool); |
| 525 | template void GetMatrixUniform<GLuint>(GLenum, GLuint *, const GLuint *, bool); |
| 526 | |
| 527 | void 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 | |
| 543 | template <typename NonFloatT> |
| 544 | void GetMatrixUniform(GLenum type, NonFloatT *dataOut, const NonFloatT *source, bool transpose) |
| 545 | { |
| 546 | UNREACHABLE(); |
| 547 | } |
| 548 | |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 549 | const 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 Madill | c1fd737 | 2018-10-26 22:48:39 -0400 | [diff] [blame] | 555 | |
| 556 | angle::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 | |
| 586 | angle::Result GetVertexRangeInfo(const gl::Context *context, |
| 587 | GLint firstVertex, |
| 588 | GLsizei vertexOrIndexCount, |
Jamie Madill | 8dc27f9 | 2018-11-29 11:45:44 -0500 | [diff] [blame^] | 589 | gl::DrawElementsType indexTypeOrInvalid, |
Jamie Madill | c1fd737 | 2018-10-26 22:48:39 -0400 | [diff] [blame] | 590 | const void *indices, |
| 591 | GLint baseVertex, |
| 592 | GLint *startVertexOut, |
| 593 | size_t *vertexCountOut) |
| 594 | { |
Jamie Madill | 8dc27f9 | 2018-11-29 11:45:44 -0500 | [diff] [blame^] | 595 | if (indexTypeOrInvalid != gl::DrawElementsType::InvalidEnum) |
Jamie Madill | c1fd737 | 2018-10-26 22:48:39 -0400 | [diff] [blame] | 596 | { |
| 597 | gl::IndexRange indexRange; |
| 598 | ANGLE_TRY(context->getGLState().getVertexArray()->getIndexRange( |
Jamie Madill | 8dc27f9 | 2018-11-29 11:45:44 -0500 | [diff] [blame^] | 599 | context, indexTypeOrInvalid, vertexOrIndexCount, indices, &indexRange)); |
Jamie Madill | c1fd737 | 2018-10-26 22:48:39 -0400 | [diff] [blame] | 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 Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 611 | } // namespace rx |