SampleLevel Texture coordinate format

Started by
2 comments, last by JB3DG 10 years, 2 months ago

Hi guys

Is the texture coordinate format for SampleLevel the same as the viewport (ie +-1.0 x and y) or is it integer based?

The reason I ask is I need to do a edge check in my pixel shader where I will sample 4 pixels around each point to see if they are black. Not sure how to go about this.

Also, anyone got a efficient solution for a white noise generator in HLSL? I found this: http://www.reedbeta.com/blog/2013/01/12/quick-and-easy-gpu-random-numbers-in-d3d11/

but from the comments I understand it can be rather slow.

Also found this in the comments:

return fract(sin(dot(n.xy, vec2(12.9898, 78.233)))* 43758.5453);

But would like to know if there is anything faster out there. I want to noise up the texture for a certain distance within the edges as I locate them.

Thanks

JB

Advertisement

As far as white noise goes, I'm playing around with this shader at the moment.


layout(location = 0) uniform float time;
out vec4 frag_color;

uint hash(uint x)
{
    x = ((x >> 16) ^ x) * 0x85ebca6b;
    x = ((x >> 13) ^ x) * 0xc2b2ae35;
    x = ((x >> 16) ^ x);
    return x;
}
 
uint hash(uvec2 v) { return hash(v.x ^ hash(v.y)); }
uint hash(uvec3 v) { return hash(v.x ^ hash(v.y) ^ hash(v.z)); }
uint hash(uvec4 v) { return hash(v.x ^ hash(v.y) ^ hash(v.z) ^ hash(v.w)); }

float floatConstruct(uint m)
{
    m &= 0x007FFFFFu;
    m |= 0x3F800000u;

    float  f = uintBitsToFloat(m);
    return f - 1.0;
}

float random(float x) { return floatConstruct(hash(floatBitsToUint(x))); }
float random(vec2 v) { return floatConstruct(hash(floatBitsToUint(v))); }
float random(vec3 v) { return floatConstruct(hash(floatBitsToUint(v))); }
float random(vec4 v) { return floatConstruct(hash(floatBitsToUint(v))); }

void main()
{
    frag_color = vec4(random(vec3(gl_FragCoord.xy, time)).xxx, 1.0);
}

It costs 0.28ms @ 1080p on my GTX 760, which is not all that much slower than the fract/sin implementation. The results however are a lot better. The fract/sin implementation produces a lot of noticeable patterns for me, and because of the way trig functions are implemented differently on various GPUs, it can have a lot of variability. This is GLSL by the way, but I'm sure you can modify it if you want to try it.

The UV coordinate range for SampleLevel is the same as Sample: (0, 0) is the top left of the texture, and (1, 1) is the bottom right. For the actual mip level parameter the range is [0, N], where N is equal to NumMipLevels - 1.

awesome stuff. thanks a bunch.

This topic is closed to new replies.

Advertisement