Sign in to follow this  

[DX] Batching... I just cant seem to get it to work.

This topic is 3858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm re downloading the SDK tonight, so i can use the debugger in it, (think I got my copy off a CD). But thought I'd try this once more here. I've given up on the Index Buffers and some stuff I read on msdn and the DX doc, microsoft seems to just use a dynamic vertex buffer. But for some reason, Everything seems to go fine except DrawPrimitive() keeps returning an invalidcall. I don't know why, as its pretty much the exact same code I've been using except with one extra loop with the rendere, that cause the routine to append more triangles to the buffer before calling Draw Primitive. heres the code: The Init() function just creates the vertex buffer, the Display() function is were everything is happening.
#include "zgfxscene.h"

bool CZScene::Init(CZGraphics *graphics)
	HRESULT result;

	m_CError.Write("CZScene 1.0", true);

	CGraphics = graphics;
	m_d3ddev  = CGraphics->Get_Device();

	// create vertex buffer
	(*m_d3ddev)->CreateVertexBuffer(sizeof(VERTEX) * 4,
    NULL, D3DFVF, D3DPOOL_MANAGED, &m_vertex_buffer, NULL);
	m_max_batch  = 40;
	m_batch_size = 0;

	return true;

bool CZScene::Display()
	HRESULT result;

	VERTEX *vertices = NULL;

	// we keep running through the list until it's empty
	while (!m_buffer.empty())

		// set the texture to be used for this pass
		m_texture = m_buffer.begin()->sprite->texture;

		// lock the buffer
		result = m_vertex_buffer->Lock (0, 0,
                             (void **) &vertices, D3DLOCK_DISCARD);
		if (result != D3D_OK) m_CError.Write("Vertex Buffer Lock Failed", true);

		// begin batching
		m_iterator = m_buffer.begin();
		while (m_iterator != m_buffer.end())

			// make sure we don't go out of bounds of our buffer
			if (m_batch_size < m_max_batch)
			// check if display object uses the same texture
			if (m_iterator->sprite->texture == m_texture)
				// setup vertices 
				vertices[m_batch_size].colour = D3DCOLOR_ARGB(m_iterator->alpha,255,255,255);
				vertices[m_batch_size].pos    = D3DXVECTOR3(m_iterator->x, m_iterator->y, 0);
				vertices[m_batch_size].normal = D3DXVECTOR3(0,0,1);
				vertices[m_batch_size].u = m_iterator->sprite->tx1;
				vertices[m_batch_size].v = m_iterator->sprite->ty1;

				vertices[m_batch_size + 1].colour = D3DCOLOR_ARGB(m_iterator->alpha,255,255,255);
				vertices[m_batch_size + 1].pos    = D3DXVECTOR3(m_iterator->x + m_iterator->sprite->width, m_iterator->y, 0);
				vertices[m_batch_size + 1].normal = D3DXVECTOR3(0,0,1);
				vertices[m_batch_size + 1].u = m_iterator->sprite->tx2;
				vertices[m_batch_size + 1].v = m_iterator->sprite->ty1;

				vertices[m_batch_size + 2].colour = D3DCOLOR_ARGB(m_iterator->alpha,255,255,255);
				vertices[m_batch_size + 2].pos    = D3DXVECTOR3(m_iterator->x + m_iterator->sprite->width, m_iterator->y + m_iterator->sprite->height, 0);
				vertices[m_batch_size + 2].normal = D3DXVECTOR3(0,0,1);
				vertices[m_batch_size + 2].u = m_iterator->sprite->tx2;
				vertices[m_batch_size + 2].v = m_iterator->sprite->ty2;

				vertices[m_batch_size + 3].colour = D3DCOLOR_ARGB(m_iterator->alpha,255,255,255);
				vertices[m_batch_size + 3].pos    = D3DXVECTOR3(m_iterator->x, m_iterator->y, 0);
				vertices[m_batch_size + 3].normal = D3DXVECTOR3(0,0,1);
				vertices[m_batch_size + 3].u = m_iterator->sprite->tx1;
				vertices[m_batch_size + 3].v = m_iterator->sprite->ty1;

				vertices[m_batch_size + 4].colour = D3DCOLOR_ARGB(m_iterator->alpha,255,255,255);
				vertices[m_batch_size + 4].pos    = D3DXVECTOR3(m_iterator->x + m_iterator->sprite->width, m_iterator->y + m_iterator->sprite->height, 0);
				vertices[m_batch_size + 4].normal = D3DXVECTOR3(0,0,1);
				vertices[m_batch_size + 4].u = m_iterator->sprite->tx2;
				vertices[m_batch_size + 4].v = m_iterator->sprite->ty2;

				vertices[m_batch_size + 5].colour = D3DCOLOR_ARGB(m_iterator->alpha,255,255,255);
				vertices[m_batch_size + 5].pos    = D3DXVECTOR3(m_iterator->x, m_iterator->y + m_iterator->sprite->height, 0);
				vertices[m_batch_size + 5].normal = D3DXVECTOR3(0,0,1);
				vertices[m_batch_size + 5].u = m_iterator->sprite->tx1;
				vertices[m_batch_size + 5].v = m_iterator->sprite->ty2;

				// increased the amount of vertices that are batched
				m_batch_size += 6;

				// remove the display command
				m_iterator = m_buffer.erase(m_iterator);
			else m_iterator++;
			else m_iterator++;

		// unlock the buffer

		// set the texture to be used
		(*m_d3ddev)->SetTexture(0, m_texture->texture);
		// set stream source
		(*m_d3ddev)->SetStreamSource(0, m_vertex_buffer,
												 0, sizeof(VERTEX));
		// set vertex shader

		// draw what ever has been batched
		result = (*m_d3ddev)->DrawPrimitive(D3DPT_TRIANGLELIST, 0, (m_batch_size / 6) );
		if (result != D3D_OK) m_CError.Write("DrawPrimitive Failed", true);
		if (result == D3DERR_INVALIDCALL) m_CError.Write("DrawPrimitive Invalid Call", true);

		// reset batch size for next batch process
		m_batch_size = 0;

	(*m_d3ddev)->Present(NULL, NULL, NULL, NULL);

	return true;

Share this post

Link to post
Share on other sites

This topic is 3858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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