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
|
/*
* Copyright 2009, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// This file implements the render state-related GAPI functions on GL.
#include "command_buffer/common/cross/cmd_buffer_format.h"
#include "command_buffer/service/cross/gl/gapi_gl.h"
namespace o3d {
namespace command_buffer {
namespace {
GLenum kGLPolygonModes[] = {
GL_POINT,
GL_LINE,
GL_FILL,
};
COMPILE_ASSERT(command_buffer::kNumPolygonMode == arraysize(kGLPolygonModes),
kGLPolygonModes_does_not_match_command_buffer_PolygonMode);
GLenum kGLComparison[] = {
GL_NEVER,
GL_LESS,
GL_EQUAL,
GL_LEQUAL,
GL_GREATER,
GL_NOTEQUAL,
GL_GEQUAL,
GL_ALWAYS,
};
COMPILE_ASSERT(command_buffer::kNumComparison == arraysize(kGLComparison),
kGLComparison_does_not_match_command_buffer_Comparison);
GLenum kGLBlendFunc[] = {
GL_ZERO,
GL_ONE,
GL_SRC_COLOR,
GL_ONE_MINUS_SRC_COLOR,
GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA,
GL_DST_ALPHA,
GL_ONE_MINUS_DST_ALPHA,
GL_DST_COLOR,
GL_ONE_MINUS_DST_COLOR,
GL_SRC_ALPHA_SATURATE,
GL_CONSTANT_COLOR,
GL_ONE_MINUS_CONSTANT_COLOR,
};
COMPILE_ASSERT(command_buffer::kNumBlendFunc == arraysize(kGLBlendFunc),
kGLBlendFunc_does_not_match_command_buffer_BlendFunc);
GLenum kGLBlendEq[] = {
GL_FUNC_ADD,
GL_FUNC_SUBTRACT,
GL_FUNC_REVERSE_SUBTRACT,
GL_MIN,
GL_MAX,
};
COMPILE_ASSERT(command_buffer::kNumBlendEq == arraysize(kGLBlendEq),
kGLBlendEq_does_not_match_command_buffer_BlendEq);
GLenum kGLStencilOp[] = {
GL_KEEP,
GL_ZERO,
GL_REPLACE,
GL_INCR,
GL_DECR,
GL_INVERT,
GL_INCR_WRAP,
GL_DECR_WRAP,
};
COMPILE_ASSERT(command_buffer::kNumStencilOp == arraysize(kGLStencilOp),
kGLStencilOp_does_not_match_command_buffer_StencilOp);
// Check that the definition of the counter-clockwise func/ops match the
// clockwise ones, just shifted by 16 bits, so that we can use
// DecodeStencilFuncOps on both of them.
#define CHECK_CCW_MATCHES_CW(FIELD) \
COMPILE_ASSERT(cmd::SetStencilTest::CW ## FIELD::kLength == \
cmd::SetStencilTest::CCW ## FIELD::kLength, \
CCW ## FIELD ## _length_does_not_match_ ## CW ## FIELD); \
COMPILE_ASSERT(cmd::SetStencilTest::CW ## FIELD::kShift + 16 == \
cmd::SetStencilTest::CCW ## FIELD::kShift, \
CCW ## FIELD ## _shift_does_not_match_ ## CW ## FIELD)
CHECK_CCW_MATCHES_CW(Func);
CHECK_CCW_MATCHES_CW(PassOp);
CHECK_CCW_MATCHES_CW(FailOp);
CHECK_CCW_MATCHES_CW(ZFailOp);
#undef CHECK_CCW_MATCHES_CW
// Decodes stencil test function and operations from the bitfield.
void DecodeStencilFuncOps(Uint32 params,
GLenum *func,
GLenum *pass,
GLenum *fail,
GLenum *zfail) {
// Sanity check. The value has already been tested in
// GAPIDecoder::DecodeSetStencilTest in gapi_decoder.cc.
DCHECK_EQ(cmd::SetStencilTest::Unused1::Get(params), 0);
// Check that the bitmask get cannot generate values outside of the allowed
// range.
COMPILE_ASSERT(cmd::SetStencilTest::CWFunc::kMask <
command_buffer::kNumComparison,
set_stencil_test_CWFunc_may_produce_invalid_values);
*func = kGLComparison[cmd::SetStencilTest::CWFunc::Get(params)];
COMPILE_ASSERT(cmd::SetStencilTest::CWPassOp::kMask <
command_buffer::kNumStencilOp,
set_stencil_test_CWPassOp_may_produce_invalid_values);
*pass = kGLStencilOp[cmd::SetStencilTest::CWPassOp::Get(params)];
COMPILE_ASSERT(cmd::SetStencilTest::CWFailOp::kMask <
command_buffer::kNumStencilOp,
set_stencil_test_CWFailOp_may_produce_invalid_values);
*fail = kGLStencilOp[cmd::SetStencilTest::CWFailOp::Get(params)];
COMPILE_ASSERT(cmd::SetStencilTest::CWZFailOp::kMask <
command_buffer::kNumStencilOp,
set_stencil_test_CWZFailOp_may_produce_invalid_values);
*zfail = kGLStencilOp[cmd::SetStencilTest::CWZFailOp::Get(params)];
}
} // anonymous namespace
void GAPIGL::SetViewport(unsigned int x,
unsigned int y,
unsigned int width,
unsigned int height,
float z_min,
float z_max) {
glViewport(x, y, width, height);
glDepthRange(z_min, z_max);
// Update the helper constant used for the D3D -> GL remapping.
// See effect_gl.cc for details.
glProgramEnvParameter4fARB(GL_VERTEX_PROGRAM_ARB, 0,
1.f / width, 1.f / height, 2.f, 0.f);
CHECK_GL_ERROR();
}
void GAPIGL::SetScissor(bool enable,
unsigned int x,
unsigned int y,
unsigned int width,
unsigned int height) {
if (enable) {
glEnable(GL_SCISSOR_TEST);
glScissor(x, y, width, height);
} else {
glDisable(GL_SCISSOR_TEST);
}
}
void GAPIGL::SetPointLineRaster(bool line_smooth,
bool point_sprite,
float point_size) {
if (line_smooth) {
glEnable(GL_LINE_SMOOTH);
} else {
glDisable(GL_LINE_SMOOTH);
}
if (point_sprite) {
glEnable(GL_POINT_SPRITE);
// TODO: check which TC gets affected by point sprites in D3D.
glActiveTextureARB(GL_TEXTURE0);
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
} else {
glActiveTextureARB(GL_TEXTURE0);
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_FALSE);
glDisable(GL_POINT_SPRITE);
}
glPointSize(point_size);
}
void GAPIGL::SetPolygonOffset(float slope_factor, float units) {
glPolygonOffset(slope_factor, units);
}
void GAPIGL::SetPolygonRaster(PolygonMode fill_mode,
FaceCullMode cull_mode) {
DCHECK_LT(fill_mode, kNumPolygonMode);
glPolygonMode(GL_FRONT_AND_BACK, kGLPolygonModes[fill_mode]);
DCHECK_LT(cull_mode, kNumFaceCullMode);
switch (cull_mode) {
case kCullCW:
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
break;
case kCullCCW:
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
break;
default:
glDisable(GL_CULL_FACE);
break;
}
}
void GAPIGL::SetAlphaTest(bool enable,
float reference,
Comparison comp) {
DCHECK_LT(comp, kNumComparison);
if (enable) {
glEnable(GL_ALPHA_TEST);
glAlphaFunc(kGLComparison[comp], reference);
} else {
glDisable(GL_ALPHA_TEST);
}
}
void GAPIGL::SetDepthTest(bool enable,
bool write_enable,
Comparison comp) {
DCHECK_LT(comp, kNumComparison);
if (enable) {
glEnable(GL_DEPTH_TEST);
glDepthFunc(kGLComparison[comp]);
} else {
glDisable(GL_DEPTH_TEST);
}
glDepthMask(write_enable);
}
void GAPIGL::SetStencilTest(bool enable,
bool separate_ccw,
unsigned int write_mask,
unsigned int compare_mask,
unsigned int ref,
Uint32 func_ops) {
if (enable) {
glEnable(GL_STENCIL_TEST);
glStencilMask(write_mask);
GLenum func;
GLenum pass;
GLenum fail;
GLenum zfail;
DecodeStencilFuncOps(func_ops, &func, &pass, &fail, &zfail);
if (separate_ccw) {
glStencilFuncSeparate(GL_FRONT, func, ref, compare_mask);
glStencilOpSeparate(GL_FRONT, pass, fail, zfail);
// Extract upper 16 bits.
Uint32 ccw_func_ops = BitField<16, 16>::Get(func_ops);
GLenum ccw_func;
GLenum ccw_pass;
GLenum ccw_fail;
GLenum ccw_zfail;
DecodeStencilFuncOps(ccw_func_ops, &ccw_func, &ccw_pass, &ccw_fail,
&ccw_zfail);
glStencilFuncSeparate(GL_BACK, ccw_func, ref, compare_mask);
glStencilOpSeparate(GL_BACK, ccw_pass, ccw_fail, ccw_zfail);
} else {
glStencilFunc(func, ref, compare_mask);
glStencilOp(pass, fail, zfail);
}
} else {
glDisable(GL_STENCIL_TEST);
}
}
void GAPIGL::SetColorWrite(bool red,
bool green,
bool blue,
bool alpha,
bool dither) {
glColorMask(red, green, blue, alpha);
if (dither) {
glEnable(GL_DITHER);
} else {
glDisable(GL_DITHER);
}
}
void GAPIGL::SetBlending(bool enable,
bool separate_alpha,
BlendEq color_eq,
BlendFunc color_src_func,
BlendFunc color_dst_func,
BlendEq alpha_eq,
BlendFunc alpha_src_func,
BlendFunc alpha_dst_func) {
DCHECK_LT(color_eq, kNumBlendEq);
DCHECK_LT(color_src_func, kNumBlendFunc);
DCHECK_LT(color_dst_func, kNumBlendFunc);
DCHECK_LT(alpha_eq, kNumBlendEq);
DCHECK_LT(alpha_src_func, kNumBlendFunc);
DCHECK_LT(alpha_dst_func, kNumBlendFunc);
if (enable) {
glEnable(GL_BLEND);
GLenum gl_color_eq = kGLBlendEq[color_eq];
GLenum gl_color_src_func = kGLBlendFunc[color_src_func];
GLenum gl_color_dst_func = kGLBlendFunc[color_dst_func];
if (separate_alpha) {
GLenum gl_alpha_eq = kGLBlendEq[alpha_eq];
GLenum gl_alpha_src_func = kGLBlendFunc[alpha_src_func];
GLenum gl_alpha_dst_func = kGLBlendFunc[alpha_dst_func];
glBlendFuncSeparate(gl_color_src_func, gl_color_dst_func,
gl_alpha_src_func, gl_alpha_dst_func);
glBlendEquationSeparate(gl_color_eq, gl_alpha_eq);
} else {
glBlendFunc(gl_color_src_func, gl_color_dst_func);
glBlendEquation(gl_color_eq);
}
} else {
glDisable(GL_BLEND);
}
}
void GAPIGL::SetBlendingColor(const RGBA &color) {
glBlendColor(color.red, color.green, color.blue, color.alpha);
}
} // namespace command_buffer
} // namespace o3d
|