138 lines
4.1 KiB
HLSL
138 lines
4.1 KiB
HLSL
#define RESOLUTION uint2(256, 256)
|
|
#define COUNT (RESOLUTION.x * RESOLUTION.y)
|
|
#define LAST_INDEX (COUNT - 1)
|
|
#define WIDTH RESOLUTION.x
|
|
#define HEIGHT RESOLUTION.y
|
|
#define Z_ORDER_CURVE
|
|
|
|
Texture2D<float> _ActiveTexelMap;
|
|
float4 _ActiveTexelMap_TexelSize;
|
|
|
|
uint2 IndexToCoords(uint index)
|
|
{
|
|
return uint2(index % RESOLUTION.x, index / RESOLUTION.x);
|
|
}
|
|
|
|
uint CoordsToIndex(float2 uv, uint2 res = RESOLUTION)
|
|
{
|
|
uint2 coords = uint2(uv.y * res.y, uv.x * res.x);
|
|
uint index = (coords.x * res.x) + (coords.y);
|
|
return index;
|
|
}
|
|
|
|
|
|
// https://github.com/d4rkc0d3r/CompactSparseTextureDemo/
|
|
|
|
// adapted from: https://lemire.me/blog/2018/01/08/how-fast-can-you-bit-interleave-32-bit-integers/
|
|
uint InterleaveWithZero(uint word)
|
|
{
|
|
word = (word ^ (word << 8)) & 0x00ff00ff;
|
|
word = (word ^ (word << 4)) & 0x0f0f0f0f;
|
|
word = (word ^ (word << 2)) & 0x33333333;
|
|
word = (word ^ (word << 1)) & 0x55555555;
|
|
return word;
|
|
}
|
|
|
|
// adapted from: https://stackoverflow.com/questions/3137266/how-to-de-interleave-bits-unmortonizing
|
|
uint DeinterleaveWithZero(uint word)
|
|
{
|
|
word &= 0x55555555;
|
|
word = (word | (word >> 1)) & 0x33333333;
|
|
word = (word | (word >> 2)) & 0x0f0f0f0f;
|
|
word = (word | (word >> 4)) & 0x00ff00ff;
|
|
word = (word | (word >> 8)) & 0x0000ffff;
|
|
return word;
|
|
}
|
|
|
|
uint2 IndexToUV(uint index)
|
|
{
|
|
#ifdef Z_ORDER_CURVE
|
|
return uint2(DeinterleaveWithZero(index), DeinterleaveWithZero(index >> 1));
|
|
#else
|
|
return uint2(index % HEIGHT, index / HEIGHT);
|
|
#endif
|
|
}
|
|
|
|
uint UVToIndex(uint2 uv)
|
|
{
|
|
#ifdef Z_ORDER_CURVE
|
|
return InterleaveWithZero(uv.x) | (InterleaveWithZero(uv.y) << 1);
|
|
#else
|
|
return uv.x + uv.y * WIDTH;
|
|
#endif
|
|
}
|
|
|
|
float CountActiveTexels(int3 uv, int2 offset)
|
|
{
|
|
uv.xy += offset;
|
|
return (float)(1 << (uv.z + uv.z)) * _ActiveTexelMap.Load(uv);
|
|
}
|
|
|
|
float CountActiveTexels(int3 uv)
|
|
{
|
|
return CountActiveTexels(uv, int2(0, 0));
|
|
}
|
|
|
|
int2 ActiveTexelIndexToUV(float index)
|
|
{
|
|
float maxLod = round(log2(HEIGHT));
|
|
int3 uv = int3(0, 0, maxLod);
|
|
|
|
if (index >= CountActiveTexels(uv))
|
|
return -1;
|
|
float activeTexelSumInPreviousLods = 0;
|
|
while (uv.z >= 1)
|
|
{
|
|
uv += int3(uv.xy, -1);
|
|
float count00 = CountActiveTexels(uv);
|
|
float count01 = CountActiveTexels(uv, int2(1, 0));
|
|
float count10 = CountActiveTexels(uv, int2(0, 1));
|
|
bool in00 = index < (activeTexelSumInPreviousLods + count00);
|
|
bool in01 = index < (activeTexelSumInPreviousLods + count00 + count01);
|
|
bool in10 = index < (activeTexelSumInPreviousLods + count00 + count01 + count10);
|
|
if (in00)
|
|
{
|
|
uv.xy += int2(0, 0);
|
|
}
|
|
else if (in01)
|
|
{
|
|
uv.xy += int2(1, 0);
|
|
activeTexelSumInPreviousLods += count00;
|
|
}
|
|
else if (in10)
|
|
{
|
|
uv.xy += int2(0, 1);
|
|
activeTexelSumInPreviousLods += count00 + count01;
|
|
}
|
|
else
|
|
{
|
|
uv.xy += int2(1, 1);
|
|
activeTexelSumInPreviousLods += count00 + count01 + count10;
|
|
}
|
|
}
|
|
return uv.xy;
|
|
}
|
|
|
|
/*
|
|
MIT License
|
|
|
|
Copyright (c) 2022 d4rkpl4y3r
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/ |