Skip to main content
GameDev.net gamedev.net
🔒 Locked

Optimize HLSL CODE

Started by codefreak2022 Oct 17, 2022 at 6:31 PM 4 replies 6.3k views
Original Post
codefreak2022
codefreak2022

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);
}

NightCreature83
NightCreature83

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.

NightCreature83
NightCreature83

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?

Tom Sloper
Tom Sloper

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.