test_glsl_shader.py 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. from panda3d import core
  2. import os
  3. import struct
  4. import pytest
  5. from _pytest.outcomes import Failed
  6. SHADERS_DIR = core.Filename.from_os_specific(os.path.dirname(__file__))
  7. def test_glsl_test(env):
  8. "Test to make sure that the GLSL tests work correctly."
  9. env.run_glsl("assert(true);")
  10. def test_glsl_test_fail(env):
  11. "Same as above, but making sure that the failure case works correctly."
  12. with pytest.raises(Failed):
  13. env.run_glsl("assert(false);")
  14. def test_glsl_sampler(env):
  15. tex1 = core.Texture("tex1-ubyte-rgba8")
  16. tex1.setup_1d_texture(1, core.Texture.T_unsigned_byte, core.Texture.F_rgba8)
  17. tex1.set_clear_color((0, 2 / 255.0, 1, 1))
  18. tex2 = core.Texture("tex2-float-rgba32")
  19. tex2.setup_2d_texture(1, 1, core.Texture.T_float, core.Texture.F_rgba32)
  20. tex2.set_clear_color((1.0, 2.0, -3.14, 0.0))
  21. tex3 = core.Texture("tex3-float-r32")
  22. tex3.setup_3d_texture(1, 1, 1, core.Texture.T_float, core.Texture.F_r32)
  23. tex3.set_clear_color((0.5, 0.0, 0.0, 1.0))
  24. preamble = """
  25. uniform sampler1D tex1;
  26. uniform sampler2D tex2;
  27. uniform sampler3D tex3;
  28. """
  29. code = """
  30. assert(texture(tex1, 0) == vec4(0, 2 / 255.0, 1, 1));
  31. assert(texture(tex2, vec2(0, 0)) == vec4(1.0, 2.0, -3.14, 0.0));
  32. assert(texture(tex3, vec3(0, 0, 0)).r == 0.5);
  33. """
  34. env.run_glsl(code, preamble, {'tex1': tex1, 'tex2': tex2, 'tex3': tex3})
  35. def test_glsl_isampler(env):
  36. from struct import pack
  37. tex1 = core.Texture("")
  38. tex1.setup_1d_texture(1, core.Texture.T_byte, core.Texture.F_rgba8i)
  39. tex1.set_ram_image(pack('bbbb', 0, 1, 2, 3))
  40. tex2 = core.Texture("")
  41. tex2.setup_2d_texture(1, 1, core.Texture.T_short, core.Texture.F_r16i)
  42. tex2.set_ram_image(pack('h', 4))
  43. tex3 = core.Texture("")
  44. tex3.setup_3d_texture(1, 1, 1, core.Texture.T_int, core.Texture.F_r32i)
  45. tex3.set_ram_image(pack('i', 5))
  46. preamble = """
  47. uniform isampler1D tex1;
  48. uniform isampler2D tex2;
  49. uniform isampler3D tex3;
  50. """
  51. code = """
  52. assert(texelFetch(tex1, 0, 0) == ivec4(0, 1, 2, 3));
  53. assert(texelFetch(tex2, ivec2(0, 0), 0) == ivec4(4, 0, 0, 1));
  54. assert(texelFetch(tex3, ivec3(0, 0, 0), 0) == ivec4(5, 0, 0, 1));
  55. """
  56. env.run_glsl(code, preamble, {'tex1': tex1, 'tex2': tex2, 'tex3': tex3})
  57. def test_glsl_usampler(env):
  58. from struct import pack
  59. tex1 = core.Texture("")
  60. tex1.setup_1d_texture(1, core.Texture.T_unsigned_byte, core.Texture.F_rgba8i)
  61. tex1.set_ram_image(pack('BBBB', 0, 1, 2, 3))
  62. tex2 = core.Texture("")
  63. tex2.setup_2d_texture(1, 1, core.Texture.T_unsigned_short, core.Texture.F_r16i)
  64. tex2.set_ram_image(pack('H', 4))
  65. tex3 = core.Texture("")
  66. tex3.setup_3d_texture(1, 1, 1, core.Texture.T_unsigned_int, core.Texture.F_r32i)
  67. tex3.set_ram_image(pack('I', 5))
  68. preamble = """
  69. uniform usampler1D tex1;
  70. uniform usampler2D tex2;
  71. uniform usampler3D tex3;
  72. """
  73. code = """
  74. assert(texelFetch(tex1, 0, 0) == uvec4(0, 1, 2, 3));
  75. assert(texelFetch(tex2, ivec2(0, 0), 0) == uvec4(4, 0, 0, 1));
  76. assert(texelFetch(tex3, ivec3(0, 0, 0), 0) == uvec4(5, 0, 0, 1));
  77. """
  78. env.run_glsl(code, preamble, {'tex1': tex1, 'tex2': tex2, 'tex3': tex3})
  79. def test_glsl_image(env):
  80. tex1 = core.Texture("")
  81. tex1.setup_1d_texture(1, core.Texture.T_unsigned_byte, core.Texture.F_rgba8)
  82. tex1.set_clear_color((0, 2 / 255.0, 1, 1))
  83. tex2 = core.Texture("")
  84. tex2.setup_2d_texture(1, 1, core.Texture.T_float, core.Texture.F_rgba32)
  85. tex2.set_clear_color((1.0, 2.0, -3.14, 0.0))
  86. preamble = """
  87. layout(rgba8) uniform image1D tex1;
  88. layout(rgba32f) uniform image2D tex2;
  89. """
  90. code = """
  91. assert(imageLoad(tex1, 0) == vec4(0, 2 / 255.0, 1, 1));
  92. assert(imageLoad(tex2, ivec2(0, 0)) == vec4(1.0, 2.0, -3.14, 0.0));
  93. """
  94. env.run_glsl(code, preamble, {'tex1': tex1, 'tex2': tex2})
  95. def test_glsl_iimage(env):
  96. from struct import pack
  97. tex1 = core.Texture("")
  98. tex1.setup_1d_texture(1, core.Texture.T_byte, core.Texture.F_rgba8i)
  99. tex1.set_ram_image(pack('bbbb', 0, 1, 2, 3))
  100. tex2 = core.Texture("")
  101. tex2.setup_2d_texture(1, 1, core.Texture.T_short, core.Texture.F_r16i)
  102. tex2.set_ram_image(pack('h', 4))
  103. tex3 = core.Texture("")
  104. tex3.setup_3d_texture(1, 1, 1, core.Texture.T_int, core.Texture.F_r32i)
  105. tex3.set_ram_image(pack('i', 5))
  106. preamble = """
  107. layout(rgba8i) uniform iimage1D tex1;
  108. layout(r16i) uniform iimage2D tex2;
  109. layout(r32i) uniform iimage3D tex3;
  110. """
  111. code = """
  112. assert(imageLoad(tex1, 0) == ivec4(0, 1, 2, 3));
  113. assert(imageLoad(tex2, ivec2(0, 0)) == ivec4(4, 0, 0, 1));
  114. assert(imageLoad(tex3, ivec3(0, 0, 0)) == ivec4(5, 0, 0, 1));
  115. """
  116. env.run_glsl(code, preamble, {'tex1': tex1, 'tex2': tex2, 'tex3': tex3})
  117. def test_glsl_uimage(env):
  118. from struct import pack
  119. tex1 = core.Texture("")
  120. tex1.setup_1d_texture(1, core.Texture.T_unsigned_byte, core.Texture.F_rgba8i)
  121. tex1.set_ram_image(pack('BBBB', 0, 1, 2, 3))
  122. tex2 = core.Texture("")
  123. tex2.setup_2d_texture(1, 1, core.Texture.T_unsigned_short, core.Texture.F_r16i)
  124. tex2.set_ram_image(pack('H', 4))
  125. tex3 = core.Texture("")
  126. tex3.setup_3d_texture(1, 1, 1, core.Texture.T_unsigned_int, core.Texture.F_r32i)
  127. tex3.set_ram_image(pack('I', 5))
  128. preamble = """
  129. layout(rgba8ui) uniform uimage1D tex1;
  130. layout(r16ui) uniform uimage2D tex2;
  131. layout(r32ui) uniform uimage3D tex3;
  132. """
  133. code = """
  134. assert(imageLoad(tex1, 0) == uvec4(0, 1, 2, 3));
  135. assert(imageLoad(tex2, ivec2(0, 0)) == uvec4(4, 0, 0, 1));
  136. assert(imageLoad(tex3, ivec3(0, 0, 0)) == uvec4(5, 0, 0, 1));
  137. """
  138. env.run_glsl(code, preamble, {'tex1': tex1, 'tex2': tex2, 'tex3': tex3})
  139. def test_glsl_ssbo(env):
  140. from struct import pack
  141. num1 = pack('<i', 1234567)
  142. num2 = pack('<i', -1234567)
  143. buffer1 = core.ShaderBuffer("buffer1", num1, core.GeomEnums.UH_static)
  144. buffer2 = core.ShaderBuffer("buffer2", num2, core.GeomEnums.UH_static)
  145. buffer3 = core.ShaderBuffer("buffer3", 4, core.GeomEnums.UH_static)
  146. preamble = """
  147. layout(std430, binding=0) readonly buffer buffer1 {
  148. int value1;
  149. };
  150. layout(std430, binding=1) buffer buffer2 {
  151. readonly int value2;
  152. };
  153. layout(std430, binding=3) buffer buffer3 {
  154. writeonly int value3;
  155. int value4;
  156. };
  157. """
  158. # Assigning value3 to 999 first proves buffers aren't accidentally aliased
  159. code = """
  160. value3 = 999;
  161. assert(value1 == 1234567);
  162. assert(value2 == -1234567);
  163. """
  164. env.run_glsl(code, preamble,
  165. {'buffer1': buffer1, 'buffer2': buffer2, 'buffer3': buffer3},
  166. version=430)
  167. def test_glsl_ssbo_array(env):
  168. from struct import pack
  169. dummy = pack('<i', 999999)
  170. num1 = pack('<i', 1234567)
  171. num2 = pack('<i', -1234567)
  172. unused = core.ShaderBuffer("unused", dummy, core.GeomEnums.UH_static)
  173. buffer1 = core.ShaderBuffer("buffer1", num1, core.GeomEnums.UH_static)
  174. buffer2 = core.ShaderBuffer("buffer2", num2, core.GeomEnums.UH_static)
  175. preamble = """
  176. struct InsideStruct {
  177. int value;
  178. };
  179. layout(std430, binding=0) buffer test {
  180. readonly InsideStruct inside[1];
  181. } test_ns[3][1];
  182. """
  183. code = """
  184. assert(test_ns[1][0].inside[0].value == 1234567);
  185. assert(test_ns[2][0].inside[0].value == -1234567);
  186. """
  187. env.run_glsl(code, preamble,
  188. {'test[0][0]': unused, 'test[1][0]': buffer1, 'test[2][0]': buffer2},
  189. version=430)
  190. def test_glsl_ssbo_runtime_length(env):
  191. from struct import pack
  192. nums = pack('<ii', 1234, 5678)
  193. ssbo = core.ShaderBuffer("ssbo", nums, core.GeomEnums.UH_static)
  194. preamble = """
  195. layout(std430, binding=0) buffer ssbo {
  196. int values[];
  197. };
  198. """
  199. code = """
  200. assert(values.length() == 2);
  201. assert(values[0] == 1234);
  202. assert(values[1] == 5678);
  203. """
  204. env.run_glsl(code, preamble, {'ssbo': ssbo}, version=430)
  205. def test_glsl_float(env):
  206. inputs = dict(
  207. zero=0,
  208. a=1.23,
  209. b=-829.123,
  210. )
  211. preamble = """
  212. uniform float zero;
  213. uniform float a;
  214. uniform float b;
  215. """
  216. code = """
  217. assert(zero == 0);
  218. assert(abs(a - 1.23) < 0.001);
  219. assert(abs(b - -829.123) < 0.001);
  220. """
  221. env.run_glsl(code, preamble, inputs)
  222. def test_glsl_int(env):
  223. inputs = dict(
  224. zero=0,
  225. intmax=0x7fffffff,
  226. intmin=-0x7fffffff,
  227. )
  228. preamble = """
  229. uniform int zero;
  230. uniform int intmax;
  231. uniform int intmin;
  232. """
  233. code = """
  234. assert(zero == 0);
  235. assert(intmax == 0x7fffffff);
  236. assert(intmin == -0x7fffffff);
  237. """
  238. env.run_glsl(code, preamble, inputs)
  239. def test_glsl_uint(env):
  240. #TODO: fix passing uints greater than intmax
  241. inputs = dict(
  242. zero=0,
  243. intmax=0x7fffffff,
  244. )
  245. preamble = """
  246. uniform uint zero;
  247. uniform uint intmax;
  248. """
  249. code = """
  250. assert(zero == 0u);
  251. assert(intmax == 0x7fffffffu);
  252. """
  253. env.run_glsl(code, preamble, inputs)
  254. #@pytest.mark.xfail(reason="https://github.com/KhronosGroup/SPIRV-Tools/issues/3387")
  255. def test_glsl_bool(env):
  256. flags = dict(
  257. flag1=False,
  258. flag2=0,
  259. flag3=0.0,
  260. flag4=True,
  261. flag5=1,
  262. flag6=3,
  263. )
  264. preamble = """
  265. uniform bool flag1;
  266. uniform bool flag2;
  267. uniform bool flag3;
  268. uniform bool flag4;
  269. uniform bool flag5;
  270. uniform bool flag6;
  271. """
  272. code = """
  273. assert(!flag1);
  274. assert(!flag2);
  275. assert(!flag3);
  276. assert(flag4);
  277. assert(flag5);
  278. assert(flag6);
  279. """
  280. env.run_glsl(code, preamble, flags)
  281. def test_glsl_mat3(env):
  282. param1 = core.LMatrix4f(core.LMatrix3f(1, 2, 3, 4, 5, 6, 7, 8, 9))
  283. param2 = core.LMatrix4d(core.LMatrix3d(10, 11, 12, 13, 14, 15, 16, 17, 18))
  284. param3 = core.NodePath("param3")
  285. param3.set_mat(core.LMatrix3(19, 20, 21, 22, 23, 24, 25, 26, 27))
  286. preamble = """
  287. uniform mat3 param1;
  288. uniform mat3 param2;
  289. uniform mat3 param3;
  290. """
  291. code = """
  292. assert(param1[0] == vec3(1, 2, 3));
  293. assert(param1[1] == vec3(4, 5, 6));
  294. assert(param1[2] == vec3(7, 8, 9));
  295. assert(param2[0] == vec3(10, 11, 12));
  296. assert(param2[1] == vec3(13, 14, 15));
  297. assert(param2[2] == vec3(16, 17, 18));
  298. assert(param3[0] == vec3(19, 20, 21));
  299. assert(param3[1] == vec3(22, 23, 24));
  300. assert(param3[2] == vec3(25, 26, 27));
  301. """
  302. env.run_glsl(code, preamble,
  303. {'param1': param1, 'param2': param2, 'param3': param3})
  304. def test_glsl_mat4(env):
  305. param1 = core.LMatrix4f(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
  306. param2 = core.LMatrix4d(17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)
  307. param3 = core.NodePath("param3")
  308. param3.set_mat(core.LMatrix4(
  309. 33, 34, 35, 36,
  310. 37, 38, 39, 40,
  311. 41, 42, 43, 44,
  312. 45, 46, 47, 48))
  313. preamble = """
  314. uniform mat4 param1;
  315. uniform mat4 param2;
  316. uniform mat4 param3;
  317. """
  318. code = """
  319. assert(param1[0] == vec4(1, 2, 3, 4));
  320. assert(param1[1] == vec4(5, 6, 7, 8));
  321. assert(param1[2] == vec4(9, 10, 11, 12));
  322. assert(param1[3] == vec4(13, 14, 15, 16));
  323. assert(param2[0] == vec4(17, 18, 19, 20));
  324. assert(param2[1] == vec4(21, 22, 23, 24));
  325. assert(param2[2] == vec4(25, 26, 27, 28));
  326. assert(param2[3] == vec4(29, 30, 31, 32));
  327. assert(param3[0] == vec4(33, 34, 35, 36));
  328. assert(param3[1] == vec4(37, 38, 39, 40));
  329. assert(param3[2] == vec4(41, 42, 43, 44));
  330. assert(param3[3] == vec4(45, 46, 47, 48));
  331. """
  332. env.run_glsl(code, preamble,
  333. {'param1': param1, 'param2': param2, 'param3': param3})
  334. def test_glsl_mat3x4(env):
  335. param1 = core.LMatrix4f(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
  336. param2 = core.LMatrix4d(17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)
  337. preamble = """
  338. uniform mat3x4 param1;
  339. uniform mat3x4 param2;
  340. """
  341. code = """
  342. assert(param1[0] == vec4(1, 2, 3, 4));
  343. assert(param1[1] == vec4(5, 6, 7, 8));
  344. assert(param1[2] == vec4(9, 10, 11, 12));
  345. assert(param2[0] == vec4(17, 18, 19, 20));
  346. assert(param2[1] == vec4(21, 22, 23, 24));
  347. assert(param2[2] == vec4(25, 26, 27, 28));
  348. """
  349. env.run_glsl(code, preamble,
  350. {'param1': param1, 'param2': param2})
  351. def test_glsl_mat4x3(env):
  352. param1 = core.LMatrix4f(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
  353. param2 = core.LMatrix4d(17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)
  354. preamble = """
  355. uniform mat4x3 param1;
  356. uniform mat4x3 param2;
  357. uniform mat4x3 param3;
  358. """
  359. code = """
  360. assert(param1[0] == vec3(1, 2, 3));
  361. assert(param1[1] == vec3(5, 6, 7));
  362. assert(param1[2] == vec3(9, 10, 11));
  363. assert(param1[3] == vec3(13, 14, 15));
  364. assert(param2[0] == vec3(17, 18, 19));
  365. assert(param2[1] == vec3(21, 22, 23));
  366. assert(param2[2] == vec3(25, 26, 27));
  367. assert(param2[3] == vec3(29, 30, 31));
  368. """
  369. env.run_glsl(code, preamble,
  370. {'param1': param1, 'param2': param2})
  371. def test_glsl_pta_int(env):
  372. pta = core.PTA_int((0, 1, 2, 3))
  373. preamble = """
  374. uniform int pta[4];
  375. """
  376. code = """
  377. assert(pta[0] == 0);
  378. assert(pta[1] == 1);
  379. assert(pta[2] == 2);
  380. assert(pta[3] == 3);
  381. """
  382. env.run_glsl(code, preamble, {'pta': pta})
  383. def test_glsl_pta_ivec4(env):
  384. pta = core.PTA_LVecBase4i(((0, 1, 2, 3), (4, 5, 6, 7)))
  385. preamble = """
  386. uniform ivec4 pta[2];
  387. """
  388. code = """
  389. assert(pta[0] == ivec4(0, 1, 2, 3));
  390. assert(pta[1] == ivec4(4, 5, 6, 7));
  391. """
  392. env.run_glsl(code, preamble, {'pta': pta})
  393. @pytest.mark.parametrize("type", (core.PTA_LVecBase3f, core.PTA_LVecBase3d, core.PTA_LVecBase3i))
  394. def test_glsl_pta_vec3(env, type):
  395. pta = type((
  396. (0, 1, 2),
  397. (3, 4, 5),
  398. (6, 7, 8),
  399. ))
  400. preamble = """
  401. uniform vec3 pta[3];
  402. """
  403. code = """
  404. assert(pta[0] == vec3(0, 1, 2));
  405. assert(pta[1] == vec3(3, 4, 5));
  406. assert(pta[2] == vec3(6, 7, 8));
  407. """
  408. env.run_glsl(code, preamble, {'pta': pta})
  409. @pytest.mark.parametrize("type", (core.PTA_LVecBase3f, core.PTA_LVecBase3d, core.PTA_LVecBase3i))
  410. def test_glsl_pta_dvec3(env, type):
  411. pta = type((
  412. (0, 1, 2),
  413. (3, 4, 5),
  414. (6, 7, 8),
  415. ))
  416. preamble = """
  417. uniform dvec3 pta[3];
  418. """
  419. code = """
  420. assert(pta[0] == vec3(0, 1, 2));
  421. assert(pta[1] == vec3(3, 4, 5));
  422. assert(pta[2] == vec3(6, 7, 8));
  423. """
  424. env.run_glsl(code, preamble, {'pta': pta})
  425. @pytest.mark.parametrize("type", (core.PTA_LVecBase4f, core.PTA_LVecBase4d, core.PTA_LVecBase4i))
  426. def test_glsl_pta_vec4(env, type):
  427. pta = type((
  428. (0, 1, 2, 3),
  429. (4, 5, 6, 7),
  430. (8, 9, 10, 11),
  431. ))
  432. preamble = """
  433. uniform vec4 pta[4];
  434. """
  435. code = """
  436. assert(pta[0] == vec4(0, 1, 2, 3));
  437. assert(pta[1] == vec4(4, 5, 6, 7));
  438. assert(pta[2] == vec4(8, 9, 10, 11));
  439. """
  440. env.run_glsl(code, preamble, {'pta': pta})
  441. @pytest.mark.parametrize("type", (core.PTA_LVecBase4f, core.PTA_LVecBase4d, core.PTA_LVecBase4i))
  442. def test_glsl_pta_dvec4(env, type):
  443. pta = type((
  444. (0, 1, 2, 3),
  445. (4, 5, 6, 7),
  446. (8, 9, 10, 11),
  447. ))
  448. preamble = """
  449. uniform dvec4 pta[4];
  450. """
  451. code = """
  452. assert(pta[0] == dvec4(0, 1, 2, 3));
  453. assert(pta[1] == dvec4(4, 5, 6, 7));
  454. assert(pta[2] == dvec4(8, 9, 10, 11));
  455. """
  456. env.run_glsl(code, preamble, {'pta': pta})
  457. @pytest.mark.parametrize("type", (core.PTA_LMatrix3f, core.PTA_LMatrix3d))
  458. def test_glsl_pta_mat3(env, type):
  459. pta = type((
  460. (0, 1, 2, 3, 4, 5, 6, 7, 8),
  461. (9, 10, 11, 12, 13, 14, 15, 16, 17),
  462. ))
  463. preamble = """
  464. uniform mat3 pta[2];
  465. """
  466. code = """
  467. assert(pta[0][0] == vec3(0, 1, 2));
  468. assert(pta[0][1] == vec3(3, 4, 5));
  469. assert(pta[0][2] == vec3(6, 7, 8));
  470. assert(pta[1][0] == vec3(9, 10, 11));
  471. assert(pta[1][1] == vec3(12, 13, 14));
  472. assert(pta[1][2] == vec3(15, 16, 17));
  473. """
  474. env.run_glsl(code, preamble, {'pta': pta})
  475. @pytest.mark.parametrize("type", (core.PTA_LMatrix4f, core.PTA_LMatrix4d))
  476. def test_glsl_pta_mat4(env, type):
  477. pta = type((
  478. (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
  479. (16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31),
  480. ))
  481. preamble = """
  482. uniform mat4 pta[2];
  483. """
  484. code = """
  485. assert(pta[0][0] == vec4(0, 1, 2, 3));
  486. assert(pta[0][1] == vec4(4, 5, 6, 7));
  487. assert(pta[0][2] == vec4(8, 9, 10, 11));
  488. assert(pta[0][3] == vec4(12, 13, 14, 15));
  489. assert(pta[1][0] == vec4(16, 17, 18, 19));
  490. assert(pta[1][1] == vec4(20, 21, 22, 23));
  491. assert(pta[1][2] == vec4(24, 25, 26, 27));
  492. assert(pta[1][3] == vec4(28, 29, 30, 31));
  493. """
  494. env.run_glsl(code, preamble, {'pta': pta})
  495. def test_glsl_param_vec4(env):
  496. param = core.ParamVecBase4((0, 1, 2, 3))
  497. preamble = """
  498. uniform vec4 param;
  499. """
  500. code = """
  501. assert(param.x == 0.0);
  502. assert(param.y == 1.0);
  503. assert(param.z == 2.0);
  504. assert(param.w == 3.0);
  505. """
  506. env.run_glsl(code, preamble, {'param': param})
  507. def test_glsl_param_ivec4(env):
  508. param = core.ParamVecBase4i((0, 1, 2, 3))
  509. preamble = """
  510. uniform ivec4 param;
  511. """
  512. code = """
  513. assert(param.x == 0);
  514. assert(param.y == 1);
  515. assert(param.z == 2);
  516. assert(param.w == 3);
  517. """
  518. env.run_glsl(code, preamble, {'param': param})
  519. def test_glsl_struct(env):
  520. preamble = """
  521. uniform struct TestStruct {
  522. vec3 a;
  523. float b;
  524. sampler2D c;
  525. float unused;
  526. vec3 d[2];
  527. vec2 e;
  528. sampler2D f;
  529. } test;
  530. """
  531. code = """
  532. assert(test.a == vec3(1, 2, 3));
  533. assert(test.b == 4);
  534. assert(texture(test.c, vec2(0, 0)).r == 5);
  535. assert(test.d[0] == vec3(6, 7, 8));
  536. assert(test.d[1] == vec3(9, 10, 11));
  537. assert(test.e == vec2(12, 13));
  538. assert(texture(test.f, vec2(0, 0)).r == 14);
  539. """
  540. tex_c = core.Texture('c')
  541. tex_c.setup_2d_texture(1, 1, core.Texture.T_float, core.Texture.F_r32)
  542. tex_c.set_clear_color((5, 0, 0, 0))
  543. tex_f = core.Texture('f')
  544. tex_f.setup_2d_texture(1, 1, core.Texture.T_float, core.Texture.F_r32)
  545. tex_f.set_clear_color((14, 0, 0, 0))
  546. env.run_glsl(code, preamble, {
  547. 'test.unused': 0,
  548. 'test.a': (1, 2, 3),
  549. 'test.b': 4,
  550. 'test.c': tex_c,
  551. 'test.d': [(6, 7, 8), (9, 10, 11)],
  552. 'test.e': [12, 13],
  553. 'test.f': tex_f,
  554. })
  555. def test_glsl_struct_nested(env):
  556. preamble = """
  557. struct TestSubStruct1 {
  558. float a;
  559. float b;
  560. };
  561. struct TestSubStruct2 {
  562. float unused;
  563. sampler2D a;
  564. vec2 b;
  565. };
  566. uniform struct TestStruct {
  567. vec3 a;
  568. TestSubStruct1 b;
  569. TestSubStruct2 c;
  570. float d;
  571. } test;
  572. """
  573. code = """
  574. assert(test.a == vec3(1, 2, 3));
  575. assert(test.b.a == 4);
  576. assert(test.b.b == 5);
  577. assert(texture(test.c.a, vec2(0, 0)).r == 6);
  578. assert(test.c.b == vec2(7, 8));
  579. assert(test.d == 9);
  580. """
  581. tex_c_a = core.Texture()
  582. tex_c_a.setup_2d_texture(1, 1, core.Texture.T_float, core.Texture.F_r32)
  583. tex_c_a.set_clear_color((6, 0, 0, 0))
  584. env.run_glsl(code, preamble, {
  585. 'test.unused': 0,
  586. 'test.a': (1, 2, 3),
  587. 'test.b.a': 4,
  588. 'test.b.b': 5,
  589. 'test.c.unused': 0,
  590. 'test.c.a': tex_c_a,
  591. 'test.c.b': (7, 8),
  592. 'test.d': 9,
  593. })
  594. def test_glsl_struct_array(env):
  595. preamble = """
  596. uniform struct TestStruct {
  597. vec3 a;
  598. sampler2D b;
  599. float unused;
  600. float c;
  601. } test[2];
  602. """
  603. code = """
  604. assert(test[0].a == vec3(1, 2, 3));
  605. assert(texture(test[0].b, vec2(0, 0)).r == 4);
  606. assert(test[0].c == 5);
  607. assert(test[1].a == vec3(6, 7, 8));
  608. assert(texture(test[1].b, vec2(0, 0)).r == 9);
  609. assert(test[1].c == 10);
  610. """
  611. tex_0_b = core.Texture()
  612. tex_0_b.setup_2d_texture(1, 1, core.Texture.T_float, core.Texture.F_r32)
  613. tex_0_b.set_clear_color((4, 0, 0, 0))
  614. tex_1_b = core.Texture()
  615. tex_1_b.setup_2d_texture(1, 1, core.Texture.T_float, core.Texture.F_r32)
  616. tex_1_b.set_clear_color((9, 0, 0, 0))
  617. env.run_glsl(code, preamble, {
  618. 'test[0].unused': 0,
  619. 'test[0].a': (1, 2, 3),
  620. 'test[0].b': tex_0_b,
  621. 'test[0].c': 5,
  622. 'test[1].unused': 0,
  623. 'test[1].a': (6, 7, 8),
  624. 'test[1].b': tex_1_b,
  625. 'test[1].c': 10,
  626. })
  627. def test_glsl_struct_pseudo_light(env):
  628. # Something that looks like a named light source, but isn't one at all
  629. preamble = """
  630. struct FakeLightParameters {
  631. vec4 specular;
  632. vec4 position;
  633. vec3 attenuation;
  634. float constantAttenuation;
  635. float radius;
  636. };
  637. uniform FakeLightParameters test;
  638. """
  639. code = """
  640. assert(test.specular == vec4(1, 2, 3, 4));
  641. assert(test.position == vec4(5, 6, 7, 8));
  642. assert(test.attenuation == vec3(9, 10, 11));
  643. assert(test.constantAttenuation == 12);
  644. assert(test.radius == 13);
  645. """
  646. env.run_glsl(code, preamble, {
  647. 'test.specular': (1, 2, 3, 4),
  648. 'test.position': (5, 6, 7, 8),
  649. 'test.attenuation': (9, 10, 11),
  650. 'test.constantAttenuation': 12,
  651. 'test.radius': 13,
  652. })
  653. def test_glsl_light(env):
  654. preamble = """
  655. uniform struct p3d_LightSourceParameters {
  656. vec4 color;
  657. vec3 ambient;
  658. vec4 diffuse;
  659. vec4 specular;
  660. vec3 position;
  661. vec4 halfVector;
  662. vec4 spotDirection;
  663. float spotCutoff;
  664. float spotCosCutoff;
  665. float spotExponent;
  666. vec3 attenuation;
  667. float constantAttenuation;
  668. float linearAttenuation;
  669. float quadraticAttenuation;
  670. } plight;
  671. """
  672. code = """
  673. assert(plight.color == vec4(1, 2, 3, 4));
  674. assert(plight.ambient == vec3(0, 0, 0));
  675. assert(plight.diffuse == vec4(1, 2, 3, 4));
  676. assert(plight.specular == vec4(5, 6, 7, 8));
  677. assert(plight.position == vec3(9, 10, 11));
  678. assert(plight.spotCutoff == 180);
  679. assert(plight.spotCosCutoff == -1);
  680. assert(plight.spotExponent == 0);
  681. assert(plight.attenuation == vec3(12, 13, 14));
  682. assert(plight.constantAttenuation == 12);
  683. assert(plight.linearAttenuation == 13);
  684. assert(plight.quadraticAttenuation == 14);
  685. """
  686. plight = core.PointLight("plight")
  687. plight.color = (1, 2, 3, 4)
  688. plight.specular_color = (5, 6, 7, 8)
  689. plight.transform = core.TransformState.make_pos((9, 10, 11))
  690. plight.attenuation = (12, 13, 14)
  691. env.run_glsl(code, preamble, {
  692. 'plight': core.NodePath(plight),
  693. })
  694. def test_glsl_named_light_source(env):
  695. spot = core.Spotlight("spot")
  696. spot.get_lens().set_fov(90, 90)
  697. spot.set_color((1, 2, 3, 4))
  698. spot.set_specular_color((5, 6, 7, 8))
  699. preamble = """
  700. struct p3d_LightSourceParameters {
  701. vec4 color;
  702. vec4 specular;
  703. };
  704. uniform p3d_LightSourceParameters spot;
  705. """
  706. code = """
  707. assert(spot.color == vec4(1, 2, 3, 4));
  708. assert(spot.specular == vec4(5, 6, 7, 8));
  709. """
  710. env.run_glsl(code, preamble, {'spot': core.NodePath(spot)})
  711. def test_glsl_state_light(env):
  712. preamble = """
  713. uniform struct p3d_LightSourceParameters {
  714. vec4 color;
  715. vec3 ambient;
  716. vec4 diffuse;
  717. vec4 specular;
  718. vec4 position;
  719. vec4 halfVector;
  720. vec4 spotDirection;
  721. float spotCutoff;
  722. float spotCosCutoff;
  723. float spotExponent;
  724. vec3 attenuation;
  725. float constantAttenuation;
  726. float linearAttenuation;
  727. float quadraticAttenuation;
  728. } p3d_LightSource[2];
  729. """
  730. code = """
  731. assert(p3d_LightSource[0].color == vec4(1, 2, 3, 4));
  732. assert(p3d_LightSource[0].ambient == vec3(0, 0, 0));
  733. assert(p3d_LightSource[0].diffuse == vec4(1, 2, 3, 4));
  734. assert(p3d_LightSource[0].specular == vec4(5, 6, 7, 8));
  735. assert(p3d_LightSource[0].position == vec4(9, 10, 11, 1));
  736. assert(p3d_LightSource[0].spotCutoff == 180);
  737. assert(p3d_LightSource[0].spotCosCutoff == -1);
  738. assert(p3d_LightSource[0].spotExponent == 0);
  739. assert(p3d_LightSource[0].attenuation == vec3(12, 13, 14));
  740. assert(p3d_LightSource[0].constantAttenuation == 12);
  741. assert(p3d_LightSource[0].linearAttenuation == 13);
  742. assert(p3d_LightSource[0].quadraticAttenuation == 14);
  743. assert(p3d_LightSource[1].color == vec4(15, 16, 17, 18));
  744. assert(p3d_LightSource[1].ambient == vec3(0, 0, 0));
  745. assert(p3d_LightSource[1].diffuse == vec4(15, 16, 17, 18));
  746. assert(p3d_LightSource[1].specular == vec4(19, 20, 21, 22));
  747. assert(p3d_LightSource[1].position == vec4(0, 1, 0, 0));
  748. assert(p3d_LightSource[1].spotCutoff == 180);
  749. assert(p3d_LightSource[1].spotCosCutoff == -1);
  750. assert(p3d_LightSource[1].spotExponent == 0);
  751. assert(p3d_LightSource[1].attenuation == vec3(1, 0, 0));
  752. assert(p3d_LightSource[1].constantAttenuation == 1);
  753. assert(p3d_LightSource[1].linearAttenuation == 0);
  754. assert(p3d_LightSource[1].quadraticAttenuation == 0);
  755. """
  756. plight = core.PointLight("plight")
  757. plight.priority = 0
  758. plight.color = (1, 2, 3, 4)
  759. plight.specular_color = (5, 6, 7, 8)
  760. plight.transform = core.TransformState.make_pos((9, 10, 11))
  761. plight.attenuation = (12, 13, 14)
  762. plight_path = core.NodePath(plight)
  763. dlight = core.DirectionalLight("dlight")
  764. dlight.priority = -1
  765. dlight.direction = (0, -1, 0)
  766. dlight.color = (15, 16, 17, 18)
  767. dlight.specular_color = (19, 20, 21, 22)
  768. dlight.transform = core.TransformState.make_pos((23, 24, 25))
  769. dlight_path = core.NodePath(dlight)
  770. lattr = core.LightAttrib.make()
  771. lattr = lattr.add_on_light(plight_path)
  772. lattr = lattr.add_on_light(dlight_path)
  773. state = core.RenderState.make(lattr)
  774. env.run_glsl(code, preamble, state=state)
  775. def test_glsl_state_light_source(env):
  776. spot = core.Spotlight("spot")
  777. spot.priority = 3
  778. spot.get_lens().set_fov(120, 120)
  779. spot.set_color((1, 2, 3, 4))
  780. spot.set_specular_color((5, 6, 7, 8))
  781. spot.attenuation = (23, 24, 25)
  782. spot.exponent = 26
  783. dire = core.DirectionalLight("dire")
  784. dire.priority = 2
  785. dire.set_color((9, 10, 11, 12))
  786. dire.set_specular_color((13, 14, 15, 16))
  787. dire.direction = (17, 18, 19)
  788. preamble = """
  789. struct p3d_LightSourceParameters {
  790. vec4 color;
  791. vec4 specular;
  792. vec4 ambient;
  793. vec4 diffuse;
  794. vec4 position;
  795. vec3 attenuation;
  796. float constantAttenuation;
  797. float linearAttenuation;
  798. float quadraticAttenuation;
  799. float spotExponent;
  800. float spotCosCutoff;
  801. float spotCutoff;
  802. mat4 shadowViewMatrix;
  803. };
  804. uniform p3d_LightSourceParameters p3d_LightSource[3];
  805. """
  806. code = """
  807. assert(p3d_LightSource[0].color == vec4(1, 2, 3, 4));
  808. assert(p3d_LightSource[0].specular == vec4(5, 6, 7, 8));
  809. assert(p3d_LightSource[0].ambient == vec4(0, 0, 0, 1));
  810. assert(p3d_LightSource[0].diffuse == vec4(1, 2, 3, 4));
  811. assert(p3d_LightSource[0].position == vec4(20, 21, 22, 1));
  812. assert(p3d_LightSource[0].attenuation == vec3(23, 24, 25));
  813. assert(p3d_LightSource[0].constantAttenuation == 23);
  814. assert(p3d_LightSource[0].linearAttenuation == 24);
  815. assert(p3d_LightSource[0].quadraticAttenuation == 25);
  816. assert(p3d_LightSource[0].spotExponent == 26);
  817. assert(p3d_LightSource[0].spotCosCutoff > 0.499);
  818. assert(p3d_LightSource[0].spotCosCutoff < 0.501);
  819. assert(p3d_LightSource[0].spotCutoff == 60);
  820. assert(p3d_LightSource[0].shadowViewMatrix[0][0] > 0.2886);
  821. assert(p3d_LightSource[0].shadowViewMatrix[0][0] < 0.2887);
  822. assert(p3d_LightSource[0].shadowViewMatrix[0][1] == 0);
  823. assert(p3d_LightSource[0].shadowViewMatrix[0][2] == 0);
  824. assert(p3d_LightSource[0].shadowViewMatrix[0][3] == 0);
  825. assert(p3d_LightSource[0].shadowViewMatrix[1][0] == 0);
  826. assert(p3d_LightSource[0].shadowViewMatrix[1][1] > 0.2886);
  827. assert(p3d_LightSource[0].shadowViewMatrix[1][1] < 0.2887);
  828. assert(p3d_LightSource[0].shadowViewMatrix[1][2] == 0);
  829. assert(p3d_LightSource[0].shadowViewMatrix[1][3] == 0);
  830. //assert(p3d_LightSource[0].shadowViewMatrix[2][0] == -0.5);
  831. //assert(p3d_LightSource[0].shadowViewMatrix[2][1] == -0.5);
  832. assert(p3d_LightSource[0].shadowViewMatrix[2][2] > -1.00002);
  833. //assert(p3d_LightSource[0].shadowViewMatrix[2][2] < -1.0);
  834. //assert(p3d_LightSource[0].shadowViewMatrix[2][3] == -1);
  835. assert(p3d_LightSource[0].shadowViewMatrix[3][0] > -16.2736);
  836. assert(p3d_LightSource[0].shadowViewMatrix[3][0] < -16.2734);
  837. assert(p3d_LightSource[0].shadowViewMatrix[3][1] > -16.8510);
  838. assert(p3d_LightSource[0].shadowViewMatrix[3][1] < -16.8508);
  839. assert(p3d_LightSource[0].shadowViewMatrix[3][2] > -22.0003);
  840. assert(p3d_LightSource[0].shadowViewMatrix[3][2] < -22.0001);
  841. assert(p3d_LightSource[0].shadowViewMatrix[3][3] > -21.0001);
  842. assert(p3d_LightSource[0].shadowViewMatrix[3][3] < -20.9999);
  843. assert(p3d_LightSource[1].color == vec4(9, 10, 11, 12));
  844. assert(p3d_LightSource[1].specular == vec4(13, 14, 15, 16));
  845. assert(p3d_LightSource[1].diffuse == vec4(9, 10, 11, 12));
  846. assert(p3d_LightSource[1].ambient == vec4(0, 0, 0, 1));
  847. assert(p3d_LightSource[1].position == vec4(-17, -18, -19, 0));
  848. assert(p3d_LightSource[1].attenuation == vec3(1, 0, 0));
  849. assert(p3d_LightSource[1].constantAttenuation == 1);
  850. assert(p3d_LightSource[1].linearAttenuation == 0);
  851. assert(p3d_LightSource[1].quadraticAttenuation == 0);
  852. assert(p3d_LightSource[1].spotExponent == 0);
  853. assert(p3d_LightSource[1].spotCosCutoff == -1);
  854. assert(p3d_LightSource[2].color == vec4(0, 0, 0, 1));
  855. assert(p3d_LightSource[2].specular == vec4(0, 0, 0, 1));
  856. assert(p3d_LightSource[2].diffuse == vec4(0, 0, 0, 1));
  857. assert(p3d_LightSource[2].ambient == vec4(0, 0, 0, 1));
  858. assert(p3d_LightSource[2].position == vec4(0, 0, 1, 0));
  859. assert(p3d_LightSource[2].attenuation == vec3(1, 0, 0));
  860. assert(p3d_LightSource[2].constantAttenuation == 1);
  861. assert(p3d_LightSource[2].linearAttenuation == 0);
  862. assert(p3d_LightSource[2].quadraticAttenuation == 0);
  863. assert(p3d_LightSource[2].spotExponent == 0);
  864. assert(p3d_LightSource[2].spotCosCutoff == -1);
  865. """
  866. node = core.NodePath("state")
  867. spot_path = node.attach_new_node(spot)
  868. spot_path.set_pos(20, 21, 22)
  869. node.set_light(spot_path)
  870. dire_path = node.attach_new_node(dire)
  871. node.set_light(dire_path)
  872. env.run_glsl(code, preamble, state=node.get_state())
  873. def test_glsl_state_material(env):
  874. mat = core.Material("mat")
  875. mat.ambient = (1, 2, 3, 4)
  876. mat.diffuse = (5, 6, 7, 8)
  877. mat.emission = (9, 10, 11, 12)
  878. mat.specular = (13, 14, 15, 0)
  879. mat.shininess = 16
  880. mat.metallic = 0.5
  881. mat.refractive_index = 21
  882. preamble = """
  883. struct p3d_MaterialParameters {
  884. vec4 ambient;
  885. vec4 diffuse;
  886. vec4 emission;
  887. vec3 specular;
  888. float shininess;
  889. float metallic;
  890. float refractiveIndex;
  891. };
  892. uniform p3d_MaterialParameters p3d_Material;
  893. """
  894. code = """
  895. assert(p3d_Material.ambient == vec4(1, 2, 3, 4));
  896. assert(p3d_Material.diffuse == vec4(5, 6, 7, 8));
  897. assert(p3d_Material.emission == vec4(9, 10, 11, 12));
  898. assert(p3d_Material.specular == vec3(13, 14, 15));
  899. assert(p3d_Material.shininess == 16);
  900. assert(p3d_Material.metallic == 0.5);
  901. assert(p3d_Material.refractiveIndex == 21);
  902. """
  903. node = core.NodePath("state")
  904. node.set_material(mat)
  905. env.run_glsl(code, preamble, state=node.get_state())
  906. def test_glsl_state_material_pbr(env):
  907. mat = core.Material("mat")
  908. mat.base_color = (1, 2, 3, 4)
  909. mat.emission = (9, 10, 11, 12)
  910. mat.roughness = 16
  911. mat.metallic = 0.5
  912. mat.refractive_index = 21
  913. preamble = """
  914. struct p3d_MaterialParameters {
  915. vec4 baseColor;
  916. vec4 emission;
  917. float metallic;
  918. float refractiveIndex;
  919. float roughness;
  920. };
  921. uniform p3d_MaterialParameters p3d_Material;
  922. """
  923. code = """
  924. assert(p3d_Material.baseColor == vec4(1, 2, 3, 4));
  925. assert(p3d_Material.emission == vec4(9, 10, 11, 12));
  926. assert(p3d_Material.roughness == 16);
  927. assert(p3d_Material.metallic == 0.5);
  928. assert(p3d_Material.refractiveIndex == 21);
  929. """
  930. node = core.NodePath("state")
  931. node.set_material(mat)
  932. env.run_glsl(code, preamble, state=node.get_state())
  933. def test_glsl_state_fog(env):
  934. fog = core.Fog("fog")
  935. fog.color = (1, 2, 3, 4)
  936. fog.exp_density = 0.5
  937. fog.set_linear_range(6, 10)
  938. preamble = """
  939. struct p3d_FogParameters {
  940. vec4 color;
  941. float density;
  942. float start;
  943. float end;
  944. float scale;
  945. };
  946. uniform p3d_FogParameters p3d_Fog;
  947. """
  948. code = """
  949. assert(p3d_Fog.color == vec4(1, 2, 3, 4));
  950. assert(p3d_Fog.density == 0.5);
  951. assert(p3d_Fog.start == 6);
  952. assert(p3d_Fog.end == 10);
  953. assert(p3d_Fog.scale == 0.25);
  954. """
  955. node = core.NodePath("state")
  956. node.set_fog(fog)
  957. env.run_glsl(code, preamble, state=node.get_state())
  958. def test_glsl_state_texture(env):
  959. def gen_texture(v):
  960. tex = core.Texture(f"tex{v}")
  961. tex.setup_2d_texture(1, 1, core.Texture.T_unsigned_byte, core.Texture.F_red)
  962. tex.set_clear_color((v / 255.0, 0, 0, 0))
  963. return tex
  964. np = core.NodePath("test")
  965. ts1 = core.TextureStage("ts1")
  966. ts1.sort = 10
  967. ts1.mode = core.TextureStage.M_modulate
  968. np.set_texture(ts1, gen_texture(1))
  969. ts2 = core.TextureStage("ts2")
  970. ts2.sort = 20
  971. ts2.mode = core.TextureStage.M_add
  972. np.set_texture(ts2, gen_texture(2))
  973. ts3 = core.TextureStage("ts3")
  974. ts3.sort = 30
  975. ts3.mode = core.TextureStage.M_modulate
  976. np.set_texture(ts3, gen_texture(3))
  977. ts4 = core.TextureStage("ts4")
  978. ts4.sort = 40
  979. ts4.mode = core.TextureStage.M_normal_height
  980. np.set_texture(ts4, gen_texture(4))
  981. ts5 = core.TextureStage("ts5")
  982. ts5.sort = 50
  983. ts5.mode = core.TextureStage.M_add
  984. np.set_texture(ts5, gen_texture(5))
  985. ts6 = core.TextureStage("ts6")
  986. ts6.sort = 60
  987. ts6.mode = core.TextureStage.M_normal
  988. np.set_texture(ts6, gen_texture(6))
  989. # Do this in multiple passes to stay under sampler limit of 16
  990. preamble = """
  991. uniform sampler2D p3d_Texture2;
  992. uniform sampler2D p3d_Texture0;
  993. uniform sampler2D p3d_Texture1;
  994. uniform sampler2D p3d_Texture3;
  995. uniform sampler2D p3d_Texture4;
  996. uniform sampler2D p3d_Texture5;
  997. uniform sampler2D p3d_Texture6;
  998. uniform sampler2D p3d_Texture[7];
  999. """
  1000. code = """
  1001. vec2 coord = vec2(0, 0);
  1002. assert(abs(texture(p3d_Texture2, coord).r - 3.0 / 255.0) < 0.001);
  1003. assert(abs(texture(p3d_Texture0, coord).r - 1.0 / 255.0) < 0.001);
  1004. assert(abs(texture(p3d_Texture1, coord).r - 2.0 / 255.0) < 0.001);
  1005. assert(abs(texture(p3d_Texture3, coord).r - 4.0 / 255.0) < 0.001);
  1006. assert(abs(texture(p3d_Texture4, coord).r - 5.0 / 255.0) < 0.001);
  1007. assert(abs(texture(p3d_Texture5, coord).r - 6.0 / 255.0) < 0.001);
  1008. assert(texture(p3d_Texture6, coord).r == 1.0);
  1009. assert(abs(texture(p3d_Texture[0], coord).r - 1.0 / 255.0) < 0.001);
  1010. assert(abs(texture(p3d_Texture[2], coord).r - 3.0 / 255.0) < 0.001);
  1011. assert(abs(texture(p3d_Texture[3], coord).r - 4.0 / 255.0) < 0.001);
  1012. assert(abs(texture(p3d_Texture[1], coord).r - 2.0 / 255.0) < 0.001);
  1013. assert(abs(texture(p3d_Texture[4], coord).r - 5.0 / 255.0) < 0.001);
  1014. assert(abs(texture(p3d_Texture[5], coord).r - 6.0 / 255.0) < 0.001);
  1015. assert(texture(p3d_Texture[6], coord).r == 1.0);
  1016. """
  1017. env.run_glsl(code, preamble, state=np.get_state())
  1018. preamble = """
  1019. uniform sampler2D p3d_TextureFF[5];
  1020. uniform sampler2D p3d_TextureModulate[3];
  1021. uniform sampler2D p3d_TextureAdd[3];
  1022. uniform sampler2D p3d_TextureNormal[3];
  1023. uniform sampler2D p3d_TextureHeight[2];
  1024. """
  1025. code = """
  1026. vec2 coord = vec2(0, 0);
  1027. assert(abs(texture(p3d_TextureFF[0], coord).r - 1.0 / 255.0) < 0.001);
  1028. assert(abs(texture(p3d_TextureFF[1], coord).r - 2.0 / 255.0) < 0.001);
  1029. assert(abs(texture(p3d_TextureFF[2], coord).r - 3.0 / 255.0) < 0.001);
  1030. assert(abs(texture(p3d_TextureFF[3], coord).r - 5.0 / 255.0) < 0.001);
  1031. assert(texture(p3d_TextureFF[4], coord).r == 1.0);
  1032. assert(abs(texture(p3d_TextureModulate[0], coord).r - 1.0 / 255.0) < 0.001);
  1033. assert(abs(texture(p3d_TextureModulate[1], coord).r - 3.0 / 255.0) < 0.001);
  1034. assert(texture(p3d_TextureModulate[2], coord).r == 1.0);
  1035. assert(abs(texture(p3d_TextureAdd[0], coord).r - 2.0 / 255.0) < 0.001);
  1036. assert(abs(texture(p3d_TextureAdd[1], coord).r - 5.0 / 255.0) < 0.001);
  1037. assert(texture(p3d_TextureAdd[2], coord) == vec4(0.0, 0.0, 0.0, 1.0));
  1038. assert(abs(texture(p3d_TextureNormal[0], coord).r - 4.0 / 255.0) < 0.001);
  1039. assert(abs(texture(p3d_TextureNormal[1], coord).r - 6.0 / 255.0) < 0.001);
  1040. assert(all(lessThan(abs(texture(p3d_TextureNormal[2], coord) - vec4(127 / 255.0, 127 / 255.0, 1.0, 0.0)), vec4(0.004))));
  1041. assert(texture(p3d_TextureHeight[0], coord).r == 4.0 / 255.0);
  1042. assert(all(lessThan(abs(texture(p3d_TextureHeight[1], coord) - vec4(127 / 255.0, 127 / 255.0, 1.0, 0.0)), vec4(0.004))));
  1043. """
  1044. env.run_glsl(code, preamble, state=np.get_state())
  1045. def test_glsl_frame_number(env):
  1046. clock = core.ClockObject.get_global_clock()
  1047. old_frame_count = clock.get_frame_count()
  1048. try:
  1049. clock.set_frame_count(123)
  1050. preamble = """
  1051. uniform int osg_FrameNumber;
  1052. """
  1053. code = """
  1054. assert(osg_FrameNumber == 123);
  1055. """
  1056. env.run_glsl(code, preamble)
  1057. finally:
  1058. clock.set_frame_count(old_frame_count)
  1059. def test_glsl_write_extract_image_buffer(env):
  1060. # Tests that we can write to a buffer texture on the GPU, and then extract
  1061. # the data on the CPU. We test two textures since there was in the past a
  1062. # where it would only work correctly for one texture.
  1063. tex1 = core.Texture("tex1")
  1064. tex1.set_clear_color(0)
  1065. tex1.setup_buffer_texture(1, core.Texture.T_unsigned_int, core.Texture.F_r32i,
  1066. core.GeomEnums.UH_static)
  1067. tex2 = core.Texture("tex2")
  1068. tex2.set_clear_color(0)
  1069. tex2.setup_buffer_texture(1, core.Texture.T_int, core.Texture.F_r32i,
  1070. core.GeomEnums.UH_static)
  1071. preamble = """
  1072. layout(r32ui) uniform uimageBuffer tex1;
  1073. layout(r32i) uniform iimageBuffer tex2;
  1074. """
  1075. code = """
  1076. assert(imageLoad(tex1, 0).r == 0u);
  1077. assert(imageLoad(tex2, 0).r == 0);
  1078. imageStore(tex1, 0, uvec4(123));
  1079. imageStore(tex2, 0, ivec4(-456));
  1080. memoryBarrier();
  1081. assert(imageLoad(tex1, 0).r == 123u);
  1082. assert(imageLoad(tex2, 0).r == -456);
  1083. """
  1084. env.run_glsl(code, preamble, {'tex1': tex1, 'tex2': tex2})
  1085. env.extract_texture_data(tex1)
  1086. env.extract_texture_data(tex2)
  1087. assert struct.unpack('I', tex1.get_ram_image()) == (123,)
  1088. assert struct.unpack('i', tex2.get_ram_image()) == (-456,)
  1089. def test_glsl_compile_error():
  1090. """Test getting compile errors from bad shaders"""
  1091. vert_path = core.Filename(SHADERS_DIR, 'glsl_bad.vert')
  1092. frag_path = core.Filename(SHADERS_DIR, 'glsl_simple.frag')
  1093. shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path)
  1094. assert shader is None
  1095. def test_glsl_from_file():
  1096. """Test compiling GLSL shaders from files"""
  1097. vert_path = core.Filename(SHADERS_DIR, 'glsl_simple.vert')
  1098. frag_path = core.Filename(SHADERS_DIR, 'glsl_simple.frag')
  1099. shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path)
  1100. assert shader is not None
  1101. def test_glsl_from_file_legacy():
  1102. """Test compiling GLSL shaders from files"""
  1103. vert_path = core.Filename(SHADERS_DIR, 'glsl_simple_legacy.vert')
  1104. frag_path = core.Filename(SHADERS_DIR, 'glsl_simple_legacy.frag')
  1105. shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path)
  1106. assert shader is not None
  1107. def test_glsl_includes():
  1108. """Test preprocessing includes in GLSL shaders"""
  1109. vert_path = core.Filename(SHADERS_DIR, 'glsl_include.vert')
  1110. frag_path = core.Filename(SHADERS_DIR, 'glsl_simple.frag')
  1111. shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path)
  1112. assert shader is not None
  1113. def test_glsl_includes_legacy():
  1114. """Test preprocessing includes in GLSL shaders"""
  1115. vert_path = core.Filename(SHADERS_DIR, 'glsl_include_legacy.vert')
  1116. frag_path = core.Filename(SHADERS_DIR, 'glsl_simple_legacy.frag')
  1117. shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path)
  1118. assert shader is not None
  1119. def test_glsl_includes_angle_nodir():
  1120. """Test preprocessing includes with angle includes without model-path"""
  1121. vert_path = core.Filename(SHADERS_DIR, 'glsl_include_angle.vert')
  1122. frag_path = core.Filename(SHADERS_DIR, 'glsl_simple.frag')
  1123. shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path)
  1124. assert shader is None
  1125. def test_glsl_includes_angle_nodir_legacy():
  1126. """Test preprocessing includes with angle includes without model-path"""
  1127. vert_path = core.Filename(SHADERS_DIR, 'glsl_include_angle_legacy.vert')
  1128. frag_path = core.Filename(SHADERS_DIR, 'glsl_simple_legacy.frag')
  1129. shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path)
  1130. assert shader is None
  1131. @pytest.fixture
  1132. def with_current_dir_on_model_path():
  1133. model_path = core.get_model_path()
  1134. model_path.prepend_directory(core.Filename.from_os_specific(os.path.dirname(__file__)))
  1135. yield
  1136. model_path.clear_local_value()
  1137. def test_glsl_includes_angle_withdir(with_current_dir_on_model_path):
  1138. """Test preprocessing includes with angle includes with model-path"""
  1139. vert_path = core.Filename(SHADERS_DIR, 'glsl_include_angle.vert')
  1140. frag_path = core.Filename(SHADERS_DIR, 'glsl_simple.frag')
  1141. shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path)
  1142. assert shader is not None
  1143. def test_glsl_includes_angle_withdir_legacy(with_current_dir_on_model_path):
  1144. """Test preprocessing includes with angle includes with model-path"""
  1145. vert_path = core.Filename(SHADERS_DIR, 'glsl_include_angle_legacy.vert')
  1146. frag_path = core.Filename(SHADERS_DIR, 'glsl_simple_legacy.frag')
  1147. shader = core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path)
  1148. assert shader is not None