test_shader.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from panda3d.core import Shader, VirtualFileSystem, Filename
  2. import time
  3. import pytest
  4. @pytest.fixture(scope="session")
  5. def vfs():
  6. return VirtualFileSystem.get_global_ptr()
  7. @pytest.fixture
  8. def ramdir():
  9. """Fixture yielding a fresh ramdisk directory."""
  10. from panda3d.core import VirtualFileMountRamdisk, Filename
  11. vfs = VirtualFileSystem.get_global_ptr()
  12. mount = VirtualFileMountRamdisk()
  13. dir = Filename.temporary("/virtual", "ram.")
  14. assert vfs.mount(mount, dir, 0)
  15. yield dir
  16. vfs.unmount(mount)
  17. def test_shader_load_multi(vfs, ramdir):
  18. # Try non-existent first.
  19. shad0 = Shader.load(Shader.SL_GLSL,
  20. vertex="/nonexistent.glsl",
  21. fragment="/nonexistent.glsl")
  22. assert shad0 is None
  23. vert_file = Filename(ramdir, "shader.glsl")
  24. frag_file = Filename(ramdir, "shader.glsl")
  25. # Now write some actual content to the shader files.
  26. vfs.write_file(vert_file, b"#version 100\nvoid main() {}\n", False)
  27. shad1 = Shader.load(Shader.SL_GLSL, vertex=vert_file, fragment=frag_file)
  28. assert shad1 is not None
  29. assert shad1.this
  30. # Load the same shader, it should return the cached result.
  31. shad2 = Shader.load(Shader.SL_GLSL, vertex=vert_file, fragment=frag_file)
  32. assert shad2 is not None
  33. assert shad1.this == shad2.this
  34. # After waiting a second to make the timestamp different, modify the
  35. # shader and load again, it should result in a different object now
  36. time.sleep(1.0)
  37. vfs.write_file(vert_file, b"#version 110\nvoid main() {}\n", False)
  38. shad2 = Shader.load(Shader.SL_GLSL, vertex=vert_file, fragment=frag_file)
  39. assert shad2.this != shad1.this
  40. def test_shader_load_compute(vfs, ramdir):
  41. # Try non-existent first.
  42. shad0 = Shader.load_compute(Shader.SL_GLSL, "/nonexistent.glsl")
  43. assert shad0 is None
  44. comp_file = Filename(ramdir, "shader.glsl")
  45. # Now write some actual content to the shader file.
  46. vfs.write_file(comp_file, b"#version 100\nvoid main() {}\n", False)
  47. shad1 = Shader.load_compute(Shader.SL_GLSL, comp_file)
  48. assert shad1 is not None
  49. assert shad1.this
  50. # Load the same shader, it should return the cached result.
  51. shad2 = Shader.load_compute(Shader.SL_GLSL, comp_file)
  52. assert shad2 is not None
  53. assert shad1.this == shad2.this
  54. # After waiting a second to make the timestamp different, modify the
  55. # shader and load again, it should result in a different object now
  56. time.sleep(1.0)
  57. vfs.write_file(comp_file, b"#version 110\nvoid main() {}\n", False)
  58. shad2 = Shader.load_compute(Shader.SL_GLSL, comp_file)
  59. assert shad2.this != shad1.this