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> |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 22 | |
| 23 | namespace rx |
| 24 | { |
| 25 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 26 | namespace |
| 27 | { |
| 28 | typedef std::pair<gl::FormatType, ColorWriteFunction> FormatWriteFunctionPair; |
| 29 | typedef std::map<gl::FormatType, ColorWriteFunction> FormatWriteFunctionMap; |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 30 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 31 | static inline void InsertFormatWriteFunctionMapping(FormatWriteFunctionMap *map, |
| 32 | GLenum format, |
| 33 | GLenum type, |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 34 | ColorWriteFunction writeFunc) |
| 35 | { |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 36 | map->insert(FormatWriteFunctionPair(gl::FormatType(format, type), writeFunc)); |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static FormatWriteFunctionMap BuildFormatWriteFunctionMap() |
| 40 | { |
Geoff Lang | 6e4cfce | 2016-06-13 15:06:31 -0400 | [diff] [blame] | 41 | using namespace angle; // For image writing functions |
| 42 | |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 43 | FormatWriteFunctionMap map; |
| 44 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 45 | // clang-format off |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 46 | // | Format | Type | Color write function | |
| 47 | InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_BYTE, WriteColor<R8G8B8A8, GLfloat> ); |
| 48 | InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_BYTE, WriteColor<R8G8B8A8S, GLfloat> ); |
| 49 | InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, WriteColor<R4G4B4A4, GLfloat> ); |
| 50 | InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, WriteColor<R5G5B5A1, GLfloat> ); |
| 51 | InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, WriteColor<R10G10B10A2, GLfloat> ); |
| 52 | InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_FLOAT, WriteColor<R32G32B32A32F, GLfloat>); |
| 53 | InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_HALF_FLOAT, WriteColor<R16G16B16A16F, GLfloat>); |
| 54 | InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_HALF_FLOAT_OES, WriteColor<R16G16B16A16F, GLfloat>); |
Vincent Lang | 25ab451 | 2016-05-13 18:13:59 +0200 | [diff] [blame] | 55 | InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_SHORT, |
| 56 | WriteColor<R16G16B16A16, GLfloat>); |
| 57 | InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_SHORT, WriteColor<R16G16B16A16S, GLfloat>); |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 58 | |
| 59 | InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8G8B8A8, GLuint> ); |
| 60 | InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_BYTE, WriteColor<R8G8B8A8S, GLint> ); |
| 61 | InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16G16B16A16, GLuint> ); |
| 62 | InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_SHORT, WriteColor<R16G16B16A16S, GLint> ); |
| 63 | InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_INT, WriteColor<R32G32B32A32, GLuint> ); |
| 64 | InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_INT, WriteColor<R32G32B32A32S, GLint> ); |
| 65 | InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV, WriteColor<R10G10B10A2, GLuint> ); |
| 66 | |
| 67 | InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_BYTE, WriteColor<R8G8B8, GLfloat> ); |
| 68 | InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_BYTE, WriteColor<R8G8B8S, GLfloat> ); |
| 69 | InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, WriteColor<R5G6B5, GLfloat> ); |
| 70 | InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, WriteColor<R11G11B10F, GLfloat> ); |
| 71 | InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV, WriteColor<R9G9B9E5, GLfloat> ); |
| 72 | InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_FLOAT, WriteColor<R32G32B32F, GLfloat> ); |
| 73 | InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_HALF_FLOAT, WriteColor<R16G16B16F, GLfloat> ); |
| 74 | InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_HALF_FLOAT_OES, WriteColor<R16G16B16F, GLfloat> ); |
Vincent Lang | 25ab451 | 2016-05-13 18:13:59 +0200 | [diff] [blame] | 75 | InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_SHORT, |
| 76 | WriteColor<R16G16B16, GLfloat>); |
| 77 | InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_SHORT, WriteColor<R16G16B16S, GLfloat>); |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 78 | |
| 79 | InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8G8B8, GLuint> ); |
| 80 | InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_BYTE, WriteColor<R8G8B8S, GLint> ); |
| 81 | InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16G16B16, GLuint> ); |
| 82 | InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_SHORT, WriteColor<R16G16B16S, GLint> ); |
| 83 | InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_UNSIGNED_INT, WriteColor<R32G32B32, GLuint> ); |
| 84 | InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_INT, WriteColor<R32G32B32S, GLint> ); |
| 85 | |
| 86 | InsertFormatWriteFunctionMapping(&map, GL_RG, GL_UNSIGNED_BYTE, WriteColor<R8G8, GLfloat> ); |
| 87 | InsertFormatWriteFunctionMapping(&map, GL_RG, GL_BYTE, WriteColor<R8G8S, GLfloat> ); |
| 88 | InsertFormatWriteFunctionMapping(&map, GL_RG, GL_FLOAT, WriteColor<R32G32F, GLfloat> ); |
| 89 | InsertFormatWriteFunctionMapping(&map, GL_RG, GL_HALF_FLOAT, WriteColor<R16G16F, GLfloat> ); |
| 90 | InsertFormatWriteFunctionMapping(&map, GL_RG, GL_HALF_FLOAT_OES, WriteColor<R16G16F, GLfloat> ); |
Vincent Lang | 25ab451 | 2016-05-13 18:13:59 +0200 | [diff] [blame] | 91 | InsertFormatWriteFunctionMapping(&map, GL_RG, GL_UNSIGNED_SHORT, WriteColor<R16G16, GLfloat>); |
| 92 | InsertFormatWriteFunctionMapping(&map, GL_RG, GL_SHORT, WriteColor<R16G16S, GLfloat>); |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 93 | |
| 94 | InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8G8, GLuint> ); |
| 95 | InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_BYTE, WriteColor<R8G8S, GLint> ); |
| 96 | InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16G16, GLuint> ); |
| 97 | InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_SHORT, WriteColor<R16G16S, GLint> ); |
| 98 | InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_UNSIGNED_INT, WriteColor<R32G32, GLuint> ); |
| 99 | InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_INT, WriteColor<R32G32S, GLint> ); |
| 100 | |
| 101 | InsertFormatWriteFunctionMapping(&map, GL_RED, GL_UNSIGNED_BYTE, WriteColor<R8, GLfloat> ); |
| 102 | InsertFormatWriteFunctionMapping(&map, GL_RED, GL_BYTE, WriteColor<R8S, GLfloat> ); |
| 103 | InsertFormatWriteFunctionMapping(&map, GL_RED, GL_FLOAT, WriteColor<R32F, GLfloat> ); |
| 104 | InsertFormatWriteFunctionMapping(&map, GL_RED, GL_HALF_FLOAT, WriteColor<R16F, GLfloat> ); |
| 105 | InsertFormatWriteFunctionMapping(&map, GL_RED, GL_HALF_FLOAT_OES, WriteColor<R16F, GLfloat> ); |
Vincent Lang | 25ab451 | 2016-05-13 18:13:59 +0200 | [diff] [blame] | 106 | InsertFormatWriteFunctionMapping(&map, GL_RED, GL_UNSIGNED_SHORT, WriteColor<R16, GLfloat>); |
| 107 | InsertFormatWriteFunctionMapping(&map, GL_RED, GL_SHORT, WriteColor<R16S, GLfloat>); |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 108 | |
| 109 | InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8, GLuint> ); |
| 110 | InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_BYTE, WriteColor<R8S, GLint> ); |
| 111 | InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16, GLuint> ); |
| 112 | InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_SHORT, WriteColor<R16S, GLint> ); |
| 113 | InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_UNSIGNED_INT, WriteColor<R32, GLuint> ); |
| 114 | InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_INT, WriteColor<R32S, GLint> ); |
| 115 | |
| 116 | InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, WriteColor<L8A8, GLfloat> ); |
| 117 | InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_UNSIGNED_BYTE, WriteColor<L8, GLfloat> ); |
| 118 | InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_UNSIGNED_BYTE, WriteColor<A8, GLfloat> ); |
| 119 | InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_FLOAT, WriteColor<L32A32F, GLfloat> ); |
| 120 | InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_FLOAT, WriteColor<L32F, GLfloat> ); |
| 121 | InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_FLOAT, WriteColor<A32F, GLfloat> ); |
| 122 | InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT, WriteColor<L16A16F, GLfloat> ); |
| 123 | InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES, WriteColor<L16A16F, GLfloat> ); |
| 124 | InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_HALF_FLOAT, WriteColor<L16F, GLfloat> ); |
| 125 | InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_HALF_FLOAT_OES, WriteColor<L16F, GLfloat> ); |
| 126 | InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_HALF_FLOAT, WriteColor<A16F, GLfloat> ); |
| 127 | InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_HALF_FLOAT_OES, WriteColor<A16F, GLfloat> ); |
| 128 | |
| 129 | InsertFormatWriteFunctionMapping(&map, GL_BGRA_EXT, GL_UNSIGNED_BYTE, WriteColor<B8G8R8A8, GLfloat> ); |
Austin Kinross | 5cf0f98 | 2015-08-12 09:35:10 -0700 | [diff] [blame] | 130 | InsertFormatWriteFunctionMapping(&map, GL_BGRA_EXT, GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT, WriteColor<A4R4G4B4, GLfloat> ); |
| 131 | InsertFormatWriteFunctionMapping(&map, GL_BGRA_EXT, GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT, WriteColor<A1R5G5B5, GLfloat> ); |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 132 | |
| 133 | InsertFormatWriteFunctionMapping(&map, GL_SRGB_EXT, GL_UNSIGNED_BYTE, WriteColor<R8G8B8, GLfloat> ); |
| 134 | InsertFormatWriteFunctionMapping(&map, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE, WriteColor<R8G8B8A8, GLfloat> ); |
| 135 | |
Yunchao He | f81ce4a | 2017-04-24 10:49:17 +0800 | [diff] [blame] | 136 | InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE, nullptr ); |
| 137 | InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE, nullptr ); |
| 138 | InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, GL_UNSIGNED_BYTE, nullptr ); |
| 139 | InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, GL_UNSIGNED_BYTE, nullptr ); |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 140 | |
Yunchao He | f81ce4a | 2017-04-24 10:49:17 +0800 | [diff] [blame] | 141 | InsertFormatWriteFunctionMapping(&map, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, nullptr ); |
| 142 | InsertFormatWriteFunctionMapping(&map, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr ); |
| 143 | InsertFormatWriteFunctionMapping(&map, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr ); |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 144 | |
Yunchao He | f81ce4a | 2017-04-24 10:49:17 +0800 | [diff] [blame] | 145 | InsertFormatWriteFunctionMapping(&map, GL_STENCIL, GL_UNSIGNED_BYTE, nullptr ); |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 146 | |
Yunchao He | f81ce4a | 2017-04-24 10:49:17 +0800 | [diff] [blame] | 147 | InsertFormatWriteFunctionMapping(&map, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr ); |
| 148 | InsertFormatWriteFunctionMapping(&map, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, nullptr ); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 149 | // clang-format on |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 150 | |
| 151 | return map; |
| 152 | } |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 153 | |
| 154 | void CopyColor(gl::ColorF *color) |
| 155 | { |
| 156 | // No-op |
| 157 | } |
| 158 | |
| 159 | void PremultiplyAlpha(gl::ColorF *color) |
| 160 | { |
| 161 | color->red *= color->alpha; |
| 162 | color->green *= color->alpha; |
| 163 | color->blue *= color->alpha; |
| 164 | } |
| 165 | |
| 166 | void UnmultiplyAlpha(gl::ColorF *color) |
| 167 | { |
| 168 | if (color->alpha != 0.0f) |
| 169 | { |
| 170 | float invAlpha = 1.0f / color->alpha; |
| 171 | color->red *= invAlpha; |
| 172 | color->green *= invAlpha; |
| 173 | color->blue *= invAlpha; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void ClipChannelsR(gl::ColorF *color) |
| 178 | { |
| 179 | color->green = 0.0f; |
| 180 | color->blue = 0.0f; |
| 181 | color->alpha = 1.0f; |
| 182 | } |
| 183 | |
| 184 | void ClipChannelsRG(gl::ColorF *color) |
| 185 | { |
| 186 | color->blue = 0.0f; |
| 187 | color->alpha = 1.0f; |
| 188 | } |
| 189 | |
| 190 | void ClipChannelsRGB(gl::ColorF *color) |
| 191 | { |
| 192 | color->alpha = 1.0f; |
| 193 | } |
| 194 | |
| 195 | void ClipChannelsLuminance(gl::ColorF *color) |
| 196 | { |
| 197 | color->alpha = 1.0f; |
| 198 | } |
| 199 | |
| 200 | void ClipChannelsAlpha(gl::ColorF *color) |
| 201 | { |
| 202 | color->red = 0.0f; |
| 203 | color->green = 0.0f; |
| 204 | color->blue = 0.0f; |
| 205 | } |
| 206 | |
| 207 | void ClipChannelsNoOp(gl::ColorF *color) |
| 208 | { |
| 209 | } |
| 210 | |
| 211 | void WriteUintColor(const gl::ColorF &color, |
| 212 | ColorWriteFunction colorWriteFunction, |
| 213 | uint8_t *destPixelData) |
| 214 | { |
| 215 | gl::ColorUI destColor( |
| 216 | static_cast<unsigned int>(color.red * 255), static_cast<unsigned int>(color.green * 255), |
| 217 | static_cast<unsigned int>(color.blue * 255), static_cast<unsigned int>(color.alpha * 255)); |
| 218 | colorWriteFunction(reinterpret_cast<const uint8_t *>(&destColor), destPixelData); |
| 219 | } |
| 220 | |
| 221 | void WriteFloatColor(const gl::ColorF &color, |
| 222 | ColorWriteFunction colorWriteFunction, |
| 223 | uint8_t *destPixelData) |
| 224 | { |
| 225 | colorWriteFunction(reinterpret_cast<const uint8_t *>(&color), destPixelData); |
| 226 | } |
| 227 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 228 | } // anonymous namespace |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 229 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 230 | PackPixelsParams::PackPixelsParams() |
| 231 | : format(GL_NONE), type(GL_NONE), outputPitch(0), packBuffer(nullptr), offset(0) |
| 232 | { |
| 233 | } |
| 234 | |
| 235 | PackPixelsParams::PackPixelsParams(const gl::Rectangle &areaIn, |
| 236 | GLenum formatIn, |
| 237 | GLenum typeIn, |
| 238 | GLuint outputPitchIn, |
| 239 | const gl::PixelPackState &packIn, |
| 240 | ptrdiff_t offsetIn) |
| 241 | : area(areaIn), |
| 242 | format(formatIn), |
| 243 | type(typeIn), |
| 244 | outputPitch(outputPitchIn), |
| 245 | packBuffer(packIn.pixelBuffer.get()), |
| 246 | pack(packIn.alignment, packIn.reverseRowOrder), |
| 247 | offset(offsetIn) |
| 248 | { |
| 249 | } |
| 250 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 251 | PackPixelsParams::PackPixelsParams(const gl::Context *context, const PackPixelsParams &other) |
| 252 | : area(other.area), |
| 253 | format(other.format), |
| 254 | type(other.type), |
| 255 | outputPitch(other.outputPitch), |
| 256 | packBuffer(other.packBuffer), |
| 257 | pack(), |
| 258 | offset(other.offset) |
| 259 | { |
| 260 | pack.copyFrom(context, other.pack); |
| 261 | } |
| 262 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 263 | void PackPixels(const PackPixelsParams ¶ms, |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 264 | const angle::Format &sourceFormat, |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 265 | int inputPitchIn, |
| 266 | const uint8_t *sourceIn, |
| 267 | uint8_t *destWithoutOffset) |
| 268 | { |
| 269 | uint8_t *destWithOffset = destWithoutOffset + params.offset; |
| 270 | |
| 271 | const uint8_t *source = sourceIn; |
| 272 | int inputPitch = inputPitchIn; |
| 273 | |
| 274 | if (params.pack.reverseRowOrder) |
| 275 | { |
| 276 | source += inputPitch * (params.area.height - 1); |
| 277 | inputPitch = -inputPitch; |
| 278 | } |
| 279 | |
Geoff Lang | ca27139 | 2017-04-05 12:30:00 -0400 | [diff] [blame] | 280 | const auto &sourceGLInfo = gl::GetSizedInternalFormatInfo(sourceFormat.glInternalFormat); |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 281 | |
| 282 | if (sourceGLInfo.format == params.format && sourceGLInfo.type == params.type) |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 283 | { |
| 284 | // Direct copy possible |
| 285 | for (int y = 0; y < params.area.height; ++y) |
| 286 | { |
| 287 | memcpy(destWithOffset + y * params.outputPitch, source + y * inputPitch, |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 288 | params.area.width * sourceGLInfo.pixelBytes); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 289 | } |
| 290 | return; |
| 291 | } |
| 292 | |
Geoff Lang | ca27139 | 2017-04-05 12:30:00 -0400 | [diff] [blame] | 293 | ASSERT(sourceGLInfo.sized); |
Jamie Madill | ec0b580 | 2016-07-04 13:11:59 -0400 | [diff] [blame] | 294 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 295 | gl::FormatType formatType(params.format, params.type); |
Jamie Madill | 3071206 | 2016-08-09 11:10:36 -0400 | [diff] [blame] | 296 | ColorCopyFunction fastCopyFunc = |
| 297 | GetFastCopyFunction(sourceFormat.fastCopyFunctions, formatType); |
Geoff Lang | ca27139 | 2017-04-05 12:30:00 -0400 | [diff] [blame] | 298 | const auto &destFormatInfo = gl::GetInternalFormatInfo(formatType.format, formatType.type); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 299 | |
| 300 | if (fastCopyFunc) |
| 301 | { |
| 302 | // Fast copy is possible through some special function |
| 303 | for (int y = 0; y < params.area.height; ++y) |
| 304 | { |
| 305 | for (int x = 0; x < params.area.width; ++x) |
| 306 | { |
| 307 | uint8_t *dest = |
| 308 | destWithOffset + y * params.outputPitch + x * destFormatInfo.pixelBytes; |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 309 | const uint8_t *src = source + y * inputPitch + x * sourceGLInfo.pixelBytes; |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 310 | |
| 311 | fastCopyFunc(src, dest); |
| 312 | } |
| 313 | } |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | ColorWriteFunction colorWriteFunction = GetColorWriteFunction(formatType); |
| 318 | |
| 319 | // Maximum size of any Color<T> type used. |
| 320 | uint8_t temp[16]; |
| 321 | static_assert(sizeof(temp) >= sizeof(gl::ColorF) && sizeof(temp) >= sizeof(gl::ColorUI) && |
| 322 | sizeof(temp) >= sizeof(gl::ColorI), |
| 323 | "Unexpected size of gl::Color struct."); |
| 324 | |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 325 | const auto &colorReadFunction = sourceFormat.colorReadFunction; |
| 326 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 327 | for (int y = 0; y < params.area.height; ++y) |
| 328 | { |
| 329 | for (int x = 0; x < params.area.width; ++x) |
| 330 | { |
| 331 | uint8_t *dest = destWithOffset + y * params.outputPitch + x * destFormatInfo.pixelBytes; |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 332 | const uint8_t *src = source + y * inputPitch + x * sourceGLInfo.pixelBytes; |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 333 | |
| 334 | // readFunc and writeFunc will be using the same type of color, CopyTexImage |
| 335 | // will not allow the copy otherwise. |
| 336 | colorReadFunction(src, temp); |
| 337 | colorWriteFunction(temp, dest); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | ColorWriteFunction GetColorWriteFunction(const gl::FormatType &formatType) |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 343 | { |
| 344 | static const FormatWriteFunctionMap formatTypeMap = BuildFormatWriteFunctionMap(); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 345 | auto iter = formatTypeMap.find(formatType); |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 346 | ASSERT(iter != formatTypeMap.end()); |
| 347 | if (iter != formatTypeMap.end()) |
| 348 | { |
| 349 | return iter->second; |
| 350 | } |
| 351 | else |
| 352 | { |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 353 | return nullptr; |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 357 | ColorCopyFunction GetFastCopyFunction(const FastCopyFunctionMap &fastCopyFunctions, |
| 358 | const gl::FormatType &formatType) |
| 359 | { |
Jamie Madill | 4f57e5f | 2016-10-27 17:36:53 -0400 | [diff] [blame] | 360 | return fastCopyFunctions.get(formatType); |
| 361 | } |
| 362 | |
| 363 | bool FastCopyFunctionMap::has(const gl::FormatType &formatType) const |
| 364 | { |
| 365 | return (get(formatType) != nullptr); |
| 366 | } |
| 367 | |
| 368 | ColorCopyFunction FastCopyFunctionMap::get(const gl::FormatType &formatType) const |
| 369 | { |
| 370 | for (size_t index = 0; index < mSize; ++index) |
| 371 | { |
| 372 | if (mData[index].format == formatType.format && mData[index].type == formatType.type) |
| 373 | { |
| 374 | return mData[index].func; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | return nullptr; |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 379 | } |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 380 | |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 381 | bool ShouldUseDebugLayers(const egl::AttributeMap &attribs) |
| 382 | { |
| 383 | EGLAttrib debugSetting = |
| 384 | attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE); |
| 385 | |
| 386 | // Prefer to enable debug layers if compiling in Debug, and disabled in Release. |
| 387 | #if !defined(NDEBUG) |
| 388 | return (debugSetting != EGL_FALSE); |
| 389 | #else |
| 390 | return (debugSetting == EGL_TRUE); |
| 391 | #endif // !defined(NDEBUG) |
| 392 | } |
| 393 | |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 394 | void CopyImageCHROMIUM(const uint8_t *sourceData, |
| 395 | size_t sourceRowPitch, |
| 396 | size_t sourcePixelBytes, |
| 397 | ColorReadFunction colorReadFunction, |
| 398 | uint8_t *destData, |
| 399 | size_t destRowPitch, |
| 400 | size_t destPixelBytes, |
| 401 | ColorWriteFunction colorWriteFunction, |
| 402 | GLenum destUnsizedFormat, |
| 403 | GLenum destComponentType, |
| 404 | size_t width, |
| 405 | size_t height, |
| 406 | bool unpackFlipY, |
| 407 | bool unpackPremultiplyAlpha, |
| 408 | bool unpackUnmultiplyAlpha) |
| 409 | { |
| 410 | using ConversionFunction = void (*)(gl::ColorF *); |
| 411 | ConversionFunction conversionFunction = CopyColor; |
| 412 | if (unpackPremultiplyAlpha != unpackUnmultiplyAlpha) |
| 413 | { |
| 414 | if (unpackPremultiplyAlpha) |
| 415 | { |
| 416 | conversionFunction = PremultiplyAlpha; |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | conversionFunction = UnmultiplyAlpha; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | auto clipChannelsFunction = ClipChannelsNoOp; |
| 425 | switch (destUnsizedFormat) |
| 426 | { |
| 427 | case GL_RED: |
| 428 | clipChannelsFunction = ClipChannelsR; |
| 429 | break; |
| 430 | case GL_RG: |
| 431 | clipChannelsFunction = ClipChannelsRG; |
| 432 | break; |
| 433 | case GL_RGB: |
| 434 | clipChannelsFunction = ClipChannelsRGB; |
| 435 | break; |
| 436 | case GL_LUMINANCE: |
| 437 | clipChannelsFunction = ClipChannelsLuminance; |
| 438 | break; |
| 439 | case GL_ALPHA: |
| 440 | clipChannelsFunction = ClipChannelsAlpha; |
| 441 | break; |
| 442 | } |
| 443 | |
| 444 | auto writeFunction = (destComponentType == GL_UNSIGNED_INT) ? WriteUintColor : WriteFloatColor; |
| 445 | |
| 446 | for (size_t y = 0; y < height; y++) |
| 447 | { |
| 448 | for (size_t x = 0; x < width; x++) |
| 449 | { |
| 450 | const uint8_t *sourcePixelData = sourceData + y * sourceRowPitch + x * sourcePixelBytes; |
| 451 | |
| 452 | gl::ColorF sourceColor; |
| 453 | colorReadFunction(sourcePixelData, reinterpret_cast<uint8_t *>(&sourceColor)); |
| 454 | |
| 455 | conversionFunction(&sourceColor); |
| 456 | clipChannelsFunction(&sourceColor); |
| 457 | |
| 458 | size_t destY = 0; |
| 459 | if (unpackFlipY) |
| 460 | { |
| 461 | destY += (height - 1); |
| 462 | destY -= y; |
| 463 | } |
| 464 | else |
| 465 | { |
| 466 | destY += y; |
| 467 | } |
| 468 | |
| 469 | uint8_t *destPixelData = destData + destY * destRowPitch + x * destPixelBytes; |
| 470 | writeFunction(sourceColor, colorWriteFunction, destPixelData); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame^] | 475 | // IncompleteTextureSet implementation. |
| 476 | IncompleteTextureSet::IncompleteTextureSet() |
| 477 | { |
| 478 | } |
| 479 | |
| 480 | IncompleteTextureSet::~IncompleteTextureSet() |
| 481 | { |
| 482 | } |
| 483 | |
| 484 | void IncompleteTextureSet::onDestroy(const gl::Context *context) |
| 485 | { |
| 486 | // Clear incomplete textures. |
| 487 | for (auto &incompleteTexture : mIncompleteTextures) |
| 488 | { |
| 489 | ANGLE_SWALLOW_ERR(incompleteTexture.second->onDestroy(context)); |
| 490 | incompleteTexture.second.set(context, nullptr); |
| 491 | } |
| 492 | mIncompleteTextures.clear(); |
| 493 | } |
| 494 | |
| 495 | gl::Error IncompleteTextureSet::getIncompleteTexture( |
| 496 | const gl::Context *context, |
| 497 | GLenum type, |
| 498 | MultisampleTextureInitializer *multisampleInitializer, |
| 499 | gl::Texture **textureOut) |
| 500 | { |
| 501 | auto iter = mIncompleteTextures.find(type); |
| 502 | if (iter != mIncompleteTextures.end()) |
| 503 | { |
| 504 | *textureOut = iter->second.get(); |
| 505 | return gl::NoError(); |
| 506 | } |
| 507 | |
| 508 | ContextImpl *implFactory = context->getImplementation(); |
| 509 | |
| 510 | const GLubyte color[] = {0, 0, 0, 255}; |
| 511 | const gl::Extents colorSize(1, 1, 1); |
| 512 | const gl::PixelUnpackState unpack(1, 0); |
| 513 | const gl::Box area(0, 0, 0, 1, 1, 1); |
| 514 | |
| 515 | // If a texture is external use a 2D texture for the incomplete texture |
| 516 | GLenum createType = (type == GL_TEXTURE_EXTERNAL_OES) ? GL_TEXTURE_2D : type; |
| 517 | |
| 518 | gl::Texture *tex = new gl::Texture(implFactory, std::numeric_limits<GLuint>::max(), createType); |
| 519 | angle::UniqueObjectPointer<gl::Texture, gl::Context> t(tex, context); |
| 520 | |
| 521 | if (createType == GL_TEXTURE_2D_MULTISAMPLE) |
| 522 | { |
| 523 | ANGLE_TRY(t->setStorageMultisample(context, createType, 1, GL_RGBA8, colorSize, true)); |
| 524 | } |
| 525 | else |
| 526 | { |
| 527 | ANGLE_TRY(t->setStorage(context, createType, 1, GL_RGBA8, colorSize)); |
| 528 | } |
| 529 | |
| 530 | if (type == GL_TEXTURE_CUBE_MAP) |
| 531 | { |
| 532 | for (GLenum face = GL_TEXTURE_CUBE_MAP_POSITIVE_X; face <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; |
| 533 | face++) |
| 534 | { |
| 535 | ANGLE_TRY( |
| 536 | t->setSubImage(context, unpack, face, 0, area, GL_RGBA, GL_UNSIGNED_BYTE, color)); |
| 537 | } |
| 538 | } |
| 539 | else if (type == GL_TEXTURE_2D_MULTISAMPLE) |
| 540 | { |
| 541 | // Call a specialized clear function to init a multisample texture. |
| 542 | ANGLE_TRY(multisampleInitializer->initializeMultisampleTextureToBlack(context, t.get())); |
| 543 | } |
| 544 | else |
| 545 | { |
| 546 | ANGLE_TRY( |
| 547 | t->setSubImage(context, unpack, createType, 0, area, GL_RGBA, GL_UNSIGNED_BYTE, color)); |
| 548 | } |
| 549 | |
| 550 | t->syncState(); |
| 551 | |
| 552 | mIncompleteTextures[type].set(context, t.release()); |
| 553 | *textureOut = mIncompleteTextures[type].get(); |
| 554 | return gl::NoError(); |
| 555 | } |
| 556 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 557 | } // namespace rx |