d7samurai

single vertex sprite

May 27th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. ////////// gpu side
  2.  
  3. struct sprite                                               // vertex shader input type ("sprite encoded vertex")
  4. {
  5.     int vertexID : SV_VERTEXID;                             // system generated, does not need to be passed / be a member in cpu struct
  6.  
  7.     float4 pos : POSITION;                                  // contains all four coordinates needed (left, top, right, bottom)
  8.     float4 tex : TEXCOORD;                                  // aka (x0, y0, x1, y1) - and ditto for texture coords
  9. };
  10.  
  11. struct vertex                                               // vertex shader output type (i.e. the pixel shader input)
  12. {
  13.     float4 pos : SV_POSITION;
  14.     float2 tex : TEXCOORD;
  15. };
  16.  
  17. vertex main(sprite input)                                   // vertex shader, will be run 4 x per sprite, with SV_VERTEXID going from 0-3
  18. {
  19.     vertex output;
  20.  
  21.     int x = input.vertexID & 2;                             // will output vertices in this order:
  22.     int y = ((input.vertexID & 1) << 1) ^ 3;                // (x0, y1), (x0, y0), (x1, y1), (x1, y0)
  23.  
  24.     output.pos = float4(input.pos[x], input.pos[y], 0.0f, 1.0f);
  25.     output.tex = float2(input.tex[x], input.tex[y]);
  26.  
  27.     return output;
  28. }
  29.  
  30. ////////// cpu side
  31.  
  32. sprite mySprite;                                            // similar to the vertex shader input type (minus vertexID)
  33. mySprite.pos = { posleft, postop, posright, posbottom };    // or x0, y0, x1, y1, if you will
  34. mySprite.tex = { texleft, textop, texright, texbottom };    // or u0, v0, u1, v1, if you will
  35.  
  36. // add sprites like that to vertex buffer ^
  37.  
  38. DrawInstanced(4, sprite_count, 0, 0)                        // 4 = vertices per instance, making SV_VERTEXID go from 0 to 3
Advertisement
Add Comment
Please, Sign In to add comment