forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_color_buffer.py
More file actions
306 lines (239 loc) · 9.54 KB
/
test_color_buffer.py
File metadata and controls
306 lines (239 loc) · 9.54 KB
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
from panda3d import core
import pytest
TEST_COLOR = core.LColor(1, 127/255.0, 0, 127/255.0)
TEST_COLOR_SCALE = core.LVecBase4(0.5, 0.5, 0.5, 0.5)
TEST_SCALED_COLOR = core.LColor(TEST_COLOR)
TEST_SCALED_COLOR.componentwise_mult(TEST_COLOR_SCALE)
FUZZ = 0.02
@pytest.fixture(scope='session', params=[False, True], ids=["shader:off", "shader:auto"])
def shader_attrib(request):
"""Returns two ShaderAttribs: one with auto shader, one without."""
if request.param:
return core.ShaderAttrib.make_default().set_shader_auto(True)
else:
return core.ShaderAttrib.make_off()
@pytest.fixture(scope='session', params=["mat:off", "mat:empty", "mat:amb", "mat:diff", "mat:both"])
def material_attrib(request):
"""Returns two MaterialAttribs: one with material, one without. It
shouldn't really matter what we set them to, since the tests in here do
not use lighting, and therefore the material should be ignored."""
if request.param == "mat:off":
return core.MaterialAttrib.make_off()
elif request.param == "mat:empty":
return core.MaterialAttrib.make(core.Material())
elif request.param == "mat:amb":
mat = core.Material()
mat.ambient = (0.1, 1, 0.5, 1)
return core.MaterialAttrib.make(mat)
elif request.param == "mat:diff":
mat = core.Material()
mat.diffuse = (0.1, 1, 0.5, 1)
return core.MaterialAttrib.make(mat)
elif request.param == "mat:both":
mat = core.Material()
mat.diffuse = (0.1, 1, 0.5, 1)
mat.ambient = (0.1, 1, 0.5, 1)
return core.MaterialAttrib.make(mat)
@pytest.fixture(scope='module', params=[False, True], ids=["srgb:off", "srgb:on"])
def color_region(request, graphics_pipe):
"""Creates and returns a DisplayRegion with a depth buffer."""
engine = core.GraphicsEngine()
engine.set_threading_model("")
# Vulkan needs no host window.
if graphics_pipe.get_interface_name() != "Vulkan":
host_fbprops = core.FrameBufferProperties()
host_fbprops.force_hardware = True
host = engine.make_output(
graphics_pipe,
'host',
0,
host_fbprops,
core.WindowProperties.size(32, 32),
core.GraphicsPipe.BF_refuse_window,
)
engine.open_windows()
if host is None:
pytest.skip("GraphicsPipe cannot make offscreen buffers")
host_gsg = host.gsg
else:
host = None
host_gsg = None
fbprops = core.FrameBufferProperties()
fbprops.force_hardware = True
fbprops.set_rgba_bits(8, 8, 8, 8)
fbprops.srgb_color = request.param
buffer = engine.make_output(
graphics_pipe,
'buffer',
0,
fbprops,
core.WindowProperties.size(32, 32),
core.GraphicsPipe.BF_refuse_window,
host_gsg,
host
)
engine.open_windows()
if buffer is None:
pytest.skip("Cannot make color buffer")
if fbprops.srgb_color != buffer.get_fb_properties().srgb_color:
pytest.skip("Cannot make buffer with required srgb_color setting")
buffer.set_clear_color_active(True)
buffer.set_clear_color((0, 0, 0, 1))
yield buffer.make_display_region()
if buffer is not None:
engine.remove_window(buffer)
def render_color_pixel(region, state, vertex_color=None):
"""Renders a fragment using the specified render settings, and returns the
resulting color value."""
# Skip auto-shader tests if we don't support Cg shaders.
if not region.window.gsg.supports_basic_shaders:
sattr = state.get_attrib(core.ShaderAttrib)
if sattr and sattr.auto_shader():
pytest.skip("Cannot test auto-shader without Cg shader support")
# Set up the scene with a blank card rendering at specified distance.
scene = core.NodePath("root")
scene.set_attrib(core.DepthTestAttrib.make(core.RenderAttrib.M_always))
camera = scene.attach_new_node(core.Camera("camera"))
camera.node().get_lens(0).set_near_far(1, 3)
camera.node().set_cull_bounds(core.OmniBoundingVolume())
if vertex_color is not None:
format = core.GeomVertexFormat.get_v3cp()
else:
format = core.GeomVertexFormat.get_v3()
vdata = core.GeomVertexData("card", format, core.Geom.UH_static)
vdata.unclean_set_num_rows(4)
vertex = core.GeomVertexWriter(vdata, "vertex")
vertex.set_data3(core.Vec3.rfu(-1, 0, 1))
vertex.set_data3(core.Vec3.rfu(-1, 0, -1))
vertex.set_data3(core.Vec3.rfu(1, 0, 1))
vertex.set_data3(core.Vec3.rfu(1, 0, -1))
if vertex_color is not None:
color = core.GeomVertexWriter(vdata, "color")
color.set_data4(vertex_color)
color.set_data4(vertex_color)
color.set_data4(vertex_color)
color.set_data4(vertex_color)
strip = core.GeomTristrips(core.Geom.UH_static)
strip.set_shade_model(core.Geom.SM_uniform)
strip.add_next_vertices(4)
strip.close_primitive()
geom = core.Geom(vdata)
geom.add_primitive(strip)
gnode = core.GeomNode("card")
gnode.add_geom(geom, state)
card = scene.attach_new_node(gnode)
card.set_pos(0, 2, 0)
card.set_scale(60)
region.active = True
region.camera = camera
color_texture = core.Texture("color")
region.window.add_render_texture(color_texture,
core.GraphicsOutput.RTM_copy_ram,
core.GraphicsOutput.RTP_color)
region.window.engine.render_frame()
region.window.clear_render_textures()
col = core.LColor()
color_texture.peek().lookup(col, 0.5, 0.5)
return col
def test_color_write_mask(color_region):
state = core.RenderState.make(
core.ColorWriteAttrib.make(core.ColorWriteAttrib.C_green),
)
result = render_color_pixel(color_region, state)
assert result == (0, 1, 0, 1)
def test_color_empty(color_region, shader_attrib, material_attrib):
state = core.RenderState.make(
shader_attrib,
material_attrib,
)
result = render_color_pixel(color_region, state)
assert result == (1, 1, 1, 1)
def test_color_off(color_region, shader_attrib, material_attrib):
state = core.RenderState.make(
core.ColorAttrib.make_off(),
shader_attrib,
material_attrib,
)
result = render_color_pixel(color_region, state)
assert result == (1, 1, 1, 1)
def test_color_flat(color_region, shader_attrib, material_attrib):
state = core.RenderState.make(
core.ColorAttrib.make_flat(TEST_COLOR),
shader_attrib,
material_attrib,
)
result = render_color_pixel(color_region, state)
assert result.almost_equal(TEST_COLOR, FUZZ)
def test_color_vertex(color_region, shader_attrib, material_attrib):
state = core.RenderState.make(
core.ColorAttrib.make_vertex(),
shader_attrib,
material_attrib,
)
result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
assert result.almost_equal(TEST_COLOR, FUZZ)
def test_color_empty_vertex(color_region, shader_attrib, material_attrib):
state = core.RenderState.make(
shader_attrib,
material_attrib,
)
result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
assert result.almost_equal(TEST_COLOR, FUZZ)
def test_color_off_vertex(color_region, shader_attrib, material_attrib):
state = core.RenderState.make(
core.ColorAttrib.make_off(),
shader_attrib,
material_attrib,
)
result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
assert result == (1, 1, 1, 1)
def test_scaled_color_empty(color_region, shader_attrib, material_attrib):
state = core.RenderState.make(
shader_attrib,
material_attrib,
)
result = render_color_pixel(color_region, state)
assert result == (1, 1, 1, 1)
def test_scaled_color_off(color_region, shader_attrib, material_attrib):
state = core.RenderState.make(
core.ColorAttrib.make_off(),
shader_attrib,
material_attrib,
)
result = render_color_pixel(color_region, state)
assert result == (1, 1, 1, 1)
def test_scaled_color_flat(color_region, shader_attrib, material_attrib):
state = core.RenderState.make(
core.ColorAttrib.make_flat(TEST_COLOR),
core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
shader_attrib,
material_attrib,
)
result = render_color_pixel(color_region, state)
assert result.almost_equal(TEST_SCALED_COLOR, FUZZ)
def test_scaled_color_vertex(color_region, shader_attrib, material_attrib):
state = core.RenderState.make(
core.ColorAttrib.make_vertex(),
core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
shader_attrib,
material_attrib,
)
result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
assert result.almost_equal(TEST_SCALED_COLOR, FUZZ)
def test_scaled_color_empty_vertex(color_region, shader_attrib, material_attrib):
state = core.RenderState.make(
core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
shader_attrib,
material_attrib,
)
result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
assert result.almost_equal(TEST_SCALED_COLOR, FUZZ)
def test_scaled_color_off_vertex(color_region, shader_attrib, material_attrib):
state = core.RenderState.make(
core.ColorAttrib.make_off(),
core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
shader_attrib,
material_attrib,
)
result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
assert result.almost_equal(TEST_COLOR_SCALE, FUZZ)