Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions dm/gfx/tests/t_int_float_view.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,40 @@ int main(void) { @autoreleasepool {
failed += tbuf_view_format(MTLPixelFormatR32Sint, NVN_DPF_FLOAT) != MTLPixelFormatR32Float;
failed += tbuf_view_format(MTLPixelFormatRG16Uint, NVN_DPF_FLOAT) != MTLPixelFormatRG16Uint; // no float format of that size keeps its bits
failed += tbuf_view_format(MTLPixelFormatR32Uint, NVN_DPF_UINT) != MTLPixelFormatR32Uint;
id<MTLDevice> dev = MTLCreateSystemDefaultDevice(); id<MTLCommandQueue> q = [dev newCommandQueue]; NSError *e = nil;
id<MTLDevice> dev = MTLCreateSystemDefaultDevice();
if (!dev) { printf("no Metal device\n"); return 1; }
id<MTLCommandQueue> q = [dev newCommandQueue]; NSError *e = nil;
MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR32Uint width:8 height:1 mipmapped:NO];
d.usage = MTLTextureUsageShaderRead | MTLTextureUsagePixelFormatView; d.storageMode = MTLStorageModeShared;
id<MTLTexture> t = [dev newTextureWithDescriptor:d]; [t replaceRegion:MTLRegionMake2D(0, 0, 8, 1) mipmapLevel:0 withBytes:words bytesPerRow:32];
id<MTLTexture> t = [dev newTextureWithDescriptor:d];
if (!t) { printf("no texture\n"); return 1; }
[t replaceRegion:MTLRegionMake2D(0, 0, 8, 1) mipmapLevel:0 withBytes:words bytesPerRow:32];
id<MTLTexture> v = [t newTextureViewWithPixelFormat:MTLPixelFormatR32Float];
if (!v) { printf("no float view\n"); return 1; }
id<MTLLibrary> lib = [dev newLibraryWithSource:@"#include <metal_stdlib>\nusing namespace metal;\n"
"kernel void cs(texture2d<float> t [[texture(0)]], device uint *o [[buffer(0)]], uint id [[thread_position_in_grid]]) { o[id] = as_type<uint>(t.read(uint2(id, 0), 0).x); }\n"
"vertex float4 vs(uint id [[vertex_id]]) { return float4(id == 1 ? 3.0 : -1.0, id == 2 ? 3.0 : -1.0, 0.0, 1.0); }\n"
"fragment uint fs(float4 p [[position]], texture2d<float> t [[texture(0)]]) { return as_type<uint>(t.read(uint2(p.xy), 0).x); }\n" options:nil error:&e];
if (!lib) { printf("shader source: %s\n", e.localizedDescription.UTF8String); return 1; }
id<MTLComputePipelineState> cp = [dev newComputePipelineStateWithFunction:[lib newFunctionWithName:@"cs"] error:&e];
id<MTLFunction> fcs = [lib newFunctionWithName:@"cs"];
if (!fcs) { printf("no cs function\n"); return 1; }
id<MTLComputePipelineState> cp = [dev newComputePipelineStateWithFunction:fcs error:&e];
MTLRenderPipelineDescriptor *pd = [MTLRenderPipelineDescriptor new]; pd.vertexFunction = [lib newFunctionWithName:@"vs"]; pd.fragmentFunction = [lib newFunctionWithName:@"fs"]; pd.colorAttachments[0].pixelFormat = MTLPixelFormatR32Uint;
id<MTLRenderPipelineState> rp = [dev newRenderPipelineStateWithDescriptor:pd error:&e];
if (!cp || !rp) { printf("pipelines: %s\n", e.localizedDescription.UTF8String); return 1; }
id<MTLBuffer> co = [dev newBufferWithLength:32 options:MTLResourceStorageModeShared], fo = [dev newBufferWithLength:32 options:MTLResourceStorageModeShared];
MTLTextureDescriptor *rd = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR32Uint width:8 height:1 mipmapped:NO]; rd.usage = MTLTextureUsageRenderTarget; rd.storageMode = MTLStorageModePrivate;
id<MTLTexture> rt = [dev newTextureWithDescriptor:rd];
if (!co || !fo || !rt) { printf("no buffer or render target\n"); return 1; }
id<MTLCommandBuffer> cb = [q commandBuffer];
id<MTLComputeCommandEncoder> ce = [cb computeCommandEncoder]; [ce setComputePipelineState:cp]; [ce setTexture:v atIndex:0]; [ce setBuffer:co offset:0 atIndex:0];
[ce dispatchThreads:MTLSizeMake(8, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)]; [ce endEncoding];
MTLRenderPassDescriptor *rpd = [MTLRenderPassDescriptor renderPassDescriptor]; rpd.colorAttachments[0].texture = rt; rpd.colorAttachments[0].loadAction = MTLLoadActionClear; rpd.colorAttachments[0].storeAction = MTLStoreActionStore;
id<MTLRenderCommandEncoder> re = [cb renderCommandEncoderWithDescriptor:rpd]; [re setRenderPipelineState:rp]; [re setFragmentTexture:v atIndex:0]; [re drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3]; [re endEncoding];
id<MTLRenderCommandEncoder> re = [cb renderCommandEncoderWithDescriptor:rpd];
if (!re) { printf("no render encoder\n"); return 1; }
[re setRenderPipelineState:rp]; [re setFragmentTexture:v atIndex:0]; [re drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3]; [re endEncoding];
id<MTLBlitCommandEncoder> be = [cb blitCommandEncoder];
if (!be) { printf("no blit encoder\n"); return 1; }
[be copyFromTexture:rt sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(8, 1, 1) toBuffer:fo destinationOffset:0 destinationBytesPerRow:32 destinationBytesPerImage:32]; [be endEncoding];
[cb commit]; [cb waitUntilCompleted];
const uint32_t *c = co.contents, *f = fo.contents;
Expand Down
Loading