File: AttribState.hpp

package info (click to toggle)
spring 105.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,860 kB
  • sloc: cpp: 467,785; ansic: 302,607; python: 12,925; java: 12,201; awk: 5,889; sh: 2,371; xml: 655; perl: 405; php: 276; objc: 194; makefile: 75; sed: 2
file content (401 lines) | stat: -rw-r--r-- 12,565 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef GL_ATTRIB_STATE_H
#define GL_ATTRIB_STATE_H

#include <array>
#include <cstdint>
#include <cstddef>

namespace GL {
	struct AttribState {
	public:
		AttribState();

		void Init();

		void Enable(uint32_t attrib); // GLenum
		void Disable(uint32_t attrib);

		void EnableDepthMask  ();
		void EnableDepthClamp ();
		void EnableDepthTest  ();
		void EnableAlphaTest  ();
		void EnableBlendMask  ();
		void EnableScissorTest();
		void EnableStencilTest();
		void EnableCullFace   ();
		void EnablePolyOfsFill();
		void EnablePolyOfsPoint();
		void EnablePolyOfsLine();

		void DisableDepthMask  ();
		void DisableDepthClamp ();
		void DisableDepthTest  ();
		void DisableAlphaTest  ();
		void DisableBlendMask  ();
		void DisableScissorTest();
		void DisableStencilTest();
		void DisableCullFace   ();
		void DisablePolyOfsFill();
		void DisablePolyOfsPoint();
		void DisablePolyOfsLine();


		void PushAllBits();
		void PushBits(uint32_t attribBits);
		void PopBits();

		void PushEnableBit();
		void PushCurrentBit();
		void PushPolygonBit();
		void PushColorBufferBit();
		void PushDepthBufferBit();
		void PushStencilBufferBit();
		void PushTextureBit();
		void PushViewPortBit();
		void PushScissorBit();


		void DepthRange(float zn, float zf);
		void PushDepthRange(float zn, float zf);
		void PushDepthRange() { PushDepthRange(depthRangeStack.Top()); }
		void PopDepthRange();

		// depth-clamp (GLboolean)
		void DepthClamp(bool enable);
		void PushDepthClamp(bool enable);
		void PushDepthClamp() { PushDepthClamp(depthClampStack.Top()); }
		void PopDepthClamp();

		// depth-test (GLboolean)
		void DepthTest(bool enable);
		void PushDepthTest(bool enable);
		void PushDepthTest() { PushDepthTest(depthTestStack.Top()); }
		void PopDepthTest();

		// depth-mask (GLboolean)
		void DepthMask(bool enable);
		void PushDepthMask(bool enable);
		void PushDepthMask() { PushDepthMask(depthMaskStack.Top()); }
		void PopDepthMask();

		// depth-func (GLenum)
		void DepthFunc(uint32_t func);
		void PushDepthFunc(uint32_t func);
		void PushDepthFunc() { PushDepthFunc(depthFuncStack.Top()); }
		void PopDepthFunc();

		// alpha-test (GLboolean)
		void AlphaTest(bool enable);
		void PushAlphaTest(bool enable);
		void PushAlphaTest() { PushAlphaTest(alphaTestStack.Top()); }
		void PopAlphaTest();

		// alpha-func (GLenum, GLfloat)
		void AlphaFunc(uint32_t func, float rval);
		void PushAlphaFunc(uint32_t func, float rval);
		void PushAlphaFunc() { PushAlphaFunc(alphaFuncStack.Top()); }
		void PopAlphaFunc();

		// blend-func (GLenum, GLenum)
		void BlendFunc(uint32_t srcFac, uint32_t dstFac);
		void PushBlendFunc(uint32_t srcFac, uint32_t dstFac);
		void PushBlendFunc() { PushBlendFunc(blendFuncStack.Top()); }
		void PopBlendFunc();

		// blending flag (GLboolean)
		void BlendMask(bool enable);
		void PushBlendMask(bool enable);
		void PushBlendMask() { PushBlendMask(blendMaskStack.Top()); }
		void PopBlendMask();

