sim_engine/renderer/mesh3d/texture/
creation.rs1use super::{
4 Arc, CpuMipChain, ImageBudget, ImageError, ImageTexelRect, Texture3d, Texture3dError,
5 Texture3dOptions, WgpuRenderer, image, mips, upload_chain,
6};
7
8impl WgpuRenderer {
9 pub fn create_texture3d_rgba8_with_options(
12 &self,
13 width: u32,
14 height: u32,
15 pixels: Vec<u8>,
16 budget: ImageBudget,
17 options: Texture3dOptions,
18 ) -> Result<Texture3d, Texture3dError> {
19 create_texture_with_options(
20 &self.device,
21 &self.queue,
22 &self.renderer_identity,
23 &self.mesh3d_renderer.textures.layout,
24 width,
25 height,
26 pixels,
27 budget,
28 options,
29 )
30 }
31
32 pub fn crop_texture3d_tile(
37 &self,
38 source: &Texture3d,
39 region: ImageTexelRect,
40 budget: ImageBudget,
41 options: Texture3dOptions,
42 ) -> Result<Texture3d, Texture3dError> {
43 crop_texture_tile(
44 &self.device,
45 &self.queue,
46 &self.renderer_identity,
47 &self.mesh3d_renderer.textures.layout,
48 source,
49 region,
50 budget,
51 options,
52 )
53 }
54}
55
56#[allow(clippy::too_many_arguments)]
57pub(in crate::renderer::mesh3d) fn create_texture_with_options(
58 device: &wgpu::Device,
59 queue: &wgpu::Queue,
60 identity: &Arc<()>,
61 layout: &wgpu::BindGroupLayout,
62 width: u32,
63 height: u32,
64 pixels: Vec<u8>,
65 budget: ImageBudget,
66 options: Texture3dOptions,
67) -> Result<Texture3d, Texture3dError> {
68 image::validate_image_shape(
69 width,
70 height,
71 pixels.len(),
72 budget,
73 device.limits().max_texture_dimension_2d,
74 )
75 .map_err(Texture3dError::Image)?;
76 if !options.preserves_alpha()
77 && let Some(texel) = pixels.chunks_exact(4).position(|pixel| pixel[3] != 255)
78 {
79 return Err(Texture3dError::NonOpaquePixel { texel });
80 }
81 let chain = CpuMipChain::new(
82 width,
83 height,
84 pixels,
85 budget,
86 device.limits().max_texture_dimension_2d,
87 options.mipmaps(),
88 )
89 .map_err(Texture3dError::Image)?;
90 Ok(upload_chain(
91 device,
92 queue,
93 identity,
94 layout,
95 chain,
96 options.preserves_alpha(),
97 ))
98}
99
100#[allow(clippy::too_many_arguments)]
101pub(in crate::renderer::mesh3d) fn crop_texture_tile(
102 device: &wgpu::Device,
103 queue: &wgpu::Queue,
104 identity: &Arc<()>,
105 layout: &wgpu::BindGroupLayout,
106 source: &Texture3d,
107 region: ImageTexelRect,
108 budget: ImageBudget,
109 options: Texture3dOptions,
110) -> Result<Texture3d, Texture3dError> {
111 if !source.belongs_to(identity) {
112 return Err(Texture3dError::RendererMismatch);
113 }
114 if !region.fits(source.size().0, source.size().1) {
115 return Err(Texture3dError::InvalidRegion);
116 }
117 let bytes = (region.width() as usize)
118 .checked_mul(region.height() as usize)
119 .and_then(|count| count.checked_mul(4))
120 .ok_or(Texture3dError::Image(ImageError::DimensionsTooLarge))?;
121 mips::validate_layout(
123 region.width(),
124 region.height(),
125 bytes,
126 budget,
127 device.limits().max_texture_dimension_2d,
128 options.mipmaps(),
129 )
130 .map_err(Texture3dError::Image)?;
131 if !options.preserves_alpha() {
132 for y in 0..region.height() as usize {
133 for x in 0..region.width() as usize {
134 let source_texel =
135 (region.y() as usize + y) * source.size().0 as usize + region.x() as usize + x;
136 if source.pixels()[source_texel * 4 + 3] != 255 {
137 return Err(Texture3dError::NonOpaquePixel {
138 texel: y * region.width() as usize + x,
139 });
140 }
141 }
142 }
143 }
144 let mut pixels = Vec::new();
145 pixels.try_reserve_exact(bytes).map_err(|_| {
146 Texture3dError::Image(ImageError::AllocationFailed {
147 requested_bytes: bytes,
148 })
149 })?;
150 if pixels.capacity() > budget.max_bytes() {
151 return Err(Texture3dError::Image(ImageError::BudgetExceeded {
152 limit: budget.max_bytes(),
153 actual: pixels.capacity(),
154 }));
155 }
156 let row_bytes = region.width() as usize * 4;
157 for y in 0..region.height() as usize {
158 let start =
159 ((region.y() as usize + y) * source.size().0 as usize + region.x() as usize) * 4;
160 pixels.extend_from_slice(&source.pixels()[start..start + row_bytes]);
161 }
162 create_texture_with_options(
163 device,
164 queue,
165 identity,
166 layout,
167 region.width(),
168 region.height(),
169 pixels,
170 budget,
171 options,
172 )
173}