test_glsl_caps.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. from panda3d import core
  2. Shader = core.Shader
  3. Stage = core.Shader.Stage
  4. def compile_and_get_caps(stage, code):
  5. registry = core.ShaderCompilerRegistry.get_global_ptr()
  6. compiler = registry.get_compiler_from_language(Shader.SL_GLSL)
  7. stream = core.StringStream(code.encode('ascii'))
  8. module = compiler.compile_now(stage, stream, 'test-shader')
  9. assert module
  10. assert module.stage == stage
  11. # Mask out this bit, every shader should have it
  12. assert (module.used_capabilities & Shader.C_basic_shader) != 0
  13. return module.used_capabilities & ~Shader.C_basic_shader
  14. def test_legacy_glsl_caps_basic_vertex():
  15. assert compile_and_get_caps(Stage.vertex, """
  16. #version 120
  17. void main() {
  18. gl_Position = vec4(0.0);
  19. }
  20. """) == 0
  21. def test_legacy_glsl_caps_basic_fragment():
  22. assert compile_and_get_caps(Stage.vertex, """
  23. #version 120
  24. void main() {
  25. gl_FragColor = vec4(0.0);
  26. }
  27. """) == 0
  28. def test_legacy_glsl_caps_unified_model():
  29. assert compile_and_get_caps(Stage.vertex, """
  30. #version 130
  31. void main() {
  32. gl_Position = vec4(0.0);
  33. }
  34. """) == Shader.C_unified_model
  35. # Optional extension doesn't trigger it
  36. assert compile_and_get_caps(Stage.vertex, """
  37. #version 120
  38. #extension GL_EXT_gpu_shader4 : enable
  39. void main() {
  40. gl_Position = vec4(0.0);
  41. }
  42. """) == 0
  43. # Required extension does
  44. assert compile_and_get_caps(Stage.vertex, """
  45. #version 120
  46. #extension GL_EXT_gpu_shader4 : require
  47. void main() {
  48. gl_Position = vec4(0.0);
  49. }
  50. """) == Shader.C_unified_model
  51. # Extension under #if doesn't either
  52. assert compile_and_get_caps(Stage.vertex, """
  53. #version 120
  54. #if foobar
  55. #extension GL_EXT_gpu_shader4 : require
  56. #endif
  57. void main() {
  58. gl_Position = vec4(0.0);
  59. }
  60. """) == 0
  61. def test_glsl_caps_basic_vertex():
  62. assert compile_and_get_caps(Stage.vertex, """
  63. #version 330
  64. void main() {
  65. gl_Position = vec4(0.0);
  66. }
  67. """) == 0
  68. def test_glsl_caps_basic_fragment():
  69. assert compile_and_get_caps(Stage.fragment, """
  70. #version 330
  71. out vec4 p3d_FragColor;
  72. void main() {
  73. p3d_FragColor = vec4(1.0);
  74. }
  75. """) == 0
  76. def test_glsl_caps_vertex_texture():
  77. assert compile_and_get_caps(Stage.vertex, """
  78. #version 330
  79. uniform sampler2D tex;
  80. void main() {
  81. gl_Position = textureLod(tex, vec2(0.0), 0);
  82. }
  83. """) == Shader.C_vertex_texture
  84. def test_glsl_caps_point_coord():
  85. assert compile_and_get_caps(Stage.fragment, """
  86. #version 330
  87. out vec4 p3d_FragColor;
  88. void main() {
  89. p3d_FragColor = vec4(gl_PointCoord, 0.0, 1.0);
  90. }
  91. """) == Shader.C_point_coord
  92. def test_glsl_caps_standard_derivatives():
  93. assert compile_and_get_caps(Stage.fragment, """
  94. #version 330
  95. out vec4 p3d_FragColor;
  96. void main() {
  97. p3d_FragColor = vec4(fwidth(gl_FragCoord.x), 0.0, 0.0, 1.0);
  98. }
  99. """) == Shader.C_standard_derivatives
  100. def test_glsl_caps_shadow_samplers():
  101. assert compile_and_get_caps(Stage.fragment, """
  102. #version 330
  103. uniform sampler2DShadow a;
  104. void main() {
  105. gl_FragColor = vec4(texture(a, vec3(0.0, 0.0, 0.0)));
  106. }
  107. """) == Shader.C_shadow_samplers
  108. def test_glsl_caps_non_square_matrices():
  109. assert compile_and_get_caps(Stage.vertex, """
  110. #version 330
  111. uniform mat3x4 a;
  112. void main() {
  113. gl_Position = a * vec3(1.0);
  114. }
  115. """) == Shader.C_non_square_matrices
  116. def test_glsl_caps_unified_model_uint_uniform():
  117. assert compile_and_get_caps(Stage.vertex, """
  118. #version 330
  119. uniform uint a;
  120. void main() {
  121. gl_Position = vec4(float(a));
  122. }
  123. """) == Shader.C_unified_model
  124. def test_glsl_caps_unified_model_int_varying():
  125. assert compile_and_get_caps(Stage.fragment, """
  126. #version 330
  127. flat in int a;
  128. void main() {
  129. gl_FragColor = vec4(float(a), 0.0, 0.0, 1.0);
  130. }
  131. """) == Shader.C_unified_model
  132. def test_glsl_caps_unified_model_round():
  133. # DO NOT match unified model for regular round()
  134. assert compile_and_get_caps(Stage.vertex, """
  135. #version 330
  136. uniform float a;
  137. void main() {
  138. gl_Position = vec4(round(a));
  139. }
  140. """) == 0
  141. # DO match it for roundEven()
  142. assert compile_and_get_caps(Stage.vertex, """
  143. #version 330
  144. uniform float a;
  145. void main() {
  146. gl_Position = vec4(roundEven(a));
  147. }
  148. """) == Shader.C_unified_model
  149. def test_glsl_caps_noperspective_interpolation():
  150. assert compile_and_get_caps(Stage.fragment, """
  151. #version 330
  152. noperspective in vec4 a;
  153. void main() {
  154. gl_FragColor = a;
  155. }
  156. """) == Shader.C_noperspective_interpolation
  157. def test_glsl_caps_texture_array():
  158. assert compile_and_get_caps(Stage.fragment, """
  159. #version 330
  160. uniform sampler2DArray a;
  161. void main() {
  162. gl_FragColor = texture(a, vec3(0.0));
  163. }
  164. """) == Shader.C_texture_array
  165. def test_glsl_caps_texture_lod():
  166. assert compile_and_get_caps(Stage.fragment, """
  167. #version 330
  168. uniform sampler2D a;
  169. void main() {
  170. gl_FragColor = textureLod(a, vec2(0.0), 0);
  171. }
  172. """) == Shader.C_texture_lod
  173. def test_glsl_caps_texture_query_size():
  174. assert compile_and_get_caps(Stage.fragment, """
  175. #version 330
  176. uniform sampler2D a;
  177. void main() {
  178. gl_FragColor = vec4(vec2(textureSize(a, 0)), 0.0, 1.0);
  179. }
  180. """) == Shader.C_texture_query_size
  181. def test_glsl_caps_sampler_cube_shadow():
  182. assert compile_and_get_caps(Stage.fragment, """
  183. #version 330
  184. uniform samplerCubeShadow a;
  185. void main() {
  186. gl_FragColor = vec4(texture(a, vec4(0.0)), 0.0, 0.0, 1.0);
  187. }
  188. """) == Shader.C_shadow_samplers | Shader.C_sampler_cube_shadow
  189. def test_glsl_caps_vertex_id():
  190. assert compile_and_get_caps(Stage.vertex, """
  191. #version 330
  192. void main() {
  193. gl_Position = vec4(float(gl_VertexID), 0.0, 0.0, 1.0);
  194. }
  195. """) == Shader.C_vertex_id
  196. def test_glsl_caps_draw_buffers():
  197. assert compile_and_get_caps(Stage.fragment, """
  198. #version 330
  199. layout(location=0) out vec4 a;
  200. layout(location=1) out vec4 b;
  201. void main() {
  202. a = vec4(1.0, 0.0, 0.0, 1.0);
  203. b = vec4(0.0, 1.0, 0.0, 1.0);
  204. }
  205. """) == Shader.C_draw_buffers
  206. def test_glsl_caps_clip_distance():
  207. assert compile_and_get_caps(Stage.vertex, """
  208. #version 330
  209. void main() {
  210. gl_Position = vec4(0.0);
  211. gl_ClipDistance[0] = 0.0;
  212. }
  213. """) == Shader.C_clip_distance
  214. def test_glsl_caps_instance_id():
  215. assert compile_and_get_caps(Stage.vertex, """
  216. #version 330
  217. void main() {
  218. gl_Position = vec4(float(gl_InstanceID), 0.0, 0.0, 1.0);
  219. }
  220. """) == Shader.C_instance_id
  221. def test_glsl_caps_geometry_shader():
  222. assert compile_and_get_caps(Stage.geometry, """
  223. #version 330
  224. layout(triangles) in;
  225. layout(triangle_strip, max_vertices=3) out;
  226. void main() {
  227. gl_Position = gl_in[0].gl_Position;
  228. EmitVertex();
  229. gl_Position = gl_in[1].gl_Position;
  230. EmitVertex();
  231. gl_Position = gl_in[2].gl_Position;
  232. EmitVertex();
  233. EndPrimitive();
  234. }
  235. """) == Shader.C_geometry_shader
  236. def test_glsl_caps_primitive_id():
  237. assert compile_and_get_caps(Stage.fragment, """
  238. #version 330
  239. void main() {
  240. gl_FragColor = vec4(float(gl_PrimitiveID), 0.0, 0.0, 1.0);
  241. }
  242. """) == Shader.C_primitive_id
  243. def test_glsl_caps_bit_encoding():
  244. assert compile_and_get_caps(Stage.vertex, """
  245. #version 330
  246. uniform uint a;
  247. void main() {
  248. gl_Position = vec4(uintBitsToFloat(a));
  249. }
  250. """) == Shader.C_unified_model | Shader.C_bit_encoding
  251. def test_glsl_caps_texture_query_lod():
  252. assert compile_and_get_caps(Stage.fragment, """
  253. #version 400
  254. uniform sampler2D a;
  255. void main() {
  256. gl_FragColor = vec4(textureQueryLod(a, vec2(0.0)), 0.0, 1.0);
  257. }
  258. """) == Shader.C_texture_query_lod
  259. def test_glsl_caps_texture_gather_red():
  260. assert compile_and_get_caps(Stage.fragment, """
  261. #version 400
  262. uniform sampler2D a;
  263. void main() {
  264. gl_FragColor = textureGather(a, vec2(0.0)) + textureGather(a, vec2(0.0), 0);
  265. }
  266. """) == Shader.C_texture_gather_red
  267. def test_glsl_caps_texture_gather_any():
  268. assert compile_and_get_caps(Stage.fragment, """
  269. #version 400
  270. uniform sampler2D a;
  271. void main() {
  272. gl_FragColor = textureGather(a, vec2(0.0), 1);
  273. }
  274. """) == Shader.C_texture_gather_any
  275. def test_glsl_caps_extended_arithmetic():
  276. assert compile_and_get_caps(Stage.fragment, """
  277. #version 400
  278. uniform uint a;
  279. uniform uint b;
  280. void main() {
  281. uint c;
  282. uint d = uaddCarry(a, b, c);
  283. gl_FragColor = vec4(a, b, c, d);
  284. }
  285. """) == Shader.C_unified_model | Shader.C_extended_arithmetic
  286. def test_glsl_caps_double():
  287. assert compile_and_get_caps(Stage.vertex, """
  288. #version 400
  289. uniform double a;
  290. void main() {
  291. gl_Position = vec4(float(a), 0.0, 0.0, 1.0);
  292. }
  293. """) == Shader.C_double
  294. def test_glsl_caps_cube_map_array():
  295. assert compile_and_get_caps(Stage.fragment, """
  296. #version 400
  297. uniform samplerCubeArray a;
  298. void main() {
  299. gl_FragColor = texture(a, vec4(0.0));
  300. }
  301. """) == Shader.C_texture_array | Shader.C_cube_map_array
  302. def test_glsl_caps_geometry_shader_instancing():
  303. assert compile_and_get_caps(Stage.geometry, """
  304. #version 400
  305. layout(triangles, invocations=2) in;
  306. layout(triangle_strip, max_vertices=3) out;
  307. void main() {
  308. gl_Position = gl_in[0].gl_Position;
  309. EmitVertex();
  310. gl_Position = gl_in[1].gl_Position;
  311. EmitVertex();
  312. gl_Position = gl_in[2].gl_Position;
  313. EmitVertex();
  314. EndPrimitive();
  315. }
  316. """) == Shader.C_geometry_shader | Shader.C_geometry_shader_instancing
  317. def test_glsl_caps_tessellation_shader():
  318. assert compile_and_get_caps(Stage.tess_control, """
  319. #version 400 core
  320. layout (vertices = 3) out;
  321. void main() {
  322. gl_TessLevelOuter[0] = 1;
  323. gl_TessLevelOuter[1] = 1;
  324. gl_TessLevelOuter[2] = 1;
  325. gl_TessLevelInner[0] = 1;
  326. }
  327. """) == Shader.C_tessellation_shader
  328. assert compile_and_get_caps(Stage.tess_evaluation, """
  329. #version 400 core
  330. layout(triangles, equal_spacing, ccw) in;
  331. void main() {
  332. gl_Position = vec4(0.0);
  333. }
  334. """) == Shader.C_tessellation_shader
  335. def test_glsl_caps_sample_variables():
  336. assert compile_and_get_caps(Stage.fragment, """
  337. #version 330
  338. #extension GL_ARB_sample_shading : require
  339. void main() {
  340. gl_FragColor = vec4(float(gl_SampleID), 0.0, 0.0, 1.0);
  341. }
  342. """) == Shader.C_sample_variables
  343. def test_glsl_caps_multisample_interpolation():
  344. assert compile_and_get_caps(Stage.fragment, """
  345. #version 400
  346. sample in vec4 a;
  347. out vec4 p3d_FragColor;
  348. void main() {
  349. p3d_FragColor = a;
  350. }
  351. """) == Shader.C_multisample_interpolation
  352. def test_glsl_caps_dynamic_indexing():
  353. assert compile_and_get_caps(Stage.fragment, """
  354. #version 400
  355. uniform int a;
  356. uniform sampler2D b[3];
  357. out vec4 p3d_FragColor;
  358. void main() {
  359. p3d_FragColor = texture(b[a], vec2(0.0));
  360. }
  361. """) == Shader.C_dynamic_indexing
  362. # NOT dynamic indexing
  363. assert compile_and_get_caps(Stage.fragment, """
  364. #version 330
  365. const int a = 2;
  366. uniform sampler2D b[3];
  367. out vec4 p3d_FragColor;
  368. void main() {
  369. p3d_FragColor = texture(b[a], vec2(0.0));
  370. }
  371. """) == 0
  372. def test_glsl_caps_atomic_counters():
  373. assert compile_and_get_caps(Stage.vertex, """
  374. #version 420
  375. uniform atomic_uint a;
  376. void main() {
  377. gl_Position = vec4(float(atomicCounter(a)));
  378. }
  379. """) == Shader.C_unified_model | Shader.C_atomic_counters
  380. def test_glsl_caps_image_load_store():
  381. assert compile_and_get_caps(Stage.fragment, """
  382. #version 420
  383. layout(rgba8) uniform readonly image2D a;
  384. out vec4 p3d_FragColor;
  385. void main() {
  386. p3d_FragColor = imageLoad(a, ivec2(0, 0));
  387. }
  388. """) == Shader.C_image_load_store
  389. def test_glsl_caps_image_atomic():
  390. assert compile_and_get_caps(Stage.fragment, """
  391. #version 420
  392. layout(r32i) uniform iimage1D a;
  393. out vec4 p3d_FragColor;
  394. void main() {
  395. imageAtomicExchange(a, 0, 0);
  396. p3d_FragColor = vec4(0.0);
  397. }
  398. """) == Shader.C_image_load_store | Shader.C_image_atomic
  399. def test_glsl_caps_image_query_size():
  400. assert compile_and_get_caps(Stage.fragment, """
  401. #version 420
  402. #extension GL_ARB_shader_image_size : require
  403. uniform writeonly image2D a;
  404. out vec4 p3d_FragColor;
  405. void main() {
  406. p3d_FragColor = vec4(vec2(imageSize(a)), 0.0, 1.0);
  407. }
  408. """) == Shader.C_image_query_size
  409. def test_glsl_caps_texture_query_levels():
  410. assert compile_and_get_caps(Stage.fragment, """
  411. #version 430
  412. uniform sampler2D a;
  413. out vec4 p3d_FragColor;
  414. void main() {
  415. p3d_FragColor = vec4(float(textureQueryLevels(a)), 0.0, 0.0, 1.0);
  416. }
  417. """) == Shader.C_texture_query_levels
  418. def test_glsl_caps_texture_storage_buffer():
  419. assert compile_and_get_caps(Stage.vertex, """
  420. #version 430
  421. buffer A {
  422. vec4 pos;
  423. } a;
  424. void main() {
  425. gl_Position = a.pos;
  426. }
  427. """) == Shader.C_storage_buffer
  428. # Don't trigger for a regular UBO
  429. assert compile_and_get_caps(Stage.vertex, """
  430. #version 430
  431. uniform A {
  432. vec4 pos;
  433. } a;
  434. void main() {
  435. gl_Position = a.pos;
  436. }
  437. """) == 0
  438. def test_glsl_caps_compute_shader():
  439. assert compile_and_get_caps(Stage.compute, """
  440. #version 430
  441. layout(local_size_x=16, local_size_y=16) in;
  442. void main() {
  443. }
  444. """) == Shader.C_compute_shader
  445. def test_glsl_caps_enhanced_layouts():
  446. assert compile_and_get_caps(Stage.vertex, """
  447. #version 450
  448. layout(location=0, component=1) out float a;
  449. void main() {
  450. gl_Position = vec4(0.0);
  451. a = 0.0;
  452. }
  453. """) == Shader.C_enhanced_layouts
  454. def test_glsl_caps_cull_distance():
  455. assert compile_and_get_caps(Stage.vertex, """
  456. #version 450
  457. void main() {
  458. gl_Position = vec4(0.0);
  459. gl_CullDistance[0] = 0.0;
  460. }
  461. """) == Shader.C_cull_distance
  462. def test_glsl_caps_derivative_control():
  463. assert compile_and_get_caps(Stage.fragment, """
  464. #version 450
  465. out vec4 p3d_FragColor;
  466. void main() {
  467. p3d_FragColor = vec4(fwidthFine(gl_FragCoord.x), 0.0, 0.0, 1.0);
  468. }
  469. """) == Shader.C_standard_derivatives | Shader.C_derivative_control
  470. def test_glsl_caps_texture_query_samples():
  471. assert compile_and_get_caps(Stage.fragment, """
  472. #version 450
  473. uniform sampler2DMS a;
  474. out vec4 p3d_FragColor;
  475. void main() {
  476. p3d_FragColor = vec4(float(textureSamples(a)), 0.0, 0.0, 1.0);
  477. }
  478. """) == Shader.C_texture_query_samples