### Create DDS textures with DXGI formats in Rust Source: https://context7.com/siegeengine/ddsfile/llms.txt Uses the Dds::new_dxgi constructor to initialize textures with modern DXGI specifications. Supports various configurations including BC7 compression, texture arrays, and cubemap arrays. ```rust use ddsfile::{Dds, NewDxgiParams, DxgiFormat, D3D10ResourceDimension, AlphaMode, Caps2}; fn main() -> Result<(), ddsfile::Error> { // Create a BC7 compressed texture (modern format) let bc7_params = NewDxgiParams { height: 1024, width: 1024, depth: None, format: DxgiFormat::BC7_UNorm_sRGB, mipmap_levels: Some(11), array_layers: None, caps2: None, is_cubemap: false, resource_dimension: D3D10ResourceDimension::Texture2D, alpha_mode: AlphaMode::Straight, }; let bc7_dds = Dds::new_dxgi(bc7_params)?; println!("Created BC7 sRGB texture: {:?}", bc7_dds.get_dxgi_format()); // Create a texture array let array_params = NewDxgiParams { height: 256, width: 256, depth: None, format: DxgiFormat::R8G8B8A8_UNorm, mipmap_levels: Some(1), array_layers: Some(4), // 4 textures in array caps2: None, is_cubemap: false, resource_dimension: D3D10ResourceDimension::Texture2D, alpha_mode: AlphaMode::Opaque, }; let array_dds = Dds::new_dxgi(array_params)?; println!("Created texture array with {} layers", array_dds.get_num_array_layers()); // Create a cubemap array with DXGI format let cubemap_array_params = NewDxgiParams { height: 512, width: 512, depth: None, format: DxgiFormat::R16G16B16A16_Float, mipmap_levels: Some(10), array_layers: Some(12), // 2 cubemaps (6 faces each) caps2: Some(Caps2::CUBEMAP | Caps2::CUBEMAP_ALLFACES), is_cubemap: true, resource_dimension: D3D10ResourceDimension::Texture2D, alpha_mode: AlphaMode::Unknown, }; let cubemap_array_dds = Dds::new_dxgi(cubemap_array_params)?; println!("Created HDR cubemap array"); Ok(()) } ``` -------------------------------- ### Access DxgiFormat metadata and extension requirements in Rust Source: https://context7.com/siegeengine/ddsfile/llms.txt Illustrates the use of the DxgiFormat enum for modern DirectX formats, including sRGB variants, HDR, BC6H/BC7 compression, and typeless formats. It highlights how to check for extension requirements and retrieve format-specific properties. ```rust use ddsfile::{DxgiFormat, DataFormat}; fn main() { // Standard formats let rgba8 = DxgiFormat::R8G8B8A8_UNorm; println!("R8G8B8A8_UNorm: {} bpp", rgba8.get_bits_per_pixel().unwrap()); println!(" Requires extension: {}", rgba8.requires_extension()); // sRGB variants let rgba8_srgb = DxgiFormat::R8G8B8A8_UNorm_sRGB; println!("\nR8G8B8A8_UNorm_sRGB:"); println!(" Requires extension: {}", rgba8_srgb.requires_extension()); // HDR formats let hdr_format = DxgiFormat::R16G16B16A16_Float; println!("\nR16G16B16A16_Float: {} bpp", hdr_format.get_bits_per_pixel().unwrap()); // BC7 high-quality compression let bc7 = DxgiFormat::BC7_UNorm; println!("\nBC7_UNorm:"); println!(" Block size: {} bytes", bc7.get_block_size().unwrap()); println!(" Pitch for 1024px: {} bytes", bc7.get_pitch(1024).unwrap()); // BC6H HDR compression let bc6h = DxgiFormat::BC6H_UF16; println!("\nBC6H_UF16 (HDR):"); println!(" Block size: {} bytes", bc6h.get_block_size().unwrap()); // Depth formats let depth = DxgiFormat::D24_UNorm_S8_UInt; println!("\nD24_UNorm_S8_UInt: {} bpp", depth.get_bits_per_pixel().unwrap()); // Typeless formats (for resource views) let typeless = DxgiFormat::R32G32B32A32_Typeless; println!("\nR32G32B32A32_Typeless: {} bpp", typeless.get_bits_per_pixel().unwrap()); println!(" Requires extension: {}", typeless.requires_extension()); } ``` -------------------------------- ### Inspect DDS files with ddsinfo Source: https://context7.com/siegeengine/ddsfile/llms.txt Instructions for building and executing the ddsinfo command-line tool to extract and display header information and format details from a DDS file. ```bash # Build and run the ddsinfo tool cargo build --bin ddsinfo # Display information about a DDS file ./target/debug/ddsinfo texture.dds ``` -------------------------------- ### Inspecting DDS Header and Capabilities in Rust Source: https://context7.com/siegeengine/ddsfile/llms.txt Shows how to read a DDS file and extract metadata from the primary header and the optional DX10 extension header. It includes checking surface capabilities like mipmaps, cubemaps, and volume textures. ```rust use ddsfile::{Dds, Caps, Caps2, HeaderFlags}; use std::fs::File; fn main() -> Result<(), ddsfile::Error> { let file = File::open("texture.dds")?; let dds = Dds::read(file)?; let header = &dds.header; println!("Header dimensions: {}x{}", header.width, header.height); if header.caps.contains(Caps::MIPMAP) { println!("Has mipmaps"); } if let Some(ref header10) = dds.header10 { println!("DXGI Format: {:?}", header10.dxgi_format); } Ok(()) } ``` -------------------------------- ### Retrieve D3DFormat metadata and calculate texture size in Rust Source: https://context7.com/siegeengine/ddsfile/llms.txt Demonstrates how to use the D3DFormat enum to access bit masks, bits-per-pixel, and block sizes for legacy formats. It also shows how to calculate the total memory size for a DXT5 compressed texture. ```rust use ddsfile::{D3DFormat, DataFormat}; fn main() { // Common uncompressed formats let rgba32 = D3DFormat::A8R8G8B8; println!("A8R8G8B8: {} bpp", rgba32.get_bits_per_pixel().unwrap()); println!(" R mask: 0x{:08x}", rgba32.r_bit_mask().unwrap()); println!(" G mask: 0x{:08x}", rgba32.g_bit_mask().unwrap()); println!(" B mask: 0x{:08x}", rgba32.b_bit_mask().unwrap()); println!(" A mask: 0x{:08x}", rgba32.a_bit_mask().unwrap()); // DXT compressed formats let dxt1 = D3DFormat::DXT1; println!("\nDXT1 (BC1):"); println!(" Block size: {} bytes", dxt1.get_block_size().unwrap()); println!(" Pitch for 256px width: {} bytes", dxt1.get_pitch(256).unwrap()); println!(" Pitch height: {} pixels", dxt1.get_pitch_height()); let dxt5 = D3DFormat::DXT5; println!("\nDXT5 (BC3):"); println!(" Block size: {} bytes", dxt5.get_block_size().unwrap()); // Float formats let float_format = D3DFormat::A16B16G16R16F; println!("\nA16B16G16R16F: {} bpp", float_format.get_bits_per_pixel().unwrap()); // Luminance formats let lum = D3DFormat::L8; println!("\nL8 (grayscale): {} bpp", lum.get_bits_per_pixel().unwrap()); // Calculate texture data size let width = 512u32; let height = 512u32; let format = D3DFormat::DXT5; let pitch = format.get_pitch(width).unwrap(); let pitch_height = format.get_pitch_height(); let rows = (height + pitch_height - 1) / pitch_height; let size = pitch * rows; println!("\n512x512 DXT5 texture size: {} bytes", size); } ``` -------------------------------- ### Accessing and Modifying Texture Data in Rust Source: https://context7.com/siegeengine/ddsfile/llms.txt Demonstrates how to calculate texture offsets using array strides and manipulate raw byte data for specific array layers. It covers creating a new DDS structure, writing color data to layers, and verifying the contents. ```rust use ddsfile::{Dds, NewDxgiParams, DxgiFormat, D3D10ResourceDimension, AlphaMode}; fn main() -> Result<(), ddsfile::Error> { let params = NewDxgiParams { height: 128, width: 128, depth: None, format: DxgiFormat::R8G8B8A8_UNorm, mipmap_levels: Some(1), array_layers: Some(3), caps2: None, is_cubemap: false, resource_dimension: D3D10ResourceDimension::Texture2D, alpha_mode: AlphaMode::Opaque, }; let mut dds = Dds::new_dxgi(params)?; if let Some(main_size) = dds.get_main_texture_size() { println!("Main texture size: {} bytes", main_size); } let array_stride = dds.get_array_stride()?; println!("Array stride: {} bytes", array_stride); let colors: [(u8, u8, u8, u8); 3] = [ (255, 0, 0, 255), (0, 255, 0, 255), (0, 0, 255, 255), ]; for layer in 0..3 { let data = dds.get_mut_data(layer)?; let (r, g, b, a) = colors[layer as usize]; for pixel in data.chunks_mut(4) { pixel[0] = r; pixel[1] = g; pixel[2] = b; pixel[3] = a; } } Ok(()) } ``` -------------------------------- ### Handle DDS file errors in Rust Source: https://context7.com/siegeengine/ddsfile/llms.txt Demonstrates how to use the Error enum to handle I/O, format validation, and out-of-bounds access errors when reading DDS files. It shows pattern matching against specific error variants to provide granular feedback. ```rust use ddsfile::{Dds, Error, NewD3dParams, D3DFormat}; use std::fs::File; fn read_dds_file(path: &str) -> Result { let file = File::open(path)?; let dds = Dds::read(file)?; Ok(dds) } fn main() { match read_dds_file("nonexistent.dds") { Ok(dds) => println!("Loaded {}x{} texture", dds.get_width(), dds.get_height()), Err(Error::Io(e)) => println!("I/O error: {}", e), Err(Error::BadMagicNumber) => println!("Not a valid DDS file"), Err(Error::InvalidField(field)) => println!("Invalid header field: {}", field), Err(Error::ShortFile) => println!("File is truncated"), Err(Error::UnsupportedFormat) => println!("Format not supported"), Err(Error::OutOfBounds) => println!("Array layer out of bounds"), Err(e) => println!("Other error: {}", e), } } ``` -------------------------------- ### Query texture format metadata in Rust Source: https://context7.com/siegeengine/ddsfile/llms.txt Retrieves format information from a loaded DDS file using D3DFormat and DxgiFormat. Provides access to properties like bits per pixel, pitch, block size, and bit masks. ```rust use ddsfile::{Dds, D3DFormat, DxgiFormat, DataFormat}; use std::fs::File; fn main() -> Result<(), ddsfile::Error> { let file = File::open("texture.dds")?; let dds = Dds::read(file)?; // Try to get format as D3DFormat if let Some(d3d_format) = dds.get_d3d_format() { println!("D3D Format: {:?}", d3d_format); // Get format-specific information if let Some(bpp) = d3d_format.get_bits_per_pixel() { println!(" Bits per pixel: {}", bpp); } if let Some(block_size) = d3d_format.get_block_size() { println!(" Block size: {} bytes (compressed)", block_size); } if let Some(pitch) = d3d_format.get_pitch(dds.get_width()) { println!(" Row pitch: {} bytes", pitch); } // Get RGBA bit masks for uncompressed formats if let Some(r_mask) = d3d_format.r_bit_mask() { println!(" R mask: 0x{:08x}", r_mask); } } // Try to get format as DxgiFormat if let Some(dxgi_format) = dds.get_dxgi_format() { println!("DXGI Format: {:?}", dxgi_format); println!(" Requires DX10 extension: {}", dxgi_format.requires_extension()); } // Use type-erased format for generic operations if let Some(format) = dds.get_format() { println!("Pitch height: {}", format.get_pitch_height()); if let Some(min_mip) = format.get_minimum_mipmap_size_in_bytes() { println!("Minimum mipmap size: {} bytes", min_mip); } } Ok(()) } ``` -------------------------------- ### Create DDS Textures with D3DFormat using ddsfile in Rust Source: https://context7.com/siegeengine/ddsfile/llms.txt Constructs new DDS textures using legacy D3D format specifications. This function supports creating various types of textures, including compressed, volume, and cubemaps. ```rust use ddsfile::{Dds, NewD3dParams, D3DFormat, Caps2}; fn main() -> Result<(), ddsfile::Error> { // Create a standard 2D texture with DXT5 compression let compressed_params = NewD3dParams { height: 512, width: 512, depth: None, format: D3DFormat::DXT5, mipmap_levels: Some(9), // Full mipmap chain for 512x512 caps2: None, }; let compressed_dds = Dds::new_d3d(compressed_params)?; println!("Created DXT5 texture: {}x{}", compressed_dds.get_width(), compressed_dds.get_height()); // Create a volume texture (3D) let volume_params = NewD3dParams { height: 64, width: 64, depth: Some(64), format: D3DFormat::A8R8G8B8, mipmap_levels: Some(1), caps2: Some(Caps2::VOLUME), }; let volume_dds = Dds::new_d3d(volume_params)?; println!("Created volume texture: {}x{}x{}", volume_dds.get_width(), volume_dds.get_height(), volume_dds.get_depth()); // Create a cubemap let cubemap_params = NewD3dParams { height: 256, width: 256, depth: None, format: D3DFormat::A8R8G8B8, mipmap_levels: Some(1), caps2: Some(Caps2::CUBEMAP | Caps2::CUBEMAP_ALLFACES), }; let cubemap_dds = Dds::new_d3d(cubemap_params)?; println!("Created cubemap with {} faces", cubemap_dds.get_num_array_layers()); Ok(()) } ``` -------------------------------- ### Write DDS Files with ddsfile in Rust Source: https://context7.com/siegeengine/ddsfile/llms.txt Serializes a DDS structure to a writable destination, including headers and texture data. This function allows for creating new DDS files or modifying existing ones. ```rust use ddsfile::{Dds, NewD3dParams, D3DFormat}; use std::fs::File; use std::io::BufWriter; fn main() -> Result<(), ddsfile::Error> { // Create a new DDS with D3D format let params = NewD3dParams { height: 256, width: 256, depth: None, format: D3DFormat::A8R8G8B8, mipmap_levels: Some(1), caps2: None, }; let mut dds = Dds::new_d3d(params)?; // Get mutable access to texture data and fill it let data = dds.get_mut_data(0)?; for (i, byte) in data.iter_mut().enumerate() { *byte = (i % 256) as u8; // Fill with pattern } // Write to file let file = File::create("output.dds")?; let mut writer = BufWriter::new(file); dds.write(&mut writer)?; println!("DDS file written successfully"); Ok(()) } ``` -------------------------------- ### Read DDS Files with ddsfile in Rust Source: https://context7.com/siegeengine/ddsfile/llms.txt Parses a DDS file from a readable source, validates headers, and provides access to texture metadata and raw data. It supports various texture properties like dimensions, mipmaps, array layers, and format types. ```rust use ddsfile::Dds; use std::fs::File; use std::io::BufReader; fn main() -> Result<(), ddsfile::Error> { // Open and read a DDS file let file = File::open("texture.dds")?; let mut reader = BufReader::new(file); let dds = Dds::read(&mut reader)?; // Access texture properties println!("Width: {}", dds.get_width()); println!("Height: {}", dds.get_height()); println!("Depth: {}", dds.get_depth()); println!("Mipmap levels: {}", dds.get_num_mipmap_levels()); println!("Array layers: {}", dds.get_num_array_layers()); // Check format type if let Some(format) = dds.get_dxgi_format() { println!("DXGI Format: {:?}", format); } else if let Some(format) = dds.get_d3d_format() { println!("D3D Format: {:?}", format); } // Access raw texture data for array layer 0 let texture_data = dds.get_data(0)?; println!("Texture data size: {} bytes", texture_data.len()); Ok(()) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.