Sign in to follow this  

help about ASM shader codes

This topic is 3868 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

here is my first asm shader codes in DX9, but when runed, I got nothing on the screen, but black, what I did wrong, could anyone help me out please? FVF struct
//FVF
struct BASIC_SHADER_VERTEX
{
	float x, y, z;
	DWORD color;
};
create vertex shader
//create vertex shader
const char* BasicShader = 
		"vs_1_1             \n"
		"dcl_position v0    \n"
		"dcl_color  v1      \n"
		"m4x4 oPos, v0, c0  \n"
		"mov oD0, v1        \n;";


	D3DVERTEXELEMENT9 decl[] =
	{
		{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0,},
		{ 0, 12,D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0,},
			D3DDECL_END()
	};



	ID3DXBuffer *pShaderBuffer;
	ID3DXBuffer *pShaderErrors;
	if(FAILED(D3DXAssembleShader(BasicShader, (UINT)strlen(BasicShader), 0, NULL, D3DXSHADER_DEBUG,
		&pShaderBuffer, &pShaderErrors)))

		return E_FAIL;

	if(FAILED(m_pD3DDevice->CreateVertexShader((DWORD*)pShaderBuffer->GetBufferPointer(),
		&m_pVertexShader)))
		return E_FAIL;

	pShaderBuffer->Release();

	if(FAILED(m_pD3DDevice->CreateVertexDeclaration(decl, &m_pVertexDeclaration)))
		return E_FAIL;

	return m_pD3DDevice->SetVertexShader(m_pVertexShader);

create vertex buffer
//create vertex buffer
	if(FAILED(m_pD3DDevice->CreateVertexBuffer(BASIC_SHADER_VERTEX_NUM * 
		sizeof(BASIC_SHADER_VERTEX), D3DUSAGE_WRITEONLY, D3DFVF_VERTEX_IN_SHADER_1,
		D3DPOOL_DEFAULT, &m_pVertexBuffer, 0)))
		return FALSE;

	m_pD3DDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(BASIC_SHADER_VERTEX));
	FillVertexBuffer();

fill vertex buffer
//fill vertex buffer
	BASIC_SHADER_VERTEX *pVertices;

	if(FAILED(m_pVertexBuffer->Lock(0, BASIC_SHADER_VERTEX_NUM * 
		sizeof(BASIC_SHADER_VERTEX), reinterpret_cast<void**>(&pVertices), 0)))
	{
		DestroyVertexBuffer();
		return FALSE;
	}

	for(int index = 0; index < BASIC_SHADER_VERTEX_NUM; ++index)
	{
		float Angle = 2.0f * D3DX_PI * ((float)index / (float)BASIC_SHADER_VERTEX_NUM);

		pVertices[index].x = cos(Angle);
		pVertices[index].y = sin(Angle);
		pVertices[index].z = 5.0f;
		pVertices[index].color = 0xffffffff;
	}

	m_pVertexBuffer->Unlock();

render codes
        D3DXMATRIX RotationMatrix1;
	D3DXMATRIX RotationMatrix2;
	D3DXMATRIX TranslationMatrix;
	D3DXMATRIX ScalingMatrix;

	D3DXMatrixRotationZ(&RotationMatrix1, static_cast<float>(GetTickCount() / 1000.0f));
	D3DXMatrixRotationZ(&RotationMatrix2, static_cast<float>(GetTickCount() / 1000.0f));
	D3DXMatrixTranslation(&TranslationMatrix, 3.0f, 0.0f, 0.0f);
	D3DXMatrixScaling(&ScalingMatrix, 1.0f, 0.5f, 1.0f);

	m_WorldMatrix = ScalingMatrix * RotationMatrix2 * TranslationMatrix 
		* RotationMatrix1;

	D3DXMATRIX ShaderMatrix = m_WorldMatrix * m_ViewMatrix * m_ProjectionMatrix;
	
	D3DXMatrixTranspose(&ShaderMatrix, &ShaderMatrix);

	m_pD3DDevice->SetVertexShaderConstantF(0,(float*)&ShaderMatrix, 4);

	m_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

	m_pD3DDevice->DrawPrimitive(D3DPT_POINTLIST, 0, BASIC_SHADER_VERTEX_NUM);

Share this post


Link to post
Share on other sites
Data format looks fine. Setting constants looks fine. I don't see you actually ever doing a SetVertexShader to activate the shader. I don't see you setting up View or Projection matrices, but I'm assuming they're fine.

Shader itself *should* be fine, but I'm not 100% sure. I never used the m4x4 macros, so I don't know if they come out as "mul, mad, mad, mad" or as "dp4, dp4, dp4, dp4". If it comes out as dp4, I'd expect it to be okay as you've transposed your matrix. If it comes out as mul/mad, you'll need to not transpose, or change the shader.

The advantage of transposed matrices is only for world/bones/view matrices, as you can omit the final column of the matrix. Using transposed matrices brings this down to 3 constants per matrix. For your test you need all 4 constants, for projection.

Also newer SDKs don't handle 1.1 shaders for the FX compiler. They might not support it for even assembling a shader outside the FX system. A quick test would be the change it to 2.0 and see if it magically works.

Share this post


Link to post
Share on other sites
try to move
m_pD3DDevice->SetVertexShader(m_pVertexShader);
and
m_pD3DDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(BASIC_SHADER_VERTEX));

inside your rendering loop instead of at creation and

and I can't find where you call:
m_pD3DDevice->SetVertexDeclaration(m_pVertexDeclaration);
neither

try to put it close to SetVertexShader / SetStreamSource

Share this post


Link to post
Share on other sites
Quote:
Original post by c3Dp
and I can't find where you call:
m_pD3DDevice->SetVertexDeclaration(m_pVertexDeclaration);
neither

try to put it close to SetVertexShader / SetStreamSource


Thanks a lot guys, especially c3Dp. The problem is fixed. Actually, I missed SetVertexDeclataion(). Now, everything is fine :)

Share this post


Link to post
Share on other sites

This topic is 3868 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