mirror of
https://github.com/UniXNU/NativeMetal.git
synced 2026-11-21 02:41:29 +03:00
chore(metal): merge pull request #37 from CrossDarwin/metal/texture-layout-policy
fix(metal): Preserve texture optimization policy across the bridge
This commit is contained in:
@@ -1330,7 +1330,7 @@ static uint64_t packedSwizzle(MTLTextureSwizzleChannels s) {
|
||||
static bool textureDescriptor(MTLTextureDescriptor *d,NmbTextureDescriptor *out) {
|
||||
if(!d)return false;
|
||||
*out={(uint64_t)d.textureType,(uint64_t)d.pixelFormat,d.width,d.height,d.depth,d.mipmapLevelCount,d.sampleCount,d.arrayLength,(uint64_t)d.usage,(uint64_t)d.resourceOptions,
|
||||
textureRotation(d),textureFlag(d,"framebufferOnly"),textureFlag(d,"isDrawable"),packedSwizzle(d.swizzle)};
|
||||
textureRotation(d),textureFlag(d,"framebufferOnly"),textureFlag(d,"isDrawable"),packedSwizzle(d.swizzle),(uint64_t)d.allowGPUOptimizedContents};
|
||||
if(singleSample() && out->samples>1) {
|
||||
out->samples=1;
|
||||
if(out->type==MTLTextureType2DMultisample)out->type=MTLTextureType2D;
|
||||
@@ -1379,6 +1379,7 @@ static bool textureDescriptor(MTLTextureDescriptor *d,NmbTextureDescriptor *out)
|
||||
- (MTLCPUCacheMode)cpuCacheMode {return (MTLCPUCacheMode)((_descriptor.options&MTLResourceCPUCacheModeMask)>>MTLResourceCPUCacheModeShift);}
|
||||
- (BOOL)isFramebufferOnly {return _descriptor.framebuffer_only!=0;}
|
||||
- (BOOL)isDrawable {return _descriptor.is_drawable!=0;}
|
||||
- (BOOL)allowGPUOptimizedContents {return _descriptor.allow_gpu_optimized_contents!=0;}
|
||||
- (IOSurfaceRef)iosurface {return _surface;}
|
||||
- (NSUInteger)iosurfacePlane {return 0;}
|
||||
- (id<MTLBuffer>)buffer {return _surface?nil:(id<MTLBuffer>)_buffer;}
|
||||
|
||||
@@ -10,6 +10,10 @@ The frontend uses metal2vulkan and Turnip with the kernel GPU interface and sele
|
||||
|
||||
All clients use single-sample rendering by default. `NATIVE_METAL_SINGLE_SAMPLE=0` preserves a process's requested sample counts; `1` forces single-sample rendering. Texture types, pipeline sample counts and resolve outputs follow the selected policy. Same-user clients share writable shader caches. Resource aliasing, swizzles, range copies, function constants, specialized link names and allocation queries reach the runtime. Queue priorities aggregate current live requests without a process-name policy.
|
||||
|
||||
Texture creation, heap size queries and texture views preserve
|
||||
`allowGPUOptimizedContents`. A caller that requests an unoptimized layout keeps
|
||||
that layout through the frontend and runtime.
|
||||
|
||||
Function lookup returns `MTLLibraryErrorFunctionNotFound` when the requested name is absent from the library.
|
||||
|
||||
Build and interface details: [documentation](https://unixnu.com/drivers/adreno-metal). Component sources and dependencies: [catalog](https://unixnu.com/reference/components).
|
||||
|
||||
@@ -153,6 +153,7 @@ struct NmbTextureDescriptor {
|
||||
uint64_t type, format, width, height, depth, levels, samples, array_length, usage, options;
|
||||
uint64_t rotation, framebuffer_only, is_drawable;
|
||||
uint64_t swizzle; /* RGBA bytes, bit 32 marks an explicit value; zero means identity */
|
||||
uint64_t allow_gpu_optimized_contents;
|
||||
};
|
||||
/* Heaps: type 0 automatic / 1 placement; offset UINT64_MAX asks an automatic heap to place the resource. */
|
||||
NmbHeap *nmb_heap_new(NmbDevice *, uint64_t type, uint64_t size, uint64_t options);
|
||||
|
||||
@@ -1438,6 +1438,7 @@ pub struct NmbTextureDescriptor {
|
||||
pub framebuffer_only: u64,
|
||||
pub is_drawable: u64,
|
||||
pub swizzle: u64,
|
||||
pub allow_gpu_optimized_contents: u64,
|
||||
}
|
||||
|
||||
// Bit 32 distinguishes an explicit all-zero swizzle from an omitted descriptor field.
|
||||
@@ -1467,10 +1468,32 @@ fn texture_descriptor(d: *const NmbTextureDescriptor) -> Result<TextureDescripto
|
||||
out.rotation = d.rotation;
|
||||
out.framebuffer_only = d.framebuffer_only != 0;
|
||||
out.is_drawable = d.is_drawable != 0;
|
||||
out.allow_gpu_optimized_contents = d.allow_gpu_optimized_contents != 0;
|
||||
if let Some(swizzle) = swizzle_of(d.swizzle)? { out.swizzle = swizzle; }
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod texture_descriptor_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn heap_layout_preserves_the_callers_optimization_policy() {
|
||||
let mut descriptor = NmbTextureDescriptor {
|
||||
kind: TextureType::Type2D.as_raw() as u64,
|
||||
format: PixelFormat::RGBA16Float.as_raw() as u64,
|
||||
width: 242, height: 242, depth: 1, levels: 1, samples: 1, array_length: 1,
|
||||
usage: TextureUsage::SHADER_READ.as_raw() as u64,
|
||||
options: ResourceOptions::private().raw(),
|
||||
rotation: 0, framebuffer_only: 0, is_drawable: 0, swizzle: 0,
|
||||
allow_gpu_optimized_contents: 0,
|
||||
};
|
||||
assert!(!texture_descriptor(&descriptor).unwrap().allow_gpu_optimized_contents);
|
||||
descriptor.allow_gpu_optimized_contents = 1;
|
||||
assert!(texture_descriptor(&descriptor).unwrap().allow_gpu_optimized_contents);
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn nmb_texture_new(device: *mut NmbDevice, descriptor: *const NmbTextureDescriptor) -> *mut NmbTexture {
|
||||
guard(std::ptr::null_mut(), || {
|
||||
@@ -1642,6 +1665,7 @@ pub extern "C" fn nmb_texture_describe(texture: *const NmbTexture, out: *mut Nmb
|
||||
framebuffer_only: u64::from(t.framebuffer_only()),
|
||||
is_drawable: u64::from(t.is_drawable()),
|
||||
swizzle: swizzle_packed(t.swizzle()),
|
||||
allow_gpu_optimized_contents: u64::from(t.allow_gpu_optimized_contents()),
|
||||
};
|
||||
Ok(())
|
||||
})
|
||||
|
||||
@@ -53,6 +53,20 @@ int main(int argc,char **argv) {@autoreleasepool {
|
||||
td.storageMode=MTLStorageModePrivate;td.usage=MTLTextureUsageShaderRead|MTLTextureUsagePixelFormatView;
|
||||
auto ht=[heap newTextureWithDescriptor:td];CHECK(ht);used=heap.usedSize;
|
||||
[ht makeAliasable];CHECK(ht.isAliasable && heap.usedSize<used);
|
||||
// Query and allocation must use the same layout policy, including texture views.
|
||||
for(bool optimized:{false,true}) {
|
||||
auto layout=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA16Float width:242 height:242 mipmapped:NO];
|
||||
layout.storageMode=MTLStorageModePrivate;layout.usage=MTLTextureUsageShaderRead;
|
||||
layout.allowGPUOptimizedContents=optimized;
|
||||
auto footprint=[dev heapTextureSizeAndAlignWithDescriptor:layout];CHECK(footprint.size && footprint.align);
|
||||
auto exact=[MTLHeapDescriptor new];exact.type=MTLHeapTypePlacement;exact.storageMode=layout.storageMode;exact.size=footprint.size;
|
||||
auto storage=[dev newHeapWithDescriptor:exact];CHECK(storage);
|
||||
auto placed=[storage newTextureWithDescriptor:layout offset:0];
|
||||
CHECK(placed && placed.heap==storage && placed.allowGPUOptimizedContents==optimized);
|
||||
CHECK(placed.allocatedSize<=storage.size);
|
||||
auto alias=[placed newTextureViewWithPixelFormat:placed.pixelFormat];CHECK(alias && alias.allowGPUOptimizedContents==optimized);
|
||||
auto overflow=[storage newTextureWithDescriptor:layout offset:footprint.align];CHECK(!overflow);
|
||||
}
|
||||
// Zero is a real swizzle, not a marker for the identity.
|
||||
td.swizzle=MTLTextureSwizzleChannelsMake(MTLTextureSwizzleZero,MTLTextureSwizzleZero,MTLTextureSwizzleZero,MTLTextureSwizzleZero);
|
||||
auto zero=[dev newTextureWithDescriptor:td];CHECK(zero && zero.swizzle.red==MTLTextureSwizzleZero && zero.swizzle.alpha==MTLTextureSwizzleZero);
|
||||
|
||||
Reference in New Issue
Block a user