Debian Codegraph @

tests/integration/graphics/test_shaders.py

raw symbols SCIP
1 import pytest
2
3 import pyglet
4
5
6 def test_shader_ubo_data_structure():
7 """Test to make sure the Structure that is created is correct for UBO's.
8
9 Includes nested structure and structure array.
10 """
11
12 vertex_source: str = """#version 150 core
13 in vec3 translate;
14 in vec4 colors;
15 in vec3 tex_coords;
16 in vec2 scale;
17 in vec3 position;
18 in float rotation;
19
20 out vec4 vertex_colors;
21 out vec3 texture_coords;
22
23 uniform WindowBlock
24 {
25 mat4 projection;
26 mat4 view;
27 } window;
28
29 mat4 m_scale = mat4(1.0);
30 mat4 m_rotation = mat4(1.0);
31 mat4 m_translate = mat4(1.0);
32
33 void main()
34 {
35 m_scale[0][0] = scale.x;
36 m_scale[1][1] = scale.y;
37 m_translate[3][0] = translate.x;
38 m_translate[3][1] = translate.y;
39 m_translate[3][2] = translate.z;
40 m_rotation[0][0] = cos(-radians(rotation));
41 m_rotation[0][1] = sin(-radians(rotation));
42 m_rotation[1][0] = -sin(-radians(rotation));
43 m_rotation[1][1] = cos(-radians(rotation));
44
45 gl_Position = window.projection * window.view * m_translate * m_rotation * m_scale * vec4(position, 1.0);
46
47 vertex_colors = colors;
48 texture_coords = tex_coords;
49 }
50 """
51
52 fragment_source: str = """#version 150 core
53 in vec4 vertex_colors;
54 in vec3 texture_coords;
55 out vec4 final_colors;
56
57 struct Within {
58 vec3 position;
59 vec2 second;
60 };
61
62 struct DataStruct {
63 vec2 tester;
64 vec4 color[3];
65 Within within[4];
66 };
67
68 layout(std140) uniform EntityDataBlock {
69 DataStruct data[5];
70 float simple_type;
71 vec2 vec_type;
72 } data_struct;
73
74 uniform sampler2D sprite_texture;
75
76 void main()
77 {
78 final_colors = texture(sprite_texture, texture_coords.xy) * vertex_colors + data_struct.data[0].color[1].y;
79 }
80 """
81
82 program = pyglet.graphics.shader.ShaderProgram(
83 pyglet.graphics.shader.Shader(vertex_source, "vertex"),
84 pyglet.graphics.shader.Shader(fragment_source, "fragment"),
85 )
86
87 ubo = program.uniform_blocks['EntityDataBlock'].create_ubo()
88
89 cls_struct = program.uniform_blocks['EntityDataBlock'].view_cls
90
91 test_data = (5.0, 1.0, 5125.0, 4.0)
92
93 with ubo as block:
94 block.data[0].color[1][:] = test_data
95 block.simple_type = 1.0
96 block.vec_type[:] = (1.0, 2.0)
97
98 # Just test a random deep node.
99 assert len(block.data[3].within[2].position[:]) == 3
100
101 data_from_buffer = ubo.read()
102
103 verified_structure = cls_struct.from_buffer_copy(data_from_buffer)
104
105 assert tuple(verified_structure.data[0].color[1][:]) == test_data
106
107 # Write to a FB to confirm the shader is actually modifying the right data.
108 # TODO: Add the projection back to shadow window.
109 # image = pyglet.image.ImageData(1, 1, 'RGBA', bytes([0, 0, 0, 255]))
110 # sprite = pyglet.sprite.Sprite(image, x=0, y=0, program=program)
111 #
112 # fb = pyglet.image.buffer.Framebuffer()
113 # texture = pyglet.image.Texture.create(1, 1)
114 # fb.attach_texture(texture)
115 # fb.bind()
116 # sprite.draw()
117 # fb.unbind()
118 #
119 # # The image should now be white. (y is 1.0)
120 # image_data = tuple(texture.get_image_data().get_data('RGBA')[:])
121 # assert image_data == (255, 255, 255, 255)
122
123
124 def test_shader_ubo_matrix_data_structure():
125 """Test UBO structure with matrix."""
126
127 vertex_source: str = """#version 150 core
128 in vec3 translate;
129 in vec4 colors;
130 in vec3 tex_coords;
131 in vec2 scale;
132 in vec3 position;
133 in float rotation;
134
135 out vec4 vertex_colors;
136 out vec3 texture_coords;
137
138 uniform WindowBlock
139 {
140 mat4 projection;
141 mat4 view;
142 } window;
143
144 mat4 m_scale = mat4(1.0);
145 mat4 m_rotation = mat4(1.0);
146 mat4 m_translate = mat4(1.0);
147
148 void main()
149 {
150 m_scale[0][0] = scale.x;
151 m_scale[1][1] = scale.y;
152 m_translate[3][0] = translate.x;
153 m_translate[3][1] = translate.y;
154 m_translate[3][2] = translate.z;
155 m_rotation[0][0] = cos(-radians(rotation));
156 m_rotation[0][1] = sin(-radians(rotation));
157 m_rotation[1][0] = -sin(-radians(rotation));
158 m_rotation[1][1] = cos(-radians(rotation));
159
160 gl_Position = window.projection * window.view * m_translate * m_rotation * m_scale * vec4(position, 1.0);
161
162 vertex_colors = colors;
163 texture_coords = tex_coords;
164 }
165 """
166
167 fragment_source: str = """#version 150 core
168 in vec4 vertex_colors;
169 in vec3 texture_coords;
170 out vec4 final_colors;
171
172 struct DataStruct {
173 mat4 projection[5];
174 vec4 color[3];
175 };
176
177 layout(std140) uniform MatrixTest {
178 DataStruct data[5];
179 } data_struct;
180
181 uniform sampler2D sprite_texture;
182
183 void main()
184 {
185 final_colors = texture(sprite_texture, texture_coords.xy) * vertex_colors + data_struct.data[1].projection[1][0];
186 }
187 """
188
189 program = pyglet.graphics.shader.ShaderProgram(
190 pyglet.graphics.shader.Shader(vertex_source, "vertex"),
191 pyglet.graphics.shader.Shader(fragment_source, "fragment"),
192 )
193
194 ubo = program.uniform_blocks['MatrixTest'].create_ubo()
195
196 cls_struct = program.uniform_blocks['MatrixTest'].view_cls
197
198 test_data = pyglet.math.Mat4.orthogonal_projection(0, 800, 0, 600, -255, 255)
199
200 with ubo as block:
201 block.data[1].projection[1][:] = test_data
202
203 data_from_buffer = ubo.read()
204
205 verified_structure = cls_struct.from_buffer_copy(data_from_buffer)
206
207 # Float imprecision's will become apparent, use approx instead...
208 for a, b in zip(verified_structure.data[1].projection[1][:], tuple(test_data)):
209 assert a == pytest.approx(b)
210
211 # Write to a FB to confirm the shader is actually modifying the right data.
212 # TODO: Add the projection back to shadow window.
213 # image = pyglet.image.ImageData(1, 1, 'RGBA', bytes([0, 0, 0, 255]))
214 # sprite = pyglet.sprite.Sprite(image, x=0, y=0, program=program)
215 #
216 # fb = pyglet.image.buffer.Framebuffer()
217 # texture = pyglet.image.Texture.create(1, 1)
218 # fb.attach_texture(texture)
219 # fb.bind()
220 # sprite.draw()
221 # fb.unbind()
222 #
223 # # The image should now be white. (y is 1.0)
224 # image_data = tuple(texture.get_image_data().get_data('RGBA')[:])
225 # assert image_data == (255, 255, 255, 255)
226
227
228 def test_shader_uniform_block_matrix():
229 vertex_source: str = """#version 150 core
230 in vec3 translate;
231 in vec4 colors;
232 in vec3 tex_coords;
233 in vec2 scale;
234 in vec3 position;
235 in float rotation;
236
237 out vec4 vertex_colors;
238 out vec3 texture_coords;
239
240 uniform WindowBlock
241 {
242 mat4 projection;
243 mat4 view;
244 } window;
245
246 mat4 m_scale = mat4(1.0);
247 mat4 m_rotation = mat4(1.0);
248 mat4 m_translate = mat4(1.0);
249
250 void main()
251 {
252 m_scale[0][0] = scale.x;
253 m_scale[1][1] = scale.y;
254 m_translate[3][0] = translate.x;
255 m_translate[3][1] = translate.y;
256 m_translate[3][2] = translate.z;
257 m_rotation[0][0] = cos(-radians(rotation));
258 m_rotation[0][1] = sin(-radians(rotation));
259 m_rotation[1][0] = -sin(-radians(rotation));
260 m_rotation[1][1] = cos(-radians(rotation));
261
262 gl_Position = window.projection * window.view * m_translate * m_rotation * m_scale * vec4(position, 1.0);
263
264 vertex_colors = colors;
265 texture_coords = tex_coords;
266 }
267 """
268
269 fragment_source: str = """#version 150 core
270 in vec4 vertex_colors;
271 in vec3 texture_coords;
272 out vec4 final_colors;
273
274 layout(std140) uniform MatrixTest {
275 mat4 projection[5];
276 } data_struct;
277
278 uniform sampler2D sprite_texture;
279
280 void main()
281 {
282 final_colors = texture(sprite_texture, texture_coords.xy) * vertex_colors + data_struct.projection[1][0];
283 }
284 """
285
286 program = pyglet.graphics.shader.ShaderProgram(
287 pyglet.graphics.shader.Shader(vertex_source, "vertex"),
288 pyglet.graphics.shader.Shader(fragment_source, "fragment"),
289 )
290
291 ubo = program.uniform_blocks['MatrixTest'].create_ubo()
292
293 cls_struct = program.uniform_blocks['MatrixTest'].view_cls
294
295 test_data = pyglet.math.Mat4.orthogonal_projection(0, 800, 0, 600, -255, 255)
296
297 with ubo as block:
298 block.projection[1][:] = test_data
299 assert len(block.projection) == 5
300
301 data_from_buffer = ubo.read()
302
303 verified_structure = cls_struct.from_buffer_copy(data_from_buffer)
304
305 # Float imprecision's will become apparent, use approx instead...
306 for a, b in zip(verified_structure.projection[1][:], tuple(test_data)):
307 assert a == pytest.approx(b)
308
309 # Write to a FB to confirm the shader is actually modifying the right data.
310 # TODO: Add the projection back to shadow window.
311 # image = pyglet.image.ImageData(1, 1, 'RGBA', bytes([0, 0, 0, 255]))
312 # sprite = pyglet.sprite.Sprite(image, x=0, y=0, program=program)
313 #
314 # fb = pyglet.image.buffer.Framebuffer()
315 # texture = pyglet.image.Texture.create(1, 1)
316 # fb.attach_texture(texture)
317 # fb.bind()
318 # sprite.draw()
319 # fb.unbind()
320 #
321 # # The image should now be white. (y is 1.0)
322 # image_data = tuple(texture.get_image_data().get_data('RGBA')[:])
323 # assert image_data == (255, 255, 255, 255)
324
325 def test_shader_uniform_matrix():
326 vertex_source: str = """#version 150 core
327 in vec3 translate;
328 in vec4 colors;
329 in vec3 tex_coords;
330 in vec2 scale;
331 in vec3 position;
332 in float rotation;
333
334 out vec4 vertex_colors;
335 out vec3 texture_coords;
336
337 uniform WindowBlock
338 {
339 mat4 projection;
340 mat4 view;
341 } window;
342
343 mat4 m_scale = mat4(1.0);
344 mat4 m_rotation = mat4(1.0);
345 mat4 m_translate = mat4(1.0);
346
347 void main()
348 {
349 m_scale[0][0] = scale.x;
350 m_scale[1][1] = scale.y;
351 m_translate[3][0] = translate.x;
352 m_translate[3][1] = translate.y;
353 m_translate[3][2] = translate.z;
354 m_rotation[0][0] = cos(-radians(rotation));
355 m_rotation[0][1] = sin(-radians(rotation));
356 m_rotation[1][0] = -sin(-radians(rotation));
357 m_rotation[1][1] = cos(-radians(rotation));
358
359 gl_Position = window.projection * window.view * m_translate * m_rotation * m_scale * vec4(position, 1.0);
360
361 vertex_colors = colors;
362 texture_coords = tex_coords;
363 }
364 """
365
366 fragment_source: str = """#version 150 core
367 in vec4 vertex_colors;
368 in vec3 texture_coords;
369 out vec4 final_colors;
370
371 uniform mat4 matrix_uniform;
372
373 uniform sampler2D sprite_texture;
374
375 void main()
376 {
377 final_colors = texture(sprite_texture, texture_coords.xy) * vertex_colors + matrix_uniform[1];
378 }
379 """
380
381 program = pyglet.graphics.shader.ShaderProgram(
382 pyglet.graphics.shader.Shader(vertex_source, "vertex"),
383 pyglet.graphics.shader.Shader(fragment_source, "fragment"),
384 )
385 test_data = pyglet.math.Mat4.orthogonal_projection(0, 800, 0, 600, -255, 255)
386
387 with program:
388 program['matrix_uniform'] = tuple(test_data)
389
390 with program:
391 fetched_data = program['matrix_uniform']
392
393 # Float imprecision's will become apparent, use approx instead...
394 for a, b in zip(fetched_data, tuple(test_data)):
395 assert a == pytest.approx(b)
396
397
398 def test_shader_uniform_matrix_array():
399 vertex_source: str = """#version 150 core
400 in vec3 translate;
401 in vec4 colors;
402 in vec3 tex_coords;
403 in vec2 scale;
404 in vec3 position;
405 in float rotation;
406
407 out vec4 vertex_colors;
408 out vec3 texture_coords;
409
410 uniform WindowBlock
411 {
412 mat4 projection;
413 mat4 view;
414 } window;
415
416 mat4 m_scale = mat4(1.0);
417 mat4 m_rotation = mat4(1.0);
418 mat4 m_translate = mat4(1.0);
419
420 void main()
421 {
422 m_scale[0][0] = scale.x;
423 m_scale[1][1] = scale.y;
424 m_translate[3][0] = translate.x;
425 m_translate[3][1] = translate.y;
426 m_translate[3][2] = translate.z;
427 m_rotation[0][0] = cos(-radians(rotation));
428 m_rotation[0][1] = sin(-radians(rotation));
429 m_rotation[1][0] = -sin(-radians(rotation));
430 m_rotation[1][1] = cos(-radians(rotation));
431
432 gl_Position = window.projection * window.view * m_translate * m_rotation * m_scale * vec4(position, 1.0);
433
434 vertex_colors = colors;
435 texture_coords = tex_coords;
436 }
437 """
438
439 fragment_source: str = """#version 150 core
440 in vec4 vertex_colors;
441 in vec3 texture_coords;
442 out vec4 final_colors;
443
444 uniform mat4 matrice_array[25];
445
446 uniform sampler2D sprite_texture;
447
448 void main()
449 {
450 final_colors = texture(sprite_texture, texture_coords.xy) * vertex_colors + matrice_array[18][0];
451 }
452 """
453
454 program = pyglet.graphics.shader.ShaderProgram(
455 pyglet.graphics.shader.Shader(vertex_source, "vertex"),
456 pyglet.graphics.shader.Shader(fragment_source, "fragment"),
457 )
458 test_data = pyglet.math.Mat4.orthogonal_projection(0, 800, 0, 600, -255, 255)
459
460 with program:
461 # Set test data
462 program['matrice_array'][18] = tuple(test_data)
463
464 with program:
465 # fetch from uniform itself.
466 fetched_data = program['matrice_array'].get()
467
468 # Float imprecision's will become apparent, use approx instead...
469 for a, b in zip(fetched_data[18], tuple(test_data)):
470 assert a == pytest.approx(b)
471
472 def test_shader_uniform_float_array():
473 vertex_source: str = """#version 150 core
474 in vec3 translate;
475 in vec4 colors;
476 in vec3 tex_coords;
477 in vec2 scale;
478 in vec3 position;
479 in float rotation;
480
481 out vec4 vertex_colors;
482 out vec3 texture_coords;
483
484 uniform WindowBlock
485 {
486 mat4 projection;
487 mat4 view;
488 } window;
489
490 mat4 m_scale = mat4(1.0);
491 mat4 m_rotation = mat4(1.0);
492 mat4 m_translate = mat4(1.0);
493
494 void main()
495 {
496 m_scale[0][0] = scale.x;
497 m_scale[1][1] = scale.y;
498 m_translate[3][0] = translate.x;
499 m_translate[3][1] = translate.y;
500 m_translate[3][2] = translate.z;
501 m_rotation[0][0] = cos(-radians(rotation));
502 m_rotation[0][1] = sin(-radians(rotation));
503 m_rotation[1][0] = -sin(-radians(rotation));
504 m_rotation[1][1] = cos(-radians(rotation));
505
506 gl_Position = window.projection * window.view * m_translate * m_rotation * m_scale * vec4(position, 1.0);
507
508 vertex_colors = colors;
509 texture_coords = tex_coords;
510 }
511 """
512
513 fragment_source: str = """#version 150 core
514 in vec4 vertex_colors;
515 in vec3 texture_coords;
516 out vec4 final_colors;
517
518 uniform float float_array[15];
519
520 uniform sampler2D sprite_texture;
521
522 void main()
523 {
524 final_colors = texture(sprite_texture, texture_coords.xy) * vertex_colors + float_array[11];
525 }
526 """
527
528 program = pyglet.graphics.shader.ShaderProgram(
529 pyglet.graphics.shader.Shader(vertex_source, "vertex"),
530 pyglet.graphics.shader.Shader(fragment_source, "fragment"),
531 )
532 test_data = 25.5
533
534 with program:
535 # Set test data
536 program['float_array'][11] = test_data
537
538 with program:
539 # fetch from uniform itself.
540 fetched_data = program['float_array'][11]
541
542 assert test_data == fetched_data
543