		// scissor-test (GLboolean)
		void ScissorTest(bool enable);
		void PushScissorTest(bool enable);
		void PushScissorTest() { PushScissorTest(scissorTestStack.Top()); }
		void PopScissorTest();

		// stencil-test (GLboolean)
		void StencilTest(bool enable);
		void PushStencilTest(bool enable);
		void PushStencilTest() { PushStencilTest(stencilTestStack.Top()); }
		void PopStencilTest();

		// stencil-mask (GLuint)
		void StencilMask(uint32_t mask);
		void PushStencilMask(uint32_t mask);
		void PushStencilMask() { PushStencilMask(stencilMaskStack.Top()); }
		void PopStencilMask();

		// stencil-func (GLenum, GLint, GLuint)
		void StencilFunc(uint32_t func, int32_t rval, uint32_t mask);
		void PushStencilFunc(uint32_t func, int32_t rval, uint32_t mask);
		void PushStencilFunc() { PushStencilFunc(stencilFuncStack.Top()); }
		void PopStencilFunc();

		// stencil-op (GLenum, GLenum, GLenum)
		void StencilOper(uint32_t sfail, uint32_t zfail, uint32_t zpass);
		void PushStencilOper(uint32_t sfail, uint32_t zfail, uint32_t zpass);
		void PushStencilOper() { PushStencilOper(stencilOperStack.Top()); }
		void PopStencilOper();

		// polygon-mode (GLenum, GLenum)
		void PolygonMode(uint32_t side, uint32_t mode);
		void PushPolygonMode(uint32_t side, uint32_t mode);
		void PushPolygonMode() { PushPolygonMode(polyModeStack.Top()); }
		void PopPolygonMode();

		// polygon-offset params (GLfloat, GLfloat)
		void PolygonOffset(float factor, float units);
		void PushPolygonOffset(float factor, float units);
		void PushPolygonOffset() { PushPolygonOffset(pofsCtrlStack.Top()); }
		void PopPolygonOffset();

		// polygon-offset {fill,point,line} flag (GLenum, GLboolean)
		#if 0
		void PolygonOffsetType(uint32_t type, bool enable);
		void PushPolygonOffsetType(uint32_t type, bool enable);
		void PopPolygonOffsetType(uint32_t type);
		#else
		void PolygonOffsetFill(bool enable);
		void PushPolygonOffsetFill(bool enable);
		void PushPolygonOffsetFill() { PushPolygonOffsetFill(pofsFlagStack[0].Top()); }
		void PopPolygonOffsetFill();

		void PolygonOffsetPoint(bool enable);
		void PushPolygonOffsetPoint(bool enable);
		void PushPolygonOffsetPoint() { PushPolygonOffsetPoint(pofsFlagStack[1].Top()); }
		void PopPolygonOffsetPoint();

		void PolygonOffsetLine(bool enable);
		void PushPolygonOffsetLine(bool enable);
		void PushPolygonOffsetLine() { PushPolygonOffsetLine(pofsFlagStack[2].Top()); }
		void PopPolygonOffsetLine();
		#endif

		// viewport (GLint)
		void ViewPort(int32_t x, int32_t y, int32_t w, int32_t h);
		void PushViewPort(int32_t x, int32_t y, int32_t w, int32_t h);
		void PushViewPort() { PushViewPort(viewportStack.Top()); }
		void PopViewPort();

		// scissor (GLint)
		void Scissor(int32_t x, int32_t y, int32_t w, int32_t h);
		void PushScissor(int32_t x, int32_t y, int32_t w, int32_t h);
		void PushScissor() { PushScissor(scissorStack.Top()); }
		void PopScissor();

		// front-face (GLuint)
		void FrontFace(uint32_t face);
		void PushFrontFace(uint32_t face);
		void PushFrontFace() { PushFrontFace(frontFaceStack.Top()); }
		void PopFrontFace();

