2d - What is the best pratice to render sprites in DirectX 11? -
i trying used directx api , wondering usual approach render sprite in directx 11 (e.g. tetris clone).
is there simmilar interface id3dx10sprite
, , if not, usual method draw sprites in directx 11?
edit: here hlsl code worked me (the computation of projection coordinate done better):
struct spritedata { float2 position; float2 size; float4 color; }; struct vsout { float4 position : sv_position; float4 color : color; }; cbuffer screensize : register(b0) { float2 screensize; float2 padding; // cbuffer must have @ least 16 bytes } structuredbuffer<spritedata> spritedata : register(t0); float2 getvertexposition(uint vid) { [branch] switch(vid) { case 0: return float2(0, 0); case 1: return float2(1, 0); case 2: return float2(0, 1); default: return float2(1, 1); } } float4 computeposition(float2 positioninscreenspace, float2 size, float2 vertexposition) { float2 origin = float2(-1, 1); float2 vertexpositioninscreenspace = positioninscreenspace + (size * vertexposition); return float4(origin.x + (vertexpositioninscreenspace.x / (screensize.x / 2)), origin.y - (vertexpositioninscreenspace.y / (screensize.y / 2)), 1, 1); } vsout vshader(uint vid : sv_vertexid, uint siid : sv_instanceid) { vsout output; output.color = spritedata[siid].color; output.position = computeposition(spritedata[siid].position, spritedata[siid].size, getvertexposition(vid)); return output; } float4 pshader(float4 position : sv_position, float4 color : color) : sv_target { return color; }
no there isn't equivalent. usual method draw triangle strip forming quad.
you make use of instancing, have update buffer sprite data (x, y screen position in pixels, id of texture fetch in texture array, scaling, rotation, layer, etc) , use shaders rendering sprites in 1 single draw call.
here's hlsl tidbits off top of head:
//-------------------------------------------------------------------------------------- // shader code sprite rendering //-------------------------------------------------------------------------------------- struct sprite { float2 position; // x, y world position float rotation; float scaling; float layer; // if have multiple layers of sprites (e.g. background sprites) uint textureid; }; structuredbuffer<sprite> spritesro : register( t0 ); texture2darray<float4> textureslices : register (t1); cbuffer cbrenderconstants : register( b0 ) { matrix g_mviewprojection; // other constants }; struct vsspriteout { float3 position : sv_position; uint textureid; }; //------------------------------------------------------------------------------------- // sprite vertex shader //------------------------------------------------------------------------------------- vsspriteout spritevs(uint vid : sv_vertexid, uint siid : sv_instanceid) { vsspriteout out = (vsspriteout)0; // vid either 0, 1, 2 or 3 // can map 0 position (0,0), 1 (0,1), 2 (1,0), 3 (1,1) // fetch sprite instance data accord siid sprite sdata = spritesro[siid]; // function f computes screen space vertex position float3 pos = f (g_mviewprojection, vid, position, rotation, scaling, layer etc) out.position = pos; out.textureid = sdata.textureid; return out; } //------------------------------------------------------------------------------------- // sprite pixel shader //------------------------------------------------------------------------------------- float4 spriteps(vsspriteout in) : sv_target { // use in.textureid fetch right texture slice in texture array return color; }
Comments
Post a Comment