@@ -344,11 +344,7 @@ impl Gpu {
344344 log:: info!( "gpu: {} has too few storage buffers per stage" , info. name) ;
345345 return Err ( format ! ( "{} has too few storage buffers per shader stage ({} < 10)" , info. name, limits. max_storage_buffers_per_shader_stage) ) ;
346346 }
347- if let Some ( ( binding, buffer) ) = limits_override ( ) {
348- limits. max_storage_buffer_binding_size = limits. max_storage_buffer_binding_size . min ( binding) ;
349- limits. max_buffer_size = limits. max_buffer_size . min ( buffer) ;
350- log:: info!( "gpu: LIGHTCRAFT_GPU_LIMITS: storage bindings ≤ {} MiB, buffers ≤ {} MiB" , binding >> 20 , buffer >> 20 ) ;
351- }
347+ limits = capped ( limits, limits_override ( ) ) ;
352348 let ( device, queue) = pollster:: block_on ( adapter. request_device ( & wgpu:: DeviceDescriptor {
353349 label : Some ( "lightcraft" ) ,
354350 required_limits : limits. clone ( ) ,
@@ -358,6 +354,18 @@ impl Gpu {
358354 Gpu :: with_device ( device, queue, info, limits, false )
359355 }
360356
357+ /// The renderer on the window's device ([`crate::adopt_window_device`]), within its limits
358+ /// lowered by `LIGHTCRAFT_GPU_LIMITS` as a device of its own would be.
359+ pub fn adopt ( device : wgpu:: Device , queue : wgpu:: Queue , info : wgpu:: AdapterInfo ) -> Result < Gpu , String > {
360+ Gpu :: adopt_capped ( device, queue, info, limits_override ( ) )
361+ }
362+
363+ /// [`Gpu::adopt`] with the limits override given (`LIGHTCRAFT_GPU_LIMITS`, parsed).
364+ fn adopt_capped ( device : wgpu:: Device , queue : wgpu:: Queue , info : wgpu:: AdapterInfo , cap : Option < ( u64 , u64 ) > ) -> Result < Gpu , String > {
365+ let limits = capped ( device. limits ( ) , cap) ;
366+ Gpu :: with_device ( device, queue, info, limits, true )
367+ }
368+
361369 /// The renderer on an existing device: the window's (`window`, see [`crate::adopt_window_device`])
362370 /// or one [`Gpu::new`] created with `limits`. Builds the kernels (~0.4–2 s: not on the UI thread).
363371 pub fn with_device ( device : wgpu:: Device , queue : wgpu:: Queue , info : wgpu:: AdapterInfo , limits : wgpu:: Limits , window : bool ) -> Result < Gpu , String > {
@@ -645,6 +653,81 @@ fn limits_override() -> Option<(u64, u64)> {
645653 parse_limits ( & std:: env:: var ( "LIGHTCRAFT_GPU_LIMITS" ) . ok ( ) ?)
646654}
647655
656+ /// `limits` lowered to `cap` ([`limits_override`]: storage binding bytes, buffer bytes).
657+ fn capped ( mut limits : wgpu:: Limits , cap : Option < ( u64 , u64 ) > ) -> wgpu:: Limits {
658+ if let Some ( ( binding, buffer) ) = cap {
659+ limits. max_storage_buffer_binding_size = limits. max_storage_buffer_binding_size . min ( binding) ;
660+ limits. max_buffer_size = limits. max_buffer_size . min ( buffer) ;
661+ log:: info!( "gpu: LIGHTCRAFT_GPU_LIMITS: storage bindings ≤ {} MiB, buffers ≤ {} MiB" , binding >> 20 , buffer >> 20 ) ;
662+ }
663+ limits
664+ }
665+
666+ /// See [`crate::read_texture`].
667+ pub ( crate ) fn read_texture (
668+ device : & wgpu:: Device ,
669+ queue : & wgpu:: Queue ,
670+ texture : & wgpu:: Texture ,
671+ width : usize ,
672+ height : usize ,
673+ ) -> Result < lightcraft_raster:: Rgba8 , String > {
674+ if width == 0 || height == 0 {
675+ return Err ( "an empty texture" . into ( ) ) ;
676+ }
677+ let ( Ok ( w32) , Ok ( h32) ) = ( u32:: try_from ( width) , u32:: try_from ( height) ) else { return Err ( "texture too large" . into ( ) ) } ;
678+ if w32 > texture. width ( ) || h32 > texture. height ( ) || texture. format ( ) != wgpu:: TextureFormat :: Rgba8Unorm {
679+ return Err ( "not an 8-bit view texture of that size" . into ( ) ) ;
680+ }
681+ // texture copies take rows of a multiple of 256 bytes
682+ let stride = width. checked_mul ( 4 ) . ok_or ( "texture too large" ) ?. next_multiple_of ( 256 ) ;
683+ let size = stride. checked_mul ( height) . ok_or ( "texture too large" ) ?;
684+ let ( Ok ( stride32) , Ok ( size64) ) = ( u32:: try_from ( stride) , u64:: try_from ( size) ) else { return Err ( "texture too large" . into ( ) ) } ;
685+ let scope = device. push_error_scope ( wgpu:: ErrorFilter :: Validation ) ;
686+ let staging = device. create_buffer ( & wgpu:: BufferDescriptor {
687+ label : Some ( "view readback" ) ,
688+ size : size64,
689+ usage : wgpu:: BufferUsages :: MAP_READ | wgpu:: BufferUsages :: COPY_DST ,
690+ mapped_at_creation : false ,
691+ } ) ;
692+ let mut enc = device. create_command_encoder ( & wgpu:: CommandEncoderDescriptor { label : Some ( "view readback" ) } ) ;
693+ enc. copy_texture_to_buffer (
694+ wgpu:: TexelCopyTextureInfo { texture, mip_level : 0 , origin : wgpu:: Origin3d :: ZERO , aspect : wgpu:: TextureAspect :: All } ,
695+ wgpu:: TexelCopyBufferInfo {
696+ buffer : & staging,
697+ layout : wgpu:: TexelCopyBufferLayout { offset : 0 , bytes_per_row : Some ( stride32) , rows_per_image : Some ( h32) } ,
698+ } ,
699+ wgpu:: Extent3d { width : w32, height : h32, depth_or_array_layers : 1 } ,
700+ ) ;
701+ queue. submit ( [ enc. finish ( ) ] ) ;
702+ if let Some ( e) = pollster:: block_on ( scope. pop ( ) ) {
703+ return Err ( format ! ( "texture copy failed: {e}" ) ) ;
704+ }
705+ let slice = staging. slice ( ..) ;
706+ let ( tx, rx) = std:: sync:: mpsc:: channel ( ) ;
707+ slice. map_async ( wgpu:: MapMode :: Read , move |r| {
708+ let _ = tx. send ( r) ;
709+ } ) ;
710+ let wait = std:: time:: Duration :: from_secs ( 10 ) ;
711+ device. poll ( wgpu:: PollType :: Wait { submission_index : None , timeout : Some ( wait) } ) . map_err ( |e| format ! ( "{e}" ) ) ?;
712+ match rx. recv_timeout ( wait) {
713+ Ok ( Ok ( ( ) ) ) => { }
714+ Ok ( Err ( e) ) => return Err ( format ! ( "{e}" ) ) ,
715+ Err ( e) => return Err ( format ! ( "map callback: {e}" ) ) ,
716+ }
717+ let bytes = slice. get_mapped_range ( ) . map_err ( |e| format ! ( "{e:?}" ) ) ?;
718+ let mut data = Vec :: with_capacity ( width * height) ;
719+ for row in bytes. chunks ( stride) . take ( height) {
720+ let row = row. get ( ..width * 4 ) . ok_or ( "short readback row" ) ?;
721+ data. extend ( row. as_chunks :: < 4 > ( ) . 0 . iter ( ) . copied ( ) ) ;
722+ }
723+ drop ( bytes) ;
724+ staging. unmap ( ) ;
725+ if data. len ( ) != width * height {
726+ return Err ( "incomplete readback" . into ( ) ) ;
727+ }
728+ Ok ( lightcraft_raster:: Rgba8 { width, height, data } )
729+ }
730+
648731fn parse_limits ( v : & str ) -> Option < ( u64 , u64 ) > {
649732 let v = v. trim ( ) . to_ascii_lowercase ( ) ;
650733 match v. as_str ( ) {
@@ -788,6 +871,54 @@ mod tests {
788871 assert_eq ! ( super :: parse_limits( "lots" ) , None ) ;
789872 }
790873
874+ /// `LIGHTCRAFT_GPU_LIMITS` caps the window's device as it caps a device of its own (review of
875+ /// PR #1065: adopting the window's device skipped it, so a 1024 × 1024 RGB float buffer — 12 MiB —
876+ /// was taken under an 8 MiB cap).
877+ #[ test]
878+ fn the_limits_override_caps_an_adopted_device ( ) {
879+ let big = wgpu:: Limits { max_storage_buffer_binding_size : 1 << 30 , max_buffer_size : 1 << 31 , ..wgpu:: Limits :: default ( ) } ;
880+ let c = super :: capped ( big. clone ( ) , super :: parse_limits ( "8" ) ) ;
881+ assert_eq ! ( ( c. max_storage_buffer_binding_size, c. max_buffer_size) , ( 8 << 20 , 16 << 20 ) ) ;
882+ let c = super :: capped ( big. clone ( ) , None ) ;
883+ assert_eq ! ( ( c. max_storage_buffer_binding_size, c. max_buffer_size) , ( 1 << 30 , 1 << 31 ) ) ;
884+
885+ let _lock = crate :: DEVICE_TEST_LOCK . lock ( ) . unwrap_or_else ( std:: sync:: PoisonError :: into_inner) ;
886+ let Some ( ( device, queue, info) ) = test_device ( ) else {
887+ lightcraft_testkit:: skip!( Gpu , "no GPU adapter" ) ;
888+ return ;
889+ } ;
890+ let full = device. limits ( ) . max_storage_buffer_binding_size . min ( device. limits ( ) . max_buffer_size ) ;
891+ if full <= 12 << 20 {
892+ lightcraft_testkit:: skip!( Gpu , "the adapter's buffers are already ≤ 12 MiB" ) ;
893+ return ;
894+ }
895+ let g = super :: Gpu :: adopt_capped ( device, queue, info, super :: parse_limits ( "8" ) ) . expect ( "kernels build" ) ;
896+ assert ! ( g. window) ;
897+ assert_eq ! ( g. limit( ) , 8 << 20 , "the cap applies to the adopted device" ) ;
898+ assert ! ( 1024 * 1024 * 3 * 4 > g. limit( ) , "a 12 MiB buffer is over it" ) ;
899+ }
900+
901+ /// A device as eframe makes the window's (the adapter's own limits).
902+ fn test_device ( ) -> Option < ( wgpu:: Device , wgpu:: Queue , wgpu:: AdapterInfo ) > {
903+ let backends = crate :: backend:: compute_backends ( ) ?;
904+ let mut desc = wgpu:: InstanceDescriptor :: new_without_display_handle ( ) ;
905+ desc. backends = backends;
906+ desc. backend_options = crate :: backend:: backend_options ( ) ;
907+ let instance = wgpu:: Instance :: new ( desc) ;
908+ let adapter = pollster:: block_on ( instance. request_adapter ( & wgpu:: RequestAdapterOptions :: default ( ) ) ) . ok ( ) ?;
909+ let info = adapter. get_info ( ) ;
910+ if info. device_type == wgpu:: DeviceType :: Cpu {
911+ return None ;
912+ }
913+ let ( device, queue) = pollster:: block_on ( adapter. request_device ( & wgpu:: DeviceDescriptor {
914+ label : Some ( "test window" ) ,
915+ required_limits : adapter. limits ( ) ,
916+ ..Default :: default ( )
917+ } ) )
918+ . ok ( ) ?;
919+ Some ( ( device, queue, info) )
920+ }
921+
791922 #[ test]
792923 fn dispatches_stay_within_the_workgroup_limit ( ) {
793924 // 1-D kernels over a 100 MP image and 2-D ones over 16k × 16k stay under 65535 per axis
0 commit comments