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