blob: 34140bfb5673b705e53dc13ca19fb43276e4bba4 [file] [log] [blame]
Jamie Madilld2b50a02016-06-09 00:13:35 -07001//
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 Madilldcc9b512017-06-05 15:28:45 -040015#include <limits>
Jamie Madilld2b50a02016-06-09 00:13:35 -070016#include <map>
17
Jamie Madillfb05bcb2017-06-07 15:43:18 -040018#include "common/angleutils.h"
Jamie Madilld2b50a02016-06-09 00:13:35 -070019#include "libANGLE/angletypes.h"
20
Jamie Madill86e0b7f2016-08-09 11:10:29 -040021namespace angle
22{
23struct Format;
Jamie Madilldb9c69e2018-07-18 17:23:47 -040024enum class FormatID;
Jamie Madill222c5172017-07-19 16:15:42 -040025} // namespace angle
Jamie Madill86e0b7f2016-08-09 11:10:29 -040026
Jamie Madilld2b50a02016-06-09 00:13:35 -070027namespace gl
28{
29struct FormatType;
30struct InternalFormat;
Jamie Madill222c5172017-07-19 16:15:42 -040031} // namespace gl
32
33namespace egl
34{
35class AttributeMap;
36} // namespace egl
Jamie Madilld2b50a02016-06-09 00:13:35 -070037
38namespace rx
39{
40
Jamie Madilldcc9b512017-06-05 15:28:45 -040041class ResourceSerial
42{
43 public:
44 constexpr ResourceSerial() : mValue(kDirty) {}
Jamie Madill9959f542017-09-12 15:22:56 -040045 explicit constexpr ResourceSerial(uintptr_t value) : mValue(value) {}
Jamie Madilldcc9b512017-06-05 15:28:45 -040046 constexpr bool operator==(ResourceSerial other) const { return mValue == other.mValue; }
47 constexpr bool operator!=(ResourceSerial other) const { return mValue != other.mValue; }
48
49 void dirty() { mValue = kDirty; }
Jamie Madill9959f542017-09-12 15:22:56 -040050 void clear() { mValue = kEmpty; }
51
52 constexpr bool valid() const { return mValue != kEmpty && mValue != kDirty; }
53 constexpr bool empty() const { return mValue == kEmpty; }
Jamie Madilldcc9b512017-06-05 15:28:45 -040054
55 private:
56 constexpr static uintptr_t kDirty = std::numeric_limits<uintptr_t>::max();
Jamie Madill9959f542017-09-12 15:22:56 -040057 constexpr static uintptr_t kEmpty = 0;
Jamie Madilldcc9b512017-06-05 15:28:45 -040058
59 uintptr_t mValue;
60};
61
Jamie Madillfb05bcb2017-06-07 15:43:18 -040062class SerialFactory;
63
64class Serial final
65{
66 public:
Jamie Madill0e7f1732017-09-09 23:32:50 -040067 constexpr Serial() : mValue(kInvalid) {}
Jamie Madillfb05bcb2017-06-07 15:43:18 -040068 constexpr Serial(const Serial &other) = default;
69 Serial &operator=(const Serial &other) = default;
70
Jamie Madill0e7f1732017-09-09 23:32:50 -040071 constexpr bool operator==(const Serial &other) const
72 {
73 return mValue != kInvalid && mValue == other.mValue;
74 }
Jamie Madillf2f6d372018-01-10 21:37:23 -050075 constexpr bool operator==(uint32_t value) const
76 {
77 return mValue != kInvalid && mValue == static_cast<uint64_t>(value);
78 }
Jamie Madill0e7f1732017-09-09 23:32:50 -040079 constexpr bool operator!=(const Serial &other) const
80 {
81 return mValue == kInvalid || mValue != other.mValue;
82 }
Jamie Madillfb05bcb2017-06-07 15:43:18 -040083 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 constexpr bool operator<=(const Serial &other) const { return mValue <= other.mValue; }
87
Jamie Madillf2f6d372018-01-10 21:37:23 -050088 constexpr bool operator<(uint32_t value) const { return mValue < static_cast<uint64_t>(value); }
89
90 // Useful for serialization.
91 constexpr uint64_t getValue() const { return mValue; }
92
Jamie Madillfb05bcb2017-06-07 15:43:18 -040093 private:
94 friend class SerialFactory;
95 constexpr explicit Serial(uint64_t value) : mValue(value) {}
96 uint64_t mValue;
Jamie Madill0e7f1732017-09-09 23:32:50 -040097 static constexpr uint64_t kInvalid = 0;
Jamie Madillfb05bcb2017-06-07 15:43:18 -040098};
99
100class SerialFactory final : angle::NonCopyable
101{
102 public:
103 SerialFactory() : mSerial(1) {}
104
105 Serial generate()
106 {
107 ASSERT(mSerial != std::numeric_limits<uint64_t>::max());
108 return Serial(mSerial++);
109 }
110
111 private:
112 uint64_t mSerial;
113};
114
Jamie Madilla5b15612016-08-09 11:10:27 -0400115using MipGenerationFunction = void (*)(size_t sourceWidth,
116 size_t sourceHeight,
117 size_t sourceDepth,
118 const uint8_t *sourceData,
119 size_t sourceRowPitch,
120 size_t sourceDepthPitch,
121 uint8_t *destData,
122 size_t destRowPitch,
123 size_t destDepthPitch);
124
Jamie Madilld2b50a02016-06-09 00:13:35 -0700125typedef void (*ColorReadFunction)(const uint8_t *source, uint8_t *dest);
126typedef void (*ColorWriteFunction)(const uint8_t *source, uint8_t *dest);
127typedef void (*ColorCopyFunction)(const uint8_t *source, uint8_t *dest);
128
Jamie Madill4f57e5f2016-10-27 17:36:53 -0400129class FastCopyFunctionMap
130{
131 public:
132 struct Entry
133 {
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400134 angle::FormatID formatID;
Jamie Madill4f57e5f2016-10-27 17:36:53 -0400135 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
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400142 bool has(angle::FormatID formatID) const;
143 ColorCopyFunction get(angle::FormatID formatID) const;
Jamie Madill4f57e5f2016-10-27 17:36:53 -0400144
145 private:
146 size_t mSize;
147 const Entry *mData;
148};
Jamie Madilld2b50a02016-06-09 00:13:35 -0700149
Corentin Wallezcda6af12017-10-30 19:20:37 -0400150struct PackPixelsParams
Jamie Madilld2b50a02016-06-09 00:13:35 -0700151{
152 PackPixelsParams();
153 PackPixelsParams(const gl::Rectangle &area,
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400154 const angle::Format &destFormat,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700155 GLuint outputPitch,
156 const gl::PixelPackState &pack,
Corentin Wallezcda6af12017-10-30 19:20:37 -0400157 gl::Buffer *packBufferIn,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700158 ptrdiff_t offset);
159
160 gl::Rectangle area;
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400161 const angle::Format *destFormat;
Jamie Madilld2b50a02016-06-09 00:13:35 -0700162 GLuint outputPitch;
163 gl::Buffer *packBuffer;
164 gl::PixelPackState pack;
165 ptrdiff_t offset;
166};
167
168void PackPixels(const PackPixelsParams &params,
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400169 const angle::Format &sourceFormat,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700170 int inputPitch,
171 const uint8_t *source,
172 uint8_t *destination);
173
Jamie Madillabaab232017-01-10 12:37:37 -0500174using InitializeTextureDataFunction = void (*)(size_t width,
175 size_t height,
176 size_t depth,
177 uint8_t *output,
178 size_t outputRowPitch,
179 size_t outputDepthPitch);
180
Jamie Madillb2e48632016-08-09 18:08:03 -0400181using LoadImageFunction = void (*)(size_t width,
182 size_t height,
183 size_t depth,
184 const uint8_t *input,
185 size_t inputRowPitch,
186 size_t inputDepthPitch,
187 uint8_t *output,
188 size_t outputRowPitch,
189 size_t outputDepthPitch);
190
191struct LoadImageFunctionInfo
192{
193 LoadImageFunctionInfo() : loadFunction(nullptr), requiresConversion(false) {}
194 LoadImageFunctionInfo(LoadImageFunction loadFunction, bool requiresConversion)
195 : loadFunction(loadFunction), requiresConversion(requiresConversion)
196 {
197 }
198
199 LoadImageFunction loadFunction;
200 bool requiresConversion;
201};
202
203using LoadFunctionMap = LoadImageFunctionInfo (*)(GLenum);
204
Jamie Madill222c5172017-07-19 16:15:42 -0400205bool ShouldUseDebugLayers(const egl::AttributeMap &attribs);
Geoff Lang24ddc7a2018-06-11 14:56:34 -0400206bool ShouldUseVirtualizedContexts(const egl::AttributeMap &attribs, bool defaultValue);
Jamie Madill222c5172017-07-19 16:15:42 -0400207
Geoff Langd93cd6c2017-08-11 16:32:41 -0400208void CopyImageCHROMIUM(const uint8_t *sourceData,
209 size_t sourceRowPitch,
210 size_t sourcePixelBytes,
211 ColorReadFunction readFunction,
212 uint8_t *destData,
213 size_t destRowPitch,
214 size_t destPixelBytes,
215 ColorWriteFunction colorWriteFunction,
216 GLenum destUnsizedFormat,
217 GLenum destComponentType,
218 size_t width,
219 size_t height,
220 bool unpackFlipY,
221 bool unpackPremultiplyAlpha,
222 bool unpackUnmultiplyAlpha);
223
Jamie Madill42975642017-10-12 12:31:51 -0400224// Incomplete textures are 1x1 textures filled with black, used when samplers are incomplete.
225// This helper class encapsulates handling incomplete textures. Because the GL back-end
226// can take advantage of the driver's incomplete textures, and because clearing multisample
227// textures is so difficult, we can keep an instance of this class in the back-end instead
228// of moving the logic to the Context front-end.
229
230// This interface allows us to call-back to init a multisample texture.
231class MultisampleTextureInitializer
232{
233 public:
Jamie Madill5ad26402017-10-17 15:32:06 -0400234 virtual ~MultisampleTextureInitializer() {}
Jamie Madill42975642017-10-12 12:31:51 -0400235 virtual gl::Error initializeMultisampleTextureToBlack(const gl::Context *context,
236 gl::Texture *glTexture) = 0;
237};
238
239class IncompleteTextureSet final : angle::NonCopyable
240{
241 public:
242 IncompleteTextureSet();
243 ~IncompleteTextureSet();
244
245 void onDestroy(const gl::Context *context);
246
247 gl::Error getIncompleteTexture(const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -0500248 gl::TextureType type,
Jamie Madill42975642017-10-12 12:31:51 -0400249 MultisampleTextureInitializer *multisampleInitializer,
250 gl::Texture **textureOut);
251
252 private:
Corentin Wallez99d492c2018-02-27 15:17:10 -0500253 gl::TextureMap mIncompleteTextures;
Jamie Madill42975642017-10-12 12:31:51 -0400254};
255
Luc Ferron46bcea52018-05-31 09:48:36 -0400256// The return value indicate if the data was updated or not.
257template <int cols, int rows>
258bool SetFloatUniformMatrix(unsigned int arrayElementOffset,
259 unsigned int elementCount,
260 GLsizei countIn,
261 GLboolean transpose,
262 const GLfloat *value,
263 uint8_t *targetData);
264
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400265// Helper method to de-tranpose a matrix uniform for an API query.
266void GetMatrixUniform(GLenum type, GLfloat *dataOut, const GLfloat *source, bool transpose);
267
268template <typename NonFloatT>
269void GetMatrixUniform(GLenum type, NonFloatT *dataOut, const NonFloatT *source, bool transpose);
270
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400271const angle::Format &GetFormatFromFormatType(GLenum format, GLenum type);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700272} // namespace rx
273
274#endif // LIBANGLE_RENDERER_RENDERER_UTILS_H_