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, |
Corentin Wallez | cda6af1 | 2017-10-30 19:20:37 -0400 | [diff] [blame] | 240 | gl::Buffer *packBufferIn, |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 241 | ptrdiff_t offsetIn) |
| 242 | : area(areaIn), |
| 243 | format(formatIn), |
| 244 | type(typeIn), |
| 245 | outputPitch(outputPitchIn), |
Corentin Wallez | cda6af1 | 2017-10-30 19:20:37 -0400 | [diff] [blame] | 246 | packBuffer(packBufferIn), |
| 247 | pack(), |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 248 | offset(offsetIn) |
| 249 | { |
Corentin Wallez | cda6af1 | 2017-10-30 19:20:37 -0400 | [diff] [blame] | 250 | pack.alignment = packIn.alignment; |
| 251 | pack.reverseRowOrder = packIn.reverseRowOrder; |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 252 | } |
| 253 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 254 | void PackPixels(const PackPixelsParams ¶ms, |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 255 | const angle::Format &sourceFormat, |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 256 | int inputPitchIn, |
| 257 | const uint8_t *sourceIn, |
| 258 | uint8_t *destWithoutOffset) |
| 259 | { |
| 260 | uint8_t *destWithOffset = destWithoutOffset + params.offset; |
| 261 | |
| 262 | const uint8_t *source = sourceIn; |
| 263 | int inputPitch = inputPitchIn; |
| 264 | |
| 265 | if (params.pack.reverseRowOrder) |
| 266 | { |
| 267 | source += inputPitch * (params.area.height - 1); |
| 268 | inputPitch = -inputPitch; |
| 269 | } |
| 270 | |
Geoff Lang | ca27139 | 2017-04-05 12:30:00 -0400 | [diff] [blame] | 271 | const auto &sourceGLInfo = gl::GetSizedInternalFormatInfo(sourceFormat.glInternalFormat); |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 272 | |
| 273 | if (sourceGLInfo.format == params.format && sourceGLInfo.type == params.type) |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 274 | { |
| 275 | // Direct copy possible |
| 276 | for (int y = 0; y < params.area.height; ++y) |
| 277 | { |
| 278 | memcpy(destWithOffset + y * params.outputPitch, source + y * inputPitch, |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 279 | params.area.width * sourceGLInfo.pixelBytes); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 280 | } |
| 281 | return; |
| 282 | } |
| 283 | |
Geoff Lang | ca27139 | 2017-04-05 12:30:00 -0400 | [diff] [blame] | 284 | ASSERT(sourceGLInfo.sized); |
Jamie Madill | ec0b580 | 2016-07-04 13:11:59 -0400 | [diff] [blame] | 285 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 286 | gl::FormatType formatType(params.format, params.type); |
Jamie Madill | 3071206 | 2016-08-09 11:10:36 -0400 | [diff] [blame] | 287 | ColorCopyFunction fastCopyFunc = |
| 288 | GetFastCopyFunction(sourceFormat.fastCopyFunctions, formatType); |
Geoff Lang | ca27139 | 2017-04-05 12:30:00 -0400 | [diff] [blame] | 289 | const auto &destFormatInfo = gl::GetInternalFormatInfo(formatType.format, formatType.type); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 290 | |
| 291 | if (fastCopyFunc) |
| 292 | { |
| 293 | // Fast copy is possible through some special function |
| 294 | for (int y = 0; y < params.area.height; ++y) |
| 295 | { |
| 296 | for (int x = 0; x < params.area.width; ++x) |
| 297 | { |
| 298 | uint8_t *dest = |
| 299 | destWithOffset + y * params.outputPitch + x * destFormatInfo.pixelBytes; |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 300 | const uint8_t *src = source + y * inputPitch + x * sourceGLInfo.pixelBytes; |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 301 | |
| 302 | fastCopyFunc(src, dest); |
| 303 | } |
| 304 | } |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | ColorWriteFunction colorWriteFunction = GetColorWriteFunction(formatType); |
| 309 | |
| 310 | // Maximum size of any Color<T> type used. |
| 311 | uint8_t temp[16]; |
| 312 | static_assert(sizeof(temp) >= sizeof(gl::ColorF) && sizeof(temp) >= sizeof(gl::ColorUI) && |
| 313 | sizeof(temp) >= sizeof(gl::ColorI), |
| 314 | "Unexpected size of gl::Color struct."); |
| 315 | |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 316 | const auto &colorReadFunction = sourceFormat.colorReadFunction; |
| 317 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 318 | for (int y = 0; y < params.area.height; ++y) |
| 319 | { |
| 320 | for (int x = 0; x < params.area.width; ++x) |
| 321 | { |
| 322 | uint8_t *dest = destWithOffset + y * params.outputPitch + x * destFormatInfo.pixelBytes; |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 323 | const uint8_t *src = source + y * inputPitch + x * sourceGLInfo.pixelBytes; |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 324 | |
| 325 | // readFunc and writeFunc will be using the same type of color, CopyTexImage |
| 326 | // will not allow the copy otherwise. |
| 327 | colorReadFunction(src, temp); |
| 328 | colorWriteFunction(temp, dest); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | ColorWriteFunction GetColorWriteFunction(const gl::FormatType &formatType) |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 334 | { |
| 335 | static const FormatWriteFunctionMap formatTypeMap = BuildFormatWriteFunctionMap(); |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 336 | auto iter = formatTypeMap.find(formatType); |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 337 | ASSERT(iter != formatTypeMap.end()); |
| 338 | if (iter != formatTypeMap.end()) |
| 339 | { |
| 340 | return iter->second; |
| 341 | } |
| 342 | else |
| 343 | { |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 344 | return nullptr; |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 348 | ColorCopyFunction GetFastCopyFunction(const FastCopyFunctionMap &fastCopyFunctions, |
| 349 | const gl::FormatType &formatType) |
| 350 | { |
Jamie Madill | 4f57e5f | 2016-10-27 17:36:53 -0400 | [diff] [blame] | 351 | return fastCopyFunctions.get(formatType); |
| 352 | } |
| 353 | |
| 354 | bool FastCopyFunctionMap::has(const gl::FormatType &formatType) const |
| 355 | { |
| 356 | return (get(formatType) != nullptr); |
| 357 | } |
| 358 | |
| 359 | ColorCopyFunction FastCopyFunctionMap::get(const gl::FormatType &formatType) const |
| 360 | { |
| 361 | for (size_t index = 0; index < mSize; ++index) |
| 362 | { |
| 363 | if (mData[index].format == formatType.format && mData[index].type == formatType.type) |
| 364 | { |
| 365 | return mData[index].func; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | return nullptr; |
Geoff Lang | 051dbc7 | 2015-01-05 15:48:58 -0500 | [diff] [blame] | 370 | } |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 371 | |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 372 | bool ShouldUseDebugLayers(const egl::AttributeMap &attribs) |
| 373 | { |
| 374 | EGLAttrib debugSetting = |
| 375 | attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE); |
| 376 | |
| 377 | // 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] | 378 | #if defined(ANGLE_ENABLE_ASSERTS) |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 379 | return (debugSetting != EGL_FALSE); |
| 380 | #else |
| 381 | return (debugSetting == EGL_TRUE); |
Geoff Lang | 58ba6bf | 2017-11-28 16:40:58 -0500 | [diff] [blame] | 382 | #endif // defined(ANGLE_ENABLE_ASSERTS) |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 383 | } |
| 384 | |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 385 | void CopyImageCHROMIUM(const uint8_t *sourceData, |
| 386 | size_t sourceRowPitch, |
| 387 | size_t sourcePixelBytes, |
| 388 | ColorReadFunction colorReadFunction, |
| 389 | uint8_t *destData, |
| 390 | size_t destRowPitch, |
| 391 | size_t destPixelBytes, |
| 392 | ColorWriteFunction colorWriteFunction, |
| 393 | GLenum destUnsizedFormat, |
| 394 | GLenum destComponentType, |
| 395 | size_t width, |
| 396 | size_t height, |
| 397 | bool unpackFlipY, |
| 398 | bool unpackPremultiplyAlpha, |
| 399 | bool unpackUnmultiplyAlpha) |
| 400 | { |
| 401 | using ConversionFunction = void (*)(gl::ColorF *); |
| 402 | ConversionFunction conversionFunction = CopyColor; |
| 403 | if (unpackPremultiplyAlpha != unpackUnmultiplyAlpha) |
| 404 | { |
| 405 | if (unpackPremultiplyAlpha) |
| 406 | { |
| 407 | conversionFunction = PremultiplyAlpha; |
| 408 | } |
| 409 | else |
| 410 | { |
| 411 | conversionFunction = UnmultiplyAlpha; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | auto clipChannelsFunction = ClipChannelsNoOp; |
| 416 | switch (destUnsizedFormat) |
| 417 | { |
| 418 | case GL_RED: |
| 419 | clipChannelsFunction = ClipChannelsR; |
| 420 | break; |
| 421 | case GL_RG: |
| 422 | clipChannelsFunction = ClipChannelsRG; |
| 423 | break; |
| 424 | case GL_RGB: |
| 425 | clipChannelsFunction = ClipChannelsRGB; |
| 426 | break; |
| 427 | case GL_LUMINANCE: |
| 428 | clipChannelsFunction = ClipChannelsLuminance; |
| 429 | break; |
| 430 | case GL_ALPHA: |
| 431 | clipChannelsFunction = ClipChannelsAlpha; |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | auto writeFunction = (destComponentType == GL_UNSIGNED_INT) ? WriteUintColor : WriteFloatColor; |
| 436 | |
| 437 | for (size_t y = 0; y < height; y++) |
| 438 | { |
| 439 | for (size_t x = 0; x < width; x++) |
| 440 | { |
| 441 | const uint8_t *sourcePixelData = sourceData + y * sourceRowPitch + x * sourcePixelBytes; |
| 442 | |
| 443 | gl::ColorF sourceColor; |
| 444 | colorReadFunction(sourcePixelData, reinterpret_cast<uint8_t *>(&sourceColor)); |
| 445 | |
| 446 | conversionFunction(&sourceColor); |
| 447 | clipChannelsFunction(&sourceColor); |
| 448 | |
| 449 | size_t destY = 0; |
| 450 | if (unpackFlipY) |
| 451 | { |
| 452 | destY += (height - 1); |
| 453 | destY -= y; |
| 454 | } |
| 455 | else |
| 456 | { |
| 457 | destY += y; |
| 458 | } |
| 459 | |
| 460 | uint8_t *destPixelData = destData + destY * destRowPitch + x * destPixelBytes; |
| 461 | writeFunction(sourceColor, colorWriteFunction, destPixelData); |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 466 | // IncompleteTextureSet implementation. |
| 467 | IncompleteTextureSet::IncompleteTextureSet() |
| 468 | { |
| 469 | } |
| 470 | |
| 471 | IncompleteTextureSet::~IncompleteTextureSet() |
| 472 | { |
| 473 | } |
| 474 | |
| 475 | void IncompleteTextureSet::onDestroy(const gl::Context *context) |
| 476 | { |
| 477 | // Clear incomplete textures. |
| 478 | for (auto &incompleteTexture : mIncompleteTextures) |
| 479 | { |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 480 | if (incompleteTexture.get() != nullptr) |
| 481 | { |
| 482 | ANGLE_SWALLOW_ERR(incompleteTexture->onDestroy(context)); |
| 483 | incompleteTexture.set(context, nullptr); |
| 484 | } |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 485 | } |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | gl::Error IncompleteTextureSet::getIncompleteTexture( |
| 489 | const gl::Context *context, |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 490 | gl::TextureType type, |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 491 | MultisampleTextureInitializer *multisampleInitializer, |
| 492 | gl::Texture **textureOut) |
| 493 | { |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 494 | *textureOut = mIncompleteTextures[type].get(); |
| 495 | if (*textureOut != nullptr) |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 496 | { |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 497 | return gl::NoError(); |
| 498 | } |
| 499 | |
| 500 | ContextImpl *implFactory = context->getImplementation(); |
| 501 | |
| 502 | const GLubyte color[] = {0, 0, 0, 255}; |
| 503 | const gl::Extents colorSize(1, 1, 1); |
Corentin Wallez | cda6af1 | 2017-10-30 19:20:37 -0400 | [diff] [blame] | 504 | gl::PixelUnpackState unpack; |
| 505 | unpack.alignment = 1; |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 506 | const gl::Box area(0, 0, 0, 1, 1, 1); |
| 507 | |
| 508 | // 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] | 509 | gl::TextureType createType = (type == gl::TextureType::External) ? gl::TextureType::_2D : type; |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 510 | |
| 511 | gl::Texture *tex = new gl::Texture(implFactory, std::numeric_limits<GLuint>::max(), createType); |
| 512 | angle::UniqueObjectPointer<gl::Texture, gl::Context> t(tex, context); |
| 513 | |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 514 | if (createType == gl::TextureType::_2DMultisample) |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 515 | { |
| 516 | ANGLE_TRY(t->setStorageMultisample(context, createType, 1, GL_RGBA8, colorSize, true)); |
| 517 | } |
| 518 | else |
| 519 | { |
| 520 | ANGLE_TRY(t->setStorage(context, createType, 1, GL_RGBA8, colorSize)); |
| 521 | } |
| 522 | |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 523 | if (type == gl::TextureType::CubeMap) |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 524 | { |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 525 | for (gl::TextureTarget face : gl::AllCubeFaceTextureTargets()) |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 526 | { |
| 527 | ANGLE_TRY( |
| 528 | t->setSubImage(context, unpack, face, 0, area, GL_RGBA, GL_UNSIGNED_BYTE, color)); |
| 529 | } |
| 530 | } |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 531 | else if (type == gl::TextureType::_2DMultisample) |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 532 | { |
| 533 | // Call a specialized clear function to init a multisample texture. |
| 534 | ANGLE_TRY(multisampleInitializer->initializeMultisampleTextureToBlack(context, t.get())); |
| 535 | } |
| 536 | else |
| 537 | { |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 538 | ANGLE_TRY(t->setSubImage(context, unpack, gl::NonCubeTextureTypeToTarget(createType), 0, |
| 539 | area, GL_RGBA, GL_UNSIGNED_BYTE, color)); |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 540 | } |
| 541 | |
Luc Ferron | 4bba74f | 2018-04-19 14:40:45 -0400 | [diff] [blame] | 542 | ANGLE_TRY(t->syncState(context)); |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 543 | |
| 544 | mIncompleteTextures[type].set(context, t.release()); |
| 545 | *textureOut = mIncompleteTextures[type].get(); |
| 546 | return gl::NoError(); |
| 547 | } |
| 548 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 549 | } // namespace rx |