Advertisement
iMackshun

geometryspriteshader.g.pica

Feb 25th, 2021
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. ; Geometry Sprite Shader (G)
  2. ; Set the geometry to process points as input, with uniforms being allocated starting at register c0
  3. .gsh point c0
  4.  
  5. ; Constants
  6. .constf myconst(0.0, 1.0, -1.0, 3.0)
  7. .alias zeros myconst.xxxx
  8. .alias ones myconst.yyyy
  9. .alias nones myconst.zzzz
  10. .alias threes myconst.wwww
  11.  
  12. ; Uniforms
  13. ;(SpriteCount, Texture Width, Texture Height, [Unused])
  14. .fvec spriteCount
  15. ;Camera Matrices
  16. .fvec viewMatrix[4]
  17. .fvec projectionMatrix[4]
  18. ;3 Float Vectors are used per sprite. It stores (X, Y, Width, Height) (UV Position X, UV Position Y, UV Region Width, UV Region Height) (R, G, B, A)
  19. .fvec spriteData[75]
  20.  
  21. ; Inputs
  22. ;v0 = Top-Left
  23. ;v1 = Bottom-Left
  24. ;v2 = Top-Right
  25.  
  26. ; Outputs
  27. .out outpos position
  28. .out outclr color
  29. .out outuv texcoord0
  30.  
  31. ; Main Function
  32. .entry gmain
  33. .proc gmain
  34. register_setup:
  35. ;LOOP DATA SETUP
  36. ;Set r10 to 0 for looping purposes.
  37. mov r10, zeros
  38. ;r11 = spriteCount
  39. mov r11.x, spriteCount.x
  40. ;r12 = ones
  41. mov r12, ones
  42.  
  43. ;CONSTANT TO SCRATCH REGISTERS SETUP
  44. ;r13 = threes
  45. mov r13, threes
  46.  
  47. ;SPRITE DATA SETUP
  48. ;Load the dimensions of the texture into r14.
  49. mov r14, zeros
  50. mov r14.xy, spriteCount.yz
  51.  
  52. loop_begin:
  53. ;Loop through all of the sprites that need to be drawn.
  54. ;Exit the loop if there are no sprites to be drawn.
  55. cmp r10.x, lt, lt, r11.x
  56. jmpc !cmp.x, loop_exit
  57.  
  58. ;Zero out r0.
  59. mov r0, zeros
  60.  
  61. ;Temporarily use r6 to store the index to the sprite data. It gets multiplied by 3 because there are 3 vectors of data per sprite.
  62. mul r6, r10, r13
  63.  
  64. ;Scale to the dimensions of the sprite.
  65. mova a0.x, r6.x
  66. mov r0.xy, spriteData[a0.x].zw
  67. mul r2, v0, r0
  68. mul r3, v1, r0
  69. mul r4, v2, r0
  70.  
  71. ;Translate the sprite.
  72. mov r0.xy, spriteData[a0.x].xy
  73. add r2, r2, r0
  74. add r3, r3, r0
  75. add r4, r4, r0
  76.  
  77. ;Calculate the position of the bottom right vertex.
  78. mov r0.xy, spriteData[a0.x].zw
  79. mov r5, r3
  80. add r5.x, r5.x, r0.x
  81.  
  82. ;Set the z and w components of the vertices.
  83. mov r2.z, zeros.x
  84. mov r3.z, zeros.x
  85. mov r4.z, zeros.x
  86. mov r5.z, zeros.x
  87. mov r2.w, ones.x
  88. mov r3.w, ones.x
  89. mov r4.w, ones.x
  90. mov r5.w, ones.x
  91.  
  92. ;Load the UV data of this sprite into r15.
  93. mov r15, spriteData[a0.x + 1]
  94.  
  95. ;Generate the triangles composing the sprite.
  96. call emit_triangles
  97.  
  98. ;Increment the counter used to control the loop.
  99. add r10, r10, r12
  100.  
  101. ;Return to the beginning of the loop.
  102. jmpc cmp.x, loop_begin
  103. loop_exit:
  104. ; Function termination
  105. end
  106. .end
  107.  
  108.  
  109. ; Subroutine
  110. ; Description: Generate the triangles composing the sprite.
  111. ; Inputs:
  112. ; r2: Top-Left Vertex Position
  113. ; r3: Bottom-Left Vertex Position
  114. ; r4: Top-Right Vertex Position
  115. ; r5: Bottom-Right Vertex Position
  116. ; r14: Texture Dimensions
  117. ; r15: Sprite UV Position and Size (Top-Left Origin)
  118. .proc emit_triangles
  119. ; Emit the upper left triangle.
  120. setemit 0
  121. mov r6, r2
  122.  
  123. ;Generate the UV coordinate of the vertex.
  124. mov r7, zeros
  125. mov r7.xy, r15.xy
  126. mul r7, r7, r14
  127.  
  128. call process_vertex
  129. emit
  130.  
  131. setemit 1
  132. mov r6, r3
  133.  
  134. ;Generate the UV coordinate of the vertex.
  135. mov r7, zeros
  136. mov r7.xy, r15.xy
  137. add r7.y, r7.y, r15.w
  138. mul r7, r7, r14
  139.  
  140. call process_vertex
  141. emit
  142.  
  143. setemit 2, prim
  144. mov r6, r4
  145.  
  146. ;Generate the UV coordinate of the vertex.
  147. mov r7, zeros
  148. mov r7.xy, r15.xy
  149. add r7.x, r7.x, r15.z
  150. mul r7, r7, r14
  151.  
  152. call process_vertex
  153. emit
  154.  
  155. ; Emit the bottom right triangle.
  156. setemit 0
  157. mov r6, r4
  158.  
  159. ;Generate the UV coordinate of the vertex.
  160. mov r7, zeros
  161. mov r7.xy, r15.xy
  162. add r7.x, r7.x, r15.z
  163. mul r7, r7, r14
  164.  
  165. call process_vertex
  166. emit
  167.  
  168. setemit 1
  169. mov r6, r3
  170.  
  171. ;Generate the UV coordinate of the vertex.
  172. mov r7, zeros
  173. mov r7.xy, r15.xy
  174. add r7.y, r7.y, r15.w
  175. mul r7, r7, r14
  176.  
  177. call process_vertex
  178. emit
  179.  
  180. setemit 2, prim
  181. mov r6, r5
  182.  
  183. ;Generate the UV coordinate of the vertex.
  184. mov r7, zeros
  185. mov r7.xy, r15.xy
  186. add r7.xy, r7.xy, r15.zw
  187. mul r7, r7, r14
  188.  
  189.  
  190. call process_vertex
  191. emit
  192. .end
  193.  
  194. ; Subroutine
  195. ; Description: Multiply by the camera matrices and generate the final UV coordinates.
  196. ; Inputs:
  197. ; r6: Vertex Position
  198. ; r7: Vertex UV
  199. .proc process_vertex
  200. ; r8 = viewMatrix * r8
  201. dp4 r8.x, viewMatrix[0], r6
  202. dp4 r8.y, viewMatrix[1], r6
  203. dp4 r8.z, viewMatrix[2], r6
  204. dp4 r8.w, viewMatrix[3], r6
  205.  
  206. ; projectionMatrix * viewMatrix * r8
  207. dp4 outpos.x, projectionMatrix[0], r8
  208. dp4 outpos.y, projectionMatrix[1], r8
  209. dp4 outpos.z, projectionMatrix[2], r8
  210. dp4 outpos.w, projectionMatrix[3], r8
  211.  
  212. ; outclr = ones
  213. mov outclr, spriteData[a0.x + 2]
  214.  
  215. ;Negate the UV coordinates Y component and invert it. (1.0f - UV.y)a
  216. mov r13, nones
  217. mul r7.y, r7.y, r13.y
  218. add r7.y, ones.y, r7.y
  219. mov r13, threes
  220. ;outuv = r7
  221. mov outuv.xy, r7.xy
  222. .end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement