-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBitpackHologramsCS.hlsl
More file actions
78 lines (66 loc) · 2.11 KB
/
BitpackHologramsCS.hlsl
File metadata and controls
78 lines (66 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
cbuffer Constants : register(b0)
{
uint N;
uint M;
uint num_holograms;
};
StructuredBuffer<float> phase : register(t0);
StructuredBuffer<float> phases : register(t1);
StructuredBuffer<int> phase_map : register(t2);
RWTexture2D<uint> hologram : register(u0);
// Quantize phase value to a level between 0 and 15
uint QuantisePhase(float phaseVal)
{
for (uint level = 0; level < 16; level++)
{
if (phaseVal >= phases[level] && phaseVal < phases[level + 1])
{
float diff1 = phaseVal - phases[level];
float diff2 = phases[level + 1] - phaseVal;
return (diff1 < diff2) ? level : (level + 1) % 16;
}
}
return 0; // Default if outside range
}
[numthreads(16, 16, 1)]
void main(uint3 DTid : SV_DispatchThreadID)
{
uint2 pos = DTid.xy;
if (pos.x >= 2 * N || pos.y >= 2 * M)
return;
uint p = pos.x; // 2M
uint q = pos.y; // 2N
// Compute corresponding phase pixel indices
uint i = p / 2;
uint j = q / 2;
uint dx = p % 2;
uint dy = q % 2;
// Determine which of the four phase_map entries to use
uint k;
if (dx == 0 && dy == 1)
k = 0; // (2i, 2j+1)
else if (dx == 0 && dy == 0)
k = 1; // (2i, 2j)
else if (dx == 1 && dy == 1)
k = 2; // (2i+1, 2j+1)
else if (dx == 1 && dy == 0)
k = 3; // (2i+1, 2j)
uint4 color = {0, 0, 0, 255};
// Single loop over all holograms
for (uint n = 0; n < num_holograms; n++)
{
uint color_id = n / 8; // 0 for R (n=0-7), 1 for G (n=8-15), 2 for B (n=16-23)
uint offset = n % 8; // Bit position within the byte (0-7)
float phase_val = phase[i + j * N + n * N * M];
uint level = QuantisePhase(phase_val);
uint bit = phase_map[level * 4 + k];
color[color_id] |= (bit << offset);
};
//uint2 uv = { p / (2 * M), q / (2 * N) };
//uint R = 255 * frac(10.0 * p / (2 * M));
//uint G = 0 ;
//uint B = 0 ;
//const uint A = 255;
//hologram[pos] = R | G << 8 | B << 16 | A << 24;
hologram[pos] = color.r | color.g << 8 | color.b << 16 | color.a << 24;
}