blob: 78444cb19c70d118ca5991adcb5708c7d5f1b31c [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 Madill222c5172017-07-19 16:15:42 -040024} // namespace angle
Jamie Madill86e0b7f2016-08-09 11:10:29 -040025
Jamie Madilld2b50a02016-06-09 00:13:35 -070026namespace gl
27{
28struct FormatType;
29struct InternalFormat;
Jamie Madill222c5172017-07-19 16:15:42 -040030} // namespace gl
31
32namespace egl
33{
34class AttributeMap;
35} // namespace egl
Jamie Madilld2b50a02016-06-09 00:13:35 -070036
37namespace rx
38{
39
Jamie Madilldcc9b512017-06-05 15:28:45 -040040class ResourceSerial
41{
42 public:
43 constexpr ResourceSerial() : mValue(kDirty) {}
Jamie Madill9959f542017-09-12 15:22:56 -040044 explicit constexpr ResourceSerial(uintptr_t value) : mValue(value) {}
Jamie Madilldcc9b512017-06-05 15:28:45 -040045 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 Madill9959f542017-09-12 15:22:56 -040049 void clear() { mValue = kEmpty; }
50
51 constexpr bool valid() const { return mValue != kEmpty && mValue != kDirty; }
52 constexpr bool empty() const { return mValue == kEmpty; }
Jamie Madilldcc9b512017-06-05 15:28:45 -040053
54 private:
55 constexpr static uintptr_t kDirty = std::numeric_limits<uintptr_t>::max();
Jamie Madill9959f542017-09-12 15:22:56 -040056 constexpr static uintptr_t kEmpty = 0;
Jamie Madilldcc9b512017-06-05 15:28:45 -040057
58 uintptr_t mValue;
59};
60
Jamie Madillfb05bcb2017-06-07 15:43:18 -040061class SerialFactory;
62
63class Serial final
64{
65 public:
Jamie Madill0e7f1732017-09-09 23:32:50 -040066 constexpr Serial() : mValue(kInvalid) {}
Jamie Madillfb05bcb2017-06-07 15:43:18 -040067 constexpr Serial(const Serial &other) = default;
68 Serial &operator=(const Serial &other) = default;
69
Jamie Madill0e7f1732017-09-09 23:32:50 -040070 constexpr bool operator==(const Serial &other) const
71 {
72 return mValue != kInvalid && mValue == other.mValue;
73 }
Jamie Madillf2f6d372018-01-10 21:37:23 -050074 constexpr bool operator==(uint32_t value) const
75 {
76 return mValue != kInvalid && mValue == static_cast<uint64_t>(value);
77 }
Jamie Madill0e7f1732017-09-09 23:32:50 -040078 constexpr bool operator!=(const Serial &other) const
79 {
80 return mValue == kInvalid || mValue != other.mValue;
81 }
Jamie Madillfb05bcb2017-06-07 15:43:18 -040082 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 Madillf2f6d372018-01-10 21:37:23 -050087 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 Madillfb05bcb2017-06-07 15:43:18 -040092 private:
93 friend class SerialFactory;
94 constexpr explicit Serial(uint64_t value) : mValue(value) {}
95 uint64_t mValue;
Jamie Madill0e7f1732017-09-09 23:32:50 -040096 static constexpr uint64_t kInvalid = 0;
Jamie Madillfb05bcb2017-06-07 15:43:18 -040097};
98
99class 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 Madilla5b15612016-08-09 11:10:27 -0400114using 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 Madilld2b50a02016-06-09 00:13:35 -0700124typedef void (*ColorReadFunction)(const uint8_t *source, uint8_t *dest);
125typedef void (*ColorWriteFunction)(const uint8_t *source, uint8_t *dest);
126typedef void (*ColorCopyFunction)(const uint8_t *source, uint8_t *dest);
127
Jamie Madill4f57e5f2016-10-27 17:36:53 -0400128class 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 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,
154 GLenum format,
155 GLenum type,
156 GLuint outputPitch,
157 const gl::PixelPackState &pack,
Corentin Wallezcda6af12017-10-30 19:20:37 -0400158 gl::Buffer *packBufferIn,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700159 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
170void PackPixels(const PackPixelsParams &params,
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400171 const angle::Format &sourceFormat,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700172 int inputPitch,
173 const uint8_t *source,
174 uint8_t *destination);
175
176ColorWriteFunction GetColorWriteFunction(const gl::FormatType &formatType);
177ColorCopyFunction GetFastCopyFunction(const FastCopyFunctionMap &fastCopyFunctions,
178 const gl::FormatType &formatType);
179
Jamie Madillabaab232017-01-10 12:37:37 -0500180using 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 Madillb2e48632016-08-09 18:08:03 -0400187using 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
197struct 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
209using LoadFunctionMap = LoadImageFunctionInfo (*)(GLenum);
210
Jamie Madill222c5172017-07-19 16:15:42 -0400211bool ShouldUseDebugLayers(const egl::AttributeMap &attribs);
Geoff Lang24ddc7a2018-06-11 14:56:34 -0400212bool ShouldUseVirtualizedContexts(const egl::AttributeMap &attribs, bool defaultValue);
Jamie Madill222c5172017-07-19 16:15:42 -0400213
Geoff Langd93cd6c2017-08-11 16:32:41 -0400214void CopyImageCHROMIUM(const uint8_t *sourceData,
215 size_t sourceRowPitch,
216 size_t sourcePixelBytes,
217 ColorReadFunction readFunction,
218 uint8_t *destData,
219 size_t destRowPitch,
220 size_t destPixelBytes,
221 ColorWriteFunction colorWriteFunction,
222 GLenum destUnsizedFormat,
223 GLenum destComponentType,
224 size_t width,
225 size_t height,
226 bool unpackFlipY,
227 bool unpackPremultiplyAlpha,
228 bool unpackUnmultiplyAlpha);
229
Jamie Madill42975642017-10-12 12:31:51 -0400230// Incomplete textures are 1x1 textures filled with black, used when samplers are incomplete.
231// This helper class encapsulates handling incomplete textures. Because the GL back-end
232// can take advantage of the driver's incomplete textures, and because clearing multisample
233// textures is so difficult, we can keep an instance of this class in the back-end instead
234// of moving the logic to the Context front-end.
235
236// This interface allows us to call-back to init a multisample texture.
237class MultisampleTextureInitializer
238{
239 public:
Jamie Madill5ad26402017-10-17 15:32:06 -0400240 virtual ~MultisampleTextureInitializer() {}
Jamie Madill42975642017-10-12 12:31:51 -0400241 virtual gl::Error initializeMultisampleTextureToBlack(const gl::Context *context,
242 gl::Texture *glTexture) = 0;
243};
244
245class IncompleteTextureSet final : angle::NonCopyable
246{
247 public:
248 IncompleteTextureSet();
249 ~IncompleteTextureSet();
250
251 void onDestroy(const gl::Context *context);
252
253 gl::Error getIncompleteTexture(const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -0500254 gl::TextureType type,
Jamie Madill42975642017-10-12 12:31:51 -0400255 MultisampleTextureInitializer *multisampleInitializer,
256 gl::Texture **textureOut);
257
258 private:
Corentin Wallez99d492c2018-02-27 15:17:10 -0500259 gl::TextureMap mIncompleteTextures;
Jamie Madill42975642017-10-12 12:31:51 -0400260};
261
Luc Ferron46bcea52018-05-31 09:48:36 -0400262// The return value indicate if the data was updated or not.
263template <int cols, int rows>
264bool SetFloatUniformMatrix(unsigned int arrayElementOffset,
265 unsigned int elementCount,
266 GLsizei countIn,
267 GLboolean transpose,
268 const GLfloat *value,
269 uint8_t *targetData);
270
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400271// Helper method to de-tranpose a matrix uniform for an API query.
272void GetMatrixUniform(GLenum type, GLfloat *dataOut, const GLfloat *source, bool transpose);
273
274template <typename NonFloatT>
275void GetMatrixUniform(GLenum type, NonFloatT *dataOut, const NonFloatT *source, bool transpose);
276
Jamie Madilld2b50a02016-06-09 00:13:35 -0700277} // namespace rx
278
279#endif // LIBANGLE_RENDERER_RENDERER_UTILS_H_