- Home /
Bitwise operations in hlsl, is there an alternative method?
So, i'm writing a shader, and part of this shader involved IBL.
I'm trying to find a good way to sample from a hemisphere (cubemap) using importance sampling, and part of that involves using the Hammersley Point method, referenced here: http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html
the code in question is this, which is GLSL:
float radicalInverse_VdC(uint bits) {
bits = (bits << 16u) | (bits >> 16u);
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
return float(bits) * 2.3283064365386963e-10; // / 0x100000000
}
//The ith point xi is then computed by
vec2 Hammersley(uint i, uint N)
{
return vec2(float(i)/float(N), radicalInverse_VdC(i));
}
the problem is, the rest of the shader is written in HLSL, and i don't want to rewrite it in GLSL. since bitwise operations are a shader model 4.0 and above feature, i'm wondering why i can't just do it in HLSL anyway. but that's not really the question here (although, if anyone can answer that would be great!). the question is - is there an alternate method of producing the same results?
cheers, Lee.
the solution i had was incredibly slow at the time, and that was to do the bitwise operation in a script and generate a lookup table for the shader to use.
it should be noted that i was doing this to create a PBR GGX shader in Unity, and while i succeeded, there's no point pursuing it now as it's the default for the current versions of Unity.
Hi! When I was writing the importance sampling, the sampling itself, not the random or calculations was the bottleneck. Usage of pre-filtered importance sampling gave me significant speed boost(I think because of cache and bandwidth problems): http://damart3d.blogspot.com/2016/05/substance-painter-shader-unreal-engine.html
Your answer
Follow this Question
Related Questions
sampler2d object has no methods 1 Answer
Bitwise & Operator in Shaders? 1 Answer
Creating a cup of water using Unity Pro Water 1 Answer
Shader float4 component values computing strangely 0 Answers
Add additional UV channel for Blit 1 Answer