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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use super::*;
impl WebGlRayTraceBackend {
/// Compiles the ray-trace program and binds the fullscreen vertex
/// array.
///
/// Both shader stages are compiled and linked synchronously and the
/// driver's own info log is returned verbatim on failure, so an
/// invalid program is an error rather than a link that silently
/// produced no executable.
///
/// The data textures are deliberately not allocated here. Each is
/// created at the shape of the block it will hold, on the first
/// upload that needs it, so an attach reserves two placeholder texels
/// rather than the whole addressable budget of two textures.
///
/// Every step that can fail cleans up the objects the earlier steps
/// created: a refused vertex array, a failed second texture, or a
/// failed initial resize deletes the program, the vertex array, and
/// whichever texture already existed before returning.
///
/// # Arguments
///
/// - `&HtmlCanvasElement` - The canvas to draw into.
/// - `u32` - The surface width in pixels.
/// - `u32` - The surface height in pixels.
///
/// # Returns
///
/// - `Result<Self, JsValue>` - The ready backend, or the reason the
/// WebGL 2 path could not be set up.
pub fn attach(canvas: &HtmlCanvasElement, width: u32, height: u32) -> Result<Self, JsValue> {
let context: WebGl2RenderingContext = attach_context(canvas)?;
if context.is_context_lost() {
return Err(backend_error(
ERROR_WEBGL_CONTEXT_LOST,
ERROR_WEBGL_CONTEXT_LOST.to_string(),
));
}
let program: WebGlProgram = link_raytrace_program(&context)?;
let uniforms: Result<(WebGlUniformLocation, WebGlUniformLocation), JsValue> =
match resolve_sampler(&context, &program, RAYTRACE_GLSL_UNIFORM_SCENE) {
Ok(scene_uniform) => {
match resolve_sampler(&context, &program, RAYTRACE_GLSL_UNIFORM_FRAME) {
Ok(frame_uniform) => Ok((scene_uniform, frame_uniform)),
Err(reason) => Err(reason),
}
}
Err(reason) => Err(reason),
};
let (scene_uniform, frame_uniform): (WebGlUniformLocation, WebGlUniformLocation) =
match uniforms {
Ok(pair) => pair,
Err(reason) => {
release_partial(&context, Some(&program), None, None, None);
return Err(reason);
}
};
let Some(vao) = context.create_vertex_array() else {
release_partial(&context, Some(&program), None, None, None);
return Err(backend_error(
ERROR_WEBGL_PROGRAM,
ERROR_WEBGL_VAO_REJECTED.to_string(),
));
};
context.bind_vertex_array(Some(&vao));
let texture_limit: u32 = texture_size_limit(&context);
let dimension_limit: u32 = texture_limit.min(viewport_dimension_limit(&context));
let scene_texture: WebGlRayTraceTexture = match create_packed_texture(
&context,
WEBGL_TEXTURE_UNIT_SCENE,
WEBGL_PLACEHOLDER_TEXELS,
WEBGL_PLACEHOLDER_TEXELS,
) {
Ok(texture) => texture,
Err(reason) => {
release_partial(&context, Some(&program), Some(&vao), None, None);
return Err(reason);
}
};
let frame_texture: WebGlRayTraceTexture = match create_packed_texture(
&context,
WEBGL_TEXTURE_UNIT_FRAME,
WEBGL_PLACEHOLDER_TEXELS,
WEBGL_PLACEHOLDER_TEXELS,
) {
Ok(texture) => texture,
Err(reason) => {
release_partial(
&context,
Some(&program),
Some(&vao),
Some(&scene_texture),
None,
);
return Err(reason);
}
};
let mut backend: WebGlRayTraceBackend = WebGlRayTraceBackend {
context,
program,
vao,
scene_texture,
frame_texture,
scene_uniform,
frame_uniform,
scene_scratch: Vec::new(),
frame_scratch: Vec::new(),
dimension_limit_value: dimension_limit,
};
let resized: Result<(), JsValue> = backend.resize(width, height);
match resized {
Ok(()) => Ok(backend),
Err(reason) => {
backend.release();
Err(reason)
}
}
}
/// The largest surface dimension, in pixels, this context accepts.
///
/// The value is the smaller of the implementation's
/// `MAX_TEXTURE_SIZE` and its `MAX_VIEWPORT_DIMS` pair: a surface
/// larger than either cannot be drawn into. It is a per-dimension
/// pixel count, not a byte budget.
///
/// # Returns
///
/// - `Option<u32>` - The limit, or `None` when the context exposes
/// none to enforce.
pub fn dimension_limit(&self) -> Option<u32> {
match self.get_dimension_limit_value() {
0 => None,
limit => Some(limit),
}
}
/// Validates a new surface size and applies the full-surface
/// viewport.
///
/// The size is rejected before any state changes when the context is
/// lost or when either dimension exceeds the per-axis limit, so a
/// failed resize leaves the viewport, and therefore the previous,
/// still renderable, surface, exactly as it was. Neither data
/// texture is touched: they are sized by the packed blocks, not by
/// the surface, so a resize creates and destroys no GL object.
///
/// # Arguments
///
/// - `u32` - The surface width in pixels.
/// - `u32` - The surface height in pixels.
///
/// # Returns
///
/// - `Result<(), JsValue>` - `Ok` once the size is accepted.
pub fn resize(&mut self, width: u32, height: u32) -> Result<(), JsValue> {
validate_webgl_surface(
self.get_context(),
width,
height,
self.get_dimension_limit_value(),
)?;
apply_full_viewport(self.get_context())
}
/// Refills the frame texture and issues the fullscreen draw.
///
/// Only the frame block is uploaded here. The scene block was
/// uploaded by [`WebGlRayTraceBackend::upload_scene`] and is left
/// untouched, so a steady frame moves no scene texel.
///
/// The viewport is re-derived from the drawing buffer here rather
/// than trusting the one set at resize, because the browser resets
/// it whenever the canvas is resized — including the resize the
/// renderer performs after this backend accepted the new size — and
/// a stale viewport draws the fullscreen triangle into a corner.
///
/// # Arguments
///
/// - `&[f32]` - The packed frame block.
///
/// # Returns
///
/// - `Result<(), JsValue>` - `Ok` once the draw was issued.
pub fn draw(&mut self, frame: &[f32]) -> Result<(), JsValue> {
if self.get_context().is_context_lost() {
return Err(backend_error(
ERROR_WEBGL_CONTEXT_LOST,
ERROR_WEBGL_CONTEXT_LOST.to_string(),
));
}
let context: WebGl2RenderingContext = self.get_context().clone();
let mut scratch: Vec<f32> = mem::take(self.get_mut_frame_scratch());
let outcome: Result<(), JsValue> =
ensure_frame_texture(&context, self.get_mut_frame_texture(), &mut scratch, frame);
self.set_frame_scratch(scratch);
outcome?;
apply_full_viewport(self.get_context())?;
self.get_context().use_program(Some(self.get_program()));
self.get_context().bind_vertex_array(Some(self.get_vao()));
bind_data_texture(
self.get_context(),
WEBGL_TEXTURE_UNIT_SCENE,
self.get_scene_texture().get_texture(),
)?;
bind_data_texture(
self.get_context(),
WEBGL_TEXTURE_UNIT_FRAME,
self.get_frame_texture().get_texture(),
)?;
self.get_context()
.uniform1i(Some(self.get_scene_uniform()), WEBGL_SAMPLER_UNIT_SCENE);
self.get_context()
.uniform1i(Some(self.get_frame_uniform()), WEBGL_SAMPLER_UNIT_FRAME);
require_clear_gl_error(
self.get_context(),
ERROR_WEBGL_CALL,
WEBGL_ERROR_SOURCE_BINDING,
)?;
self.get_context().draw_arrays(
WebGl2RenderingContext::TRIANGLES,
WEBGL_FIRST_VERTEX,
WEBGL_VERTEX_COUNT,
);
require_clear_gl_error(self.get_context(), ERROR_WEBGL_CALL, "drawArrays")
}
/// Uploads the packed scene block into its data texture.
///
/// The texture grows to the shape the block needs when it outgrows
/// the current one, and keeps its allocation when it does not, so a
/// repeated update at a fixed size issues one `texSubImage2D` and
/// creates no GL object. Growth allocates the new texture, writes
/// the whole block into it, swaps it in, and only then deletes the
/// texture it replaced — a block the driver refuses leaves the
/// previous, still valid, scene bound and readable.
///
/// # Arguments
///
/// - `&[f32]` - The packed scene block.
///
/// # Returns
///
/// - `Result<(), JsValue>` - `Ok` once the block is live on the GPU.
pub fn upload_scene(&mut self, packed: &[f32]) -> Result<(), JsValue> {
if self.get_context().is_context_lost() {
return Err(backend_error(
ERROR_WEBGL_CONTEXT_LOST,
ERROR_WEBGL_CONTEXT_LOST.to_string(),
));
}
let context: WebGl2RenderingContext = self.get_context().clone();
let mut scratch: Vec<f32> = mem::take(self.get_mut_scene_scratch());
let outcome: Result<(), JsValue> =
ensure_scene_texture(&context, self.get_mut_scene_texture(), &mut scratch, packed);
self.set_scene_scratch(scratch);
outcome
}
/// Deletes every GL object the backend created.
///
/// # Returns
///
/// - `()` - Cleanup never fails; an object the driver already
/// reclaimed is simply not deleted again. A lost context has
/// already discarded every object, so none is deleted.
pub fn release(&mut self) {
if self.get_context().is_context_lost() {
return;
}
self.get_context()
.delete_texture(Some(self.get_scene_texture().get_texture()));
self.get_context()
.delete_texture(Some(self.get_frame_texture().get_texture()));
self.get_context().delete_vertex_array(Some(self.get_vao()));
self.get_context().delete_program(Some(self.get_program()));
}
}