• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Medo Mex

Drawing multiple lines with one draw call

9 posts in this topic

I'm trying to fill the indices to draw multiple lines with a single draw call:

DWORD linesCount = 4; // Draw 4 lines with one draw call
TVertex* vrts = new TVertex[linesCount]();

for(DWORD i = 0; i < linesCount; i++)
     vrts[i+0].pos = v0; vrts[0].otherPos = v1;
     vrts[i+1].pos = v1; vrts[1].otherPos = v0;
     vrts[i+2].pos = v0; vrts[2].otherPos = v1;
     vrts[i+3].pos = v1; vrts[3].otherPos = v0;

     g_fThickness = 0.5f;
     vrts[i+0].thickness = D3DXVECTOR3( -g_fThickness, 0.f, g_fThickness * 0.5f );
     vrts[i+1].thickness = D3DXVECTOR3(  g_fThickness, 0.f, g_fThickness * 0.5f );
     vrts[i+2].thickness = D3DXVECTOR3(  g_fThickness, 0.f, g_fThickness * 0.5f );
     vrts[i+3].thickness = D3DXVECTOR3( -g_fThickness, 0.f, g_fThickness * 0.5f );

     vrts[i+0].texOffset = D3DXVECTOR4( g_fThickness, g_fThickness, 0.f, 0.f );
     vrts[i+1].texOffset = D3DXVECTOR4( g_fThickness, g_fThickness, 0.25f, 0.f );
     vrts[i+2].texOffset = D3DXVECTOR4( g_fThickness, g_fThickness, 0.f, 0.25f );
     vrts[i+3].texOffset = D3DXVECTOR4( g_fThickness, g_fThickness, 0.25f, 0.25f );

device->DrawIndexedPrimitiveUP( D3DPT_TRIANGLELIST, 0, linesCount*4, linesCount*2, indexBuffer, D3DFMT_INDEX32, vrts, sizeof( TVertex ) );


Now, I'm not sure how to fill indexBuffer, what values I should give to the index buffer?

Edited by Medo3337

Share this post

Link to post
Share on other sites

First off, use D3DPT_LINELIST or D3DPT_LINESTRIP. Then the index buffer values correspond to vertex buffer positions.


So if the lines are not connected there are 8 vertices. The indices are 0-7.

If the lines are connected then there are 5 vertices and you don't need an index buffer and you can use DrawPrimitiveUP.


Share this post

Link to post
Share on other sites


I would advice you to use DrawIndexedPrimitive instead DrawIndexedPrimitiveUP which is slow.



Create VB, IB and vertex declaration instead FVF.


Given vertex input you gave in thread that you have linked


struct TInputVertex
float4 pos : POSITION; // Position of this vertex
float4 otherPos : NORMAL; // Position of the other vertex at the other end of the line.
float4 texOffset : TEXCOORD0; // Tex coord offset.
float3 thickness : TEXCOORD1; // Thickness info.


Vetex declaration becomes


    D3DVERTEXELEMENT9 vertexElements[] =

    HRESULT hr = d3d9device->CreateVertexDeclaration(vertexElements, &vDeclaration);
        ...// act on error




struct Vertex
    D3DXVECTOR4 pos;
    D3DXVECTOR4 normal;
    D3DXVECTOR4 texCoordOff;
    D3DXVECTOR3 thickness;


VB and IB


    const std::size_t maxLinesCnt = 1000;
    const std::size_t vertsPerLine  = 4;
    const std::size_t triPerLine      = 2;
    const std::size_t numVerts      = maxLinesCnt * vertsPerLine;
    UINT len     = sizeof(Vertex) * numVerts;
    hr = d3d9device->CreateVertexBuffer(len, usage, 0, pool, &vBuffer, nullptr);
        ...// act on error
    D3DFORMAT indFormat  = (std::numeric_limits<WORD>::max() < numVerts) ? D3DFMT_INDEX32 : D3DFMT_INDEX16;
    std::size_t indBytes = (std::numeric_limits<WORD>::max() < numVerts) ? sizeof(DWORD) : sizeof(WORD);
    len = indBytes * maxLinesCnt * triPerLine * 3;
    hr = d3d9device->CreateIndexBuffer(len, usage, indFormat, pool, &iBuffer, nullptr);
        ...// act on error


Fill your VB and IB and set these before drawing:


d3d9device->SetStreamSource(0, vBuffer, 0, sizeof(Vertex));
d3d9device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, ...);
Edited by belfegor

