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__)) def test_glsl_test(env): "Test to make sure that the GLSL tests work correctly." env.run_glsl("assert(true);") def test_glsl_test_fail(env): "Same as above, but making sure that the failure case works correctly." with pytest.raises(Failed): env.run_glsl("assert(false);") def test_glsl_sampler(env): tex1 = core.Texture("tex1-ubyte-rgba8") tex1.setup_1d_texture(1, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) tex1.set_clear_color((0, 2 / 255.0, 1, 1)) tex2 = core.Texture("tex2-float-rgba32") tex2.setup_2d_texture(1, 1, core.Texture.T_float, core.Texture.F_rgba32) tex2.set_clear_color((1.0, 2.0, -3.14, 0.0)) tex3 = core.Texture("tex3-float-r32") tex3.setup_3d_texture(1, 1, 1, core.Texture.T_float, core.Texture.F_r32) tex3.set_clear_color((0.5, 0.0, 0.0, 1.0)) preamble = """ uniform sampler1D tex1; uniform sampler2D tex2; uniform sampler3D tex3; """ code = """ assert(texture(tex1, 0) == vec4(0, 2 / 255.0, 1, 1)); assert(texture(tex2, vec2(0, 0)) == vec4(1.0, 2.0, -3.14, 0.0)); assert(texture(tex3, vec3(0, 0, 0)).r == 0.5); """ env.run_glsl(code, preamble, {'tex1': tex1, 'tex2': tex2, 'tex3': tex3}) def test_glsl_texture_size(env): tex1_0 = core.Texture("tex1-0-1d") tex1_0.setup_1d_texture(128, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) tex1_1 = core.Texture("tex1-1-1d") tex1_1.setup_1d_texture(256, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) tex2 = core.Texture("tex2-2d") tex2.setup_2d_texture(64, 32, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) tex3 = core.Texture("tex3-cube") tex3.setup_cube_map(16, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) tex4 = core.Texture("tex4-3d") tex4.setup_3d_texture(8, 4, 2, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) preamble = """ uniform sampler1D tex1[2]; uniform sampler2D tex2; uniform samplerCube tex3; uniform sampler3D tex4; """ code = """ assert(textureSize(tex1[0], 0) == 128); assert(textureSize(tex1[1], 0) == 256); assert(textureSize(tex2, 0) == ivec2(64, 32)); assert(textureSize(tex3, 0) == ivec2(16, 16)); assert(textureSize(tex4, 0) == ivec3(8, 4, 2)); // dummy sample this texture so it doesn't get optimized out assert(texture(tex1[0], 0) != vec4(2, 2, 2, 2)); """ env.run_glsl(code, preamble, {'tex1[0]': tex1_0, 'tex1[1]': tex1_1, 'tex2': tex2, 'tex3': tex3, 'tex4': tex4}) def test_glsl_texture_query_levels(env): tex1_0 = core.Texture("tex1-0-1d") tex1_0.setup_1d_texture(128, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) tex1_0.set_minfilter(core.SamplerState.FT_linear_mipmap_linear) tex1_1 = core.Texture("tex1-1-1d") tex1_1.setup_1d_texture(256, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) tex1_1.set_minfilter(core.SamplerState.FT_nearest) tex2 = core.Texture("tex2-2d") tex2.setup_2d_texture(64, 32, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) tex2.set_minfilter(core.SamplerState.FT_linear_mipmap_linear) tex3 = core.Texture("tex3-cube") tex3.setup_cube_map(16, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) tex3.set_minfilter(core.SamplerState.FT_linear_mipmap_linear) tex4 = core.Texture("tex4-3d") tex4.setup_3d_texture(8, 4, 2, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) tex4.set_minfilter(core.SamplerState.FT_linear_mipmap_linear) preamble = """ uniform sampler1D tex1[2]; uniform sampler2D tex2; uniform samplerCube tex3; uniform sampler3D tex4; """ code = """ assert(textureQueryLevels(tex1[1]) == 1); assert(textureQueryLevels(tex1[0]) == 8); assert(textureQueryLevels(tex2) == 7); assert(textureQueryLevels(tex3) == 5); assert(textureQueryLevels(tex4) == 4); """ env.run_glsl(code, preamble, {'tex1[0]': tex1_0, 'tex1[1]': tex1_1, 'tex2': tex2, 'tex3': tex3, 'tex4': tex4}, version=430) def test_glsl_isampler(env): from struct import pack tex1 = core.Texture("") tex1.setup_1d_texture(1, core.Texture.T_byte, core.Texture.F_rgba8i) tex1.set_ram_image(pack('bbbb', 0, 1, 2, 3)) tex2 = core.Texture("") tex2.setup_2d_texture(1, 1, core.Texture.T_short, core.Texture.F_r16i) tex2.set_ram_image(pack('h', 4)) tex3 = core.Texture("") tex3.setup_3d_texture(1, 1, 1, core.Texture.T_int, core.Texture.F_r32i) tex3.set_ram_image(pack('i', 5)) preamble = """ uniform isampler1D tex1; uniform isampler2D tex2; uniform isampler3D tex3; """ code = """ assert(texelFetch(tex1, 0, 0) == ivec4(0, 1, 2, 3)); assert(texelFetch(tex2, ivec2(0, 0), 0) == ivec4(4, 0, 0, 1)); assert(texelFetch(tex3, ivec3(0, 0, 0), 0) == ivec4(5, 0, 0, 1)); """ env.run_glsl(code, preamble, {'tex1': tex1, 'tex2': tex2, 'tex3': tex3}) def test_glsl_usampler(env): from struct import pack tex1 = core.Texture("") tex1.setup_1d_texture(1, core.Texture.T_unsigned_byte, core.Texture.F_rgba8i) tex1.set_ram_image(pack('BBBB', 0, 1, 2, 3)) tex2 = core.Texture("") tex2.setup_2d_texture(1, 1, core.Texture.T_unsigned_short, core.Texture.F_r16i) tex2.set_ram_image(pack('H', 4)) tex3 = core.Texture("") tex3.setup_3d_texture(1, 1, 1, core.Texture.T_unsigned_int, core.Texture.F_r32i) tex3.set_ram_image(pack('I', 5)) preamble = """ uniform usampler1D tex1; uniform usampler2D tex2; uniform usampler3D tex3; """ code = """ assert(texelFetch(tex1, 0, 0) == uvec4(0, 1, 2, 3)); assert(texelFetch(tex2, ivec2(0, 0), 0) == uvec4(4, 0, 0, 1)); assert(texelFetch(tex3, ivec3(0, 0, 0), 0) == uvec4(5, 0, 0, 1)); """ env.run_glsl(code, preamble, {'tex1': tex1, 'tex2': tex2, 'tex3': tex3}) def test_glsl_image(env): tex1 = core.Texture("") tex1.setup_1d_texture(1, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) tex1.set_clear_color((0, 2 / 255.0, 1, 1)) tex2 = core.Texture("") tex2.setup_2d_texture(1, 1, core.Texture.T_float, core.Texture.F_rgba32) tex2.set_clear_color((1.0, 2.0, -3.14, 0.0)) preamble = """ layout(rgba8) uniform image1D tex1; layout(rgba32f) uniform image2D tex2; """ code = """ assert(imageLoad(tex1, 0) == vec4(0, 2 / 255.0, 1, 1)); assert(imageLoad(tex2, ivec2(0, 0)) == vec4(1.0, 2.0, -3.14, 0.0)); """ env.run_glsl(code, preamble, {'tex1': tex1, 'tex2': tex2}) def test_glsl_iimage(env): from struct import pack tex1 = core.Texture("") tex1.setup_1d_texture(1, core.Texture.T_byte, core.Texture.F_rgba8i) tex1.set_ram_image(pack('bbbb', 0, 1, 2, 3)) tex2 = core.Texture("") tex2.setup_2d_texture(1, 1, core.Texture.T_short, core.Texture.F_r16i) tex2.set_ram_image(pack('h', 4)) tex3 = core.Texture("") tex3.setup_3d_texture(1, 1, 1, core.Texture.T_int, core.Texture.F_r32i) tex3.set_ram_image(pack('i', 5)) preamble = """ layout(rgba8i) uniform iimage1D tex1; layout(r16i) uniform iimage2D tex2; layout(r32i) uniform iimage3D tex3; """ code = """ assert(imageLoad(tex1, 0) == ivec4(0, 1, 2, 3)); assert(imageLoad(tex2, ivec2(0, 0)) == ivec4(4, 0, 0, 1)); assert(imageLoad(tex3, ivec3(0, 0, 0)) == ivec4(5, 0, 0, 1)); """ env.run_glsl(code, preamble, {'tex1': tex1, 'tex2': tex2, 'tex3': tex3}) def test_glsl_uimage(env): from struct import pack tex1 = core.Texture("") tex1.setup_1d_texture(1, core.Texture.T_unsigned_byte, core.Texture.F_rgba8i) tex1.set_ram_image(pack('BBBB', 0, 1, 2, 3)) tex2 = core.Texture("") tex2.setup_2d_texture(1, 1, core.Texture.T_unsigned_short, core.Texture.F_r16i) tex2.set_ram_image(pack('H', 4)) tex3 = core.Texture("") tex3.setup_3d_texture(1, 1, 1, core.Texture.T_unsigned_int, core.Texture.F_r32i) tex3.set_ram_image(pack('I', 5)) preamble = """ layout(rgba8ui) uniform uimage1D tex1; layout(r16ui) uniform uimage2D tex2; layout(r32ui) uniform uimage3D tex3; """ code = """ assert(imageLoad(tex1, 0) == uvec4(0, 1, 2, 3)); assert(imageLoad(tex2, ivec2(0, 0)) == uvec4(4, 0, 0, 1)); assert(imageLoad(tex3, ivec3(0, 0, 0)) == uvec4(5, 0, 0, 1)); """ env.run_glsl(code, preamble, {'tex1': tex1, 'tex2': tex2, 'tex3': tex3}) def test_glsl_image_size(env): tex1_0 = core.Texture("tex1-0-1d") tex1_0.setup_1d_texture(128, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) tex1_1 = core.Texture("tex1-1-1d") tex1_1.setup_1d_texture(256, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) tex2 = core.Texture("tex2-2d") tex2.setup_2d_texture(64, 32, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) tex3 = core.Texture("tex3-cube") tex3.setup_cube_map(16, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) tex4 = core.Texture("tex4-3d") tex4.setup_3d_texture(8, 4, 2, core.Texture.T_unsigned_byte, core.Texture.F_rgba8) preamble = """ uniform writeonly image1D tex1[2]; uniform writeonly image2D tex2; uniform writeonly imageCube tex3; uniform writeonly image3D tex4; """ code = """ assert(imageSize(tex1[0]) == 128); assert(imageSize(tex1[1]) == 256); assert(imageSize(tex2) == ivec2(64, 32)); assert(imageSize(tex3) == ivec2(16, 16)); assert(imageSize(tex4) == ivec3(8, 4, 2)); """ env.run_glsl(code, preamble, {'tex1[0]': tex1_0, 'tex1[1]': tex1_1, 'tex2': tex2, 'tex3': tex3, 'tex4': tex4}, version=430) def test_glsl_ssbo(env): from struct import pack, unpack num1 = pack(' 0.499); assert(p3d_LightSource[0].spotCosCutoff < 0.501); assert(p3d_LightSource[0].spotCutoff == 60); assert(p3d_LightSource[0].shadowViewMatrix[0][0] > 0.2886); assert(p3d_LightSource[0].shadowViewMatrix[0][0] < 0.2887); assert(p3d_LightSource[0].shadowViewMatrix[0][1] == 0); assert(p3d_LightSource[0].shadowViewMatrix[0][2] == 0); assert(p3d_LightSource[0].shadowViewMatrix[0][3] == 0); assert(p3d_LightSource[0].shadowViewMatrix[1][0] == 0.5); assert(p3d_LightSource[0].shadowViewMatrix[1][1] == 0.5); assert(p3d_LightSource[0].shadowViewMatrix[1][2] > 1.0); assert(p3d_LightSource[0].shadowViewMatrix[1][2] < 1.00002); assert(p3d_LightSource[0].shadowViewMatrix[1][3] == 1); assert(p3d_LightSource[0].shadowViewMatrix[2][0] == 0); assert(p3d_LightSource[0].shadowViewMatrix[2][1] > 0.2886); assert(p3d_LightSource[0].shadowViewMatrix[2][1] < 0.2887); assert(p3d_LightSource[0].shadowViewMatrix[2][2] == 0); assert(p3d_LightSource[0].shadowViewMatrix[2][3] == 0); assert(p3d_LightSource[0].shadowViewMatrix[3][0] > -16.2736); assert(p3d_LightSource[0].shadowViewMatrix[3][0] < -16.2734); assert(p3d_LightSource[0].shadowViewMatrix[3][1] > -16.8510); assert(p3d_LightSource[0].shadowViewMatrix[3][1] < -16.8508); assert(p3d_LightSource[0].shadowViewMatrix[3][2] > -22.0003); assert(p3d_LightSource[0].shadowViewMatrix[3][2] < -22.0001); assert(p3d_LightSource[0].shadowViewMatrix[3][3] > -21.0001); assert(p3d_LightSource[0].shadowViewMatrix[3][3] < -20.9999); assert(p3d_LightSource[1].color == vec4(9, 10, 11, 12)); assert(p3d_LightSource[1].specular == vec4(13, 14, 15, 16)); assert(p3d_LightSource[1].diffuse == vec4(9, 10, 11, 12)); assert(p3d_LightSource[1].ambient == vec4(0, 0, 0, 1)); assert(p3d_LightSource[1].position == vec4(-17, -18, -19, 0)); assert(p3d_LightSource[1].attenuation == vec3(1, 0, 0)); assert(p3d_LightSource[1].constantAttenuation == 1); assert(p3d_LightSource[1].linearAttenuation == 0); assert(p3d_LightSource[1].quadraticAttenuation == 0); assert(p3d_LightSource[1].spotExponent == 0); assert(p3d_LightSource[1].spotCosCutoff == -1); assert(p3d_LightSource[2].color == vec4(0, 0, 0, 1)); assert(p3d_LightSource[2].specular == vec4(0, 0, 0, 1)); assert(p3d_LightSource[2].diffuse == vec4(0, 0, 0, 1)); assert(p3d_LightSource[2].ambient == vec4(0, 0, 0, 1)); assert(p3d_LightSource[2].position == vec4(0, 0, 1, 0)); assert(p3d_LightSource[2].attenuation == vec3(1, 0, 0)); assert(p3d_LightSource[2].constantAttenuation == 1); assert(p3d_LightSource[2].linearAttenuation == 0); assert(p3d_LightSource[2].quadraticAttenuation == 0); assert(p3d_LightSource[2].spotExponent == 0); assert(p3d_LightSource[2].spotCosCutoff == -1); """ node = core.NodePath("state") spot_path = node.attach_new_node(spot) spot_path.set_pos(20, 21, 22) node.set_light(spot_path) dire_path = node.attach_new_node(dire) node.set_light(dire_path) env.run_glsl(code, preamble, state=node.get_state()) def test_glsl_state_material(env): mat = core.Material("mat") mat.ambient = (1, 2, 3, 4) mat.diffuse = (5, 6, 7, 8) mat.emission = (9, 10, 11, 12) mat.specular = (13, 14, 15, 0) mat.shininess = 16 mat.metallic = 0.5 mat.refractive_index = 21 preamble = """ struct p3d_MaterialParameters { vec4 ambient; vec4 diffuse; vec4 emission; vec3 specular; float shininess; float metallic; float refractiveIndex; }; uniform p3d_MaterialParameters p3d_Material; """ code = """ assert(p3d_Material.ambient == vec4(1, 2, 3, 4)); assert(p3d_Material.diffuse == vec4(5, 6, 7, 8)); assert(p3d_Material.emission == vec4(9, 10, 11, 12)); assert(p3d_Material.specular == vec3(13, 14, 15)); assert(p3d_Material.shininess == 16); assert(p3d_Material.metallic == 0.5); assert(p3d_Material.refractiveIndex == 21); """ node = core.NodePath("state") node.set_material(mat) env.run_glsl(code, preamble, state=node.get_state()) def test_glsl_state_material_pbr(env): mat = core.Material("mat") mat.base_color = (1, 2, 3, 4) mat.emission = (9, 10, 11, 12) mat.roughness = 16 mat.metallic = 0.5 mat.refractive_index = 21 preamble = """ struct p3d_MaterialParameters { vec4 baseColor; vec4 emission; float metallic; float refractiveIndex; float roughness; }; uniform p3d_MaterialParameters p3d_Material; """ code = """ assert(p3d_Material.baseColor == vec4(1, 2, 3, 4)); assert(p3d_Material.emission == vec4(9, 10, 11, 12)); assert(p3d_Material.roughness == 16); assert(p3d_Material.metallic == 0.5); assert(p3d_Material.refractiveIndex == 21); """ node = core.NodePath("state") node.set_material(mat) env.run_glsl(code, preamble, state=node.get_state()) def test_glsl_state_fog(env): fog = core.Fog("fog") fog.color = (1, 2, 3, 4) fog.exp_density = 0.5 fog.set_linear_range(6, 10) preamble = """ struct p3d_FogParameters { vec4 color; float density; float start; float end; float scale; }; uniform p3d_FogParameters p3d_Fog; """ code = """ assert(p3d_Fog.color == vec4(1, 2, 3, 4)); assert(p3d_Fog.density == 0.5); assert(p3d_Fog.start == 6); assert(p3d_Fog.end == 10); assert(p3d_Fog.scale == 0.25); """ node = core.NodePath("state") node.set_fog(fog) env.run_glsl(code, preamble, state=node.get_state()) def test_glsl_state_texture(env): def gen_texture(v): tex = core.Texture(f"tex{v}") tex.setup_2d_texture(1, 1, core.Texture.T_unsigned_byte, core.Texture.F_red) tex.set_clear_color((v / 255.0, 0, 0, 0)) return tex np = core.NodePath("test") ts1 = core.TextureStage("ts1") ts1.sort = 10 ts1.mode = core.TextureStage.M_modulate np.set_texture(ts1, gen_texture(1)) ts2 = core.TextureStage("ts2") ts2.sort = 20 ts2.mode = core.TextureStage.M_add np.set_texture(ts2, gen_texture(2)) ts3 = core.TextureStage("ts3") ts3.sort = 30 ts3.mode = core.TextureStage.M_modulate np.set_texture(ts3, gen_texture(3)) ts4 = core.TextureStage("ts4") ts4.sort = 40 ts4.mode = core.TextureStage.M_normal_height np.set_texture(ts4, gen_texture(4)) ts5 = core.TextureStage("ts5") ts5.sort = 50 ts5.mode = core.TextureStage.M_add np.set_texture(ts5, gen_texture(5)) ts6 = core.TextureStage("ts6") ts6.sort = 60 ts6.mode = core.TextureStage.M_normal np.set_texture(ts6, gen_texture(6)) # Do this in multiple passes to stay under sampler limit of 16 preamble = """ uniform sampler2D p3d_Texture2; uniform sampler2D p3d_Texture0; uniform sampler2D p3d_Texture1; uniform sampler2D p3d_Texture3; uniform sampler2D p3d_Texture4; uniform sampler2D p3d_Texture5; uniform sampler2D p3d_Texture6; uniform sampler2D p3d_Texture[7]; """ code = """ vec2 coord = vec2(0, 0); assert(abs(texture(p3d_Texture2, coord).r - 3.0 / 255.0) < 0.001); assert(abs(texture(p3d_Texture0, coord).r - 1.0 / 255.0) < 0.001); assert(abs(texture(p3d_Texture1, coord).r - 2.0 / 255.0) < 0.001); assert(abs(texture(p3d_Texture3, coord).r - 4.0 / 255.0) < 0.001); assert(abs(texture(p3d_Texture4, coord).r - 5.0 / 255.0) < 0.001); assert(abs(texture(p3d_Texture5, coord).r - 6.0 / 255.0) < 0.001); assert(texture(p3d_Texture6, coord).r == 1.0); assert(abs(texture(p3d_Texture[0], coord).r - 1.0 / 255.0) < 0.001); assert(abs(texture(p3d_Texture[2], coord).r - 3.0 / 255.0) < 0.001); assert(abs(texture(p3d_Texture[3], coord).r - 4.0 / 255.0) < 0.001); assert(abs(texture(p3d_Texture[1], coord).r - 2.0 / 255.0) < 0.001); assert(abs(texture(p3d_Texture[4], coord).r - 5.0 / 255.0) < 0.001); assert(abs(texture(p3d_Texture[5], coord).r - 6.0 / 255.0) < 0.001); assert(texture(p3d_Texture[6], coord).r == 1.0); """ env.run_glsl(code, preamble, state=np.get_state()) preamble = """ uniform sampler2D p3d_TextureFF[5]; uniform sampler2D p3d_TextureModulate[3]; uniform sampler2D p3d_TextureAdd[3]; uniform sampler2D p3d_TextureNormal[3]; uniform sampler2D p3d_TextureHeight[2]; """ code = """ vec2 coord = vec2(0, 0); assert(abs(texture(p3d_TextureFF[0], coord).r - 1.0 / 255.0) < 0.001); assert(abs(texture(p3d_TextureFF[1], coord).r - 2.0 / 255.0) < 0.001); assert(abs(texture(p3d_TextureFF[2], coord).r - 3.0 / 255.0) < 0.001); assert(abs(texture(p3d_TextureFF[3], coord).r - 5.0 / 255.0) < 0.001); assert(texture(p3d_TextureFF[4], coord).r == 1.0); assert(abs(texture(p3d_TextureModulate[0], coord).r - 1.0 / 255.0) < 0.001); assert(abs(texture(p3d_TextureModulate[1], coord).r - 3.0 / 255.0) < 0.001); assert(texture(p3d_TextureModulate[2], coord).r == 1.0); assert(abs(texture(p3d_TextureAdd[0], coord).r - 2.0 / 255.0) < 0.001); assert(abs(texture(p3d_TextureAdd[1], coord).r - 5.0 / 255.0) < 0.001); assert(texture(p3d_TextureAdd[2], coord) == vec4(0.0, 0.0, 0.0, 1.0)); assert(abs(texture(p3d_TextureNormal[0], coord).r - 4.0 / 255.0) < 0.001); assert(abs(texture(p3d_TextureNormal[1], coord).r - 6.0 / 255.0) < 0.001); assert(all(lessThan(abs(texture(p3d_TextureNormal[2], coord) - vec4(127 / 255.0, 127 / 255.0, 1.0, 0.0)), vec4(0.004)))); assert(texture(p3d_TextureHeight[0], coord).r == 4.0 / 255.0); assert(all(lessThan(abs(texture(p3d_TextureHeight[1], coord) - vec4(127 / 255.0, 127 / 255.0, 1.0, 0.0)), vec4(0.004)))); """ env.run_glsl(code, preamble, state=np.get_state()) def test_glsl_frame_number(env): clock = core.ClockObject.get_global_clock() old_frame_count = clock.get_frame_count() try: clock.set_frame_count(123) preamble = """ uniform int osg_FrameNumber; """ code = """ assert(osg_FrameNumber == 123); """ env.run_glsl(code, preamble) finally: clock.set_frame_count(old_frame_count) def test_glsl_write_extract_image_buffer(env): # Tests that we can write to a buffer texture on the GPU, and then extract # the data on the CPU. We test two textures since there was in the past a # where it would only work correctly for one texture. tex1 = core.Texture("tex1") tex1.set_clear_color(0) tex1.setup_buffer_texture(1, core.Texture.T_unsigned_int, core.Texture.F_r32i, core.GeomEnums.UH_static) tex2 = core.Texture("tex2") tex2.set_clear_color(0) tex2.setup_buffer_texture(1, core.Texture.T_int, core.Texture.F_r32i, core.GeomEnums.UH_static) preamble = """ layout(r32ui) uniform uimageBuffer tex1; layout(r32i) uniform iimageBuffer tex2; """ code = """ assert(imageLoad(tex1, 0).r == 0u); assert(imageLoad(tex2, 0).r == 0); imageStore(tex1, 0, uvec4(123)); imageStore(tex2, 0, ivec4(-456)); memoryBarrier(); assert(imageLoad(tex1, 0).r == 123u); assert(imageLoad(tex2, 0).r == -456); """ env.run_glsl(code, preamble, {'tex1': tex1, 'tex2': tex2}) env.extract_texture_data(tex1) env.extract_texture_data(tex2) assert struct.unpack('I', tex1.get_ram_image()) == (123,) assert struct.unpack('i', tex2.get_ram_image()) == (-456,) def test_glsl_compile_error(): """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') shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path) assert shader is None def test_glsl_from_file(): """Test compiling GLSL shaders from files""" vert_path = core.Filename(SHADERS_DIR, 'glsl_simple.vert') frag_path = core.Filename(SHADERS_DIR, 'glsl_simple.frag') shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path) assert shader is not None def test_glsl_from_file_legacy(): """Test compiling GLSL shaders from files""" vert_path = core.Filename(SHADERS_DIR, 'glsl_simple_legacy.vert') frag_path = core.Filename(SHADERS_DIR, 'glsl_simple_legacy.frag') shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path) assert shader is not None def test_glsl_includes(): """Test preprocessing includes in GLSL shaders""" vert_path = core.Filename(SHADERS_DIR, 'glsl_include.vert') frag_path = core.Filename(SHADERS_DIR, 'glsl_simple.frag') shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path) assert shader is not None def test_glsl_includes_legacy(): """Test preprocessing includes in GLSL shaders""" vert_path = core.Filename(SHADERS_DIR, 'glsl_include_legacy.vert') frag_path = core.Filename(SHADERS_DIR, 'glsl_simple_legacy.frag') shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path) assert shader is not None def test_glsl_includes_angle_nodir(): """Test preprocessing includes with angle includes without model-path""" vert_path = core.Filename(SHADERS_DIR, 'glsl_include_angle.vert') frag_path = core.Filename(SHADERS_DIR, 'glsl_simple.frag') shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path) assert shader is None def test_glsl_includes_angle_nodir_legacy(): """Test preprocessing includes with angle includes without model-path""" vert_path = core.Filename(SHADERS_DIR, 'glsl_include_angle_legacy.vert') frag_path = core.Filename(SHADERS_DIR, 'glsl_simple_legacy.frag') shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path) assert shader is None @pytest.fixture def with_current_dir_on_model_path(): model_path = core.get_model_path() model_path.prepend_directory(core.Filename.from_os_specific(os.path.dirname(__file__))) yield model_path.clear_local_value() def test_glsl_includes_angle_withdir(with_current_dir_on_model_path): """Test preprocessing includes with angle includes with model-path""" vert_path = core.Filename(SHADERS_DIR, 'glsl_include_angle.vert') frag_path = core.Filename(SHADERS_DIR, 'glsl_simple.frag') shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path) assert shader is not None def test_glsl_includes_angle_withdir_legacy(with_current_dir_on_model_path): """Test preprocessing includes with angle includes with model-path""" vert_path = core.Filename(SHADERS_DIR, 'glsl_include_angle_legacy.vert') frag_path = core.Filename(SHADERS_DIR, 'glsl_simple_legacy.frag') shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path) assert shader is not None