Advertisement

Optimize HLSL CODE

Started by October 17, 2022 06:31 PM
4 comments, last by Tom Sloper 2 years, 1 month ago

The following HLSL pixel shader code is used to perform a 4x4 downsample using a simple box filter. give a way to greatly reduce cost of this shader.


float4 g_OffsetsSample[16];
texture g_Texture;
sampler2D g_Sampler = sampler_state;
{
texture = (g_Texture);
MinFilter = Point;
MagFilter = Point;
MipFilter = Point;
};

float4 PSMain(float2 vTexCoord : TEXCOORD0) : COLOR
{
float4 sample = 0.0f;
for(int i =0; i<16;i++)
{
sample += tex2D(g_Sampler, vTexCoord + g_OffsetsSample .xy);
}

return sample * (1/16.f);
}

Well the easiest is to remove your for loop since you are not using your loop variable. But since you need the values from the 16 neighbours I doubt you are going to find something that will really optimise this since you need 16 texture accesses.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Advertisement

@NightCreature83 Any more suggestions to optimize this code?

@codefreak2022 I need to reduce the shader cost here.

Actually I should have been a bit more clear is that not using the loop index here an error in your shader or is that what you meant to write? Only in the last case can you remove the loop otherwise you need it.
Also how did you find that this shader was too expensive?

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

codefreak2022 said:
give a way to greatly reduce cost of this shader.

Your other post looked suspiciously like a job interview test, and it has been locked. This one also is now locked. You have to solve job interview tests (and school tests) on your own.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement