blob: ca2b4466a5f23d204bb7451847c082899bc65901 [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 }
74 constexpr bool operator!=(const Serial &other) const
75 {
76 return mValue == kInvalid || mValue != other.mValue;
77 }
Jamie Madillfb05bcb2017-06-07 15:43:18 -040078 constexpr bool operator>(const Serial &other) const { return mValue > other.mValue; }
79 constexpr bool operator>=(const Serial &other) const { return mValue >= other.mValue; }
80 constexpr bool operator<(const Serial &other) const { return mValue < other.mValue; }
81 constexpr bool operator<=(const Serial &other) const { return mValue <= other.mValue; }
82
83 private:
84 friend class SerialFactory;
85 constexpr explicit Serial(uint64_t value) : mValue(value) {}
86 uint64_t mValue;
Jamie Madill0e7f1732017-09-09 23:32:50 -040087 static constexpr uint64_t kInvalid = 0;
Jamie Madillfb05bcb2017-06-07 15:43:18 -040088};
89
90class SerialFactory final : angle::NonCopyable
91{
92 public:
93 SerialFactory() : mSerial(1) {}
94
95 Serial generate()
96 {
97 ASSERT(mSerial != std::numeric_limits<uint64_t>::max());
98 return Serial(mSerial++);
99 }
100
101 private:
102 uint64_t mSerial;
103};
104
Jamie Madilla5b15612016-08-09 11:10:27 -0400105using MipGenerationFunction = void (*)(size_t sourceWidth,
106 size_t sourceHeight,
107 size_t sourceDepth,
108 const uint8_t *sourceData,
109 size_t sourceRowPitch,
110 size_t sourceDepthPitch,
111 uint8_t *destData,
112 size_t destRowPitch,
113 size_t destDepthPitch);
114
Jamie Madilld2b50a02016-06-09 00:13:35 -0700115typedef void (*ColorReadFunction)(const uint8_t *source, uint8_t *dest);
116typedef void (*ColorWriteFunction)(const uint8_t *source, uint8_t *dest);
117typedef void (*ColorCopyFunction)(const uint8_t *source, uint8_t *dest);
118
Jamie Madill4f57e5f2016-10-27 17:36:53 -0400119class FastCopyFunctionMap
120{
121 public:
122 struct Entry
123 {
124 GLenum format;
125 GLenum type;
126 ColorCopyFunction func;
127 };
128
129 constexpr FastCopyFunctionMap() : FastCopyFunctionMap(nullptr, 0) {}
130
131 constexpr FastCopyFunctionMap(const Entry *data, size_t size) : mSize(size), mData(data) {}
132
133 bool has(const gl::FormatType &formatType) const;
134 ColorCopyFunction get(const gl::FormatType &formatType) const;
135
136 private:
137 size_t mSize;
138 const Entry *mData;
139};
Jamie Madilld2b50a02016-06-09 00:13:35 -0700140
Jamie Madill4928b7c2017-06-20 12:57:39 -0400141struct PackPixelsParams : private angle::NonCopyable
Jamie Madilld2b50a02016-06-09 00:13:35 -0700142{
143 PackPixelsParams();
144 PackPixelsParams(const gl::Rectangle &area,
145 GLenum format,
146 GLenum type,
147 GLuint outputPitch,
148 const gl::PixelPackState &pack,
149 ptrdiff_t offset);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400150 PackPixelsParams(const gl::Context *context, const PackPixelsParams &other);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700151
152 gl::Rectangle area;
153 GLenum format;
154 GLenum type;
155 GLuint outputPitch;
156 gl::Buffer *packBuffer;
157 gl::PixelPackState pack;
158 ptrdiff_t offset;
159};
160
161void PackPixels(const PackPixelsParams &params,
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400162 const angle::Format &sourceFormat,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700163 int inputPitch,
164 const uint8_t *source,
165 uint8_t *destination);
166
167ColorWriteFunction GetColorWriteFunction(const gl::FormatType &formatType);
168ColorCopyFunction GetFastCopyFunction(const FastCopyFunctionMap &fastCopyFunctions,
169 const gl::FormatType &formatType);
170
Jamie Madillabaab232017-01-10 12:37:37 -0500171using InitializeTextureDataFunction = void (*)(size_t width,
172 size_t height,
173 size_t depth,
174 uint8_t *output,
175 size_t outputRowPitch,
176 size_t outputDepthPitch);
177
Jamie Madillb2e48632016-08-09 18:08:03 -0400178using LoadImageFunction = void (*)(size_t width,
179 size_t height,
180 size_t depth,
181 const uint8_t *input,
182 size_t inputRowPitch,
183 size_t inputDepthPitch,
184 uint8_t *output,
185 size_t outputRowPitch,
186 size_t outputDepthPitch);
187
188struct LoadImageFunctionInfo
189{
190 LoadImageFunctionInfo() : loadFunction(nullptr), requiresConversion(false) {}
191 LoadImageFunctionInfo(LoadImageFunction loadFunction, bool requiresConversion)
192 : loadFunction(loadFunction), requiresConversion(requiresConversion)
193 {
194 }
195
196 LoadImageFunction loadFunction;
197 bool requiresConversion;
198};
199
200using LoadFunctionMap = LoadImageFunctionInfo (*)(GLenum);
201
Jamie Madill222c5172017-07-19 16:15:42 -0400202bool ShouldUseDebugLayers(const egl::AttributeMap &attribs);
203
Geoff Langd93cd6c2017-08-11 16:32:41 -0400204void CopyImageCHROMIUM(const uint8_t *sourceData,
205 size_t sourceRowPitch,
206 size_t sourcePixelBytes,
207 ColorReadFunction readFunction,
208 uint8_t *destData,
209 size_t destRowPitch,
210 size_t destPixelBytes,
211 ColorWriteFunction colorWriteFunction,
212 GLenum destUnsizedFormat,
213 GLenum destComponentType,
214 size_t width,
215 size_t height,
216 bool unpackFlipY,
217 bool unpackPremultiplyAlpha,
218 bool unpackUnmultiplyAlpha);
219
Jamie Madill42975642017-10-12 12:31:51 -0400220// Incomplete textures are 1x1 textures filled with black, used when samplers are incomplete.
221// This helper class encapsulates handling incomplete textures. Because the GL back-end
222// can take advantage of the driver's incomplete textures, and because clearing multisample
223// textures is so difficult, we can keep an instance of this class in the back-end instead
224// of moving the logic to the Context front-end.
225
226// This interface allows us to call-back to init a multisample texture.
227class MultisampleTextureInitializer
228{
229 public:
Jamie Madill5ad26402017-10-17 15:32:06 -0400230 virtual ~MultisampleTextureInitializer() {}
Jamie Madill42975642017-10-12 12:31:51 -0400231 virtual gl::Error initializeMultisampleTextureToBlack(const gl::Context *context,
232 gl::Texture *glTexture) = 0;
233};
234
235class IncompleteTextureSet final : angle::NonCopyable
236{
237 public:
238 IncompleteTextureSet();
239 ~IncompleteTextureSet();
240
241 void onDestroy(const gl::Context *context);
242
243 gl::Error getIncompleteTexture(const gl::Context *context,
244 GLenum type,
245 MultisampleTextureInitializer *multisampleInitializer,
246 gl::Texture **textureOut);
247
248 private:
249 gl::TextureMap mIncompleteTextures;
250};
251
Jamie Madilld2b50a02016-06-09 00:13:35 -0700252} // namespace rx
253
254#endif // LIBANGLE_RENDERER_RENDERER_UTILS_H_