Jump to content
  • Advertisement
Sign in to follow this  
huaner

[HLSL] Error X4541:vertex shader must minimally write all components of POSITION

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

Hi: I have question again!I write it by reference something on book.But the wrong is error X4541:vertex shader must minimally write all components of POSITION.Because I'am a newer, So I have many low-grade mistakes. I'am very very sorry.It's too prolixly.So I hope you may be tolerant. Thanks a lot!
matrix ModelViewProj : WORLDVIEWPROJ; 
matrix ModelWorld : WORLD; 
float4 lightPos; 

sampler BaseSampler;
sampler BumpSampler;

struct a2v
{ 
     float4 position : POSITION;
     float3 normal : NORMAL;
     float2 tex0 : TEXCOORD0;
     float3 tangent : TANGENT;
     float3 binormal : BINORMAL;
};

//vertex to pixel shader structure
struct v2p
{
     float4 position : POSITION;
     float2 tex0 : TEXCOORD0;
     float2 tex1 : TEXCOORD1;
     float3 lightVec : TEXCOORD2;
     float att : TEXCOORD3;
};

//pixel shader to screen
struct p2f
{
     float4 color : COLOR0;
};

void vs(a2v InVS, v2p OutVP)
{
	OutVP.position = mul(InVS.position, ModelViewProj);

	float4 posWorld = mul(InVS.position, ModelWorld);
	float3 light = normalize(lightPos - posWorld);
	float3x3 TBNMatrix = float3x3(InVS.tangent, InVS.binormal ,     

                         InVS.normal); 
	OutVP.lightVec = mul(TBNMatrix, light);
	OutVP.att = 1/(1 +(0.005 * distance(lightPos.xyz, posWorld))) ;
 
        OutVP.tex0 = InVS.tex0;
        OutVP.tex1 = InVS.tex0;
}

void ps(v2p INPS, p2f OUTPS )
{
    float4 color = tex2D(BaseSampler, INPS.tex0);

    float3 normal = 2.0f * tex2D(BumpSampler, INPS.tex1).rgb - 1.0f;

    float3 light = normalize(INPS.lightVec);

    float diffuse = saturate(dot(normal, light));

    OUTPS.color = INPS.att * color * diffuse;
}
the related code of application are:
bool Setup()
{
	HRESULT hr = 0;

	Device->CreateVertexBuffer(
		6 * sizeof(MultiTexVertex), 
		D3DUSAGE_WRITEONLY,
		MultiTexVertex::FVF,
		D3DPOOL_MANAGED,
		&QuadVB,
		0);

	MultiTexVertex* v = 0;
	QuadVB->Lock(0, 0, (void**)&v, 0);

	v[0] = MultiTexVertex(-10.0f, -10.0f, 5.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);
	v[1] = MultiTexVertex(-10.0f,  10.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
	v[2] = MultiTexVertex( 10.0f,  10.0f, 5.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);

	v[3] = MultiTexVertex(-10.0f, -10.0f, 5.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);
	v[4] = MultiTexVertex( 10.0f,  10.0f, 5.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
	v[5] = MultiTexVertex( 10.0f, -10.0f, 5.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f);

	QuadVB->Unlock();

	//vertex
	ID3DXBuffer* shaderVS      = 0;
	ID3DXBuffer* errorBufferVS = 0;

	hr = D3DXCompileShaderFromFile(
		"ps_multitex.txt",
		0,
		0,
		"vs", 
		"vs_1_1",
		D3DXSHADER_DEBUG, 
		&shaderVS,
		&errorBufferVS,
		&MultiVSCT);

	if( errorBufferVS )
	{
		::MessageBox(0, (char*)errorBufferVS->GetBufferPointer(), 0, 0);
		d3d::Release<ID3DXBuffer*>(errorBufferVS);
	}

	if(FAILED(hr))
	{
		::MessageBox(0, "D3DXCompileShaderFromFile() - FAILED", 0, 0);
		return false;
	}

	hr = Device->CreateVertexShader(
		(DWORD*)shaderVS->GetBufferPointer(),
		&MultiVS);

	if(FAILED(hr))
	{
		::MessageBox(0, "CreateVertexShader - FAILED", 0, 0);
		return false;
	}

	d3d::Release<ID3DXBuffer*>(shaderVS);

	//pixel
	ID3DXBuffer* shader      = 0;
	ID3DXBuffer* errorBuffer = 0;

	hr = D3DXCompileShaderFromFile(
		"ps_multitex.txt",
		0,
		0,
		"main", 
		"ps",
		D3DXSHADER_DEBUG, 
		&shader,
		&errorBuffer,
		&MultiTexCT);

	if( errorBuffer )
	{
		::MessageBox(0, (char*)errorBuffer->GetBufferPointer(), 0, 0);
		d3d::Release<ID3DXBuffer*>(errorBuffer);
	}

	if(FAILED(hr))
	{
		::MessageBox(0, "D3DXCompileShaderFromFile() - FAILED", 0, 0);
		return false;
	}

	hr = Device->CreatePixelShader(
		(DWORD*)shader->GetBufferPointer(),
		&MultiTexPS);

	if(FAILED(hr))
	{
		::MessageBox(0, "CreateVertexShader - FAILED", 0, 0);
		return false;
	}

	d3d::Release<ID3DXBuffer*>(shader);

	D3DXCreateTextureFromFile(Device, "rock.jpg", &BaseTex);
	D3DXCreateTextureFromFile(Device, "rockNormal.bmp", &SpotLightTex);


	D3DXMatrixPerspectiveFovLH(
			&P,	D3DX_PI * 0.25f, 
			(float)Width / (float)Height, 1.0f, 1000.0f);

	Device->SetRenderState(D3DRS_LIGHTING, false);

	BaseTexHandle      = MultiTexCT->GetConstantByName(0, "BaseSampler");
	SpotLightTexHandle = MultiTexCT->GetConstantByName(0, "BumpSampler");
	WorldMatrixHandle = MultiVSCT->GetConstantByName(0, "ModelViewProj");
	WorldProjMatrixHandle = MultiVSCT->GetConstantByName(0, "ModelWorld");
	LightPosHandle = MultiVSCT->GetConstantByName(0, "lightPos");

	MultiVSCT->SetDefaults(Device);
	D3DXVECTOR4 lightpos(-1.0f, 2.0f, 0.0f, 0.0f);
	MultiVSCT->SetVector(Device, LightPosHandle, &lightpos);

	UINT count;
	MultiTexCT->GetConstantDesc(BaseTexHandle,      &BaseTexDesc, &count);
	MultiTexCT->GetConstantDesc(SpotLightTexHandle, &SpotLightTexDesc, &count);
	MultiTexCT->SetDefaults(Device);

	return true;
}
EDIT: Please format your posts using 'source' tags, and choose more relevant subject lines in the future. [Edited by - jollyjeffers on November 16, 2006 8:30:36 AM]

Share this post


Link to post
Share on other sites
Advertisement

You need to declare your output structure as an "output".

as in:

// note i added "out" to tell compiler this is an output
void vs(a2v InVS, out v2p OutVP)

another option is to just return the structure:

v2p vs(a2v InVS)
{
v2p OutVP;
// ... paste old code

return OutVP;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!