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