		// cull-face (GLenum)
		void CullFace(uint32_t face);
		void PushCullFace(uint32_t face);
		void PushCullFace() { PushCullFace(cullFaceStack.Top()); }
		void PopCullFace();

		// culling flag (GLboolean)
		void CullFlag(bool enable);
		void PushCullFlag(bool enable);
		void PushCullFlag() { PushCullFlag(cullFlagStack.Top()); }
		void PopCullFlag();

		// blend-color (GLfloat)
		void BlendColor(float r, float g, float b, float a);
		void PushBlendColor(float r, float g, float b, float a);
		void PushBlendColor() { PushBlendColor(blendColorStack.Top()); }
		void PopBlendColor();

		// color-mask (GLboolean)
		void ColorMask(bool r, bool g, bool b, bool a);
		void PushColorMask(bool r, bool g, bool b, bool a);
		void PushColorMask() { PushColorMask(colorMaskStack.Top()); }
		void PopColorMask();

		// uncaptured state
		void Clear(uint32_t bits);
		void ClearAccum(float r, float g, float b, float a);
		void ClearColor(float r, float g, float b, float a);
		void ClearColor(const float* c) { ClearColor(c[0], c[1], c[2], c[3]); }
		void ClearDepth(float depth);
		void ClearStencil(uint32_t rval);

	private:
		struct DepthRangeState {
			float zn;
			float zf;
		};
		struct AlphaFuncState {
			uint32_t func;
			float rval;
		};
		struct BlendFuncState {
			uint32_t srcFac;
			uint32_t dstFac;
		};
		struct BlendColorState {
			float r;
			float g;
			float b;
			float a;
		};
		struct ColorMaskState {
			uint32_t r;
			uint32_t g;
			uint32_t b;
			uint32_t a;
		};
		struct StencilFuncState {
			uint32_t func;
			 int32_t rval;
			uint32_t mask;
		};
		struct StencilOperState {
			uint32_t sfail;
			uint32_t zfail;
			uint32_t zpass;
		};
		struct PolyModeState {
			uint32_t side;
			uint32_t mode;
		};
		struct PolyOffsetState {
			float factor;
			float units;
		};
		struct ViewPortState {
			int32_t x;
			int32_t y;
			int32_t w;
			int32_t h;
		};
		struct ScissorState {
			int32_t x;
			int32_t y;
			int32_t w;
			int32_t h;
		};

		void DepthRange(const DepthRangeState& v) { DepthRange(v.zn, v.zf); }
		void AlphaFunc(const AlphaFuncState& v) { AlphaFunc(v.func, v.rval); }
		void BlendFunc(const BlendFuncState& v) { BlendFunc(v.srcFac, v.dstFac); }
		void BlendColor(const BlendColorState& v) { BlendColor(v.r, v.g, v.b, v.a); }
		void ColorMask(const ColorMaskState& v) { ColorMask(v.r, v.g, v.b, v.a); }
		void StencilFunc(const StencilFuncState& v) { StencilFunc(v.func, v.rval, v.mask); }
		void StencilOper(const StencilOperState& v) { StencilOper(v.sfail, v.zfail, v.zpass); }
		void PolygonMode(const PolyModeState& v) { PolygonMode(v.side, v.mode); }
		void PolygonOffset(const PolyOffsetState& v) { PolygonOffset(v.factor, v.units); }
		void ViewPort(const ViewPortState& v) { ViewPort(v.x, v.y, v.w, v.h); }
		void Scissor(const ScissorState& v) { Scissor(v.x, v.y, v.w, v.h); }

