From 369dccbab95e0c250d89d3154b673ac1b93952b2 Mon Sep 17 00:00:00 2001 From: Mitchell Stokes Date: Mon, 13 May 2019 20:26:27 -0700 Subject: [PATCH] tests: Add tests around compiling GLSL and Cg shaders (#622) --- tests/display/cg_bad.sha | 21 ++++++++++++++ tests/display/cg_simple.sha | 21 ++++++++++++++ tests/display/glsl_bad.vert | 12 ++++++++ tests/display/glsl_include.vert | 7 +++++ tests/display/glsl_include_inputs.vert | 5 ++++ tests/display/glsl_simple.frag | 5 ++++ tests/display/glsl_simple.vert | 11 ++++++++ tests/display/test_cg_shader.py | 28 +++++++++++++++++++ tests/display/test_glsl_shader.py | 38 ++++++++++++++++++++++++++ 9 files changed, 148 insertions(+) create mode 100644 tests/display/cg_bad.sha create mode 100644 tests/display/cg_simple.sha create mode 100644 tests/display/glsl_bad.vert create mode 100644 tests/display/glsl_include.vert create mode 100644 tests/display/glsl_include_inputs.vert create mode 100644 tests/display/glsl_simple.frag create mode 100644 tests/display/glsl_simple.vert create mode 100644 tests/display/test_cg_shader.py diff --git a/tests/display/cg_bad.sha b/tests/display/cg_bad.sha new file mode 100644 index 0000000000..7f3a77eda4 --- /dev/null +++ b/tests/display/cg_bad.sha @@ -0,0 +1,21 @@ +//Cg +// + +void vshader(float4 vtx_position : POSITION, + float2 vtx_texcoord0 : TEXCOORD0, + uniform float4x4 mat_modelproj, + out float4 l_position : POSITION, + out float2 l_texcoord0 : TEXCOORD0) +{ + l_position = mul(mat_modelproj, vtx_position); + l_texcoord0 = vtx_texcoord0; +} + +void fshader(float2 l_texcoord0 : TEXCOORD0, + uniform sampler2D tex_0 : TEXUNIT0, + out float4 o_color : COLOR) +{ + float4 texColor = tex2D(tex_0, l_texcoord0); + does_not_exist = texColor * 2 * (texColor.w - 0.5); +} + diff --git a/tests/display/cg_simple.sha b/tests/display/cg_simple.sha new file mode 100644 index 0000000000..64e419fd0e --- /dev/null +++ b/tests/display/cg_simple.sha @@ -0,0 +1,21 @@ +//Cg +// + +void vshader(float4 vtx_position : POSITION, + float2 vtx_texcoord0 : TEXCOORD0, + uniform float4x4 mat_modelproj, + out float4 l_position : POSITION, + out float2 l_texcoord0 : TEXCOORD0) +{ + l_position = mul(mat_modelproj, vtx_position); + l_texcoord0 = vtx_texcoord0; +} + +void fshader(float2 l_texcoord0 : TEXCOORD0, + uniform sampler2D tex_0 : TEXUNIT0, + out float4 o_color : COLOR) +{ + float4 texColor = tex2D(tex_0, l_texcoord0); + o_color = texColor * 2 * (texColor.w - 0.5); +} + diff --git a/tests/display/glsl_bad.vert b/tests/display/glsl_bad.vert new file mode 100644 index 0000000000..8a324916ec --- /dev/null +++ b/tests/display/glsl_bad.vert @@ -0,0 +1,12 @@ +#version 130 + +// Uniform inputs +uniform mat4 p3d_ModelViewProjectionMatrix; + +// Vertex inputs +in vec4 p3d_Vertex; + +void main() { + gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex; + does_not_exist = p3d_Vertex; +} diff --git a/tests/display/glsl_include.vert b/tests/display/glsl_include.vert new file mode 100644 index 0000000000..1826fdf677 --- /dev/null +++ b/tests/display/glsl_include.vert @@ -0,0 +1,7 @@ +#version 130 + +#pragma include "glsl_include_inputs.vert" + +void main() { + gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex; +} diff --git a/tests/display/glsl_include_inputs.vert b/tests/display/glsl_include_inputs.vert new file mode 100644 index 0000000000..125078ce16 --- /dev/null +++ b/tests/display/glsl_include_inputs.vert @@ -0,0 +1,5 @@ +// Uniform inputs +uniform mat4 p3d_ModelViewProjectionMatrix; + +// Vertex inputs +in vec4 p3d_Vertex; diff --git a/tests/display/glsl_simple.frag b/tests/display/glsl_simple.frag new file mode 100644 index 0000000000..5f190e015c --- /dev/null +++ b/tests/display/glsl_simple.frag @@ -0,0 +1,5 @@ +#version 130 + +void main() { + gl_FragColor = vec4(0, 0, 0, 1); +} diff --git a/tests/display/glsl_simple.vert b/tests/display/glsl_simple.vert new file mode 100644 index 0000000000..ea3afd7498 --- /dev/null +++ b/tests/display/glsl_simple.vert @@ -0,0 +1,11 @@ +#version 130 + +// Uniform inputs +uniform mat4 p3d_ModelViewProjectionMatrix; + +// Vertex inputs +in vec4 p3d_Vertex; + +void main() { + gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex; +} diff --git a/tests/display/test_cg_shader.py b/tests/display/test_cg_shader.py new file mode 100644 index 0000000000..8a6c4d7a74 --- /dev/null +++ b/tests/display/test_cg_shader.py @@ -0,0 +1,28 @@ +import os + +from panda3d import core + + +SHADERS_DIR = core.Filename.from_os_specific(os.path.dirname(__file__)) + + +def run_cg_compile_check(gsg, shader_path, expect_fail=False): + """Compile supplied Cg shader path and check for errors""" + shader = core.Shader.load(shader_path, core.Shader.SL_Cg) + # assert shader.is_prepared(gsg.prepared_objects) + if expect_fail: + assert shader is None + else: + assert shader is not None + + +def test_cg_compile_error(gsg): + """Test getting compile errors from bad Cg shaders""" + shader_path = core.Filename(SHADERS_DIR, 'cg_bad.sha') + run_cg_compile_check(gsg, shader_path, expect_fail=True) + + +def test_cg_from_file(gsg): + """Test compiling Cg shaders from files""" + shader_path = core.Filename(SHADERS_DIR, 'cg_simple.sha') + run_cg_compile_check(gsg, shader_path) diff --git a/tests/display/test_glsl_shader.py b/tests/display/test_glsl_shader.py index 8deadf40d9..cb7ab165d4 100644 --- a/tests/display/test_glsl_shader.py +++ b/tests/display/test_glsl_shader.py @@ -1,9 +1,13 @@ from panda3d import core +import os import struct import pytest from _pytest.outcomes import Failed +SHADERS_DIR = core.Filename.from_os_specific(os.path.dirname(__file__)) + + # This is the template for the compute shader that is used by run_glsl_test. # It defines an assert() macro that writes failures to a buffer, indexed by # line number. @@ -102,6 +106,19 @@ def run_glsl_test(gsg, body, preamble="", inputs={}, version=150, exts=set()): pytest.fail("{0} GLSL assertions triggered:\n{1}".format(count, formatted)) +def run_glsl_compile_check(gsg, vert_path, frag_path, expect_fail=False): + """Compile supplied GLSL shader paths and check for errors""" + shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path) + assert shader is not None + + shader.prepare_now(gsg.prepared_objects, gsg) + assert shader.is_prepared(gsg.prepared_objects) + if expect_fail: + assert shader.get_error_flag() + else: + assert not shader.get_error_flag() + + def test_glsl_test(gsg): "Test to make sure that the GLSL tests work correctly." @@ -329,3 +346,24 @@ def test_glsl_write_extract_image_buffer(gsg): assert struct.unpack('I', tex1.get_ram_image()) == (123,) assert struct.unpack('i', tex2.get_ram_image()) == (-456,) + + +def test_glsl_compile_error(gsg): + """Test getting compile errors from bad shaders""" + vert_path = core.Filename(SHADERS_DIR, 'glsl_bad.vert') + frag_path = core.Filename(SHADERS_DIR, 'glsl_simple.frag') + run_glsl_compile_check(gsg, vert_path, frag_path, expect_fail=True) + + +def test_glsl_from_file(gsg): + """Test compiling GLSL shaders from files""" + vert_path = core.Filename(SHADERS_DIR, 'glsl_simple.vert') + frag_path = core.Filename(SHADERS_DIR, 'glsl_simple.frag') + run_glsl_compile_check(gsg, vert_path, frag_path) + + +def test_glsl_includes(gsg): + """Test preprocessing includes in GLSL shaders""" + vert_path = core.Filename(SHADERS_DIR, 'glsl_include.vert') + frag_path = core.Filename(SHADERS_DIR, 'glsl_simple.frag') + run_glsl_compile_check(gsg, vert_path, frag_path)