Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2016 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | // renderer_utils: |
| 7 | // Helper methods pertaining to most or all back-ends. |
| 8 | // |
| 9 | |
| 10 | #ifndef LIBANGLE_RENDERER_RENDERER_UTILS_H_ |
| 11 | #define LIBANGLE_RENDERER_RENDERER_UTILS_H_ |
| 12 | |
| 13 | #include <cstdint> |
| 14 | |
Jamie Madill | dcc9b51 | 2017-06-05 15:28:45 -0400 | [diff] [blame] | 15 | #include <limits> |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 16 | #include <map> |
| 17 | |
Jamie Madill | fb05bcb | 2017-06-07 15:43:18 -0400 | [diff] [blame] | 18 | #include "common/angleutils.h" |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 19 | #include "libANGLE/angletypes.h" |
| 20 | |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 21 | namespace angle |
| 22 | { |
| 23 | struct Format; |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 24 | } // namespace angle |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 25 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 26 | namespace gl |
| 27 | { |
| 28 | struct FormatType; |
| 29 | struct InternalFormat; |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 30 | } // namespace gl |
| 31 | |
| 32 | namespace egl |
| 33 | { |
| 34 | class AttributeMap; |
| 35 | } // namespace egl |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 36 | |
| 37 | namespace rx |
| 38 | { |
| 39 | |
Jamie Madill | dcc9b51 | 2017-06-05 15:28:45 -0400 | [diff] [blame] | 40 | class ResourceSerial |
| 41 | { |
| 42 | public: |
| 43 | constexpr ResourceSerial() : mValue(kDirty) {} |
Jamie Madill | 9959f54 | 2017-09-12 15:22:56 -0400 | [diff] [blame] | 44 | explicit constexpr ResourceSerial(uintptr_t value) : mValue(value) {} |
Jamie Madill | dcc9b51 | 2017-06-05 15:28:45 -0400 | [diff] [blame] | 45 | constexpr bool operator==(ResourceSerial other) const { return mValue == other.mValue; } |
| 46 | constexpr bool operator!=(ResourceSerial other) const { return mValue != other.mValue; } |
| 47 | |
| 48 | void dirty() { mValue = kDirty; } |
Jamie Madill | 9959f54 | 2017-09-12 15:22:56 -0400 | [diff] [blame] | 49 | void clear() { mValue = kEmpty; } |
| 50 | |
| 51 | constexpr bool valid() const { return mValue != kEmpty && mValue != kDirty; } |
| 52 | constexpr bool empty() const { return mValue == kEmpty; } |
Jamie Madill | dcc9b51 | 2017-06-05 15:28:45 -0400 | [diff] [blame] | 53 | |
| 54 | private: |
| 55 | constexpr static uintptr_t kDirty = std::numeric_limits<uintptr_t>::max(); |
Jamie Madill | 9959f54 | 2017-09-12 15:22:56 -0400 | [diff] [blame] | 56 | constexpr static uintptr_t kEmpty = 0; |
Jamie Madill | dcc9b51 | 2017-06-05 15:28:45 -0400 | [diff] [blame] | 57 | |
| 58 | uintptr_t mValue; |
| 59 | }; |
| 60 | |
Jamie Madill | fb05bcb | 2017-06-07 15:43:18 -0400 | [diff] [blame] | 61 | class SerialFactory; |
| 62 | |
| 63 | class Serial final |
| 64 | { |
| 65 | public: |
Jamie Madill | 0e7f173 | 2017-09-09 23:32:50 -0400 | [diff] [blame] | 66 | constexpr Serial() : mValue(kInvalid) {} |
Jamie Madill | fb05bcb | 2017-06-07 15:43:18 -0400 | [diff] [blame] | 67 | constexpr Serial(const Serial &other) = default; |
| 68 | Serial &operator=(const Serial &other) = default; |
| 69 | |
Jamie Madill | 0e7f173 | 2017-09-09 23:32:50 -0400 | [diff] [blame] | 70 | constexpr bool operator==(const Serial &other) const |
| 71 | { |
| 72 | return mValue != kInvalid && mValue == other.mValue; |
| 73 | } |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 74 | constexpr bool operator==(uint32_t value) const |
| 75 | { |
| 76 | return mValue != kInvalid && mValue == static_cast<uint64_t>(value); |
| 77 | } |
Jamie Madill | 0e7f173 | 2017-09-09 23:32:50 -0400 | [diff] [blame] | 78 | constexpr bool operator!=(const Serial &other) const |
| 79 | { |
| 80 | return mValue == kInvalid || mValue != other.mValue; |
| 81 | } |
Jamie Madill | fb05bcb | 2017-06-07 15:43:18 -0400 | [diff] [blame] | 82 | constexpr bool operator>(const Serial &other) const { return mValue > other.mValue; } |
| 83 | constexpr bool operator>=(const Serial &other) const { return mValue >= other.mValue; } |
| 84 | constexpr bool operator<(const Serial &other) const { return mValue < other.mValue; } |
| 85 | constexpr bool operator<=(const Serial &other) const { return mValue <= other.mValue; } |
| 86 | |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 87 | constexpr bool operator<(uint32_t value) const { return mValue < static_cast<uint64_t>(value); } |
| 88 | |
| 89 | // Useful for serialization. |
| 90 | constexpr uint64_t getValue() const { return mValue; } |
| 91 | |
Jamie Madill | fb05bcb | 2017-06-07 15:43:18 -0400 | [diff] [blame] | 92 | private: |
| 93 | friend class SerialFactory; |
| 94 | constexpr explicit Serial(uint64_t value) : mValue(value) {} |
| 95 | uint64_t mValue; |
Jamie Madill | 0e7f173 | 2017-09-09 23:32:50 -0400 | [diff] [blame] | 96 | static constexpr uint64_t kInvalid = 0; |
Jamie Madill | fb05bcb | 2017-06-07 15:43:18 -0400 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | class SerialFactory final : angle::NonCopyable |
| 100 | { |
| 101 | public: |
| 102 | SerialFactory() : mSerial(1) {} |
| 103 | |
| 104 | Serial generate() |
| 105 | { |
| 106 | ASSERT(mSerial != std::numeric_limits<uint64_t>::max()); |
| 107 | return Serial(mSerial++); |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | uint64_t mSerial; |
| 112 | }; |
| 113 | |
Jamie Madill | a5b1561 | 2016-08-09 11:10:27 -0400 | [diff] [blame] | 114 | using MipGenerationFunction = void (*)(size_t sourceWidth, |
| 115 | size_t sourceHeight, |
| 116 | size_t sourceDepth, |
| 117 | const uint8_t *sourceData, |
| 118 | size_t sourceRowPitch, |
| 119 | size_t sourceDepthPitch, |
| 120 | uint8_t *destData, |
| 121 | size_t destRowPitch, |
| 122 | size_t destDepthPitch); |
| 123 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 124 | typedef void (*ColorReadFunction)(const uint8_t *source, uint8_t *dest); |
| 125 | typedef void (*ColorWriteFunction)(const uint8_t *source, uint8_t *dest); |
| 126 | typedef void (*ColorCopyFunction)(const uint8_t *source, uint8_t *dest); |
| 127 | |
Jamie Madill | 4f57e5f | 2016-10-27 17:36:53 -0400 | [diff] [blame] | 128 | class FastCopyFunctionMap |
| 129 | { |
| 130 | public: |
| 131 | struct Entry |
| 132 | { |
| 133 | GLenum format; |
| 134 | GLenum type; |
| 135 | ColorCopyFunction func; |
| 136 | }; |
| 137 | |
| 138 | constexpr FastCopyFunctionMap() : FastCopyFunctionMap(nullptr, 0) {} |
| 139 | |
| 140 | constexpr FastCopyFunctionMap(const Entry *data, size_t size) : mSize(size), mData(data) {} |
| 141 | |
| 142 | bool has(const gl::FormatType &formatType) const; |
| 143 | ColorCopyFunction get(const gl::FormatType &formatType) const; |
| 144 | |
| 145 | private: |
| 146 | size_t mSize; |
| 147 | const Entry *mData; |
| 148 | }; |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 149 | |
Corentin Wallez | cda6af1 | 2017-10-30 19:20:37 -0400 | [diff] [blame] | 150 | struct PackPixelsParams |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 151 | { |
| 152 | PackPixelsParams(); |
| 153 | PackPixelsParams(const gl::Rectangle &area, |
| 154 | GLenum format, |
| 155 | GLenum type, |
| 156 | GLuint outputPitch, |
| 157 | const gl::PixelPackState &pack, |
Corentin Wallez | cda6af1 | 2017-10-30 19:20:37 -0400 | [diff] [blame] | 158 | gl::Buffer *packBufferIn, |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 159 | ptrdiff_t offset); |
| 160 | |
| 161 | gl::Rectangle area; |
| 162 | GLenum format; |
| 163 | GLenum type; |
| 164 | GLuint outputPitch; |
| 165 | gl::Buffer *packBuffer; |
| 166 | gl::PixelPackState pack; |
| 167 | ptrdiff_t offset; |
| 168 | }; |
| 169 | |
| 170 | void PackPixels(const PackPixelsParams ¶ms, |
Jamie Madill | 86e0b7f | 2016-08-09 11:10:29 -0400 | [diff] [blame] | 171 | const angle::Format &sourceFormat, |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 172 | int inputPitch, |
| 173 | const uint8_t *source, |
| 174 | uint8_t *destination); |
| 175 | |
| 176 | ColorWriteFunction GetColorWriteFunction(const gl::FormatType &formatType); |
| 177 | ColorCopyFunction GetFastCopyFunction(const FastCopyFunctionMap &fastCopyFunctions, |
| 178 | const gl::FormatType &formatType); |
| 179 | |
Jamie Madill | abaab23 | 2017-01-10 12:37:37 -0500 | [diff] [blame] | 180 | using InitializeTextureDataFunction = void (*)(size_t width, |
| 181 | size_t height, |
| 182 | size_t depth, |
| 183 | uint8_t *output, |
| 184 | size_t outputRowPitch, |
| 185 | size_t outputDepthPitch); |
| 186 | |
Jamie Madill | b2e4863 | 2016-08-09 18:08:03 -0400 | [diff] [blame] | 187 | using LoadImageFunction = void (*)(size_t width, |
| 188 | size_t height, |
| 189 | size_t depth, |
| 190 | const uint8_t *input, |
| 191 | size_t inputRowPitch, |
| 192 | size_t inputDepthPitch, |
| 193 | uint8_t *output, |
| 194 | size_t outputRowPitch, |
| 195 | size_t outputDepthPitch); |
| 196 | |
| 197 | struct LoadImageFunctionInfo |
| 198 | { |
| 199 | LoadImageFunctionInfo() : loadFunction(nullptr), requiresConversion(false) {} |
| 200 | LoadImageFunctionInfo(LoadImageFunction loadFunction, bool requiresConversion) |
| 201 | : loadFunction(loadFunction), requiresConversion(requiresConversion) |
| 202 | { |
| 203 | } |
| 204 | |
| 205 | LoadImageFunction loadFunction; |
| 206 | bool requiresConversion; |
| 207 | }; |
| 208 | |
| 209 | using LoadFunctionMap = LoadImageFunctionInfo (*)(GLenum); |
| 210 | |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 211 | bool ShouldUseDebugLayers(const egl::AttributeMap &attribs); |
| 212 | |
Geoff Lang | d93cd6c | 2017-08-11 16:32:41 -0400 | [diff] [blame] | 213 | void CopyImageCHROMIUM(const uint8_t *sourceData, |
| 214 | size_t sourceRowPitch, |
| 215 | size_t sourcePixelBytes, |
| 216 | ColorReadFunction readFunction, |
| 217 | uint8_t *destData, |
| 218 | size_t destRowPitch, |
| 219 | size_t destPixelBytes, |
| 220 | ColorWriteFunction colorWriteFunction, |
| 221 | GLenum destUnsizedFormat, |
| 222 | GLenum destComponentType, |
| 223 | size_t width, |
| 224 | size_t height, |
| 225 | bool unpackFlipY, |
| 226 | bool unpackPremultiplyAlpha, |
| 227 | bool unpackUnmultiplyAlpha); |
| 228 | |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 229 | // Incomplete textures are 1x1 textures filled with black, used when samplers are incomplete. |
| 230 | // This helper class encapsulates handling incomplete textures. Because the GL back-end |
| 231 | // can take advantage of the driver's incomplete textures, and because clearing multisample |
| 232 | // textures is so difficult, we can keep an instance of this class in the back-end instead |
| 233 | // of moving the logic to the Context front-end. |
| 234 | |
| 235 | // This interface allows us to call-back to init a multisample texture. |
| 236 | class MultisampleTextureInitializer |
| 237 | { |
| 238 | public: |
Jamie Madill | 5ad2640 | 2017-10-17 15:32:06 -0400 | [diff] [blame] | 239 | virtual ~MultisampleTextureInitializer() {} |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 240 | virtual gl::Error initializeMultisampleTextureToBlack(const gl::Context *context, |
| 241 | gl::Texture *glTexture) = 0; |
| 242 | }; |
| 243 | |
| 244 | class IncompleteTextureSet final : angle::NonCopyable |
| 245 | { |
| 246 | public: |
| 247 | IncompleteTextureSet(); |
| 248 | ~IncompleteTextureSet(); |
| 249 | |
| 250 | void onDestroy(const gl::Context *context); |
| 251 | |
| 252 | gl::Error getIncompleteTexture(const gl::Context *context, |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 253 | gl::TextureType type, |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 254 | MultisampleTextureInitializer *multisampleInitializer, |
| 255 | gl::Texture **textureOut); |
| 256 | |
| 257 | private: |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 258 | gl::TextureMap mIncompleteTextures; |
Jamie Madill | 4297564 | 2017-10-12 12:31:51 -0400 | [diff] [blame] | 259 | }; |
| 260 | |
Luc Ferron | 46bcea5 | 2018-05-31 09:48:36 -0400 | [diff] [blame] | 261 | // The return value indicate if the data was updated or not. |
| 262 | template <int cols, int rows> |
| 263 | bool SetFloatUniformMatrix(unsigned int arrayElementOffset, |
| 264 | unsigned int elementCount, |
| 265 | GLsizei countIn, |
| 266 | GLboolean transpose, |
| 267 | const GLfloat *value, |
| 268 | uint8_t *targetData); |
| 269 | |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 270 | // Helper method to de-tranpose a matrix uniform for an API query. |
| 271 | void GetMatrixUniform(GLenum type, GLfloat *dataOut, const GLfloat *source, bool transpose); |
| 272 | |
| 273 | template <typename NonFloatT> |
| 274 | void GetMatrixUniform(GLenum type, NonFloatT *dataOut, const NonFloatT *source, bool transpose); |
| 275 | |
Jamie Madill | d2b50a0 | 2016-06-09 00:13:35 -0700 | [diff] [blame] | 276 | } // namespace rx |
| 277 | |
| 278 | #endif // LIBANGLE_RENDERER_RENDERER_UTILS_H_ |