Share this post

Link to post
Share on other sites

Ok, i looked a bit at volume lines demo from CodeSampler, but today i am to lazy to make project and test it out with above modifications.

I guess you could do something like this:



struct Line
    D3DXVECTOR4 pt0;
    D3DXVECTOR4 pt1;
std::vector<Line> vLines;
// fill lines
Vertex* vrts;
vBuffer->Lock(... &vrts);
std::size_t numTriDraw = 0u;
for(std::size_t i = 0; i < vLines.size(); ++i)
    if( i > maxNumLines)
        d3d9device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, ..., numTriDraw);
        vBuffer->Lock( ... &vrts);
        numTriDraw = 0u;
    numTriDraw += triPerLine;
    const D3DXVECTOR4& v0 = vLines[i].pt0;
    const D3DXVECTOR4& v1 = vLines[i].pt1;

    vrts[i*4 + 0].pos = v0;        vrts[i*4 + 0].otherPos = v1;
    vrts[i*4 + 1].pos = v1;        vrts[i*4 + 1].otherPos = v0;
    vrts[i*4 + 2].pos = v0;        vrts[i*4 + 2].otherPos = v1;
    vrts[i*4 + 3].pos = v1;        vrts[i*4 + 3].otherPos = v0;

    vrts[i*4 + 0].thickness = D3DXVECTOR3( -g_fThickness, 0.f, g_fThickness * 0.5f );
    vrts[i*4 + 1].thickness = D3DXVECTOR3(  g_fThickness, 0.f, g_fThickness * 0.5f );
    vrts[i*4 + 2].thickness = D3DXVECTOR3(  g_fThickness, 0.f, g_fThickness * 0.5f );
    vrts[i*4 + 3].thickness = D3DXVECTOR3( -g_fThickness, 0.f, g_fThickness * 0.5f );

    vrts[i*4 + 0].texOffset = D3DXVECTOR4( g_fThickness, g_fThickness, 0.f, 0.f );
    vrts[i*4 + 1].texOffset = D3DXVECTOR4( g_fThickness, g_fThickness, 0.25f, 0.f );
    vrts[i*4 + 2].texOffset = D3DXVECTOR4( g_fThickness, g_fThickness, 0.f, 0.25f );
    vrts[i*4 + 3].texOffset = D3DXVECTOR4( g_fThickness, g_fThickness, 0.25f, 0.25f );
if(numTriDraw > 0u)
    d3d9device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, ..., numTriDraw);
Edited by belfegor

Share this post

Link to post
Share on other sites

Good job! Well done :-)


Can you give me idea how should I generally determine the index buffer? How did you know that it's 0, 2, 1, 2, 3, 1?


pIndices[0+i] = 0+j;
pIndices[1+i] = 2+j;
pIndices[2+i] = 1+j;
pIndices[3+i] = 2+j;
pIndices[4+i] = 3+j;
pIndices[5+i] = 1+j;

Share this post

Link to post
Share on other sites

Triangle strip MSDN


I don't know to create the right algorithm for triangle strips (since it has a bit weird order), but in this case it was easy since there is 2 triangle strip per line, so i just follow rule for first two triangles:


The system uses vertices v1, v2, and v3 to draw the first triangle; v2, v4, and v3 to draw the second triangle...


0 1 2

1 3 2


Since that was original way to create each line vertices (with triangle strip in mind), and also It depends in what order vertices are fed to vertex buffer, so i changed indices to CCW order:

0 2 1

2 3 1


I just saw something in original code, to get default cull mode comment this in shader:

technique VolumeLine
    pass p0
        //CullMode = none; // THIS
        AlphaBlendEnable = true;
        SrcBlend = one;
        DestBlend = one;
        alpharef = 0.9;
        alphafunc = GreaterEqual;
        ZEnable = false;
        VertexShader = compile vs_2_0 VolumeLineVS();
        PixelShader = compile ps_2_0 VolumeLinePS();


and also (redundant) in cpp:

// Set up some device states.
    D3DXMatrixPerspectiveFovLH( &g_mProjection, 45.f,
        (float)d3dpp.BackBufferWidth / (float)d3dpp.BackBufferHeight,
        0.1f, 100.f );

    g_pD3DDevice->SetTransform( D3DTS_PROJECTION, &g_mProjection );
    g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
    //g_pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); // THIS

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0