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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! Internal GPU texture backing for [`crate::Texture`] parcels.
//!
//! Public callers acquire [`crate::Texture`] (a parcel wrapper) from
//! [`crate::RetainedPool::acquire_texture`] or [`crate::Context::acquire_transient_texture`].
use crate::backend::GpuBackend;
use crate::device::Device;
use crate::handles::TextureHandle;
use crate::types::{ResourceAccess, ResourceCategory, ResourceHandle, TextureFlags, TextureFormat, TextureKind};
use crate::vram_allocator::{ParcelDeed, ParcelType};
use anyhow::Result;
use std::sync::{Arc, Mutex};
/// Linear buffer layout for copying a 2D texture subresource into a buffer.
///
/// `logical_bytes` is the tight linear size clients observe (`width * height * bpp`).
/// `staging_bytes`, `row_pitch`, and `footprint_offset` describe how rows are laid out in
/// the destination buffer (DX12 may pad rows to alignment; Vulkan/Metal use tight rows).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextureCopyFootprint {
pub width: u32,
pub height: u32,
pub format: TextureFormat,
pub logical_bytes: u64,
pub staging_bytes: u64,
pub row_pitch: u32,
/// Byte offset of the subresource footprint within the staging buffer (DX12 placed copy).
pub footprint_offset: u64,
}
impl TextureCopyFootprint {
pub fn tight_row_bytes(&self) -> u32 {
self.width.saturating_mul(self.format.bytes_per_pixel())
}
}
/// Low-level GPU texture allocation (internal to Goldy).
#[derive(Clone)]
pub(crate) struct TextureBacking {
_device: Option<Device>,
backend: Arc<Mutex<Box<dyn GpuBackend>>>,
pub(crate) handle: TextureHandle,
width: u32,
height: u32,
format: TextureFormat,
access: TextureKind,
flags: TextureFlags,
owned: bool,
bindless_storage: Option<u32>,
bindless_sampled: Option<u32>,
/// Accounting deed for observer + allocator notification on drop.
deed: Option<ParcelDeed>,
}
impl TextureBacking {
/// Attach the accounting deed (called from [`Device::alloc_texture`] only).
pub(crate) fn set_deed(&mut self, deed: ParcelDeed) {
self.deed = Some(deed);
}
/// Create a new empty texture with the specified access pattern.
///
/// The texture is created with uninitialized data. Use `write()` to
/// upload image data after creation.
///
/// # Access Patterns
///
/// - `TextureKind::Interpolated`: Hardware filtering between neighbors (texture units).
/// Use for textures sampled with bilinear/trilinear filtering.
///
/// - `TextureKind::Direct`: Direct 2D indexing without filtering.
/// Use for storage images, compute output, or when you need exact pixel values.
///
/// # Arguments
///
/// * `device` - The GPU device to create the texture on
/// * `width` - Width in pixels
/// * `height` - Height in pixels
/// * `format` - Pixel format
/// * `access` - Spatial access pattern
/// * `flags` - Additional texture flags (copy operations, render target)
///
/// # Errors
///
/// Returns an error if GPU resource allocation fails.
pub(crate) fn new(
device: &Device,
width: u32,
height: u32,
format: TextureFormat,
access: TextureKind,
flags: TextureFlags,
) -> Result<Self> {
tracing::debug!(width, height, ?format, ?access, ?flags, "Creating texture");
let (handle, bindless_storage, bindless_sampled) = {
let mut backend = device.inner.backend.lock().unwrap();
let handle = backend.create_texture(device.inner.handle, width, height, format, access, flags)?;
let bindless_storage = backend.texture_bindless_index(handle);
let bindless_sampled = backend.texture_bindless_sampled_index(handle);
(handle, bindless_storage, bindless_sampled)
};
Ok(Self {
_device: Some(device.clone()),
backend: Arc::clone(&device.inner.backend),
handle,
width,
height,
format,
access,
flags,
owned: true,
bindless_storage,
bindless_sampled,
deed: None,
})
}
/// 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.
///
/// See `Device::alloc_texture` for access pattern documentation.
///
/// # 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
/// * `access` - Spatial access pattern
/// * `flags` - Additional texture flags
///
/// # Errors
///
/// Returns an error if:
/// - GPU resource allocation fails
/// - Data size doesn't match expected size
pub(crate) fn with_data(
device: &Device,
data: &[u8],
width: u32,
height: u32,
format: TextureFormat,
access: TextureKind,
flags: TextureFlags,
) -> 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, access, flags)?;
#[allow(deprecated)]
texture.write(data)?;
Ok(texture)
}
/// Write pixel data to a subregion of the texture.
///
/// The data must match the specified width and height for the texture's format.
/// The region must fit within the texture bounds.
///
/// # Arguments
///
/// * `x` - Left offset in pixels
/// * `y` - Top offset in pixels
/// * `width` - Width of the region in pixels
/// * `height` - Height of the region in pixels
/// * `data` - Raw pixel data (must match width * height * bytes_per_pixel)
///
/// # Errors
///
/// Returns an error if:
/// - Region is out of bounds
/// - Data size doesn't match expected size
/// - GPU upload fails
#[deprecated(
since = "0.1.0",
note = "Use MemoryExchange::bind_deposit_texture() for batched, non-blocking uploads. \
This method submits synchronously and stalls the GPU."
)]
pub fn write_region(&self, x: u32, y: u32, width: u32, height: u32, data: &[u8]) -> Result<()> {
if x + width > self.width || y + height > self.height {
anyhow::bail!(
"Region out of bounds: {}x{} at ({},{}) exceeds {}x{} texture",
width,
height,
x,
y,
self.width,
self.height
);
}
let expected_size = (width * height * self.format.bytes_per_pixel()) as usize;
if data.len() != expected_size {
anyhow::bail!(
"Data size mismatch: expected {} bytes for {}x{} region, got {}",
expected_size,
width,
height,
data.len()
);
}
let mut backend = self.backend.lock().unwrap();
backend.write_texture_region(self.handle, x, y, width, height, data)
}
/// 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
#[deprecated(
since = "0.1.0",
note = "Use MemoryExchange::bind_deposit_texture() for batched, non-blocking uploads. \
This method submits synchronously and stalls the GPU."
)]
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 {
let bytes = u64::from(self.width) * u64::from(self.height) * u64::from(self.format.bytes_per_pixel());
usize::try_from(bytes).unwrap_or(usize::MAX)
}
/// Get the backend handle for this texture.
pub fn gpu_handle(&self) -> TextureHandle {
self.handle
}
/// Resource descriptor index for how this texture will be accessed in the current dispatch.
///
/// Crate-internal: the public binding path is [`Self::handle`] / scheme `with_parcel`.
pub(crate) fn resource_index(&self, access: ResourceAccess) -> Option<u32> {
match (self.access, access) {
(TextureKind::Interpolated, ResourceAccess::Read) => self.bindless_storage,
(TextureKind::Interpolated, ResourceAccess::Write | ResourceAccess::ReadWrite) => None,
(TextureKind::Direct, ResourceAccess::Read) => None,
(TextureKind::Direct, ResourceAccess::Write | ResourceAccess::ReadWrite) => self.bindless_storage,
(TextureKind::DirectInterpolated, ResourceAccess::Read) => self.bindless_sampled,
(TextureKind::DirectInterpolated, ResourceAccess::Write | ResourceAccess::ReadWrite) => {
self.bindless_storage
}
}
}
/// Opaque typed resource descriptor identity for validation and retention checks.
pub fn handle(&self, access: ResourceAccess) -> Option<ResourceHandle> {
self.resource_index(access).map(|i| {
let category = match (self.access, access) {
(TextureKind::DirectInterpolated, ResourceAccess::Read) => ResourceCategory::Texture,
_ => ResourceCategory::from(self.access),
};
ResourceHandle::new(category, i)
})
}
/// Get the access pattern this texture was created with.
pub fn access(&self) -> TextureKind {
self.access
}
/// Creation flags ([`TextureFlags`]) used when this texture was allocated.
///
/// Views from [`Self::borrow`] keep the parent's flags. Non-owning textures
/// that wrap externally owned GPU images (such as swapchain drawables)
/// report [`TextureFlags::empty()`].
pub fn flags(&self) -> TextureFlags {
self.flags
}
/// Whether dropping this texture destroys the GPU resource (`true`) or not (`false`).
///
/// Borrowed textures ([`Self::borrow`]) and other non-owning views of
/// externally managed resources return `false`.
pub fn is_owned(&self) -> bool {
self.owned
}
/// Attach a human-readable name to this texture for GPU debuggers and
/// validation layer messages.
///
/// On Vulkan the name is forwarded to `vkSetDebugUtilsObjectNameEXT`
/// (active only when validation layers are enabled). On other backends the
/// call is a no-op, so callers do not need to add any feature guards.
pub fn set_debug_name(&self, name: &str) {
self.backend.lock().unwrap().set_texture_debug_name(self.handle, name);
}
/// Create a non-owning view of this texture.
///
/// The returned `Texture` shares the same GPU resource and handle but does
/// **not** destroy the underlying resource when dropped. Use this when you
/// need to hand a reference into a system (e.g. a bind map) that may drop
/// it before the original owner is done — for example to avoid a
/// use-after-free when the bind map entry is evicted while the caller still
/// holds the original `Texture`.
pub fn borrow(&self) -> Self {
Self {
_device: self._device.clone(),
backend: Arc::clone(&self.backend),
handle: self.handle,
width: self.width,
height: self.height,
format: self.format,
access: self.access,
flags: self.flags,
owned: false,
bindless_storage: self.bindless_storage,
bindless_sampled: self.bindless_sampled,
deed: None,
}
}
/// Create a borrowed texture wrapping an externally-owned GPU resource.
///
/// The returned `Texture` provides the same read/query API but does **not**
/// destroy the underlying resource when dropped. Used for transient resources
/// like surface frame drawables whose lifetime is managed elsewhere.
///
/// Swapchain drawables on surfaces with compute-to-surface support are
/// writable, so we tag them as `TextureKind::Direct` (storage image).
pub(crate) fn borrowed(
backend: Arc<Mutex<Box<dyn GpuBackend>>>,
handle: TextureHandle,
width: u32,
height: u32,
format: TextureFormat,
) -> Self {
let (bindless_storage, bindless_sampled) = {
let backend = backend.lock().unwrap();
(
backend.texture_bindless_index(handle),
backend.texture_bindless_sampled_index(handle),
)
};
Self {
_device: None,
backend,
handle,
width,
height,
format,
access: TextureKind::Direct,
flags: TextureFlags::empty(),
owned: false,
bindless_storage,
bindless_sampled,
deed: None,
}
}
}
impl Drop for TextureBacking {
fn drop(&mut self) {
if !self.owned {
return;
}
tracing::trace!(
width = self.width,
height = self.height,
format = ?self.format,
"Destroying texture"
);
if let Ok(mut backend) = self.backend.lock() {
backend.destroy_texture(self.handle);
}
if let Some(deed) = self.deed.as_ref() {
let byte_size = self.byte_size() as u64;
deed.notify_freed(byte_size, byte_size, ParcelType::Texture);
}
}
}
#[cfg(test)]
#[allow(deprecated)]
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 = TextureBacking::new(
&device,
256,
256,
TextureFormat::Rgba8Unorm,
TextureKind::Interpolated,
TextureFlags::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 = TextureBacking::with_data(
&device,
&data,
2,
2,
TextureFormat::Rgba8Unorm,
TextureKind::Interpolated,
TextureFlags::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 = TextureBacking::with_data(
&device,
&data,
2,
2,
TextureFormat::Rgba8Unorm,
TextureKind::Interpolated,
TextureFlags::COPY_DST,
);
assert!(result.is_err());
}
#[test]
fn test_texture_write() {
let device = create_test_device();
let texture = TextureBacking::new(
&device,
2,
2,
TextureFormat::Rgba8Unorm,
TextureKind::Interpolated,
TextureFlags::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 = TextureBacking::new(
&device,
2,
2,
TextureFormat::Rgba8Unorm,
TextureKind::Interpolated,
TextureFlags::COPY_DST,
)
.unwrap();
let data = vec![0u8; 4]; // Too small
let result = texture.write(&data);
assert!(result.is_err());
}
#[test]
fn test_texture_r8_unorm() {
let device = create_test_device();
let data = vec![128u8; 64 * 64]; // 1 byte per pixel
let texture = TextureBacking::with_data(
&device,
&data,
64,
64,
TextureFormat::R8Unorm,
TextureKind::Interpolated,
TextureFlags::COPY_DST,
)
.unwrap();
assert_eq!(texture.width(), 64);
assert_eq!(texture.height(), 64);
assert_eq!(texture.format(), TextureFormat::R8Unorm);
assert_eq!(texture.byte_size(), 64 * 64);
}
#[test]
fn test_texture_rg8_unorm() {
let device = create_test_device();
let data = vec![0u8; 32 * 32 * 2]; // 2 bytes per pixel
let texture = TextureBacking::with_data(
&device,
&data,
32,
32,
TextureFormat::Rg8Unorm,
TextureKind::Interpolated,
TextureFlags::COPY_DST,
)
.unwrap();
assert_eq!(texture.width(), 32);
assert_eq!(texture.height(), 32);
assert_eq!(texture.format(), TextureFormat::Rg8Unorm);
assert_eq!(texture.byte_size(), 32 * 32 * 2);
}
#[test]
fn test_texture_r8_data_size_validation() {
let device = create_test_device();
let data = vec![0u8; 100]; // Wrong size for 64x64 R8
let result = TextureBacking::with_data(
&device,
&data,
64,
64,
TextureFormat::R8Unorm,
TextureKind::Interpolated,
TextureFlags::COPY_DST,
);
assert!(result.is_err());
}
}