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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Texture blitting utilities for fullscreen quad rendering.
//!
//! This module provides a simple API for rendering textures to the screen,
//! useful for video backgrounds, post-processing, and image display.
use astrelis_core::profiling::profile_function;
use crate::Renderer;
use crate::capability::{GpuRequirements, RenderCapability};
use crate::context::GraphicsContext;
use crate::types::{GpuTexture, TypedBuffer};
use std::sync::Arc;
/// A renderer for blitting textures to the screen.
///
/// This provides an easy way to render a texture as a fullscreen quad,
/// useful for video backgrounds, splash screens, or post-processing effects.
///
/// # Example
///
/// ```ignore
/// let blit_renderer = BlitRenderer::new(context);
///
/// // In render loop:
/// blit_renderer.blit(&mut render_pass, &texture_view);
/// ```
impl RenderCapability for BlitRenderer {
fn requirements() -> GpuRequirements {
GpuRequirements::none()
}
fn name() -> &'static str {
"BlitRenderer"
}
}
pub struct BlitRenderer {
pipeline: wgpu::RenderPipeline,
bind_group_layout: wgpu::BindGroupLayout,
sampler: wgpu::Sampler,
vertex_buffer: TypedBuffer<f32>,
context: Arc<GraphicsContext>,
}
impl BlitRenderer {
/// Create a new blit renderer.
///
/// # Arguments
///
/// * `context` - The graphics context
/// * `target_format` - The format of the render target (typically the surface format)
pub fn new(context: Arc<GraphicsContext>, target_format: wgpu::TextureFormat) -> Self {
Self::new_with_options(context, target_format, BlitOptions::default())
}
/// Create a new blit renderer with custom options.
pub fn new_with_options(
context: Arc<GraphicsContext>,
target_format: wgpu::TextureFormat,
options: BlitOptions,
) -> Self {
profile_function!();
let renderer = Renderer::new(context.clone());
// Create shader
let shader = renderer.create_shader(Some("Blit Shader"), include_str!("shaders/blit.wgsl"));
// Create sampler
let sampler = context.device().create_sampler(&wgpu::SamplerDescriptor {
label: Some("Blit Sampler"),
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
mag_filter: options.filter_mode,
min_filter: options.filter_mode,
mipmap_filter: wgpu::FilterMode::Nearest,
..Default::default()
});
// Create bind group layout
let bind_group_layout =
context
.device()
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Blit Bind Group Layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
view_dimension: wgpu::TextureViewDimension::D2,
sample_type: wgpu::TextureSampleType::Float { filterable: true },
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
],
});
// Create pipeline layout
let pipeline_layout =
context
.device()
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Blit Pipeline Layout"),
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});
// Create pipeline
let pipeline = context
.device()
.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Blit Pipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("vs_main"),
buffers: &[wgpu::VertexBufferLayout {
array_stride: 16,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x2],
}],
compilation_options: wgpu::PipelineCompilationOptions::default(),
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState {
format: target_format,
blend: options.blend_state,
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: wgpu::PipelineCompilationOptions::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: None,
polygon_mode: wgpu::PolygonMode::Fill,
unclipped_depth: false,
conservative: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
cache: None,
});
// Create fullscreen quad vertex buffer
#[rustfmt::skip]
let vertices: [f32; 24] = [
// Position (clip space) UV
-1.0, -1.0, 0.0, 1.0,
1.0, -1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 0.0,
-1.0, -1.0, 0.0, 1.0,
1.0, 1.0, 1.0, 0.0,
-1.0, 1.0, 0.0, 0.0,
];
let vertex_buffer =
renderer.create_typed_vertex_buffer(Some("Blit Vertex Buffer"), &vertices);
Self {
pipeline,
bind_group_layout,
sampler,
vertex_buffer,
context,
}
}
/// Create a bind group for a texture.
///
/// You can cache this bind group if you're blitting the same texture repeatedly.
pub fn create_bind_group(&self, texture_view: &wgpu::TextureView) -> wgpu::BindGroup {
self.context
.device()
.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Blit Bind Group"),
layout: &self.bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(texture_view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(&self.sampler),
},
],
})
}
/// Blit a texture to the render target as a fullscreen quad.
///
/// # Arguments
///
/// * `render_pass` - The render pass to draw to
/// * `texture_view` - The texture to blit
///
/// Note: This creates a new bind group each call. For better performance
/// with frequently-blitted textures, use `create_bind_group` and `blit_with_bind_group`.
pub fn blit(&self, render_pass: &mut wgpu::RenderPass, texture_view: &wgpu::TextureView) {
profile_function!();
let bind_group = self.create_bind_group(texture_view);
self.blit_with_bind_group(render_pass, &bind_group);
}
/// Blit using a pre-created bind group.
///
/// More efficient than `blit` when the same texture is blitted multiple times.
pub fn blit_with_bind_group(
&self,
render_pass: &mut wgpu::RenderPass,
bind_group: &wgpu::BindGroup,
) {
render_pass.push_debug_group("BlitRenderer::blit");
render_pass.set_pipeline(&self.pipeline);
render_pass.set_bind_group(0, bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice());
render_pass.draw(0..6, 0..1);
render_pass.pop_debug_group();
}
/// Get the bind group layout for custom pipelines.
pub fn bind_group_layout(&self) -> &wgpu::BindGroupLayout {
&self.bind_group_layout
}
}
/// Options for configuring the blit renderer.
#[derive(Debug, Clone)]
pub struct BlitOptions {
/// Filter mode for texture sampling (Linear or Nearest)
pub filter_mode: wgpu::FilterMode,
/// Blend state for the blit operation
pub blend_state: Option<wgpu::BlendState>,
}
impl Default for BlitOptions {
fn default() -> Self {
Self {
filter_mode: wgpu::FilterMode::Linear,
blend_state: Some(wgpu::BlendState::REPLACE),
}
}
}
impl BlitOptions {
/// Create options for opaque blitting (no blending).
pub fn opaque() -> Self {
Self {
filter_mode: wgpu::FilterMode::Linear,
blend_state: Some(wgpu::BlendState::REPLACE),
}
}
/// Create options for alpha-blended blitting.
pub fn alpha_blend() -> Self {
Self {
filter_mode: wgpu::FilterMode::Linear,
blend_state: Some(wgpu::BlendState::ALPHA_BLENDING),
}
}
/// Create options for nearest-neighbor filtering (pixel art).
pub fn nearest() -> Self {
Self {
filter_mode: wgpu::FilterMode::Nearest,
blend_state: Some(wgpu::BlendState::REPLACE),
}
}
/// Set the filter mode.
pub fn with_filter(mut self, filter: wgpu::FilterMode) -> Self {
self.filter_mode = filter;
self
}
/// Set the blend state.
pub fn with_blend(mut self, blend: Option<wgpu::BlendState>) -> Self {
self.blend_state = blend;
self
}
}
/// Helper to upload texture data from CPU to GPU.
///
/// Useful for video frame upload or dynamic texture updates.
pub struct TextureUploader {
/// GPU texture with cached view and metadata.
texture: GpuTexture,
}
impl TextureUploader {
/// Create a new texture uploader with the specified dimensions.
///
/// # Arguments
///
/// * `context` - The graphics context
/// * `width` - Texture width in pixels
/// * `height` - Texture height in pixels
/// * `format` - Texture format (e.g., Rgba8UnormSrgb for standard images)
pub fn new(
context: &GraphicsContext,
width: u32,
height: u32,
format: wgpu::TextureFormat,
) -> Self {
let texture = GpuTexture::new_2d(
context.device(),
Some("Uploadable Texture"),
width,
height,
format,
wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
);
Self { texture }
}
/// Upload pixel data to the texture.
///
/// # Arguments
///
/// * `context` - The graphics context
/// * `data` - Raw pixel data (must match texture format and dimensions)
pub fn upload(&self, context: &GraphicsContext, data: &[u8]) {
use crate::extension::AsWgpu;
let bytes_per_pixel = self.texture.format().block_copy_size(None).unwrap_or(4);
let bytes_per_row = self.texture.width() * bytes_per_pixel;
context.queue().write_texture(
wgpu::TexelCopyTextureInfo {
texture: self.texture.as_wgpu(),
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
data,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(bytes_per_row),
rows_per_image: Some(self.texture.height()),
},
wgpu::Extent3d {
width: self.texture.width(),
height: self.texture.height(),
depth_or_array_layers: 1,
},
);
}
/// Upload a subregion of the texture.
///
/// # Arguments
///
/// * `context` - The graphics context
/// * `data` - Raw pixel data for the region
/// * `x`, `y` - Top-left corner of the region
/// * `width`, `height` - Dimensions of the region
pub fn upload_region(
&self,
context: &GraphicsContext,
data: &[u8],
x: u32,
y: u32,
width: u32,
height: u32,
) {
use crate::extension::AsWgpu;
let bytes_per_pixel = self.texture.format().block_copy_size(None).unwrap_or(4);
let bytes_per_row = width * bytes_per_pixel;
context.queue().write_texture(
wgpu::TexelCopyTextureInfo {
texture: self.texture.as_wgpu(),
mip_level: 0,
origin: wgpu::Origin3d { x, y, z: 0 },
aspect: wgpu::TextureAspect::All,
},
data,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(bytes_per_row),
rows_per_image: Some(height),
},
wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
);
}
/// Resize the texture (creates a new texture internally).
pub fn resize(&mut self, context: &GraphicsContext, width: u32, height: u32) {
if self.texture.width() == width && self.texture.height() == height {
return;
}
*self = Self::new(context, width, height, self.texture.format());
}
/// Get the texture view for rendering.
pub fn view(&self) -> &wgpu::TextureView {
self.texture.view()
}
/// Get the underlying texture.
pub fn texture(&self) -> &wgpu::Texture {
use crate::extension::AsWgpu;
self.texture.as_wgpu()
}
/// Get the texture dimensions.
pub fn size(&self) -> (u32, u32) {
(self.texture.width(), self.texture.height())
}
/// Get the texture format.
pub fn format(&self) -> wgpu::TextureFormat {
self.texture.format()
}
}