Jump to content
  • Advertisement
Sign in to follow this  
akira32

How to get a integer value from effect file

This topic is 3981 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 want to set a integer value to shader file as value of zero for initializing. And then in shader file, accumulate the integer value when each time enter pixel shader function. But I use the GetInt function of ID3DXEffect, I always get the value I use the SetInt() function of ID3DXEffect to set.
ID3DXEffect*            g_pEffect_ASM;
CDXUTMesh		m_MeshShip;

	m_hSM_0_Light_Area = g_pEffect_ASM->GetParameterByName(NULL, "SM_0_Light_Area");
	m_hSM_1_Light_Area = g_pEffect_ASM->GetParameterByName(NULL, "SM_1_Light_Area");
	m_hSM_2_Light_Area = g_pEffect_ASM->GetParameterByName(NULL, "SM_2_Light_Area");
	m_hSM_3_Light_Area = g_pEffect_ASM->GetParameterByName(NULL, "SM_3_Light_Area");

g_pEffect_ASM->SetInt(m_hSM_0_Light_Area, 0);
g_pEffect_ASM->SetInt(m_hSM_1_Light_Area, 0);
g_pEffect_ASM->SetInt(m_hSM_2_Light_Area, 0);
g_pEffect_ASM->SetInt(m_hSM_3_Light_Area, 0);

	D3DMATERIAL9 *pMtrl;
	pMtrl = m_MeshShip.m_pMaterials;

	IDirect3DBaseTexture9** ppTexArray=m_MeshShip.m_pTextures;

	for(unsigned int i=0; i<m_MeshShip.m_dwNumMaterials; i++) 
	{
		vTemp.x = pMtrl->Diffuse.r;
		vTemp.y = pMtrl->Diffuse.g;
		vTemp.z = pMtrl->Diffuse.b;
		vTemp.w = pMtrl->Diffuse.a;
		if(m_hMaterialColor_ASM != NULL)
		{
			g_pEffect_ASM->SetVector(m_hMaterialColor_ASM, &vTemp);
		}		

		if (ppTexArray==NULL)
		{
			HRESULT hr;
			hr=g_pEffect_ASM->SetBool(m_hUseDiffuseTexture_ASM,FALSE);
		}
		else
		{
			HRESULT hr;
			hr=g_pEffect_ASM->SetTexture("DiffuseTexture", ppTexArray);
			hr=g_pEffect_ASM->SetBool(m_hUseDiffuseTexture_ASM,TRUE);
		}

		g_pEffect_ASM->CommitChanges();
		m_MeshShip.GetLocalMesh()->DrawSubset(i);	
		pMtrl++;
	}

g_pEffect_ASM->GetInt(m_hSM_0_Light_Area,&m_nSM_0_Light_Area);
g_pEffect_ASM->GetInt(m_hSM_1_Light_Area,&m_nSM_1_Light_Area);
g_pEffect_ASM->GetInt(m_hSM_2_Light_Area,&m_nSM_2_Light_Area);
g_pEffect_ASM->GetInt(m_hSM_3_Light_Area,&m_nSM_3_Light_Area);

DXUTOutputDebugString(L"%d,%d,%d,%d\n",m_nSM_0_Light_Area,m_nSM_1_Light_Area,m_nSM_2_Light_Area,m_nSM_3_Light_Area);


There is the realtive shader code
extern int SM_0_Light_Area;
extern int SM_1_Light_Area;
extern int SM_2_Light_Area;
extern int SM_3_Light_Area;

// -------------------------------------------------------------
// pixel shader that creates scene
// -------------------------------------------------------------
float4 PSScene(VS_OUTPUT In) : COLOR
{   
    float4 Color = In.Ambient;
    //float4 Color = 0.0f;
    
//	float ShadowMap = tex2Dproj( ShadowMapSamp1, In.ShadowMapUV ).x;

	//float ShadowMap = OffsetLookup(ShadowMapSamp1, In.ShadowMapUV, float2(0.0f, 0.0f));
	
	float ShadowMap;
	float4 loc=In.ShadowMapUV;
	float2 TexCoord_Tmp = loc.xy / loc.w;
	
	if (TexCoord_Tmp.x<0.5f && TexCoord_Tmp.y>0.5f)//0
	{
		ShadowMap = OffsetLookup2(ShadowMapSamp0, In.ShadowMapUV, float2(0.0f, -0.5f));
		SM_0_Light_Area++;	
	}
	else if (TexCoord_Tmp.x>0.5f && TexCoord_Tmp.y>0.5f)//1
	{
		ShadowMap = OffsetLookup2(ShadowMapSamp1, In.ShadowMapUV, float2(-0.5f, -0.5f));
		SM_1_Light_Area++;
	}
	else if (TexCoord_Tmp.x<0.5f && TexCoord_Tmp.y<0.5f)//2
	{
		ShadowMap = OffsetLookup2(ShadowMapSamp2, In.ShadowMapUV, float2(0.0f, 0.0f));
		SM_2_Light_Area++;
	}
	else if (TexCoord_Tmp.x>0.5f && TexCoord_Tmp.y<0.5f)//3
	{
		ShadowMap = OffsetLookup2(ShadowMapSamp3, In.ShadowMapUV, float2(-0.5f, 0.0f));
		SM_3_Light_Area=++;
	}			
    
    // value of shadow_map bigger or equal than depth value: return diffuse
    //Color += (In.Depth - ShadowMap > 0.0f ) ? 0.0f : 1.0f;
    Color *= (In.Depth - ShadowMap > 0.0f ) ? 0.7f : 1.0f;
    
    if (bUseDiffuseTexture==true)
		Color *= tex2D(tex_col_diffuse, In.v2_tc.xy);
    
    return Color;
}  

technique TShader
{
    pass P0
    {
        VertexShader = compile vs_1_1 VSCreateShadowMap();
        PixelShader = compile ps_2_0 PSCreateShadowMap();
    }
    
    pass P1
    {
        VertexShader = compile vs_1_1 VSScene();
        PixelShader  = compile ps_2_0 PSScene();        
    }

}

Share this post


Link to post
Share on other sites
Advertisement
Each vertex and pixel gets its own "instance" of the shader. They can't communicate directly in a single pass (plus they run in parallel). Also, effect variables are input-only. You can modify them in the shader, but they never get sent back to the CPU so it's a lost cause (unless you're just using it as a local variable). You need to write your results out to the rendertarget if you want to read it back.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zipster
You need to write your results out to the rendertarget if you want to read it back.


Could you tell me more detail about "result out to the rendertarget"?
Do I create another pass to carry the integer in effect file and save the integer value to the texel in pixel shader(for rendertarget's pass)?

Share this post


Link to post
Share on other sites
Quote:
Original post by akira32
Do I create another pass to carry the integer in effect file and save the integer value to the texel in pixel shader(for rendertarget's pass)?
Yes, thats pretty much it - call GetRenderTargetData() to pull the values back to a CPU-addressable location at the end...

hth
Jack

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!