@vitessce/spatial-accelerated 3.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.js +52925 -0
- package/dist-tsc/SpatialWrapper.d.ts +2 -0
- package/dist-tsc/SpatialWrapper.d.ts.map +1 -0
- package/dist-tsc/SpatialWrapper.js +45 -0
- package/dist-tsc/VolumeDataManager.d.ts +170 -0
- package/dist-tsc/VolumeDataManager.d.ts.map +1 -0
- package/dist-tsc/VolumeDataManager.js +1273 -0
- package/dist-tsc/VolumeDataManager.test.d.ts +2 -0
- package/dist-tsc/VolumeDataManager.test.d.ts.map +1 -0
- package/dist-tsc/VolumeDataManager.test.js +164 -0
- package/dist-tsc/VolumeRenderManager.d.ts +265 -0
- package/dist-tsc/VolumeRenderManager.d.ts.map +1 -0
- package/dist-tsc/VolumeRenderManager.js +396 -0
- package/dist-tsc/VolumeShader.d.ts +262 -0
- package/dist-tsc/VolumeShader.d.ts.map +1 -0
- package/dist-tsc/VolumeShader.js +73 -0
- package/dist-tsc/VolumeView.d.ts +2 -0
- package/dist-tsc/VolumeView.d.ts.map +1 -0
- package/dist-tsc/VolumeView.js +521 -0
- package/dist-tsc/index.d.ts +2 -0
- package/dist-tsc/index.d.ts.map +1 -0
- package/dist-tsc/index.js +1 -0
- package/dist-tsc/shaders/GaussianFragmentShader.d.ts +2 -0
- package/dist-tsc/shaders/GaussianFragmentShader.d.ts.map +1 -0
- package/dist-tsc/shaders/GaussianFragmentShader.js +242 -0
- package/dist-tsc/shaders/GaussianVertexShader.d.ts +2 -0
- package/dist-tsc/shaders/GaussianVertexShader.d.ts.map +1 -0
- package/dist-tsc/shaders/GaussianVertexShader.js +51 -0
- package/dist-tsc/shaders/VolumeFragmentShader.d.ts +2 -0
- package/dist-tsc/shaders/VolumeFragmentShader.d.ts.map +1 -0
- package/dist-tsc/shaders/VolumeFragmentShader.js +1817 -0
- package/dist-tsc/shaders/VolumeVertexShader.d.ts +2 -0
- package/dist-tsc/shaders/VolumeVertexShader.d.ts.map +1 -0
- package/dist-tsc/shaders/VolumeVertexShader.js +110 -0
- package/package.json +63 -0
- package/src/SpatialWrapper.js +60 -0
- package/src/VolumeDataManager.js +1515 -0
- package/src/VolumeDataManager.test.js +180 -0
- package/src/VolumeRenderManager.js +589 -0
- package/src/VolumeShader.js +74 -0
- package/src/VolumeView.js +651 -0
- package/src/index.js +1 -0
- package/src/shaders/GaussianFragmentShader.js +243 -0
- package/src/shaders/GaussianVertexShader.js +52 -0
- package/src/shaders/VolumeFragmentShader.js +1818 -0
- package/src/shaders/VolumeVertexShader.js +111 -0
|
@@ -0,0 +1,1817 @@
|
|
|
1
|
+
// Attribution: Originally written by @alexandrairger
|
|
2
|
+
// Moved from .glsl file to .js file in this commit: https://github.com/vitessce/vitessce/pull/2264/commits/7f17fc003674a4ed1a8de6140db802781871cf88
|
|
3
|
+
/*
|
|
4
|
+
* This shader implements ray-marching volume rendering with:
|
|
5
|
+
* - Multi-resolution level-of-detail (LOD) support
|
|
6
|
+
* - Brick-based caching system for large datasets
|
|
7
|
+
* - Multi-channel rendering with independent color/opacity
|
|
8
|
+
* - Adaptive sampling based on distance and resolution
|
|
9
|
+
* - Support for different rendering styles (MIP, MinIP, standard)
|
|
10
|
+
*
|
|
11
|
+
* ========================================
|
|
12
|
+
* BRICK CACHE AND PAGE TABLE EXPLANATION
|
|
13
|
+
* ========================================
|
|
14
|
+
* Volumes can be gigabytes in size, but GPU memory is limited.
|
|
15
|
+
* We need a way to efficiently render these datasets without
|
|
16
|
+
* loading everything into memory at once.
|
|
17
|
+
*
|
|
18
|
+
* SOLUTION: A two-tier caching system using a Brick Cache and a Page Table:
|
|
19
|
+
*
|
|
20
|
+
* 1. BRICK CACHE (The Data Storage):
|
|
21
|
+
* - Think of the 3D dataset as a large cube divided into smaller 32x32x32
|
|
22
|
+
* voxel "bricks"
|
|
23
|
+
* - The Brick Cache is a 3D texture (2048x2048x128) that acts as a "cache"
|
|
24
|
+
* or "working memory" for these bricks
|
|
25
|
+
* - Only the most recently used bricks are kept in this cache
|
|
26
|
+
* - When you need data from a brick that's not in the cache, it gets loaded
|
|
27
|
+
* from disk and stored in the cache (replacing the least recently used brick)
|
|
28
|
+
*
|
|
29
|
+
* 2. PAGE TABLE (The Index/Map):
|
|
30
|
+
* - The Page Table is an index that tells us where
|
|
31
|
+
* each brick is stored in the Brick Cache
|
|
32
|
+
* - Each entry in the Page Table corresponds to one brick in the 3D dataset
|
|
33
|
+
* - The Page Table entry contains:
|
|
34
|
+
* * Whether the brick is currently loaded in the cache (resident flag)
|
|
35
|
+
* In other words, the brick is in the cache and available for immediate use.
|
|
36
|
+
* * Whether the brick has been initialized with data (initialized flag)
|
|
37
|
+
* In other words, the data is known to be available in the CPU, but
|
|
38
|
+
* we still need to request this data to be loaded into the brick cache.
|
|
39
|
+
* * The minimum and maximum values in the brick (for optimization)
|
|
40
|
+
* * The exact coordinates where the brick is stored in the Brick Cache
|
|
41
|
+
* - This is similar to how a library's card catalog tells you which shelf
|
|
42
|
+
* and position a book is located
|
|
43
|
+
*
|
|
44
|
+
* HOW IT WORKS TOGETHER:
|
|
45
|
+
* 1. When rendering a pixel, the shader needs to sample data from a specific
|
|
46
|
+
* location in the 3D dataset
|
|
47
|
+
* 2. It calculates which brick contains that location
|
|
48
|
+
* 3. It looks up that brick in the Page Table to see if it's loaded in the cache
|
|
49
|
+
* 4. If loaded: it uses the coordinates from the Page Table to sample from the
|
|
50
|
+
* Brick Cache
|
|
51
|
+
* 5. If not loaded: it requests the brick to be loaded (and continues with
|
|
52
|
+
* lower resolution data until the brick arrives)
|
|
53
|
+
* The request is performed by modifying the gRequest value
|
|
54
|
+
* to append the coordinate of the brick being requested.
|
|
55
|
+
*
|
|
56
|
+
* MULTI-RESOLUTION ADAPTATION:
|
|
57
|
+
* The system supports multiple levels of detail (LOD) to handle different viewing
|
|
58
|
+
* distances and performance requirements:
|
|
59
|
+
*
|
|
60
|
+
* RESOLUTION LEVELS:
|
|
61
|
+
* - Level 0: Highest detail (1:1 scale) - every voxel is represented
|
|
62
|
+
* - Level 1: 2:1 scale - every 2x2x2 block of voxels becomes 1 voxel
|
|
63
|
+
* - Level 2: 4:1 scale - every 4x4x4 block of voxels becomes 1 voxel
|
|
64
|
+
* - Level 3: 8:1 scale - every 8x8x8 block of voxels becomes 1 voxel
|
|
65
|
+
* - ...and so on up to Level 9 (512:1 scale)
|
|
66
|
+
*
|
|
67
|
+
* HOW BRICK CACHE HANDLES MULTIPLE RESOLUTIONS:
|
|
68
|
+
* - Each resolution level has its own set of bricks in the same cache
|
|
69
|
+
* - Higher resolution levels use more bricks (more detail)
|
|
70
|
+
* - Lower resolution levels use fewer bricks (less detail)
|
|
71
|
+
* - The cache can store bricks from different resolution levels simultaneously
|
|
72
|
+
* - When you zoom in, the system loads higher resolution bricks
|
|
73
|
+
* - When you zoom out, it can use lower resolution bricks (faster rendering)
|
|
74
|
+
*
|
|
75
|
+
* HOW PAGE TABLE HANDLES MULTIPLE RESOLUTIONS:
|
|
76
|
+
* - Each resolution level has its own "anchor point" in the page table
|
|
77
|
+
* - The anchor point defines where that resolution's brick entries start
|
|
78
|
+
* - Different channels can have different resolution ranges available
|
|
79
|
+
* - The system automatically selects the best available resolution for each channel
|
|
80
|
+
* - If a high-resolution brick isn't loaded, it falls back to lower resolution
|
|
81
|
+
*
|
|
82
|
+
* ADAPTIVE RESOLUTION SELECTION:
|
|
83
|
+
* - Distance-based: Objects far from the camera use lower resolution
|
|
84
|
+
* - Performance-based: When rendering is slow, it uses lower resolution
|
|
85
|
+
* - Quality-based: When high quality is needed, it requests higher resolution
|
|
86
|
+
* - Channel-specific: Each channel can have different resolution requirements
|
|
87
|
+
*
|
|
88
|
+
* EXAMPLE SCENARIO:
|
|
89
|
+
* 1. User is viewing a large dataset from far away → Uses Level 5-7 bricks
|
|
90
|
+
* 2. User zooms in on a specific region → System requests Level 2-3 bricks
|
|
91
|
+
* 3. User continues zooming → System loads Level 0-1 bricks for maximum detail
|
|
92
|
+
* 4. If Level 0 bricks aren't loaded yet → Temporarily shows Level 1 bricks
|
|
93
|
+
* 5. Once Level 0 bricks arrive → Smoothly transitions to highest detail
|
|
94
|
+
*
|
|
95
|
+
* Diagrams:
|
|
96
|
+
*
|
|
97
|
+
* 1. Ray-Marching Through Volume:
|
|
98
|
+
* Camera
|
|
99
|
+
* |
|
|
100
|
+
* v
|
|
101
|
+
* ┌─────────────────────────────────────┐
|
|
102
|
+
* │ │
|
|
103
|
+
* │ ┌───┐ ┌───┐ ┌───┐ ┌───┐ │
|
|
104
|
+
* │ │B0 │ │B1 │ │B2 │ │B3 │ ... │ <- Bricks (32x32x32 voxels)
|
|
105
|
+
* │ └───┘ └───┘ └───┘ └───┘ │
|
|
106
|
+
* │ │
|
|
107
|
+
* │ Ray marches through volume │
|
|
108
|
+
* │ sampling at regular intervals │
|
|
109
|
+
* │ │
|
|
110
|
+
* └─────────────────────────────────────┘
|
|
111
|
+
*
|
|
112
|
+
* 2. Multi-Resolution LOD System:
|
|
113
|
+
* Resolution Levels (0 = highest, 9 = lowest):
|
|
114
|
+
*
|
|
115
|
+
* Level 0: ████████████████████████████ (1:1 scale)
|
|
116
|
+
* Level 1: ████████░░░░░░░░████████░░░░ (2:1 scale)
|
|
117
|
+
* Level 2: ████░░░░░░░░░░░░████░░░░░░░░ (4:1 scale)
|
|
118
|
+
* Level 3: ██░░░░░░░░░░░░░░██░░░░░░░░░░ (8:1 scale)
|
|
119
|
+
* ...
|
|
120
|
+
* Level 9: █░░░░░░░░░░░░░░░░░░░░░░░░░░░ (512:1 scale)
|
|
121
|
+
*
|
|
122
|
+
* 3. Brick Cache Layout:
|
|
123
|
+
* ┌─────────────────────────────────────┐
|
|
124
|
+
* │ 2048 x 2048 x 128 texture │
|
|
125
|
+
* │ │
|
|
126
|
+
* │ ┌───┐ ┌───┐ ┌───┐ ┌───┐ │
|
|
127
|
+
* │ │B0 │ │B1 │ │B2 │ │B3 │ ... │ <- Brick cache entries
|
|
128
|
+
* │ └───┘ └───┘ └───┘ └───┘ │
|
|
129
|
+
* │ │
|
|
130
|
+
* │ Each brick = 32x32x32 voxels │
|
|
131
|
+
* │ Total: 64x64x16 = 65,536 bricks │
|
|
132
|
+
* └─────────────────────────────────────┘
|
|
133
|
+
*
|
|
134
|
+
* 4. Page Table Structure:
|
|
135
|
+
* ┌─────────────────────────────────────┐
|
|
136
|
+
* │ 32-bit entry per brick: │
|
|
137
|
+
* │ │
|
|
138
|
+
* │ [31] Resident flag (1=loaded) │
|
|
139
|
+
* │ [30] Initialized flag (1=ready) │
|
|
140
|
+
* │ [29:23] Min value (7 bits) │
|
|
141
|
+
* │ [22:16] Max value (7 bits) │
|
|
142
|
+
* │ [15:10] X offset in cache (6 bits) │
|
|
143
|
+
* │ [9:4] Y offset in cache (6 bits) │
|
|
144
|
+
* │ [3:0] Z offset in cache (4 bits) │
|
|
145
|
+
* └─────────────────────────────────────┘
|
|
146
|
+
*
|
|
147
|
+
* 5. Multi-Channel Rendering:
|
|
148
|
+
* Channel 0: ████████████████████████████
|
|
149
|
+
* Channel 1: ░░░░░░░░░░░░░░░░░░░░░░░░░░░░
|
|
150
|
+
* Channel 2: ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
|
|
151
|
+
* Channel 3: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
|
|
152
|
+
* Channel 4: ████████████████████████████
|
|
153
|
+
* Channel 5: ░░░░░░░░░░░░░░░░░░░░░░░░░░░░
|
|
154
|
+
* Channel 6: ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
|
|
155
|
+
*
|
|
156
|
+
* Each channel has independent:
|
|
157
|
+
* - Color mapping
|
|
158
|
+
* - Opacity
|
|
159
|
+
* - Resolution range
|
|
160
|
+
* - Contrast limits
|
|
161
|
+
*
|
|
162
|
+
* 6. Adaptive Sampling:
|
|
163
|
+
* Distance-based LOD selection:
|
|
164
|
+
*
|
|
165
|
+
* Near: ████████████████████████████ (High res, small steps)
|
|
166
|
+
* Medium: ████████░░░░░░░░████████░░░░ (Medium res, medium steps)
|
|
167
|
+
* Far: ██░░░░░░░░░░░░░░██░░░░░░░░░░ (Low res, large steps)
|
|
168
|
+
*
|
|
169
|
+
* Other features:
|
|
170
|
+
* - Ray-box intersection for volume bounds
|
|
171
|
+
* - Adaptive step size based on LOD
|
|
172
|
+
* - Trilinear interpolation for smooth sampling
|
|
173
|
+
* - Early termination for opaque regions
|
|
174
|
+
* - Request/usage tracking for brick loading
|
|
175
|
+
* - Support for clipping planes
|
|
176
|
+
* - Jittered sampling for noise reduction
|
|
177
|
+
*/
|
|
178
|
+
// lang: glsl
|
|
179
|
+
export const volumeFragmentShader = `//
|
|
180
|
+
// #include <packing>
|
|
181
|
+
precision highp float;
|
|
182
|
+
precision highp int;
|
|
183
|
+
precision highp sampler3D;
|
|
184
|
+
precision highp usampler3D;
|
|
185
|
+
|
|
186
|
+
// ========================================
|
|
187
|
+
// INPUT VARIABLES (from vertex shader)
|
|
188
|
+
// ========================================
|
|
189
|
+
// Unnormalized ray direction from camera
|
|
190
|
+
in vec3 rayDirUnnorm;
|
|
191
|
+
// Camera position in world space
|
|
192
|
+
in vec3 cameraCorrected;
|
|
193
|
+
|
|
194
|
+
// ========================================
|
|
195
|
+
// TEXTURE SAMPLERS
|
|
196
|
+
// ========================================
|
|
197
|
+
// 3D texture containing cached brick data (2048x2048x128)
|
|
198
|
+
// (2048*2048*128)/(32*32*32) = 16,384 bricks can be stored?
|
|
199
|
+
uniform sampler3D brickCacheTex;
|
|
200
|
+
// 3D texture containing page table entries (brick metadata)
|
|
201
|
+
uniform usampler3D pageTableTex;
|
|
202
|
+
|
|
203
|
+
// ========================================
|
|
204
|
+
// RENDERING PARAMETERS/CONSTANTS
|
|
205
|
+
// ========================================
|
|
206
|
+
// Rendering style: 0=MIP, 1=MinIP, 2=standard volume rendering
|
|
207
|
+
uniform int u_renderstyle;
|
|
208
|
+
// Global opacity multiplier for volume rendering
|
|
209
|
+
uniform float opacity;
|
|
210
|
+
|
|
211
|
+
// ========================================
|
|
212
|
+
// CONTRAST LIMITS (per channel)
|
|
213
|
+
// per channel min/max values for value normalization
|
|
214
|
+
// ========================================
|
|
215
|
+
uniform vec2 clim0;
|
|
216
|
+
uniform vec2 clim1;
|
|
217
|
+
uniform vec2 clim2;
|
|
218
|
+
uniform vec2 clim3;
|
|
219
|
+
uniform vec2 clim4;
|
|
220
|
+
uniform vec2 clim5;
|
|
221
|
+
uniform vec2 clim6;
|
|
222
|
+
|
|
223
|
+
// ========================================
|
|
224
|
+
// CLIPPING PLANES
|
|
225
|
+
// e.g., for X-axis clipping: (min_x, max_x) or (-1, -1) if disabled
|
|
226
|
+
// ========================================
|
|
227
|
+
uniform vec2 xClip;
|
|
228
|
+
uniform vec2 yClip;
|
|
229
|
+
uniform vec2 zClip;
|
|
230
|
+
|
|
231
|
+
// ========================================
|
|
232
|
+
// CHANNEL COLORS AND OPACITIES
|
|
233
|
+
// rgb -- color values, a -- visibility (boolean)
|
|
234
|
+
// ========================================
|
|
235
|
+
uniform vec4 color0;
|
|
236
|
+
uniform vec4 color1;
|
|
237
|
+
uniform vec4 color2;
|
|
238
|
+
uniform vec4 color3;
|
|
239
|
+
uniform vec4 color4;
|
|
240
|
+
uniform vec4 color5;
|
|
241
|
+
uniform vec4 color6;
|
|
242
|
+
|
|
243
|
+
// maps colors to physical spaces
|
|
244
|
+
uniform int channelMapping[7];
|
|
245
|
+
|
|
246
|
+
// ========================================
|
|
247
|
+
// VOLUME AND RESOLUTION PARAMETERS
|
|
248
|
+
// ========================================
|
|
249
|
+
// Volume bounding box size in world space
|
|
250
|
+
uniform highp vec3 boxSize;
|
|
251
|
+
// Rendering resolution level (affects step size)
|
|
252
|
+
// stepsize, correlates with resolution
|
|
253
|
+
uniform int renderRes;
|
|
254
|
+
// Volume dimensions in voxels (x, y, z)
|
|
255
|
+
// resolution 0 voxel extents
|
|
256
|
+
uniform uvec3 voxelExtents;
|
|
257
|
+
// Global resolution range: (min_res, max_res)
|
|
258
|
+
// global range of requested resolutions
|
|
259
|
+
uniform ivec2 resGlobal;
|
|
260
|
+
// Maximum number of active channels
|
|
261
|
+
// max number of channels (relevant for the cache statistics)
|
|
262
|
+
// between 1 and 7
|
|
263
|
+
uniform int maxChannels;
|
|
264
|
+
|
|
265
|
+
// ========================================
|
|
266
|
+
// PER-CHANNEL RESOLUTION RANGES
|
|
267
|
+
// per color channel resolution range
|
|
268
|
+
// Each channel can have different available resolution levels
|
|
269
|
+
// e.g., for Channel 0: (min_res, max_res)
|
|
270
|
+
// ========================================
|
|
271
|
+
uniform ivec2 res0;
|
|
272
|
+
uniform ivec2 res1;
|
|
273
|
+
uniform ivec2 res2;
|
|
274
|
+
uniform ivec2 res3;
|
|
275
|
+
uniform ivec2 res4;
|
|
276
|
+
uniform ivec2 res5;
|
|
277
|
+
uniform ivec2 res6;
|
|
278
|
+
// Channel 7: unused
|
|
279
|
+
uniform ivec2 res7;
|
|
280
|
+
|
|
281
|
+
// ========================================
|
|
282
|
+
// LEVEL-OF-DETAIL PARAMETERS
|
|
283
|
+
// controls how fast we decrease the resolution
|
|
284
|
+
// ========================================
|
|
285
|
+
// LOD factor for distance-based resolution selection
|
|
286
|
+
uniform float lodFactor;
|
|
287
|
+
|
|
288
|
+
// ========================================
|
|
289
|
+
// ANCHOR POINTS (per resolution level)
|
|
290
|
+
// per resolution anchor point for pagetable
|
|
291
|
+
// ========================================
|
|
292
|
+
// Anchor points define the origin of page table for each resolution level
|
|
293
|
+
// Resolution 0 anchor point (highest detail)
|
|
294
|
+
uniform uvec3 anchor0;
|
|
295
|
+
uniform uvec3 anchor1;
|
|
296
|
+
uniform uvec3 anchor2;
|
|
297
|
+
uniform uvec3 anchor3;
|
|
298
|
+
uniform uvec3 anchor4;
|
|
299
|
+
uniform uvec3 anchor5;
|
|
300
|
+
uniform uvec3 anchor6;
|
|
301
|
+
uniform uvec3 anchor7;
|
|
302
|
+
uniform uvec3 anchor8;
|
|
303
|
+
uniform uvec3 anchor9;
|
|
304
|
+
// Resolution 9 anchor point (lowest detail)
|
|
305
|
+
|
|
306
|
+
// ========================================
|
|
307
|
+
// SCALE FACTORS (per resolution level)
|
|
308
|
+
// per resolution downsample factor
|
|
309
|
+
// ========================================
|
|
310
|
+
// Scale factors determine voxel size at each resolution level
|
|
311
|
+
// Resolution 0 scale factors (should be 1,1,1)
|
|
312
|
+
uniform vec3 scale0;
|
|
313
|
+
uniform vec3 scale1;
|
|
314
|
+
uniform vec3 scale2;
|
|
315
|
+
uniform vec3 scale3;
|
|
316
|
+
uniform vec3 scale4;
|
|
317
|
+
uniform vec3 scale5;
|
|
318
|
+
uniform vec3 scale6;
|
|
319
|
+
uniform vec3 scale7;
|
|
320
|
+
uniform vec3 scale8;
|
|
321
|
+
uniform vec3 scale9;
|
|
322
|
+
// Resolution 9 scale factors
|
|
323
|
+
|
|
324
|
+
// ========================================
|
|
325
|
+
// VARYING VARIABLES (unused but required)
|
|
326
|
+
// ========================================
|
|
327
|
+
// Fragment position (unused)
|
|
328
|
+
varying vec4 glPosition;
|
|
329
|
+
// World space coordinates (used for depth only)
|
|
330
|
+
varying vec3 worldSpaceCoords;
|
|
331
|
+
|
|
332
|
+
// ========================================
|
|
333
|
+
// OUTPUT VARIABLES (multiple render targets)
|
|
334
|
+
// output buffers
|
|
335
|
+
// ========================================
|
|
336
|
+
// Final rendered color (sRGB)
|
|
337
|
+
layout(location = 0) out vec4 gColor;
|
|
338
|
+
// Brick loading requests (packed coordinates)
|
|
339
|
+
layout(location = 1) out vec4 gRequest;
|
|
340
|
+
// Brick usage tracking (for cache management)
|
|
341
|
+
layout(location = 2) out vec4 gUsage;
|
|
342
|
+
|
|
343
|
+
// ========================================
|
|
344
|
+
// CONSTANTS
|
|
345
|
+
// ========================================
|
|
346
|
+
// Size of each brick in voxels (32x32x32)
|
|
347
|
+
const float BRICK_SIZE = 32.0;
|
|
348
|
+
// Brick cache texture width
|
|
349
|
+
const float BRICK_CACHE_SIZE_X = 2048.0;
|
|
350
|
+
// Brick cache texture height
|
|
351
|
+
const float BRICK_CACHE_SIZE_Y = 2048.0;
|
|
352
|
+
// Brick cache texture depth
|
|
353
|
+
const float BRICK_CACHE_SIZE_Z = 128.0;
|
|
354
|
+
// Number of bricks in X (64)
|
|
355
|
+
const float BRICK_CACHE_BRICKS_X = BRICK_CACHE_SIZE_X / BRICK_SIZE;
|
|
356
|
+
// Number of bricks in Y (64)
|
|
357
|
+
const float BRICK_CACHE_BRICKS_Y = BRICK_CACHE_SIZE_Y / BRICK_SIZE;
|
|
358
|
+
// Number of bricks in Z (4)
|
|
359
|
+
const float BRICK_CACHE_BRICKS_Z = BRICK_CACHE_SIZE_Z / BRICK_SIZE;
|
|
360
|
+
|
|
361
|
+
// ========================================
|
|
362
|
+
// RAY-VOLUME INTERSECTION
|
|
363
|
+
// calculating the intersection of the ray with the bounding box
|
|
364
|
+
// ========================================
|
|
365
|
+
// Calculates the intersection of a ray with the volume's bounding box
|
|
366
|
+
// Returns (entry_time, exit_time) for the ray-box intersection
|
|
367
|
+
// Handles clipping planes by adjusting the bounding box
|
|
368
|
+
//
|
|
369
|
+
// Parameters:
|
|
370
|
+
// orig - vec3: Ray origin point in world space
|
|
371
|
+
// dir - vec3: Ray direction vector (should be normalized)
|
|
372
|
+
//
|
|
373
|
+
// Returns:
|
|
374
|
+
// vec2: (entry_time, exit_time) where:
|
|
375
|
+
// - entry_time: Distance along ray to enter the volume
|
|
376
|
+
// - exit_time: Distance along ray to exit the volume
|
|
377
|
+
// - If no intersection: entry_time > exit_time
|
|
378
|
+
vec2 intersect_hit(vec3 orig, vec3 dir) {
|
|
379
|
+
// Start with full volume bounds
|
|
380
|
+
vec3 boxMin = vec3(-0.5) * boxSize;
|
|
381
|
+
vec3 boxMax = vec3(0.5) * boxSize;
|
|
382
|
+
|
|
383
|
+
// Apply clipping planes if they're active (xClip.x > -1.0 means active)
|
|
384
|
+
if (xClip.x > -1.0) {
|
|
385
|
+
boxMin.x = xClip.x - (boxSize.x / 2.0);
|
|
386
|
+
if (xClip.y < boxSize.x)
|
|
387
|
+
boxMax.x = xClip.y - (boxSize.x / 2.0);
|
|
388
|
+
}
|
|
389
|
+
if (yClip.x > -1.0) {
|
|
390
|
+
boxMin.y = yClip.x - (boxSize.y / 2.0);
|
|
391
|
+
if (yClip.y < boxSize.y)
|
|
392
|
+
boxMax.y = yClip.y - (boxSize.y / 2.0);
|
|
393
|
+
}
|
|
394
|
+
if (zClip.x > -1.0) {
|
|
395
|
+
boxMin.z = zClip.x - (boxSize.z / 2.0);
|
|
396
|
+
if (zClip.y < boxSize.z)
|
|
397
|
+
boxMax.z = zClip.y - (boxSize.z / 2.0);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// Standard ray-box intersection algorithm
|
|
401
|
+
vec3 invDir = 1.0 / dir;
|
|
402
|
+
vec3 tmin0 = (boxMin - orig) * invDir;
|
|
403
|
+
vec3 tmax0 = (boxMax - orig) * invDir;
|
|
404
|
+
vec3 tmin = min(tmin0, tmax0);
|
|
405
|
+
vec3 tmax = max(tmin0, tmax0);
|
|
406
|
+
float t0 = max(tmin.x, max(tmin.y, tmin.z)); // Entry time
|
|
407
|
+
float t1 = min(tmax.x, min(tmax.y, tmax.z)); // Exit time
|
|
408
|
+
return vec2(t0, t1);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// ========================================
|
|
412
|
+
// UTILITY FUNCTIONS
|
|
413
|
+
// ========================================
|
|
414
|
+
|
|
415
|
+
// Pseudo-random number generator for jittered sampling
|
|
416
|
+
// random number generator based on the uv coordinate
|
|
417
|
+
// Author @patriciogv - 2015
|
|
418
|
+
// http://patriciogonzalezvivo.com
|
|
419
|
+
//
|
|
420
|
+
// Parameters:
|
|
421
|
+
// None (uses gl_FragCoord.xy as input)
|
|
422
|
+
//
|
|
423
|
+
// Returns:
|
|
424
|
+
// float: Random value between 0.0 and 1.0 based on fragment coordinates
|
|
425
|
+
float random() {
|
|
426
|
+
return fract(sin(dot(gl_FragCoord.xy, vec2(12.9898,78.233)))* 43758.5453123);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Convert from linear RGB to sRGB color space
|
|
430
|
+
// Implements the standard sRGB transfer function for gamma correction
|
|
431
|
+
//
|
|
432
|
+
// Parameters:
|
|
433
|
+
// x - float: Linear RGB value between 0.0 and 1.0
|
|
434
|
+
//
|
|
435
|
+
// Returns:
|
|
436
|
+
// float: sRGB value between 0.0 and 1.0
|
|
437
|
+
float linear_to_srgb(float x) {
|
|
438
|
+
if (x <= 0.0031308f) {
|
|
439
|
+
return 12.92f * x;
|
|
440
|
+
}
|
|
441
|
+
return 1.055f * pow(x, 1.f / 2.4f) - 0.055f;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// Convert from linear RGB to sRGB color space (vector version)
|
|
445
|
+
// Applies sRGB conversion to each RGB component while preserving alpha
|
|
446
|
+
//
|
|
447
|
+
// Parameters:
|
|
448
|
+
// x - vec4: Linear RGBA color with components between 0.0 and 1.0
|
|
449
|
+
//
|
|
450
|
+
// Returns:
|
|
451
|
+
// vec4: sRGB RGBA color with components between 0.0 and 1.0
|
|
452
|
+
vec4 linear_to_srgb(vec4 x) {
|
|
453
|
+
return vec4(linear_to_srgb(x.r), linear_to_srgb(x.g), linear_to_srgb(x.b), x.a);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// ========================================
|
|
457
|
+
// PAGE TABLE COORDINATE PACKING
|
|
458
|
+
// transform the pagetable coordinate into a RGBA8 value
|
|
459
|
+
// ========================================
|
|
460
|
+
// Packs 3D page table coordinates into RGBA8 texture format
|
|
461
|
+
// Uses 10 bits for X, 10 bits for Y, 12 bits for Z
|
|
462
|
+
//
|
|
463
|
+
// Parameters:
|
|
464
|
+
// coord - uvec3: 3D coordinates to pack (X, Y, Z components)
|
|
465
|
+
// - X coordinate: 10-bit unsigned integer (0-1023)
|
|
466
|
+
// - Y coordinate: 10-bit unsigned integer (0-1023)
|
|
467
|
+
// - Z coordinate: 12-bit unsigned integer (0-4095)
|
|
468
|
+
//
|
|
469
|
+
// Returns:
|
|
470
|
+
// vec4: RGBA8 encoded coordinates with components between 0.0 and 1.0
|
|
471
|
+
// - R: Upper 8 bits of packed 32-bit value
|
|
472
|
+
// - G: Middle-upper 8 bits of packed 32-bit value
|
|
473
|
+
// - B: Middle-lower 8 bits of packed 32-bit value
|
|
474
|
+
// - A: Lower 8 bits of packed 32-bit value
|
|
475
|
+
vec4 packPTCoordToRGBA8(uvec3 coord) {
|
|
476
|
+
|
|
477
|
+
uint x = coord.x & 0x3FFu; // 10 bits for X coordinate
|
|
478
|
+
uint y = coord.y & 0x3FFu; // 10 bits for Y coordinate
|
|
479
|
+
uint z = coord.z & 0xFFFu; // 12 bits for Z coordinate
|
|
480
|
+
|
|
481
|
+
// Pack into 32-bit integer
|
|
482
|
+
uint packed =
|
|
483
|
+
(x << 22u) |
|
|
484
|
+
(y << 12u) |
|
|
485
|
+
(z);
|
|
486
|
+
|
|
487
|
+
// Decompose into RGBA8 format
|
|
488
|
+
return vec4(
|
|
489
|
+
float((packed >> 24u) & 0xFFu) / 255.0,
|
|
490
|
+
float((packed >> 16u) & 0xFFu) / 255.0,
|
|
491
|
+
float((packed >> 8u) & 0xFFu) / 255.0,
|
|
492
|
+
float(packed & 0xFFu) / 255.0
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// ========================================
|
|
497
|
+
// RESOLUTION AND ANCHOR POINT ACCESSORS
|
|
498
|
+
// ========================================
|
|
499
|
+
|
|
500
|
+
// Get anchor point for a specific resolution level
|
|
501
|
+
// Anchor points define the origin of the page table for each resolution
|
|
502
|
+
//
|
|
503
|
+
// Parameters:
|
|
504
|
+
// index - int: Resolution level index (0-9, where 0 is highest resolution)
|
|
505
|
+
//
|
|
506
|
+
// Returns:
|
|
507
|
+
// uvec3: 3D anchor point coordinates in the page table, or (-1, -1, -1) if invalid
|
|
508
|
+
uvec3 getAnchorPoint(int index) {
|
|
509
|
+
if (index == 0) return anchor0;
|
|
510
|
+
if (index == 1) return anchor1;
|
|
511
|
+
if (index == 2) return anchor2;
|
|
512
|
+
if (index == 3) return anchor3;
|
|
513
|
+
if (index == 4) return anchor4;
|
|
514
|
+
if (index == 5) return anchor5;
|
|
515
|
+
if (index == 6) return anchor6;
|
|
516
|
+
if (index == 7) return anchor7;
|
|
517
|
+
if (index == 8) return anchor8;
|
|
518
|
+
if (index == 9) return anchor9;
|
|
519
|
+
return uvec3(-1, -1, -1);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// Find the lowest available resolution level
|
|
523
|
+
// Returns the highest resolution index that has valid data
|
|
524
|
+
//
|
|
525
|
+
// Parameters:
|
|
526
|
+
// None
|
|
527
|
+
//
|
|
528
|
+
// Returns:
|
|
529
|
+
// int: Highest resolution level index (0-9) that has valid anchor point data
|
|
530
|
+
// Returns 9 if no valid resolution levels are found
|
|
531
|
+
int getLowestRes() {
|
|
532
|
+
for (int i = 0; i < 10; i++) {
|
|
533
|
+
if (getAnchorPoint(i) == uvec3(0,0,0)) {
|
|
534
|
+
return i - 1;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
return 9;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// Get the downsample factor for a resolution level
|
|
541
|
+
// Scale factors determine the voxel size at each resolution
|
|
542
|
+
//
|
|
543
|
+
// Parameters:
|
|
544
|
+
// index - int: Resolution level index (0-9, where 0 is highest resolution)
|
|
545
|
+
//
|
|
546
|
+
// Returns:
|
|
547
|
+
// vec3: Scale factors (x, y, z) for the resolution level, or (-1, -1, -1) if invalid
|
|
548
|
+
// Higher scale factors indicate larger voxels (lower resolution)
|
|
549
|
+
vec3 getScale(int index) {
|
|
550
|
+
if (index == 0) return scale0;
|
|
551
|
+
if (index == 1) return scale1;
|
|
552
|
+
if (index == 2) return scale2;
|
|
553
|
+
if (index == 3) return scale3;
|
|
554
|
+
if (index == 4) return scale4;
|
|
555
|
+
if (index == 5) return scale5;
|
|
556
|
+
if (index == 6) return scale6;
|
|
557
|
+
if (index == 7) return scale7;
|
|
558
|
+
if (index == 8) return scale8;
|
|
559
|
+
if (index == 9) return scale9;
|
|
560
|
+
return vec3(-1.0, -1.0, -1.0);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// Get the resolution range for a color channel
|
|
564
|
+
// Returns (min_res, max_res) for the channel
|
|
565
|
+
//
|
|
566
|
+
// Parameters:
|
|
567
|
+
// index - int: Channel index (0-6)
|
|
568
|
+
//
|
|
569
|
+
// Returns:
|
|
570
|
+
// ivec2: Resolution range as (min_resolution_level, max_resolution_level)
|
|
571
|
+
// Returns (-1, -1) if channel index is invalid
|
|
572
|
+
ivec2 getRes(int index) {
|
|
573
|
+
if (index == 0) return res0;
|
|
574
|
+
if (index == 1) return res1;
|
|
575
|
+
if (index == 2) return res2;
|
|
576
|
+
if (index == 3) return res3;
|
|
577
|
+
if (index == 4) return res4;
|
|
578
|
+
if (index == 5) return res5;
|
|
579
|
+
if (index == 6) return res6;
|
|
580
|
+
return ivec2(-1, -1);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
// Get the min/max values (contrast limits) for a color channel
|
|
584
|
+
// Returns (min_value, max_value) for normalization
|
|
585
|
+
//
|
|
586
|
+
// Parameters:
|
|
587
|
+
// index - int: Channel index (0-6)
|
|
588
|
+
//
|
|
589
|
+
// Returns:
|
|
590
|
+
// vec2: Contrast limits as (min_value, max_value) for data normalization
|
|
591
|
+
// Returns (-1.0, -1.0) if channel index is invalid
|
|
592
|
+
vec2 getClim(int index) {
|
|
593
|
+
if (index == 0) return clim0;
|
|
594
|
+
if (index == 1) return clim1;
|
|
595
|
+
if (index == 2) return clim2;
|
|
596
|
+
if (index == 3) return clim3;
|
|
597
|
+
if (index == 4) return clim4;
|
|
598
|
+
if (index == 5) return clim5;
|
|
599
|
+
if (index == 6) return clim6;
|
|
600
|
+
return vec2(-1.0, -1.0);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// ========================================
|
|
604
|
+
// COORDINATE TRANSFORMATIONS
|
|
605
|
+
// ========================================
|
|
606
|
+
|
|
607
|
+
// Convert normalized coordinates (0-1) to voxel coordinates.
|
|
608
|
+
// get the voxel coordinate in the specified resolution from the normalized coordinate
|
|
609
|
+
//
|
|
610
|
+
// Parameters:
|
|
611
|
+
// normalized - vec3: Normalized coordinates in range [0,1] for each axis
|
|
612
|
+
// res - int: Resolution level (0=highest detail, 9=lowest detail)
|
|
613
|
+
//
|
|
614
|
+
// Returns:
|
|
615
|
+
// vec3: Voxel coordinates in the volume space at the specified resolution
|
|
616
|
+
vec3 getVoxelFromNormalized(vec3 normalized, int res) {
|
|
617
|
+
vec3 extents = (vec3(voxelExtents) / getScale(res)); // Voxel extents at this resolution
|
|
618
|
+
vec3 voxel = normalized * extents;
|
|
619
|
+
return voxel;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// Convert voxel coordinates to normalized coordinates (0-1)
|
|
623
|
+
// get the normalized coordinate based on the voxel coordinate in the specified resolution
|
|
624
|
+
//
|
|
625
|
+
// Parameters:
|
|
626
|
+
// voxel - vec3: Voxel coordinates in the volume space
|
|
627
|
+
// res - int: Resolution level (0=highest detail, 9=lowest detail)
|
|
628
|
+
//
|
|
629
|
+
// Returns:
|
|
630
|
+
// vec3: Normalized coordinates in range [0,1] for each axis
|
|
631
|
+
vec3 getNormalizedFromVoxel(vec3 voxel, int res) {
|
|
632
|
+
vec3 extents = (vec3(voxelExtents) / getScale(res)); // Voxel extents at this resolution
|
|
633
|
+
vec3 normalized = voxel / extents;
|
|
634
|
+
return normalized;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// Convert normalized coordinates to brick coordinates.
|
|
638
|
+
// get the brick coordinate in the specified resolution based on the normalized coordinate
|
|
639
|
+
// needed for pagetable calculations
|
|
640
|
+
//
|
|
641
|
+
// Parameters:
|
|
642
|
+
// normalized - vec3: Normalized coordinates in range [0,1] for each axis
|
|
643
|
+
// res - int: Resolution level (0=highest detail, 9=lowest detail)
|
|
644
|
+
//
|
|
645
|
+
// Returns:
|
|
646
|
+
// vec3: Brick coordinates (each brick is 32x32x32 voxels)
|
|
647
|
+
vec3 getBrickFromNormalized(vec3 normalized, int res) {
|
|
648
|
+
vec3 voxel = getVoxelFromNormalized(normalized, res);
|
|
649
|
+
vec3 brick = floor(voxel / 32.0); // Each brick is 32x32x32 voxels
|
|
650
|
+
return brick;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// Convert voxel coordinates to brick coordinates.
|
|
654
|
+
// get the brick coordinate in the specified resolution based on the voxel coordinate
|
|
655
|
+
//
|
|
656
|
+
// Parameters:
|
|
657
|
+
// voxel - vec3: Voxel coordinates in the volume space
|
|
658
|
+
// res - int: Resolution level (0=highest detail, 9=lowest detail)
|
|
659
|
+
//
|
|
660
|
+
// Returns:
|
|
661
|
+
// vec3: Brick coordinates (each brick is 32x32x32 voxels)
|
|
662
|
+
vec3 getBrickFromVoxel(vec3 voxel, int res) {
|
|
663
|
+
vec3 brick = floor(voxel / 32.0); // Each brick is 32x32x32 voxels
|
|
664
|
+
return brick;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// ========================================
|
|
668
|
+
// CHANNEL-SPECIFIC ACCESSORS
|
|
669
|
+
// ========================================
|
|
670
|
+
|
|
671
|
+
// Get channel offset in page table.
|
|
672
|
+
// get the vector for the specified channel slot in the pagetable
|
|
673
|
+
// Different channels are stored at different Z-offsets in the page table
|
|
674
|
+
//
|
|
675
|
+
// Parameters:
|
|
676
|
+
// index - int: Channel index (0-6)
|
|
677
|
+
//
|
|
678
|
+
// Returns:
|
|
679
|
+
// uvec3: 3D offset coordinates for the channel in the page table
|
|
680
|
+
uvec3 getChannelOffset(int index) {
|
|
681
|
+
if (index == 0) return uvec3(0, 0, 1);
|
|
682
|
+
if (index == 1) return uvec3(0, 1, 0);
|
|
683
|
+
if (index == 2) return uvec3(0, 1, 1);
|
|
684
|
+
if (index == 3) return uvec3(1, 0, 0);
|
|
685
|
+
if (index == 4) return uvec3(1, 0, 1);
|
|
686
|
+
if (index == 5) return uvec3(1, 1, 0);
|
|
687
|
+
if (index == 6) return uvec3(1, 1, 1);
|
|
688
|
+
return uvec3(0, 0, 0);
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
// Get color for a channel.
|
|
692
|
+
// get the color per color channel
|
|
693
|
+
//
|
|
694
|
+
// Parameters:
|
|
695
|
+
// index - int: Channel index (0-6)
|
|
696
|
+
//
|
|
697
|
+
// Returns:
|
|
698
|
+
// vec3: RGB color values for the specified channel
|
|
699
|
+
vec3 getChannelColor(int index) {
|
|
700
|
+
if (index == 0) return color0.xyz;
|
|
701
|
+
if (index == 1) return color1.xyz;
|
|
702
|
+
if (index == 2) return color2.xyz;
|
|
703
|
+
if (index == 3) return color3.xyz;
|
|
704
|
+
if (index == 4) return color4.xyz;
|
|
705
|
+
if (index == 5) return color5.xyz;
|
|
706
|
+
if (index == 6) return color6.xyz;
|
|
707
|
+
return vec3(0.0, 0.0, 0.0);
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// Get opacity for a channel
|
|
711
|
+
// get the opacity (used as visibility) per color channel
|
|
712
|
+
//
|
|
713
|
+
// Parameters:
|
|
714
|
+
// index - int: Channel index (0-6)
|
|
715
|
+
//
|
|
716
|
+
// Returns:
|
|
717
|
+
// float: Opacity value (0.0-1.0) for the specified channel
|
|
718
|
+
float getChannelOpacity(int index) {
|
|
719
|
+
if (index == 0) return color0.w;
|
|
720
|
+
if (index == 1) return color1.w;
|
|
721
|
+
if (index == 2) return color2.w;
|
|
722
|
+
if (index == 3) return color3.w;
|
|
723
|
+
if (index == 4) return color4.w;
|
|
724
|
+
if (index == 5) return color5.w;
|
|
725
|
+
if (index == 6) return color6.w;
|
|
726
|
+
return 0.0;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
// ========================================
|
|
730
|
+
// PAGE TABLE DECODING
|
|
731
|
+
// ========================================
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* retrieving the brick based on:
|
|
735
|
+
* location -- normalized coordinate
|
|
736
|
+
* targetRes -- target resolution
|
|
737
|
+
* channel -- physical channel slot
|
|
738
|
+
* rnd -- random number for jittering requests
|
|
739
|
+
* query -- whether to query the brick (we dont query for interblock interpolation)
|
|
740
|
+
* colorIndex -- color index for querying the min max values
|
|
741
|
+
*
|
|
742
|
+
* returns:
|
|
743
|
+
* w >= 0 -- xyz contains brick cache coordinate, w stores resolution
|
|
744
|
+
* w == -1 -- not resident in any resolution, should be treated as empty
|
|
745
|
+
* w == -2 -- empty (with respect to current transfer function)
|
|
746
|
+
* w == -3 -- constant full (with respect to current transfer function)
|
|
747
|
+
* w == -4 -- constant value within range, x stores that value
|
|
748
|
+
*
|
|
749
|
+
* bit layout:
|
|
750
|
+
* [1] 31 | 0 — flag resident
|
|
751
|
+
* [1] 30 | 1 — flag init
|
|
752
|
+
* [7] 23…29 | 2…8 — min → 128
|
|
753
|
+
* [7] 16…22 | 9…15 — max → 128
|
|
754
|
+
* [6] 10…15 | 16…21 — x offset in brick cache → max 64
|
|
755
|
+
* [6] 4…9 | 22…27 — y offset in brick cache → max 64
|
|
756
|
+
* [4] 0…3 | 28…31 — z offset in brick cache → max 16, effectively 4
|
|
757
|
+
*/
|
|
758
|
+
|
|
759
|
+
/*
|
|
760
|
+
Page table entry format (32 bits):
|
|
761
|
+
[31] | 0 — flag resident (1=loaded in cache)
|
|
762
|
+
[30] | 1 — flag init (1=initialized)
|
|
763
|
+
[29:23] | 2…8 — min value (7 bits) → 128 levels
|
|
764
|
+
[22:16] | 9…15 — max value (7 bits) → 128 levels
|
|
765
|
+
[15:10] | 16…21 — x offset in brick cache (6 bits) → 64 bricks
|
|
766
|
+
[9:4] | 22…27 — y offset in brick cache (6 bits) → 64 bricks
|
|
767
|
+
[3:0] | 28…31 — z offset in brick cache (4 bits) → 16 bricks
|
|
768
|
+
*/
|
|
769
|
+
|
|
770
|
+
// Query page table to find brick location and status
|
|
771
|
+
// Searches for a brick at the specified location across multiple resolution levels
|
|
772
|
+
// and returns its cache coordinates and status information
|
|
773
|
+
//
|
|
774
|
+
// Parameters:
|
|
775
|
+
// location - vec3: Normalized coordinates (0-1) within the volume
|
|
776
|
+
// targetRes - int: Target resolution level to start searching from
|
|
777
|
+
// channel - int: Channel index (0-6) to query
|
|
778
|
+
// rnd - float: Random value (0-1) used for brick loading request selection
|
|
779
|
+
// query - bool: Whether to allow brick loading requests (true) or just query (false)
|
|
780
|
+
// colorIndex - int
|
|
781
|
+
//
|
|
782
|
+
// Returns:
|
|
783
|
+
// ivec4: (x_offset, y_offset, z_offset, status) where:
|
|
784
|
+
// - x_offset, y_offset, z_offset: Brick cache coordinates if found
|
|
785
|
+
// - status: Resolution level (>=0) if found, or status code:
|
|
786
|
+
// * -1: Not found at any resolution level
|
|
787
|
+
// * -2: Empty brick (all values below threshold)
|
|
788
|
+
// * -3: Constant full brick (all values above threshold)
|
|
789
|
+
// * -4: Constant value brick (uniform value)
|
|
790
|
+
// add maxres here
|
|
791
|
+
ivec4 getBrickLocation(vec3 location, int targetRes, int channel, float rnd, bool query, int colorIndex) {
|
|
792
|
+
|
|
793
|
+
// min max for current color
|
|
794
|
+
vec2 clim = getClim(colorIndex);
|
|
795
|
+
|
|
796
|
+
// resolution ranges, TODO: connect this back to color
|
|
797
|
+
int channelMin = getRes(channel).x;
|
|
798
|
+
int channelMax = getRes(channel).y;
|
|
799
|
+
|
|
800
|
+
// Clamp resolution to channel's available range
|
|
801
|
+
int currentRes = clamp(targetRes, channelMin, channelMax);
|
|
802
|
+
currentRes = clamp(currentRes, resGlobal.x, resGlobal.y);
|
|
803
|
+
int lowestRes = clamp(resGlobal.y, channelMin, channelMax);
|
|
804
|
+
|
|
805
|
+
// Determine if this channel should request brick loading.
|
|
806
|
+
// request the current channel based on probability
|
|
807
|
+
bool requestChannel = false;
|
|
808
|
+
if (int(floor(rnd * float(maxChannels))) == colorIndex) {
|
|
809
|
+
requestChannel = true;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
// Try progressively lower resolutions until we find data.
|
|
813
|
+
// loop through resolutions
|
|
814
|
+
while (currentRes <= lowestRes) {
|
|
815
|
+
|
|
816
|
+
// Calculate page table coordinates for this brick
|
|
817
|
+
uvec3 anchorPoint = getAnchorPoint(currentRes);
|
|
818
|
+
vec3 brickLocation = getBrickFromNormalized(location, currentRes);
|
|
819
|
+
uvec3 channelOffset = getChannelOffset(channel);
|
|
820
|
+
vec3 coordinate = floor(vec3(anchorPoint * channelOffset)) + brickLocation;
|
|
821
|
+
|
|
822
|
+
// Special handling for resolution 0 (highest detail)
|
|
823
|
+
if (currentRes == 0) {
|
|
824
|
+
int zExtent = int(ceil(float(voxelExtents.z) / 32.0));
|
|
825
|
+
coordinate = vec3(anchorPoint) + vec3(0.0, 0.0, zExtent * channel) + brickLocation;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
// Query the page table.
|
|
829
|
+
// get PT entry
|
|
830
|
+
uint ptEntry = texelFetch(pageTableTex, ivec3(coordinate), 0).r;
|
|
831
|
+
|
|
832
|
+
// Check if brick is initialized.
|
|
833
|
+
// check if the PT entry is initialized
|
|
834
|
+
uint isInit = (ptEntry >> 30u) & 1u;
|
|
835
|
+
if (isInit == 0u) {
|
|
836
|
+
currentRes++;
|
|
837
|
+
// Request brick loading if needed
|
|
838
|
+
if (requestChannel == true && (gRequest.a + gRequest.b + gRequest.g + gRequest.r == 0.0) && query == true) {
|
|
839
|
+
gRequest = packPTCoordToRGBA8(uvec3(coordinate));
|
|
840
|
+
}
|
|
841
|
+
continue;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
// Extract min/max values from page table entry.
|
|
845
|
+
// get the min max values of the brick
|
|
846
|
+
uint umin = ((ptEntry >> 23u) & 0x7Fu);
|
|
847
|
+
uint umax = ((ptEntry >> 16u) & 0x7Fu);
|
|
848
|
+
float min = float(int(umin)) / 127.0;
|
|
849
|
+
float max = float(int(umax)) / 127.0;
|
|
850
|
+
|
|
851
|
+
// Check if brick is empty (all values below threshold).
|
|
852
|
+
// exit early if brick is constant
|
|
853
|
+
if (float(max) <= clim.x) {
|
|
854
|
+
return ivec4(0,0,0,-2);
|
|
855
|
+
// EMPTY
|
|
856
|
+
} else if (float(min) >= clim.y) {
|
|
857
|
+
return ivec4(0,0,0,-3); // CONSTANT FULL
|
|
858
|
+
} else if ((umax - umin) < 2u) {
|
|
859
|
+
return ivec4(min,0,0,-4); // CONSTANT OTHER VALUE
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
// Check if brick is resident in cache.
|
|
863
|
+
// return brick cache location if resident
|
|
864
|
+
// continue to next resolution if not resident
|
|
865
|
+
uint isResident = (ptEntry >> 31u) & 1u;
|
|
866
|
+
if (isResident == 0u) {
|
|
867
|
+
currentRes++;
|
|
868
|
+
// Request brick loading if needed
|
|
869
|
+
if (requestChannel == true && (gRequest.a + gRequest.b + gRequest.g + gRequest.r == 0.0) && query == true) {
|
|
870
|
+
gRequest = packPTCoordToRGBA8(uvec3(coordinate));
|
|
871
|
+
}
|
|
872
|
+
continue;
|
|
873
|
+
} else {
|
|
874
|
+
// Extract brick cache coordinates
|
|
875
|
+
uint xBrickCache = (ptEntry >> 10u) & 0x3Fu;
|
|
876
|
+
uint yBrickCache = (ptEntry >> 4u) & 0x3Fu;
|
|
877
|
+
uint zBrickCache = ptEntry & 0xFu;
|
|
878
|
+
uvec3 brickCacheCoord = uvec3(xBrickCache, yBrickCache, zBrickCache);
|
|
879
|
+
|
|
880
|
+
return ivec4(brickCacheCoord, currentRes);
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
// not resident in any resolution, should be treated as empty
|
|
885
|
+
return ivec4(0,0,0,-1); // Not found
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
// Request brick loading for a specific location and resolution.
|
|
889
|
+
// Initiates a request to load a brick from disk into the brick cache.
|
|
890
|
+
// set the brick request for the specified slot channel
|
|
891
|
+
//
|
|
892
|
+
// Parameters:
|
|
893
|
+
// location - vec3: Normalized world space coordinates (0.0 to 1.0) where the brick is needed
|
|
894
|
+
// targetRes - int: Target resolution level (0-9, where 0 is highest resolution)
|
|
895
|
+
// channel - int: Channel index (0-6) for multi-channel datasets
|
|
896
|
+
// rnd - float: Random value between 0.0 and 1.0 used for load balancing across channels
|
|
897
|
+
//
|
|
898
|
+
// Returns:
|
|
899
|
+
// void: No return value, but sets gRequest output variable if conditions are met
|
|
900
|
+
void setBrickRequest(vec3 location, int targetRes, int channel, float rnd) {
|
|
901
|
+
uvec3 anchorPoint = getAnchorPoint(targetRes);
|
|
902
|
+
vec3 brickLocation = getBrickFromNormalized(location, targetRes);
|
|
903
|
+
uvec3 channelOffset = getChannelOffset(channel);
|
|
904
|
+
vec3 coordinate = floor(vec3(anchorPoint * channelOffset)) + brickLocation;
|
|
905
|
+
|
|
906
|
+
// Special handling for resolution 0
|
|
907
|
+
if (targetRes == 0) {
|
|
908
|
+
int zExtent = int(ceil(float(voxelExtents.z) / 32.0));
|
|
909
|
+
coordinate = vec3(anchorPoint) + vec3(0.0, 0.0, zExtent * channel) + brickLocation;
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
// Pack coordinates and set request
|
|
913
|
+
if (int(floor(rnd * float(maxChannels))) == channel) {
|
|
914
|
+
gRequest = packPTCoordToRGBA8(uvec3(coordinate));
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
// Track brick usage for cache management.
|
|
919
|
+
// Records which brick in the cache is being accessed for LRU (Least Recently Used) eviction.
|
|
920
|
+
// set the usage for the specified brick
|
|
921
|
+
//
|
|
922
|
+
// Parameters:
|
|
923
|
+
// brickCacheOffset - ivec3: 3D coordinates of the brick within the brick cache texture
|
|
924
|
+
// t_hit_min_os - float: Ray entry time in object space (start of ray-volume intersection)
|
|
925
|
+
// t_hit_max_os - float: Ray exit time in object space (end of ray-volume intersection)
|
|
926
|
+
// t_os - float: Current sampling position along the ray in object space
|
|
927
|
+
// rnd - float: Random value between 0.0 and 1.0 used for probabilistic usage tracking
|
|
928
|
+
//
|
|
929
|
+
// Returns:
|
|
930
|
+
// void: No return value, but sets gUsage output variable to track cache access patterns
|
|
931
|
+
void setUsage(ivec3 brickCacheOffset, float t_hit_min_os, float t_hit_max_os, float t_os, float rnd) {
|
|
932
|
+
float normalized_t_os = (t_os - t_hit_min_os) / (t_hit_max_os - t_hit_min_os); // Normalize to 0-1
|
|
933
|
+
if (normalized_t_os <= rnd || gUsage == vec4(0.0, 0.0, 0.0, 0.0)) {
|
|
934
|
+
gUsage = vec4(vec3(brickCacheOffset) / 255.0, 1.0);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
// ========================================
|
|
939
|
+
// UTILITY FUNCTIONS
|
|
940
|
+
// ========================================
|
|
941
|
+
|
|
942
|
+
// Get maximum component of a 3D vector.
|
|
943
|
+
// get the max value of a vec3
|
|
944
|
+
//
|
|
945
|
+
// Parameters:
|
|
946
|
+
// v - vec3: Input 3D vector
|
|
947
|
+
//
|
|
948
|
+
// Returns:
|
|
949
|
+
// float: The maximum value among the x, y, and z components
|
|
950
|
+
float vec3_max(vec3 v) {
|
|
951
|
+
return max(v.x, max(v.y, v.z));
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
// Get minimum component of a 3D vector.
|
|
955
|
+
// get the min value of a vec3
|
|
956
|
+
//
|
|
957
|
+
// Parameters:
|
|
958
|
+
// v - vec3: Input 3D vector
|
|
959
|
+
//
|
|
960
|
+
// Returns:
|
|
961
|
+
// float: The minimum value among the x, y, and z components
|
|
962
|
+
float vec3_min(vec3 v) {
|
|
963
|
+
return min(v.x, min(v.y, v.z));
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
// Calculate level-of-detail based on distance.
|
|
967
|
+
// Uses logarithmic scaling to determine appropriate resolution level.
|
|
968
|
+
// get the LOD based on the distance to the camera
|
|
969
|
+
//
|
|
970
|
+
// Parameters:
|
|
971
|
+
// distance - float: Distance from camera to sampling point
|
|
972
|
+
// highestRes - int: Highest available resolution level (typically 0)
|
|
973
|
+
// lowestRes - int: Lowest available resolution level (typically 9)
|
|
974
|
+
// lodFactor - float: Scaling factor that controls LOD sensitivity
|
|
975
|
+
//
|
|
976
|
+
// Returns:
|
|
977
|
+
// int: Resolution level index (0-9) where 0 is highest resolution
|
|
978
|
+
int getLOD(float distance, int highestRes, int lowestRes, float lodFactor) {
|
|
979
|
+
int lod = int(log2(distance * lodFactor));
|
|
980
|
+
return clamp(lod, highestRes, lowestRes);
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
// Calculate step size for ray marching at a given resolution.
|
|
984
|
+
// Determines optimal sampling step size based on voxel dimensions and ray direction.
|
|
985
|
+
// get the voxel step in object space
|
|
986
|
+
//
|
|
987
|
+
// Parameters:
|
|
988
|
+
// res - int: Resolution level index (0-9, where 0 is highest resolution)
|
|
989
|
+
// osDir - vec3: Ray direction vector in object space (should be normalized)
|
|
990
|
+
//
|
|
991
|
+
// Returns:
|
|
992
|
+
// float: Optimal step size in object space units for stable ray marching
|
|
993
|
+
float voxelStepOS(int res, vec3 osDir) {
|
|
994
|
+
vec3 voxelSize = getScale(res) / vec3(voxelExtents);
|
|
995
|
+
vec3 dt_vec = voxelSize / abs(osDir);
|
|
996
|
+
return min(dt_vec.x, min(dt_vec.y, dt_vec.z));
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
// ========================================
|
|
1000
|
+
// INTERPOLATION FUNCTIONS
|
|
1001
|
+
// ========================================
|
|
1002
|
+
|
|
1003
|
+
// Linear interpolation between two values.
|
|
1004
|
+
// Performs smooth interpolation between two scalar values.
|
|
1005
|
+
//
|
|
1006
|
+
// Parameters:
|
|
1007
|
+
// v0 - float: First value to interpolate from
|
|
1008
|
+
// v1 - float: Second value to interpolate to
|
|
1009
|
+
// fx - float: Interpolation factor between 0.0 and 1.0
|
|
1010
|
+
//
|
|
1011
|
+
// Returns:
|
|
1012
|
+
// float: Interpolated value between v0 and v1 based on fx
|
|
1013
|
+
float lerp(float v0, float v1, float fx) {
|
|
1014
|
+
return mix(v0, v1, fx); // (1-fx)·v0 + fx·v1
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
// Bilinear interpolation between four values
|
|
1018
|
+
// Performs 2D interpolation using four corner values arranged in a square
|
|
1019
|
+
//
|
|
1020
|
+
// Parameters:
|
|
1021
|
+
// v00 - float: Bottom-left corner value
|
|
1022
|
+
// v10 - float: Bottom-right corner value
|
|
1023
|
+
// v01 - float: Top-left corner value
|
|
1024
|
+
// v11 - float: Top-right corner value
|
|
1025
|
+
// f - vec2: 2D interpolation factors (x, y) between 0.0 and 1.0
|
|
1026
|
+
//
|
|
1027
|
+
// Returns:
|
|
1028
|
+
// float: Interpolated value from the four corner values
|
|
1029
|
+
float bilerp(float v00, float v10, float v01, float v11, vec2 f) {
|
|
1030
|
+
float c0 = mix(v00, v10, f.x); // Interpolate in X on bottom row
|
|
1031
|
+
float c1 = mix(v01, v11, f.x); // Interpolate in X on top row
|
|
1032
|
+
return mix(c0, c1, f.y); // Now interpolate those in Y
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
// Trilinear interpolation between eight values
|
|
1036
|
+
// Performs 3D interpolation using eight corner values arranged in a cube
|
|
1037
|
+
//
|
|
1038
|
+
// Parameters:
|
|
1039
|
+
// v000 - float: Bottom-left-back corner value
|
|
1040
|
+
// v100 - float: Bottom-right-back corner value
|
|
1041
|
+
// v010 - float: Bottom-left-front corner value
|
|
1042
|
+
// v110 - float: Bottom-right-front corner value
|
|
1043
|
+
// v001 - float: Top-left-back corner value
|
|
1044
|
+
// v101 - float: Top-right-back corner value
|
|
1045
|
+
// v011 - float: Top-left-front corner value
|
|
1046
|
+
// v111 - float: Top-right-front corner value
|
|
1047
|
+
// f - vec3: 3D interpolation factors (x, y, z) between 0.0 and 1.0
|
|
1048
|
+
//
|
|
1049
|
+
// Returns:
|
|
1050
|
+
// float: Interpolated value from the eight corner values
|
|
1051
|
+
float trilerp(
|
|
1052
|
+
float v000, float v100, float v010, float v110,
|
|
1053
|
+
float v001, float v101, float v011, float v111,
|
|
1054
|
+
vec3 f) { // f = fract(coord)
|
|
1055
|
+
// Interpolate along X for each of the four bottom-face voxels
|
|
1056
|
+
float c00 = mix(v000, v100, f.x);
|
|
1057
|
+
float c10 = mix(v010, v110, f.x);
|
|
1058
|
+
float c01 = mix(v001, v101, f.x);
|
|
1059
|
+
float c11 = mix(v011, v111, f.x);
|
|
1060
|
+
|
|
1061
|
+
// Interpolate those along Y
|
|
1062
|
+
float c0 = mix(c00, c10, f.y);
|
|
1063
|
+
float c1 = mix(c01, c11, f.y);
|
|
1064
|
+
|
|
1065
|
+
// Final interpolation along Z
|
|
1066
|
+
return mix(c0, c1, f.z);
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
// ========================================
|
|
1070
|
+
// BRICK CACHE SAMPLING
|
|
1071
|
+
// ========================================
|
|
1072
|
+
|
|
1073
|
+
// Sample a value from the brick cache texture.
|
|
1074
|
+
// Converts brick coordinates and voxel position to texture coordinates for sampling.
|
|
1075
|
+
// sample the brick cache based on the brick cache coordinate and the in-brick coordinate
|
|
1076
|
+
//
|
|
1077
|
+
// Parameters:
|
|
1078
|
+
// brickCacheCoord - vec3: 3D coordinates of the brick within the brick cache
|
|
1079
|
+
// voxelInBrick - vec3: 3D coordinates of the voxel within the brick (0-31 in each dimension)
|
|
1080
|
+
//
|
|
1081
|
+
// Returns:
|
|
1082
|
+
// float: Sampled voxel value from the brick cache texture (typically normalized 0.0-1.0)
|
|
1083
|
+
float sampleBrick(vec3 brickCacheCoord, vec3 voxelInBrick) {
|
|
1084
|
+
vec3 brickCacheCoordNormalized = vec3(
|
|
1085
|
+
(float(brickCacheCoord.x) * BRICK_SIZE + float(voxelInBrick.x)) / BRICK_CACHE_SIZE_X,
|
|
1086
|
+
(float(brickCacheCoord.y) * BRICK_SIZE + float(voxelInBrick.y)) / BRICK_CACHE_SIZE_Y,
|
|
1087
|
+
(float(brickCacheCoord.z) * BRICK_SIZE + float(voxelInBrick.z)) / BRICK_CACHE_SIZE_Z
|
|
1088
|
+
);
|
|
1089
|
+
return texture(brickCacheTex, brickCacheCoordNormalized).r;
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
/**
|
|
1093
|
+
* main renderloop
|
|
1094
|
+
*/
|
|
1095
|
+
void main(void) {
|
|
1096
|
+
|
|
1097
|
+
// ========================================
|
|
1098
|
+
// INITIALIZATION
|
|
1099
|
+
// ========================================
|
|
1100
|
+
|
|
1101
|
+
// Initialize all render targets (multiple output textures)
|
|
1102
|
+
gRequest = vec4(0,0,0,0); // Brick loading requests
|
|
1103
|
+
gUsage = vec4(0,0,0,0); // Brick usage tracking
|
|
1104
|
+
gColor = vec4(0.0, 0.0, 0.0, 0.0); // Final color output
|
|
1105
|
+
|
|
1106
|
+
// out color sums up our accumulated value before writing it into the gColor buffer
|
|
1107
|
+
vec4 outColor = vec4(0.0, 0.0, 0.0, 0.0); // Accumulated color
|
|
1108
|
+
|
|
1109
|
+
// Generate random number for jittered sampling (reduces artifacts)
|
|
1110
|
+
float rnd = random();
|
|
1111
|
+
|
|
1112
|
+
// Get the lowest available resolution level
|
|
1113
|
+
int lowestDataRes = getLowestRes();
|
|
1114
|
+
|
|
1115
|
+
// ========================================
|
|
1116
|
+
// RAY-VOLUME INTERSECTION
|
|
1117
|
+
// ========================================
|
|
1118
|
+
|
|
1119
|
+
// Normalize the view ray direction
|
|
1120
|
+
vec3 ws_rayDir = normalize(rayDirUnnorm);
|
|
1121
|
+
|
|
1122
|
+
// Calculate intersection with volume bounding box
|
|
1123
|
+
// Returns (entry_time, exit_time) for the ray-box intersection
|
|
1124
|
+
vec2 t_hit = intersect_hit(cameraCorrected, ws_rayDir);
|
|
1125
|
+
if (t_hit.x >= t_hit.y) { discard; } // Ray misses volume entirely
|
|
1126
|
+
|
|
1127
|
+
t_hit.x = max(t_hit.x, 0.0); // Clamp entry to 0 (no negative distances)
|
|
1128
|
+
float t = t_hit.x;
|
|
1129
|
+
|
|
1130
|
+
// Calculate distance from camera for LOD selection
|
|
1131
|
+
float distance = abs((cameraCorrected / boxSize).z + (ws_rayDir / boxSize).z * t );
|
|
1132
|
+
|
|
1133
|
+
// ========================================
|
|
1134
|
+
// COORDINATE SPACE CONVERSION
|
|
1135
|
+
// ========================================
|
|
1136
|
+
|
|
1137
|
+
// Convert from world space to object space (normalized 0-1 coordinates)
|
|
1138
|
+
float ws2os = length(ws_rayDir / boxSize); // Scale factor for conversion
|
|
1139
|
+
float t_hit_min_os = t_hit.x * ws2os; // Entry point in object space
|
|
1140
|
+
float t_hit_max_os = t_hit.y * ws2os; // Exit point in object space
|
|
1141
|
+
float t_os = t_hit_min_os; // Current position in object space
|
|
1142
|
+
|
|
1143
|
+
// Calculate effective LOD factor based on volume size.
|
|
1144
|
+
// voxel edge is the max extent of the volume
|
|
1145
|
+
float voxelEdge = float(max(voxelExtents.x, max(voxelExtents.y, voxelExtents.z)));
|
|
1146
|
+
|
|
1147
|
+
// calculate LOD factor based on the voxel edge
|
|
1148
|
+
float lodFactorEffective = lodFactor * voxelEdge / 256.0;
|
|
1149
|
+
|
|
1150
|
+
// ========================================
|
|
1151
|
+
// RESOLUTION AND SAMPLING SETUP
|
|
1152
|
+
// ========================================
|
|
1153
|
+
|
|
1154
|
+
// Determine target resolution based on distance (LOD)
|
|
1155
|
+
int targetRes = getLOD(t, 0, 9, lodFactorEffective);
|
|
1156
|
+
|
|
1157
|
+
// Set adaptive stepping resolution
|
|
1158
|
+
int stepResAdaptive = renderRes;
|
|
1159
|
+
int stepResEffective = clamp(stepResAdaptive, 0, lowestDataRes);
|
|
1160
|
+
|
|
1161
|
+
// Convert ray to object space coordinates
|
|
1162
|
+
vec3 os_rayDir = normalize(ws_rayDir / boxSize);
|
|
1163
|
+
vec3 os_rayOrigin = cameraCorrected / boxSize + vec3(0.5);
|
|
1164
|
+
|
|
1165
|
+
// Calculate step size based on current resolution
|
|
1166
|
+
float dt = voxelStepOS(stepResEffective, os_rayDir);
|
|
1167
|
+
|
|
1168
|
+
// ========================================
|
|
1169
|
+
// SAMPLING POSITION INITIALIZATION
|
|
1170
|
+
// ========================================
|
|
1171
|
+
|
|
1172
|
+
// Convert to normalized sampling coordinates (0-1 range)
|
|
1173
|
+
vec3 p = cameraCorrected + t_hit.x * ws_rayDir;
|
|
1174
|
+
p = p / boxSize + vec3(0.5); // Transform to 0-1 range
|
|
1175
|
+
|
|
1176
|
+
// Calculate step vector in normalized space
|
|
1177
|
+
vec3 dp = (os_rayDir * dt);
|
|
1178
|
+
|
|
1179
|
+
// Apply jittered sampling to reduce artifacts
|
|
1180
|
+
p += dp * (rnd);
|
|
1181
|
+
// Avoid boundary issues
|
|
1182
|
+
p = clamp(p, 0.0 + 0.0000028, 1.0 - 0.0000028);
|
|
1183
|
+
|
|
1184
|
+
// ========================================
|
|
1185
|
+
// RENDERING VARIABLES
|
|
1186
|
+
// ========================================
|
|
1187
|
+
|
|
1188
|
+
// Color accumulation for front-to-back compositing.
|
|
1189
|
+
// color accumulation variables, are calculated per 'slice'
|
|
1190
|
+
vec3 rgbCombo = vec3(0.0);
|
|
1191
|
+
float total = 0.0;
|
|
1192
|
+
|
|
1193
|
+
// For alpha blending.
|
|
1194
|
+
// alpha accumulation variable runs globally
|
|
1195
|
+
float alphaMultiplicator = 1.0;
|
|
1196
|
+
|
|
1197
|
+
// Request tracking (for brick loading).
|
|
1198
|
+
// if we have a request for a brick which not visible in lower
|
|
1199
|
+
// resolutions, we can overwrite it once
|
|
1200
|
+
bool overWrittenRequest = false;
|
|
1201
|
+
|
|
1202
|
+
// Current state tracking
|
|
1203
|
+
vec3 currentTargetResPTCoord = vec3(0,0,0);
|
|
1204
|
+
int currentLOD = targetRes;
|
|
1205
|
+
|
|
1206
|
+
// ========================================
|
|
1207
|
+
// CHANNEL-SPECIFIC CONSTANTS
|
|
1208
|
+
// ========================================
|
|
1209
|
+
|
|
1210
|
+
// Pre-compute channel properties for efficiency.
|
|
1211
|
+
// constants per color channel
|
|
1212
|
+
vec3 [] c_color = vec3[7](getChannelColor(0), getChannelColor(1), getChannelColor(2), getChannelColor(3), getChannelColor(4), getChannelColor(5), getChannelColor(6));
|
|
1213
|
+
float [] c_opacity = float[7](getChannelOpacity(0), getChannelOpacity(1), getChannelOpacity(2), getChannelOpacity(3), getChannelOpacity(4), getChannelOpacity(5), getChannelOpacity(6));
|
|
1214
|
+
// resolution ranges (currently) per color channel
|
|
1215
|
+
// TODO: figure out how to hook it up with frontend
|
|
1216
|
+
int [] c_res_min = int[7](getRes(0).x, getRes(1).x, getRes(2).x, getRes(3).x, getRes(4).x, getRes(5).x, getRes(6).x);
|
|
1217
|
+
int [] c_res_max = int[7](getRes(0).y, getRes(1).y, getRes(2).y, getRes(3).y, getRes(4).y, getRes(5).y, getRes(6).y);
|
|
1218
|
+
|
|
1219
|
+
// ========================================
|
|
1220
|
+
// PER-CHANNEL STATE ARRAYS
|
|
1221
|
+
// ========================================
|
|
1222
|
+
|
|
1223
|
+
// Current state for each channel.
|
|
1224
|
+
// current state variables per color channel
|
|
1225
|
+
|
|
1226
|
+
// current resolution
|
|
1227
|
+
int [] c_res_current = int[7](0,0,0,0,0,0,0);
|
|
1228
|
+
// current value
|
|
1229
|
+
float [] c_val_current = float[7](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
|
|
1230
|
+
// current brick cache coordinate
|
|
1231
|
+
vec3 [] c_brickCacheCoord_current = vec3[7](vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0));
|
|
1232
|
+
// current voxel in current resolution
|
|
1233
|
+
vec3 [] c_voxel_current = vec3[7](vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0));
|
|
1234
|
+
// current pagetable coordinate
|
|
1235
|
+
vec3 [] c_ptCoord_current = vec3[7](vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0));
|
|
1236
|
+
// current render mode -- 0: empty (add 0), 1: constant (add current val), 2: voxel (query new voxel)
|
|
1237
|
+
// upon change of PT we re-query anyways
|
|
1238
|
+
int [] c_renderMode_current = int[7](-1, -1, -1, -1, -1, -1, -1);
|
|
1239
|
+
|
|
1240
|
+
// Adjacent brick caching for interpolation
|
|
1241
|
+
// current pagetable coordinate of the adjacent bricks in X, Y, Z or diagonal direction
|
|
1242
|
+
vec3 [] c_PT_X_adjacent = vec3[7](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
|
|
1243
|
+
vec3 [] c_PT_Y_adjacent = vec3[7](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
|
|
1244
|
+
vec3 [] c_PT_Z_adjacent = vec3[7](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
|
|
1245
|
+
vec3 [] c_PT_XYZ_adjacent = vec3[7](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
|
|
1246
|
+
// corresponding brick coordinates of the adjacent bricks in X, Y, Z or diagonal direction
|
|
1247
|
+
vec4 [] c_brick_X_adjacent = vec4[7](vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0));
|
|
1248
|
+
vec4 [] c_brick_Y_adjacent = vec4[7](vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0));
|
|
1249
|
+
vec4 [] c_brick_Z_adjacent = vec4[7](vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0));
|
|
1250
|
+
vec4 [] c_brick_XYZ_adjacent = vec4[7](vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0));
|
|
1251
|
+
|
|
1252
|
+
// Min/max tracking for MIP/MinIP rendering.
|
|
1253
|
+
// min and max values of the current color
|
|
1254
|
+
// used for minimum/maximum intensity projection
|
|
1255
|
+
float [] c_minVal = float[7](-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0);
|
|
1256
|
+
float [] c_maxVal = float[7](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
|
|
1257
|
+
|
|
1258
|
+
// Per-resolution coordinate tracking
|
|
1259
|
+
vec3 [] r_ptCoord = vec3[10](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
|
|
1260
|
+
vec3 [] r_voxel = vec3[10](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
|
|
1261
|
+
vec3 [] r_prevPTCoord = vec3[10](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
|
|
1262
|
+
vec3 [] r_prevVoxel = vec3[10](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
|
|
1263
|
+
|
|
1264
|
+
// resolution changed flag
|
|
1265
|
+
bool resolutionChanged = false;
|
|
1266
|
+
// number of repetitions (for debugging purposes)
|
|
1267
|
+
int reps = 0;
|
|
1268
|
+
|
|
1269
|
+
// ========================================
|
|
1270
|
+
// MAIN RAY-MARCHING LOOP
|
|
1271
|
+
// ========================================
|
|
1272
|
+
|
|
1273
|
+
// while we are 'in' the volume.
|
|
1274
|
+
// Continue marching until we exit the volume or reach maximum opacity.
|
|
1275
|
+
while (t_os < t_hit_max_os && t_os >= t_hit_min_os
|
|
1276
|
+
&& vec3_max(p) < 1.0 && vec3_min(p) >= 0.0
|
|
1277
|
+
) {
|
|
1278
|
+
|
|
1279
|
+
// Reset per-sample accumulation.
|
|
1280
|
+
// initialize slice values
|
|
1281
|
+
vec3 rgbCombo = vec3(0.0);
|
|
1282
|
+
float total = 0.0;
|
|
1283
|
+
|
|
1284
|
+
// Update target resolution based on current distance (with jitter).
|
|
1285
|
+
// calculate target resolution based on distance and lod factor
|
|
1286
|
+
targetRes = getLOD(t, 0, 9, lodFactorEffective * (0.999 + 0.002 * rnd));
|
|
1287
|
+
|
|
1288
|
+
// ========================================
|
|
1289
|
+
// RESOLUTION CHANGE HANDLING
|
|
1290
|
+
// ========================================
|
|
1291
|
+
|
|
1292
|
+
// if target resolution changed, update the current resolution and stepsize
|
|
1293
|
+
if (targetRes != currentLOD) {
|
|
1294
|
+
currentLOD = targetRes;
|
|
1295
|
+
stepResAdaptive++;
|
|
1296
|
+
stepResEffective = clamp(stepResAdaptive, 0, lowestDataRes);
|
|
1297
|
+
|
|
1298
|
+
// Adjust sampling position for new resolution
|
|
1299
|
+
p -= dp * rnd;
|
|
1300
|
+
dt = voxelStepOS(stepResEffective, os_rayDir);
|
|
1301
|
+
dp = os_rayDir * dt;
|
|
1302
|
+
p += dp * rnd;
|
|
1303
|
+
resolutionChanged = true;
|
|
1304
|
+
|
|
1305
|
+
// Check bounds after resolution change
|
|
1306
|
+
if (p.x < 0.0 || p.x >= 1.0 || p.y < 0.0 || p.y >= 1.0 || p.z < 0.0 || p.z >= 1.0) {
|
|
1307
|
+
break;
|
|
1308
|
+
}
|
|
1309
|
+
} else {
|
|
1310
|
+
resolutionChanged = false;
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
// ========================================
|
|
1314
|
+
// UPDATE COORDINATES FOR ALL RESOLUTIONS
|
|
1315
|
+
// ========================================
|
|
1316
|
+
|
|
1317
|
+
// Calculate page table coordinates and voxel positions for all resolution levels
|
|
1318
|
+
for (int r = 0; r < 10; r++) {
|
|
1319
|
+
r_prevPTCoord[r] = r_ptCoord[r];
|
|
1320
|
+
r_prevVoxel[r] = r_voxel[r];
|
|
1321
|
+
r_ptCoord[r] = getBrickFromNormalized(p, r);
|
|
1322
|
+
r_voxel[r] = getVoxelFromNormalized(p, r);
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
// ========================================
|
|
1326
|
+
// RENDER MODE DEFINITIONS
|
|
1327
|
+
// ========================================
|
|
1328
|
+
// 0: empty brick (no data)
|
|
1329
|
+
// 1: constant brick (uniform value)
|
|
1330
|
+
// 2: voxel brick (variable data)
|
|
1331
|
+
|
|
1332
|
+
// initialize the per channel slice values
|
|
1333
|
+
vec3 sliceColor = vec3(0.0);
|
|
1334
|
+
float sliceAlpha = 0.0;
|
|
1335
|
+
|
|
1336
|
+
// ========================================
|
|
1337
|
+
// MULTI-CHANNEL SAMPLING
|
|
1338
|
+
// ========================================
|
|
1339
|
+
|
|
1340
|
+
// Process each channel independently.
|
|
1341
|
+
// iterate over up to 7 channels by color
|
|
1342
|
+
for (int c = 0; c < 7; c++) {
|
|
1343
|
+
// Skip channels with zero opacity.
|
|
1344
|
+
// skip if opacity is 0 or if color is not mapped to a physical slot
|
|
1345
|
+
if (c_opacity[c] <= 0.000001) {
|
|
1346
|
+
continue;
|
|
1347
|
+
} else if (channelMapping[c] == -1) {
|
|
1348
|
+
continue;
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
// physical slot in pagetable
|
|
1352
|
+
int slot = channelMapping[c];
|
|
1353
|
+
|
|
1354
|
+
// keep track of status
|
|
1355
|
+
bool newBrick = false;
|
|
1356
|
+
bool newVoxel = false;
|
|
1357
|
+
// best possible resolution
|
|
1358
|
+
int bestRes = clamp(targetRes, c_res_min[c], c_res_max[c]);
|
|
1359
|
+
|
|
1360
|
+
// Check if we need to load a new brick at a better resolution.
|
|
1361
|
+
// check if any new better resolution could be available, if so, we need to re-query the brick
|
|
1362
|
+
bool betterResChanged = false;
|
|
1363
|
+
for (int r = bestRes; r <= c_res_current[c]; r++ ) {
|
|
1364
|
+
if (r_ptCoord[r] != r_prevPTCoord[r]) {
|
|
1365
|
+
betterResChanged = true;
|
|
1366
|
+
break;
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
// Determine if we need to load new brick data.
|
|
1371
|
+
// check if we need to re-query the brick / voxel or reuse past 'val'
|
|
1372
|
+
if (r_ptCoord[bestRes] != r_prevPTCoord[bestRes]
|
|
1373
|
+
|| c_renderMode_current[c] == -1
|
|
1374
|
+
|| resolutionChanged == true
|
|
1375
|
+
|| betterResChanged
|
|
1376
|
+
) {
|
|
1377
|
+
newBrick = true;
|
|
1378
|
+
newVoxel = true;
|
|
1379
|
+
} else if (c_renderMode_current[c] == 2) {
|
|
1380
|
+
newVoxel = true;
|
|
1381
|
+
} else if (c_renderMode_current[c] == 0) {
|
|
1382
|
+
continue; // Skip empty bricks
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
// ========================================
|
|
1386
|
+
// BRICK LOADING AND CACHING
|
|
1387
|
+
// ========================================
|
|
1388
|
+
|
|
1389
|
+
// check if a new brick is available in the best possible resolution
|
|
1390
|
+
if (newBrick) {
|
|
1391
|
+
// Query page table for brick location and status
|
|
1392
|
+
ivec4 brickCacheInfo = getBrickLocation(p, bestRes, slot, rnd, true, c);
|
|
1393
|
+
// check information about the newly queried brick
|
|
1394
|
+
// if the res is not at best res, possibly the same as previous brick
|
|
1395
|
+
if (brickCacheInfo.w == -1 || brickCacheInfo.w == -2) {
|
|
1396
|
+
// Empty brick - no data available.
|
|
1397
|
+
// we can skip the rest of the loop
|
|
1398
|
+
c_val_current[c] = 0.0;
|
|
1399
|
+
c_renderMode_current[c] = 0;
|
|
1400
|
+
c_minVal[c] = 0.0;
|
|
1401
|
+
continue;
|
|
1402
|
+
} else if (brickCacheInfo.w == -3) {
|
|
1403
|
+
// Solid brick - constant maximum value.
|
|
1404
|
+
// we set the value and do not need to query a voxel
|
|
1405
|
+
c_val_current[c] = 1.0;
|
|
1406
|
+
c_renderMode_current[c] = 1;
|
|
1407
|
+
c_maxVal[c] = 1.0;
|
|
1408
|
+
newVoxel = false;
|
|
1409
|
+
} else if (brickCacheInfo.w == -4) {
|
|
1410
|
+
// Constant brick - uniform value.
|
|
1411
|
+
// static value -- we set the value and do not need to query a voxel
|
|
1412
|
+
float val = float(brickCacheInfo.x);
|
|
1413
|
+
c_val_current[c] = max(0.0, (val - getClim(c).x) / (getClim(c).y - getClim(c).x));
|
|
1414
|
+
c_renderMode_current[c] = 1;
|
|
1415
|
+
newVoxel = false;
|
|
1416
|
+
} else if (brickCacheInfo.w >= 0) {
|
|
1417
|
+
// Voxel brick - variable data, load from cache.
|
|
1418
|
+
// new brick -- we set the coordinate and resolution and need to query a voxel
|
|
1419
|
+
c_res_current[c] = brickCacheInfo.w;
|
|
1420
|
+
c_ptCoord_current[c] = r_ptCoord[c_res_current[c]];
|
|
1421
|
+
c_brickCacheCoord_current[c] = vec3(brickCacheInfo.xyz);
|
|
1422
|
+
c_renderMode_current[c] = 2;
|
|
1423
|
+
newVoxel = true;
|
|
1424
|
+
|
|
1425
|
+
// Track brick usage for cache management.
|
|
1426
|
+
// we set the usage of the brick based on the channel and the relative distance into the cube
|
|
1427
|
+
if (int(floor(rnd * float(maxChannels))) == c) {
|
|
1428
|
+
setUsage(brickCacheInfo.xyz, t_hit_min_os, t_hit_max_os, t_os, rnd);
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
// ========================================
|
|
1434
|
+
// VOXEL SAMPLING WITH INTERPOLATION
|
|
1435
|
+
// ========================================
|
|
1436
|
+
|
|
1437
|
+
// we need to query a new voxel e.g. sample the brick cache
|
|
1438
|
+
if (newVoxel) {
|
|
1439
|
+
c_voxel_current[c] = r_voxel[c_res_current[c]];
|
|
1440
|
+
|
|
1441
|
+
// we clamp the coordinate to be inside the brick and sample the volume
|
|
1442
|
+
reps++;
|
|
1443
|
+
|
|
1444
|
+
// Calculate position within the brick (0-31 range)
|
|
1445
|
+
vec3 voxelInBrick = mod(c_voxel_current[c], 32.0);
|
|
1446
|
+
vec3 clampedVoxelInBrick = clamp(voxelInBrick, 0.5, 31.5);
|
|
1447
|
+
// Sample the brick cache texture
|
|
1448
|
+
float val = sampleBrick(c_brickCacheCoord_current[c].xyz, clampedVoxelInBrick);
|
|
1449
|
+
|
|
1450
|
+
// ========================================
|
|
1451
|
+
// HIGH-QUALITY INTERPOLATION (renderRes == 0)
|
|
1452
|
+
// ========================================
|
|
1453
|
+
|
|
1454
|
+
// interblock interpolation
|
|
1455
|
+
if (renderRes == 0) {
|
|
1456
|
+
// calculate what axis we need to interpolate
|
|
1457
|
+
|
|
1458
|
+
// Check if we're near brick boundaries (need interpolation)
|
|
1459
|
+
bvec3 clampedMin = lessThan(voxelInBrick, clampedVoxelInBrick);
|
|
1460
|
+
bvec3 clampedMax = greaterThan(voxelInBrick, clampedVoxelInBrick);
|
|
1461
|
+
bvec3 clamped = bvec3(clampedMin.x || clampedMax.x, clampedMin.y || clampedMax.y, clampedMin.z || clampedMax.z);
|
|
1462
|
+
vec3 diff = voxelInBrick - clampedVoxelInBrick;
|
|
1463
|
+
|
|
1464
|
+
if (any(clampedMin) || any(clampedMax)) {
|
|
1465
|
+
int boundaryAxes = int(clamped.x) + int(clamped.y) + int(clamped.z);
|
|
1466
|
+
float f = 0.0;
|
|
1467
|
+
|
|
1468
|
+
if (boundaryAxes == 1) {
|
|
1469
|
+
// Linear interpolation across one boundary
|
|
1470
|
+
vec3 otherGlobalVoxelPos = vec3(0,0,0);
|
|
1471
|
+
vec3 otherP = vec3(0,0,0);
|
|
1472
|
+
float otherVoxelVal = 0.0;
|
|
1473
|
+
|
|
1474
|
+
// Determine which axis we're interpolating across
|
|
1475
|
+
if (clampedMin.x) {
|
|
1476
|
+
otherGlobalVoxelPos = c_voxel_current[c] - vec3(1.0, 0.0, 0.0);
|
|
1477
|
+
otherP = getNormalizedFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
|
|
1478
|
+
f = abs(diff.x);
|
|
1479
|
+
} else if (clampedMax.x) {
|
|
1480
|
+
otherGlobalVoxelPos = c_voxel_current[c] + vec3(1.0, 0.0, 0.0);
|
|
1481
|
+
otherP = getNormalizedFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
|
|
1482
|
+
f = abs(diff.x);
|
|
1483
|
+
} else if (clampedMin.y) {
|
|
1484
|
+
otherGlobalVoxelPos = c_voxel_current[c] - vec3(0.0, 1.0, 0.0);
|
|
1485
|
+
otherP = getNormalizedFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
|
|
1486
|
+
f = abs(diff.y);
|
|
1487
|
+
} else if (clampedMax.y) {
|
|
1488
|
+
otherGlobalVoxelPos = c_voxel_current[c] + vec3(0.0, 1.0, 0.0);
|
|
1489
|
+
otherP = getNormalizedFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
|
|
1490
|
+
f = abs(diff.y);
|
|
1491
|
+
} else if (clampedMin.z) {
|
|
1492
|
+
otherGlobalVoxelPos = c_voxel_current[c] - vec3(0.0, 0.0, 1.0);
|
|
1493
|
+
otherP = getNormalizedFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
|
|
1494
|
+
f = abs(diff.z);
|
|
1495
|
+
} else if (clampedMax.z) {
|
|
1496
|
+
otherGlobalVoxelPos = c_voxel_current[c] + vec3(0.0, 0.0, 1.0);
|
|
1497
|
+
otherP = getNormalizedFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
|
|
1498
|
+
f = abs(diff.z);
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
// Sample the neighboring voxel
|
|
1502
|
+
vec3 otherPTcoord = getBrickFromNormalized(otherP, c_res_current[c]);
|
|
1503
|
+
otherPTcoord = getBrickFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
|
|
1504
|
+
vec3 otherVoxelInBrick = mod(otherGlobalVoxelPos, 32.0);
|
|
1505
|
+
otherVoxelInBrick -= diff;
|
|
1506
|
+
|
|
1507
|
+
// Check if neighbor is outside volume bounds
|
|
1508
|
+
if (otherP.x < 0.0 || otherP.x >= 1.0 || otherP.y < 0.0 || otherP.y >= 1.0 || otherP.z < 0.0 || otherP.z >= 1.0) {
|
|
1509
|
+
otherVoxelVal = val;
|
|
1510
|
+
} else if (otherPTcoord == c_PT_XYZ_adjacent[c].xyz && c_brick_XYZ_adjacent[c].w >= 0.0) {
|
|
1511
|
+
// Use cached adjacent brick
|
|
1512
|
+
otherVoxelVal = sampleBrick(c_brick_XYZ_adjacent[c].xyz, otherVoxelInBrick);
|
|
1513
|
+
} else {
|
|
1514
|
+
// Load new adjacent brick
|
|
1515
|
+
ivec4 otherBrickCacheInfo = ivec4(-1);
|
|
1516
|
+
if (otherPTcoord == c_PT_XYZ_adjacent[c].xyz) {
|
|
1517
|
+
otherBrickCacheInfo = ivec4(c_brick_XYZ_adjacent[c]);
|
|
1518
|
+
} else {
|
|
1519
|
+
otherBrickCacheInfo = getBrickLocation(otherP, c_res_current[c], slot, rnd, false, c);
|
|
1520
|
+
}
|
|
1521
|
+
if (otherBrickCacheInfo.w == -1 || otherBrickCacheInfo.w == -2) {
|
|
1522
|
+
otherVoxelVal = val;
|
|
1523
|
+
} else if (otherBrickCacheInfo.w == -3) {
|
|
1524
|
+
otherVoxelVal = 1.0;
|
|
1525
|
+
} else if (otherBrickCacheInfo.w == -4) {
|
|
1526
|
+
otherVoxelVal = float(otherBrickCacheInfo.x);
|
|
1527
|
+
} else {
|
|
1528
|
+
// TODO: we do not recalculate the voxelInBrick based on the resolution
|
|
1529
|
+
otherVoxelVal = sampleBrick(vec3(otherBrickCacheInfo.xyz), otherVoxelInBrick);
|
|
1530
|
+
}
|
|
1531
|
+
c_PT_XYZ_adjacent[c] = getBrickFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
|
|
1532
|
+
c_brick_XYZ_adjacent[c] = vec4(otherBrickCacheInfo);
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
// Perform linear interpolation
|
|
1536
|
+
float originalVal = val;
|
|
1537
|
+
val = lerp(originalVal, otherVoxelVal, f);
|
|
1538
|
+
} else if (boundaryAxes == 2) {
|
|
1539
|
+
// Bilinear interpolation across two boundaries
|
|
1540
|
+
vec3 offA = vec3(0.0);
|
|
1541
|
+
vec3 offB = vec3(0.0);
|
|
1542
|
+
vec2 f = vec2(0.0);
|
|
1543
|
+
|
|
1544
|
+
// Determine which two axes we're interpolating across
|
|
1545
|
+
if (clamped.x && clamped.y) {
|
|
1546
|
+
offA.x = clampedMin.x ? -1.0 : 1.0;
|
|
1547
|
+
offB.y = clampedMin.y ? -1.0 : 1.0;
|
|
1548
|
+
f = vec2(abs(diff.x), abs(diff.y));
|
|
1549
|
+
} else if (clamped.x && clamped.z) {
|
|
1550
|
+
offA.x = clampedMin.x ? -1.0 : 1.0;
|
|
1551
|
+
offB.z = clampedMin.z ? -1.0 : 1.0;
|
|
1552
|
+
f = vec2(abs(diff.x), abs(diff.z));
|
|
1553
|
+
} else if (clamped.y && clamped.z) {
|
|
1554
|
+
offA.y = clampedMin.y ? -1.0 : 1.0;
|
|
1555
|
+
offB.z = clampedMin.z ? -1.0 : 1.0;
|
|
1556
|
+
f = vec2(abs(diff.y), abs(diff.z));
|
|
1557
|
+
}
|
|
1558
|
+
|
|
1559
|
+
// Macro for sampling at offset positions
|
|
1560
|
+
#define SAMPLE_AT_OFFSET(OFF, DEST) \
|
|
1561
|
+
{ \
|
|
1562
|
+
vec3 otherGlobalVoxelPos = c_voxel_current[c] + (OFF); \
|
|
1563
|
+
vec3 otherP = getNormalizedFromVoxel( \
|
|
1564
|
+
otherGlobalVoxelPos, c_res_current[c]); \
|
|
1565
|
+
\
|
|
1566
|
+
if ( any(lessThan(otherP, vec3(0.0))) \
|
|
1567
|
+
|| any(greaterThanEqual(otherP, vec3(1.0))) ) { \
|
|
1568
|
+
DEST = val; \
|
|
1569
|
+
} else { \
|
|
1570
|
+
vec3 otherPTcoord = getBrickFromNormalized( \
|
|
1571
|
+
otherP, c_res_current[c]); \
|
|
1572
|
+
vec3 otherVoxelInBrick = mod(otherGlobalVoxelPos, 32.0) - diff; \
|
|
1573
|
+
\
|
|
1574
|
+
bool matched = false; \
|
|
1575
|
+
if (otherPTcoord == c_PT_X_adjacent[c].xyz && c_brick_X_adjacent[c].w >= 0.0) { \
|
|
1576
|
+
DEST = sampleBrick(c_brick_X_adjacent[c].xyz, otherVoxelInBrick); \
|
|
1577
|
+
matched = true; \
|
|
1578
|
+
} else if (otherPTcoord == c_PT_Y_adjacent[c].xyz && c_brick_Y_adjacent[c].w >= 0.0) { \
|
|
1579
|
+
DEST = sampleBrick(c_brick_Y_adjacent[c].xyz, otherVoxelInBrick); \
|
|
1580
|
+
matched = true; \
|
|
1581
|
+
} else if (otherPTcoord == c_PT_Z_adjacent[c].xyz && c_brick_Z_adjacent[c].w >= 0.0) { \
|
|
1582
|
+
DEST = sampleBrick(c_brick_Z_adjacent[c].xyz, otherVoxelInBrick); \
|
|
1583
|
+
matched = true; \
|
|
1584
|
+
} else if (otherPTcoord == c_PT_XYZ_adjacent[c].xyz && c_brick_XYZ_adjacent[c].w >= 0.0) { \
|
|
1585
|
+
DEST = sampleBrick(c_brick_XYZ_adjacent[c].xyz, otherVoxelInBrick); \
|
|
1586
|
+
matched = true; \
|
|
1587
|
+
} \
|
|
1588
|
+
ivec4 info = ivec4(-1); \
|
|
1589
|
+
if (otherPTcoord == c_PT_X_adjacent[c].xyz) { info = ivec4(c_brick_X_adjacent[c]); } \
|
|
1590
|
+
else if (otherPTcoord == c_PT_Y_adjacent[c].xyz) { info = ivec4(c_brick_Y_adjacent[c]); } \
|
|
1591
|
+
else if (otherPTcoord == c_PT_Z_adjacent[c].xyz) { info = ivec4(c_brick_Z_adjacent[c]); } \
|
|
1592
|
+
else if (otherPTcoord == c_PT_XYZ_adjacent[c].xyz) { info = ivec4(c_brick_XYZ_adjacent[c]); } \
|
|
1593
|
+
else { info = getBrickLocation(otherP, c_res_current[c], slot, rnd, false, c); } \
|
|
1594
|
+
\
|
|
1595
|
+
if (!matched) { \
|
|
1596
|
+
if (info.w == -1 || info.w == -2) { \
|
|
1597
|
+
DEST = val; \
|
|
1598
|
+
} else if (info.w == -3) { \
|
|
1599
|
+
DEST = 1.0; \
|
|
1600
|
+
} else if (info.w == -4) { \
|
|
1601
|
+
DEST = float(info.x); \
|
|
1602
|
+
} else { \
|
|
1603
|
+
DEST = sampleBrick(vec3(info.xyz), otherVoxelInBrick); \
|
|
1604
|
+
} \
|
|
1605
|
+
if (abs((OFF).x) > 0.5 && abs((OFF).y) < 0.5 && abs((OFF).z) < 0.5) { \
|
|
1606
|
+
c_PT_X_adjacent[c] = otherPTcoord; \
|
|
1607
|
+
c_brick_X_adjacent[c] = vec4(info); \
|
|
1608
|
+
} else if (abs((OFF).y) > 0.5 && abs((OFF).x) < 0.5 && abs((OFF).z) < 0.5) { \
|
|
1609
|
+
c_PT_Y_adjacent[c] = otherPTcoord; \
|
|
1610
|
+
c_brick_Y_adjacent[c] = vec4(info); \
|
|
1611
|
+
} else if (abs((OFF).z) > 0.5 && abs((OFF).x) < 0.5 && abs((OFF).y) < 0.5) { \
|
|
1612
|
+
c_PT_Z_adjacent[c] = otherPTcoord; \
|
|
1613
|
+
c_brick_Z_adjacent[c] = vec4(info); \
|
|
1614
|
+
} else { \
|
|
1615
|
+
c_PT_XYZ_adjacent[c] = otherPTcoord; \
|
|
1616
|
+
c_brick_XYZ_adjacent[c] = vec4(info); \
|
|
1617
|
+
} \
|
|
1618
|
+
} \
|
|
1619
|
+
} \
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
// Sample the four corners for bilinear interpolation
|
|
1623
|
+
float v00 = val;
|
|
1624
|
+
float v10; float v01; float v11;
|
|
1625
|
+
SAMPLE_AT_OFFSET(offA, v10);
|
|
1626
|
+
SAMPLE_AT_OFFSET(offB, v01);
|
|
1627
|
+
SAMPLE_AT_OFFSET(offA + offB, v11);
|
|
1628
|
+
|
|
1629
|
+
val = bilerp(v00, v10, v01, v11, f);
|
|
1630
|
+
|
|
1631
|
+
#undef SAMPLE_AT_OFFSET
|
|
1632
|
+
|
|
1633
|
+
} else if (boundaryAxes == 3) {
|
|
1634
|
+
// Trilinear interpolation across all three boundaries
|
|
1635
|
+
|
|
1636
|
+
vec3 offA = vec3(0.0);
|
|
1637
|
+
vec3 offB = vec3(0.0);
|
|
1638
|
+
vec3 offC = vec3(0.0);
|
|
1639
|
+
vec3 f = vec3(0.0);
|
|
1640
|
+
|
|
1641
|
+
offA.x = clampedMin.x ? -1.0 : 1.0;
|
|
1642
|
+
offB.y = clampedMin.y ? -1.0 : 1.0;
|
|
1643
|
+
offC.z = clampedMin.z ? -1.0 : 1.0;
|
|
1644
|
+
|
|
1645
|
+
f = vec3(abs(diff.x), abs(diff.y), abs(diff.z));
|
|
1646
|
+
|
|
1647
|
+
// Macro for sampling at offset positions
|
|
1648
|
+
#define SAMPLE_AT_OFFSET(OFF, DEST) \
|
|
1649
|
+
{ \
|
|
1650
|
+
vec3 otherGlobalVoxelPos = c_voxel_current[c] + (OFF); \
|
|
1651
|
+
vec3 otherP = getNormalizedFromVoxel(otherGlobalVoxelPos, c_res_current[c]); \
|
|
1652
|
+
if (any(lessThan(otherP, vec3(0.0))) || any(greaterThanEqual(otherP, vec3(1.0)))) { \
|
|
1653
|
+
DEST = val; \
|
|
1654
|
+
} else { \
|
|
1655
|
+
vec3 otherPTcoord = getBrickFromNormalized(otherP, c_res_current[c]); \
|
|
1656
|
+
vec3 otherVoxelInBrick = mod(otherGlobalVoxelPos, 32.0) - diff; \
|
|
1657
|
+
if (otherPTcoord == c_PT_X_adjacent[c] && c_brick_X_adjacent[c].w >= 0.0) { \
|
|
1658
|
+
DEST = sampleBrick(c_brick_X_adjacent[c].xyz, otherVoxelInBrick); \
|
|
1659
|
+
} else if (otherPTcoord == c_PT_Y_adjacent[c].xyz && c_brick_Y_adjacent[c].w >= 0.0) { \
|
|
1660
|
+
DEST = sampleBrick(c_brick_Y_adjacent[c].xyz, otherVoxelInBrick); \
|
|
1661
|
+
} else if (otherPTcoord == c_PT_Z_adjacent[c].xyz && c_brick_Z_adjacent[c].w >= 0.0) { \
|
|
1662
|
+
DEST = sampleBrick(c_brick_Z_adjacent[c].xyz, otherVoxelInBrick); \
|
|
1663
|
+
} else if (otherPTcoord == c_PT_XYZ_adjacent[c].xyz && c_brick_XYZ_adjacent[c].w >= 0.0) { \
|
|
1664
|
+
DEST = sampleBrick(c_brick_XYZ_adjacent[c].xyz, otherVoxelInBrick); \
|
|
1665
|
+
} else { \
|
|
1666
|
+
ivec4 otherBrickCacheInfo = getBrickLocation(otherP, c_res_current[c], slot, rnd, false, c); \
|
|
1667
|
+
vec3 otherVoxelInBrick = mod(otherGlobalVoxelPos, 32.0) - diff; \
|
|
1668
|
+
if (otherBrickCacheInfo.w == -1 || otherBrickCacheInfo.w == -2) { \
|
|
1669
|
+
DEST = val; \
|
|
1670
|
+
} else if (otherBrickCacheInfo.w == -3) { \
|
|
1671
|
+
DEST = 1.0; \
|
|
1672
|
+
} else if (otherBrickCacheInfo.w == -4) { \
|
|
1673
|
+
DEST = float(otherBrickCacheInfo.x); \
|
|
1674
|
+
} else { \
|
|
1675
|
+
DEST = sampleBrick(vec3(otherBrickCacheInfo.xyz), otherVoxelInBrick); \
|
|
1676
|
+
} \
|
|
1677
|
+
} \
|
|
1678
|
+
} \
|
|
1679
|
+
}
|
|
1680
|
+
|
|
1681
|
+
// Sample all eight corners for trilinear interpolation
|
|
1682
|
+
float v000 = val;
|
|
1683
|
+
float v100; float v010; float v001; float v110; float v101; float v011; float v111;
|
|
1684
|
+
SAMPLE_AT_OFFSET(offA, v100);
|
|
1685
|
+
SAMPLE_AT_OFFSET(offB, v010);
|
|
1686
|
+
SAMPLE_AT_OFFSET(offC, v001);
|
|
1687
|
+
SAMPLE_AT_OFFSET(offA + offB, v110);
|
|
1688
|
+
SAMPLE_AT_OFFSET(offA + offC, v101);
|
|
1689
|
+
SAMPLE_AT_OFFSET(offB + offC, v011);
|
|
1690
|
+
SAMPLE_AT_OFFSET(offA + offB + offC, v111);
|
|
1691
|
+
|
|
1692
|
+
val = trilerp(v000, v100, v010, v001, v110, v101, v011, v111, f);
|
|
1693
|
+
|
|
1694
|
+
#undef SAMPLE_AT_OFFSET
|
|
1695
|
+
|
|
1696
|
+
}
|
|
1697
|
+
|
|
1698
|
+
} else {
|
|
1699
|
+
// No boundary interpolation needed - clear adjacent brick cache.
|
|
1700
|
+
// no adjacent bricks -> reset the adjacent trackers
|
|
1701
|
+
c_PT_X_adjacent[c] = c_PT_Y_adjacent[c] = c_PT_Z_adjacent[c] = c_PT_XYZ_adjacent[c] = vec3(-1.0);
|
|
1702
|
+
c_brick_X_adjacent[c] = c_brick_Y_adjacent[c] = c_brick_Z_adjacent[c] = c_brick_XYZ_adjacent[c] = vec4(-1.0);
|
|
1703
|
+
}
|
|
1704
|
+
}
|
|
1705
|
+
|
|
1706
|
+
// ========================================
|
|
1707
|
+
// VALUE NORMALIZATION AND TRACKING
|
|
1708
|
+
// ========================================
|
|
1709
|
+
|
|
1710
|
+
// Normalize value to 0-1 range using channel-specific contrast limits.
|
|
1711
|
+
// we normalize the (accumulated) value to the range of the color channel
|
|
1712
|
+
c_val_current[c] = max(0.0, (val - getClim(c).x) / (getClim(c).y - getClim(c).x));
|
|
1713
|
+
|
|
1714
|
+
// Track min/max values for MIP/MinIP rendering.
|
|
1715
|
+
// update the min and max values for the min/max projection
|
|
1716
|
+
if (c_minVal[c] == -1.0) {
|
|
1717
|
+
c_minVal[c] = c_val_current[c];
|
|
1718
|
+
} else {
|
|
1719
|
+
c_minVal[c] = min(c_minVal[c], c_val_current[c]);
|
|
1720
|
+
}
|
|
1721
|
+
c_maxVal[c] = max(c_maxVal[c], c_val_current[c]);
|
|
1722
|
+
|
|
1723
|
+
}
|
|
1724
|
+
|
|
1725
|
+
// ========================================
|
|
1726
|
+
// BRICK REQUEST GENERATION
|
|
1727
|
+
// ========================================
|
|
1728
|
+
|
|
1729
|
+
// Request higher resolution bricks if we're using lower resolution than optimal.
|
|
1730
|
+
// potentially overwrite brick request
|
|
1731
|
+
if (!overWrittenRequest
|
|
1732
|
+
&& c_res_current[c] != bestRes
|
|
1733
|
+
&& c_val_current[c] > 0.0
|
|
1734
|
+
&& c_renderMode_current[c] == 2
|
|
1735
|
+
&& int(floor(rnd * float(maxChannels))) == c) {
|
|
1736
|
+
setBrickRequest(p, bestRes, slot, rnd);
|
|
1737
|
+
overWrittenRequest = true;
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
// ========================================
|
|
1741
|
+
// CHANNEL COMPOSITING
|
|
1742
|
+
// ========================================
|
|
1743
|
+
|
|
1744
|
+
// Accumulate this channel's contribution.
|
|
1745
|
+
// sum up the values onto the slice values
|
|
1746
|
+
total += c_val_current[c];
|
|
1747
|
+
rgbCombo += c_val_current[c] * c_color[c];
|
|
1748
|
+
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
// ========================================
|
|
1752
|
+
// FRONT-TO-BACK COMPOSITING
|
|
1753
|
+
// ========================================
|
|
1754
|
+
|
|
1755
|
+
// Clamp total intensity and calculate alpha.
|
|
1756
|
+
// add the calculated slice to the total color
|
|
1757
|
+
total = clamp(total, 0.0, 1.0);
|
|
1758
|
+
sliceAlpha = total * opacity * dt * 32.0; // Scale by step size and brick size
|
|
1759
|
+
sliceColor = rgbCombo;
|
|
1760
|
+
|
|
1761
|
+
// Front-to-back alpha blending
|
|
1762
|
+
outColor.rgb += sliceAlpha * alphaMultiplicator * sliceColor;
|
|
1763
|
+
outColor.a += sliceAlpha * alphaMultiplicator;
|
|
1764
|
+
alphaMultiplicator *= (1.0 - sliceAlpha);
|
|
1765
|
+
|
|
1766
|
+
// Early termination for opaque regions (standard rendering only).
|
|
1767
|
+
// check if we can exit early
|
|
1768
|
+
if (outColor.a > 0.99 && u_renderstyle == 0) { break; }
|
|
1769
|
+
|
|
1770
|
+
// ========================================
|
|
1771
|
+
// ADVANCE RAY POSITION
|
|
1772
|
+
// ========================================
|
|
1773
|
+
|
|
1774
|
+
// Move to next sample position
|
|
1775
|
+
t += dt;
|
|
1776
|
+
p += dp;
|
|
1777
|
+
t_os += dt;
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
// ========================================
|
|
1781
|
+
// RENDERING STYLE POST-PROCESSING
|
|
1782
|
+
// ========================================
|
|
1783
|
+
|
|
1784
|
+
if (u_renderstyle == 1) {
|
|
1785
|
+
// Minimum Intensity Projection (MinIP)
|
|
1786
|
+
// Shows the minimum value encountered along each ray
|
|
1787
|
+
outColor = vec4(0.0);
|
|
1788
|
+
for (int c = 0; c < 7; c++) {
|
|
1789
|
+
if (c_color[c] != vec3(0.0, 0.0, 0.0)) {
|
|
1790
|
+
outColor.rgb += c_minVal[c] * c_color[c];
|
|
1791
|
+
outColor.a += c_minVal[c];
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
} else if (u_renderstyle == 0) {
|
|
1795
|
+
// Maximum Intensity Projection (MIP)
|
|
1796
|
+
// Shows the maximum value encountered along each ray
|
|
1797
|
+
outColor = vec4(0.0);
|
|
1798
|
+
for (int c = 0; c < 7; c++) {
|
|
1799
|
+
if (c_color[c] != vec3(0.0, 0.0, 0.0)) {
|
|
1800
|
+
outColor.rgb += c_maxVal[c] * c_color[c];
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
outColor.a = 1.0;
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1806
|
+
// ========================================
|
|
1807
|
+
// FINAL OUTPUT
|
|
1808
|
+
// ========================================
|
|
1809
|
+
|
|
1810
|
+
// Convert from linear to sRGB color space and set all render targets
|
|
1811
|
+
gColor = vec4(linear_to_srgb(outColor.r),
|
|
1812
|
+
linear_to_srgb(outColor.g),
|
|
1813
|
+
linear_to_srgb(outColor.b),
|
|
1814
|
+
outColor.a);
|
|
1815
|
+
|
|
1816
|
+
}
|
|
1817
|
+
`;
|