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
//! GPU texture management.
//!
//! Textures are GPU images that can be sampled in shaders.
//! Unlike render targets, textures are typically created from CPU data
//! (images, procedural data) and then sampled during rendering.
use crate::backend::{GpuBackend, TextureHandle};
use crate::device::Device;
use crate::types::{TextureFormat, TextureUsage};
use anyhow::Result;
use std::sync::{Arc, Mutex};
/// A GPU texture that can be sampled in shaders.
///
/// Textures hold image data on the GPU and can be bound to shaders
/// for sampling operations (e.g., applying textures to 3D models).
pub struct Texture {
backend: Arc<Mutex<Box<dyn GpuBackend>>>,
pub(crate) handle: TextureHandle,
width: u32,
height: u32,
format: TextureFormat,
}
impl Texture {
/// Create a new empty texture.
///
/// The texture is created with uninitialized data. Use `write()` to
/// upload image data after creation.
///
/// # Arguments
///
/// * `device` - The GPU device to create the texture on
/// * `width` - Width in pixels
/// * `height` - Height in pixels
/// * `format` - Pixel format
/// * `usage` - How the texture will be used
///
/// # Errors
///
/// Returns an error if GPU resource allocation fails.
pub fn new(
device: &Device,
width: u32,
height: u32,
format: TextureFormat,
usage: TextureUsage,
) -> Result<Self> {
let handle = {
let mut backend = device.backend.lock().unwrap();
backend.create_texture(device.handle, width, height, format, usage)?
};
Ok(Self {
backend: Arc::clone(&device.backend),
handle,
width,
height,
format,
})
}
/// Create a texture initialized with data.
///
/// The data must be in the correct format for the texture's pixel format.
/// For RGBA8 textures, this is 4 bytes per pixel in RGBA order.
///
/// # Arguments
///
/// * `device` - The GPU device to create the texture on
/// * `data` - Raw pixel data (must match width * height * bytes_per_pixel)
/// * `width` - Width in pixels
/// * `height` - Height in pixels
/// * `format` - Pixel format
/// * `usage` - How the texture will be used
///
/// # Errors
///
/// Returns an error if:
/// - GPU resource allocation fails
/// - Data size doesn't match expected size
pub fn with_data(
device: &Device,
data: &[u8],
width: u32,
height: u32,
format: TextureFormat,
usage: TextureUsage,
) -> Result<Self> {
let expected_size = (width * height * format.bytes_per_pixel()) as usize;
if data.len() != expected_size {
anyhow::bail!(
"Data size mismatch: expected {} bytes, got {} bytes",
expected_size,
data.len()
);
}
let texture = Self::new(device, width, height, format, usage)?;
texture.write(data)?;
Ok(texture)
}
/// Write pixel data to the texture.
///
/// The data must match the texture's dimensions and format.
///
/// # Arguments
///
/// * `data` - Raw pixel data (must match width * height * bytes_per_pixel)
///
/// # Errors
///
/// Returns an error if:
/// - Data size doesn't match expected size
/// - GPU upload fails
pub fn write(&self, data: &[u8]) -> Result<()> {
let expected_size = (self.width * self.height * self.format.bytes_per_pixel()) as usize;
if data.len() != expected_size {
anyhow::bail!(
"Data size mismatch: expected {} bytes, got {} bytes",
expected_size,
data.len()
);
}
let mut backend = self.backend.lock().unwrap();
backend.write_texture(self.handle, data, self.width, self.height)
}
/// Get the width in pixels.
pub fn width(&self) -> u32 {
self.width
}
/// Get the height in pixels.
pub fn height(&self) -> u32 {
self.height
}
/// Get the texture format.
pub fn format(&self) -> TextureFormat {
self.format
}
/// Get the size of the texture data in bytes.
pub fn byte_size(&self) -> usize {
(self.width * self.height * self.format.bytes_per_pixel()) as usize
}
/// Get the backend handle for this texture.
///
/// This is used for binding the texture to shaders via bind groups.
pub fn handle(&self) -> TextureHandle {
self.handle
}
}
impl Drop for Texture {
fn drop(&mut self) {
if let Ok(mut backend) = self.backend.lock() {
backend.destroy_texture(self.handle);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::backend::mock::MockBackend;
fn create_test_device() -> Device {
Device::from_backend(Box::new(MockBackend::new())).unwrap()
}
#[test]
fn test_texture_creation() {
let device = create_test_device();
let texture = Texture::new(
&device,
256,
256,
TextureFormat::Rgba8Unorm,
TextureUsage::SAMPLED | TextureUsage::COPY_DST,
)
.unwrap();
assert_eq!(texture.width(), 256);
assert_eq!(texture.height(), 256);
assert_eq!(texture.format(), TextureFormat::Rgba8Unorm);
assert_eq!(texture.byte_size(), 256 * 256 * 4);
}
#[test]
fn test_texture_with_data() {
let device = create_test_device();
// Create a 2x2 RGBA texture
let data = vec![
255, 0, 0, 255, // Red
0, 255, 0, 255, // Green
0, 0, 255, 255, // Blue
255, 255, 255, 255, // White
];
let texture = Texture::with_data(
&device,
&data,
2,
2,
TextureFormat::Rgba8Unorm,
TextureUsage::SAMPLED | TextureUsage::COPY_DST,
)
.unwrap();
assert_eq!(texture.width(), 2);
assert_eq!(texture.height(), 2);
}
#[test]
fn test_texture_with_data_size_mismatch() {
let device = create_test_device();
// Data is too small for a 2x2 RGBA texture
let data = vec![255, 0, 0, 255]; // Only 1 pixel
let result = Texture::with_data(
&device,
&data,
2,
2,
TextureFormat::Rgba8Unorm,
TextureUsage::SAMPLED | TextureUsage::COPY_DST,
);
assert!(result.is_err());
}
#[test]
fn test_texture_write() {
let device = create_test_device();
let texture = Texture::new(
&device,
2,
2,
TextureFormat::Rgba8Unorm,
TextureUsage::SAMPLED | TextureUsage::COPY_DST,
)
.unwrap();
let data = vec![0u8; 2 * 2 * 4];
texture.write(&data).unwrap();
}
#[test]
fn test_texture_write_size_mismatch() {
let device = create_test_device();
let texture = Texture::new(
&device,
2,
2,
TextureFormat::Rgba8Unorm,
TextureUsage::SAMPLED | TextureUsage::COPY_DST,
)
.unwrap();
let data = vec![0u8; 4]; // Too small
let result = texture.write(&data);
assert!(result.is_err());
}
}