		void PushDepthRange(const DepthRangeState& v) { PushDepthRange(v.zn, v.zf); }
		void PushAlphaFunc(const AlphaFuncState& v) { PushAlphaFunc(v.func, v.rval); }
		void PushBlendFunc(const BlendFuncState& v) { PushBlendFunc(v.srcFac, v.dstFac); }
		void PushBlendColor(const BlendColorState& v) { PushBlendColor(v.r, v.g, v.b, v.a); }
		void PushColorMask(const ColorMaskState& v) { PushColorMask(v.r, v.g, v.b, v.a); }
		void PushStencilFunc(const StencilFuncState& v) { PushStencilFunc(v.func, v.rval, v.mask); }
		void PushStencilOper(const StencilOperState& v) { PushStencilOper(v.sfail, v.zfail, v.zpass); }
		void PushPolygonMode(const PolyModeState& v) { PushPolygonMode(v.side, v.mode); }
		void PushPolygonOffset(const PolyOffsetState& v) { PushPolygonOffset(v.factor, v.units); }
		void PushViewPort(const ViewPortState& v) { PushViewPort(v.x, v.y, v.w, v.h); }
		void PushScissor(const ScissorState& v) { PushScissor(v.x, v.y, v.w, v.h); }

	private:
		template<typename T, size_t S> struct ArrayStack {
		static_assert((S & (S - 1)) == 0, "stack size must be a power of two");
		public:
			void Fill(const T& v) { stack.fill(v); }

			const T& Push(const T& v) {
				// return the top element post-push
				// assert(size < S);

				size += (size < S);
				// size &= (S - 1);

				return (stack[size - 1] = v);
			}
			const T& Pop(bool post) {
				// return the top element pre- or post-pop
				// assert(size > post);

				post &= (size > 1);
				size -= (size > 0);
				// size &= (S - 1);

				return stack[size - post];
			}

			#if 1
			const T& Top() const { return stack[(size - 1) & (S - 1)]; }
			      T& Top()       { return stack[(size - 1) & (S - 1)]; }
			#else
			const T& Top() const { return stack[size - 1]; }
			      T& Top()       { return stack[size - 1]; }
			#endif

		private:
			std::array<T, S> stack;

			uint8_t size = 0;
		};

		ArrayStack<uint32_t        , 64>  attribBitsStack;
		ArrayStack<DepthRangeState , 64>  depthRangeStack;
		ArrayStack<bool            , 64>  depthClampStack;
		ArrayStack<bool            , 64>   depthTestStack;
		ArrayStack<bool            , 64>   depthMaskStack;
		ArrayStack<uint32_t        , 64>   depthFuncStack;
		ArrayStack<bool            , 64>   alphaTestStack;
		ArrayStack<AlphaFuncState  , 64>   alphaFuncStack;
		ArrayStack<BlendFuncState  , 64>   blendFuncStack;
		ArrayStack<bool            , 64>   blendMaskStack;
		ArrayStack<bool            , 64> scissorTestStack;
		ArrayStack<bool            , 64> stencilTestStack;
		ArrayStack<uint32_t        , 64> stencilMaskStack;
		ArrayStack<StencilFuncState, 64> stencilFuncStack;
		ArrayStack<StencilOperState, 64> stencilOperStack;
		ArrayStack<PolyModeState   , 64>    polyModeStack;
		ArrayStack<PolyOffsetState , 64>    pofsCtrlStack;
		ArrayStack<bool            , 64>    pofsFlagStack[3];
		ArrayStack<uint32_t        , 64>   frontFaceStack;
		ArrayStack<uint32_t        , 64>    cullFaceStack;
		ArrayStack<bool            , 64>    cullFlagStack;
		ArrayStack<BlendColorState , 64>  blendColorStack;
		ArrayStack<ColorMaskState  , 64>   colorMaskStack;
		ArrayStack<ViewPortState   , 64>    viewportStack;
		ArrayStack< ScissorState   , 64>     scissorStack;
	};


	struct ScopedDepthFunc {
	public:
		ScopedDepthFunc(uint32_t func);
		~ScopedDepthFunc();

	private:
		uint32_t prevFunc = 0;
	};


	struct ScopedCullFace {
	public:
		ScopedCullFace(uint32_t face);
		~ScopedCullFace();

	private:
		uint32_t prevFace = 0;
	};


	AttribState* SetAttribStatePointer(bool mainThread);
	AttribState* GetAttribStatePointer();
}

#define glAttribStatePtr GL::GetAttribStatePointer()
#endif