mirror of
https://github.com/UniXNU/NativeMetal.git
synced 2026-11-21 02:41:29 +03:00
feat(metal): pass tile pipelines and tile dispatches to the runtime
A render pipeline state made from a tile descriptor, the tile stage's buffers, textures and samplers, threadgroup memory and dispatchThreadsPerTile reach the runtime, which runs a tile function as a compute dispatch between two instances of the pass. A pass carries its tile size and tile memory lengths. The device does not offer tile shaders: QuartzCore reads the private supportsTileShaders, which stays no. metal-tile=1 answers yes for a measurement. With it QuartzCore made two to three tile pipelines and one to four dispatches in a ten minute run, so the path does not matter for its frame times; its pictures were not compared.
This commit is contained in:
@@ -371,6 +371,8 @@ static id deviceForPort(unsigned port) {
|
||||
// Turnip compiles inside vkCreate*Pipelines on the calling thread: the runtime makes the call there instead of on a thread it
|
||||
// starts for it (its relay exists for a driver whose compiler service takes the asking thread's class).
|
||||
setenv("METAL2VULKAN_COMPILE_ON_ASKING_THREAD", "1", 1);
|
||||
// metal-tile=1: the device also answers the family that has tile shaders (bridge: tile_shaders_offered).
|
||||
if (bootArgSet("metal-tile=1")) setenv("NATIVE_METAL_TILE", "1", 1); else unsetenv("NATIVE_METAL_TILE");
|
||||
{
|
||||
// metal-flow=<bits>: 1 the lowering thread, 2 commit submission, 4 the warm pass; all three without the argument.
|
||||
char args[1100] = {}, flow[16] = "";
|
||||
|
||||
+108
-28
@@ -172,6 +172,7 @@ static std::atomic<uint64_t> gPasses,gDraws,gPassPixels,gDispatches,gBlits,gBigg
|
||||
// What the screen-size passes (2 Mpx and up) could draw on, for the SA row: how many ended, of them those whose draws' scissors reach
|
||||
// 90 % of the target and those that drew nothing, and the pixels of the targets and of the scissors' bounding boxes. A bound from above:
|
||||
// a draw may cover less than its scissor, and a pass that never sets one counts whole.
|
||||
static std::atomic<uint64_t> gTileDispatches,gTilePipelines; // tile dispatches encoded and tile pipeline states made (the SA row)
|
||||
static std::atomic<uint64_t> gBigPassesEnded,gBigPassesWhole,gBigPassesEmpty,gBigPassTargetPixels,gBigPassScissorPixels;
|
||||
// Where the time goes on the CPU side of the bridge: pipeline, library and function creation (count, total and slowest, in microseconds), and the
|
||||
// last commits with what each held and how long the GPU took (commit to completion).
|
||||
@@ -907,10 +908,10 @@ static void journal(const std::string &line) {
|
||||
if(gJournalPath.empty())return;
|
||||
std::lock_guard<std::mutex> lock(gJournalLock);
|
||||
if(FILE *f=fopen(gJournalPath.c_str(),"a")){fwrite(line.data(),1,line.size(),f);fputc('\n',f);fclose(f);}
|
||||
}
|
||||
// A test boot ends in a reset: every 32 lines the file is synced to disk.
|
||||
static uint32_t lines=0;
|
||||
if(++lines%32==0){const int fd=open(gJournalPath.c_str(),O_RDONLY);if(fd>=0){fsync(fd);close(fd);}}
|
||||
}
|
||||
|
||||
// A selector this file lacks (iPadOS 27's QuartzCore sends setResponsibleTaskIDs:count: to every command buffer, which 17 never did): answered
|
||||
// once as a no-op returning nil/0, and noted once, instead of the NSInvalidArgumentException that ended the render server. respondsToSelector:
|
||||
@@ -2580,6 +2581,7 @@ static uint64_t encodeCommandOf(id object) {
|
||||
// The pass's target size, its current scissor (x0, y0, x1, y1; the whole target until set) and the bounding box of the scissors its
|
||||
// draws ran under (empty until a draw), for the SA row.
|
||||
uint32_t _targetW,_targetH,_scissor[4],_drawn[4];
|
||||
uint32_t _tileW,_tileH; // the pass's tile size (tileWidth, tileHeight)
|
||||
}
|
||||
@property(nonatomic) uint64_t passKey; // colour 0 format | load << 16 | surface << 18 | big << 19
|
||||
@end
|
||||
@@ -2708,14 +2710,39 @@ static uint64_t encodeCommandOf(id object) {
|
||||
- (void)setFragmentTexture:(id<MTLTexture>)t atTextureIndex:(NSUInteger)ti samplerState:(id<MTLSamplerState>)s atSamplerIndex:(NSUInteger)si {
|
||||
[self texture:t index:ti stage:2];[self sampler:s index:si stage:2];
|
||||
}
|
||||
// Tile shaders and imageblock tile memory (Apple GPU families 4 and up) are not offered: supportsTileShaders answers no, and a call that
|
||||
// reaches one anyway fails its command buffer.
|
||||
- (NSUInteger)tileWidth {return 0;}
|
||||
- (NSUInteger)tileHeight {return 0;}
|
||||
- (void)setTileTexture:(id<MTLTexture>)t atIndex:(NSUInteger)i {(void)t;(void)i;[self.commandBuffer refuse:@"tile shaders are not supported"];}
|
||||
- (void)setTileBytes:(const void *)p length:(NSUInteger)n atIndex:(NSUInteger)i {(void)p;(void)n;(void)i;[self.commandBuffer refuse:@"tile shaders are not supported"];}
|
||||
- (void)dispatchThreadsPerTile:(MTLSize)size {(void)size;[self.commandBuffer refuse:@"tile shaders are not supported"];}
|
||||
- (void)dispatchThreadsPerTile:(MTLSize)size inRegion:(MTLRegion)region {(void)size;(void)region;[self.commandBuffer refuse:@"tile shaders are not supported"];}
|
||||
// Tile shaders (a render pipeline state made from a tile descriptor, dispatched per tile of the pass) go to the runtime, which runs the
|
||||
// tile function as a compute dispatch between two instances of the pass. The tile stage is stage 3 of the binders.
|
||||
// The tile size is the pass descriptor's, or Metal's default for the pass's samples (32, and 16 at 4 samples); it is answered here
|
||||
// because the runtime's encoder is made later, on the encoding thread.
|
||||
- (NSUInteger)tileWidth {return _tileW;}
|
||||
- (NSUInteger)tileHeight {return _tileH;}
|
||||
- (void)setTileBuffer:(id<MTLBuffer>)b offset:(NSUInteger)o atIndex:(NSUInteger)i {[self buffer:b offset:o index:i stage:3];}
|
||||
- (void)setTileBufferOffset:(NSUInteger)o atIndex:(NSUInteger)i {[self offset:o index:i stage:3];}
|
||||
- (void)setTileBuffers:(const id<MTLBuffer> __unsafe_unretained [])b offsets:(const NSUInteger [])o withRange:(NSRange)r {
|
||||
for(NSUInteger k=0;k<r.length;k++)[self buffer:b[k] offset:o[k] index:r.location+k stage:3];
|
||||
}
|
||||
- (void)setTileBytes:(const void *)p length:(NSUInteger)n atIndex:(NSUInteger)i {[self bytes:p length:n index:i stage:3];}
|
||||
- (void)setTileTexture:(id<MTLTexture>)t atIndex:(NSUInteger)i {[self texture:t index:i stage:3];}
|
||||
- (void)setTileTextures:(const id<MTLTexture> __unsafe_unretained [])t withRange:(NSRange)r {
|
||||
for(NSUInteger k=0;k<r.length;k++)[self texture:t[k] index:r.location+k stage:3];
|
||||
}
|
||||
- (void)setTileSamplerState:(id<MTLSamplerState>)s atIndex:(NSUInteger)i {[self sampler:s index:i stage:3];}
|
||||
- (void)setTileSamplerState:(id<MTLSamplerState>)s lodMinClamp:(float)a lodMaxClamp:(float)b atIndex:(NSUInteger)i {[self sampler:s lodMin:a max:b index:i stage:3];}
|
||||
- (void)setTileSamplerStates:(const id<MTLSamplerState> __unsafe_unretained [])s withRange:(NSRange)r {
|
||||
for(NSUInteger k=0;k<r.length;k++)[self sampler:s[k] index:r.location+k stage:3];
|
||||
}
|
||||
- (void)setThreadgroupMemoryLength:(NSUInteger)length offset:(NSUInteger)offset atIndex:(NSUInteger)index {
|
||||
ENCODE([self check:nmb_render_set_threadgroup_memory_length(self.handle,length,offset,(uint32_t)index) what:"setThreadgroupMemoryLength"]);
|
||||
}
|
||||
- (void)dispatchThreadsPerTile:(MTLSize)size {
|
||||
const uint64_t threads[3]={size.width,size.height,size.depth};gTileDispatches++;
|
||||
ENCODE([self check:nmb_render_dispatch_threads_per_tile(self.handle,threads) what:"dispatchThreadsPerTile"]);
|
||||
}
|
||||
- (void)dispatchThreadsPerTile:(MTLSize)size inRegion:(MTLRegion)region {
|
||||
const uint64_t threads[3]={size.width,size.height,size.depth};
|
||||
const uint64_t words[6]={region.origin.x,region.origin.y,region.origin.z,region.size.width,region.size.height,region.size.depth};gTileDispatches++;
|
||||
ENCODE([self check:nmb_render_dispatch_threads_per_tile_in_region(self.handle,threads,words) what:"dispatchThreadsPerTile"]);
|
||||
}
|
||||
- (void)setStencilReferenceValue:(uint32_t)v {if(gDrawStats)[self noteState:kDSOther key:UINT32_MAX object:nullptr value:0];ENCODE([self check:nmb_render_set_stencil_reference(self.handle,v,v) what:"setStencilReferenceValue"]);}
|
||||
- (void)setStencilFrontReferenceValue:(uint32_t)f backReferenceValue:(uint32_t)b {
|
||||
if(gDrawStats)[self noteState:kDSOther key:UINT32_MAX object:nullptr value:0];ENCODE([self check:nmb_render_set_stencil_reference(self.handle,f,b) what:"setStencilReferenceValues"]);
|
||||
@@ -3299,6 +3326,7 @@ static char kUserDictionaryKey;
|
||||
pass.depth.clear[0]=d.depthAttachment.clearDepth;pass.stencil.clear[0]=d.stencilAttachment.clearStencil;
|
||||
pass.depth.resolve_filter=d.depthAttachment.depthResolveFilter;pass.stencil.resolve_filter=d.stencilAttachment.stencilResolveFilter;
|
||||
pass.width=d.renderTargetWidth;pass.height=d.renderTargetHeight;pass.array_length=d.renderTargetArrayLength;pass.samples=singleSample() && d.defaultRasterSampleCount?1:d.defaultRasterSampleCount;
|
||||
pass.tile_width=d.tileWidth;pass.tile_height=d.tileHeight;pass.imageblock_sample_length=d.imageblockSampleLength;pass.threadgroup_memory_length=d.threadgroupMemoryLength;
|
||||
if(d.visibilityResultBuffer) {
|
||||
if(![(id)d.visibilityResultBuffer isKindOfClass:[NBBuffer class]]){[self refuse:@"foreign visibility result buffer"];return nil;}
|
||||
[self own:d.visibilityResultBuffer];
|
||||
@@ -3309,6 +3337,13 @@ static char kUserDictionaryKey;
|
||||
if(made) {
|
||||
((NBRenderEncoder *)made).resolveCopies=std::move(resolveCopies);
|
||||
id<MTLTexture> t0=d.colorAttachments[0].texture;
|
||||
{
|
||||
// tileWidth and tileHeight: the descriptor's, or Metal's default for the pass's samples (32, and 16 at 4 samples).
|
||||
const NSUInteger samples=t0?t0.sampleCount:(NSUInteger)pass.samples;
|
||||
const uint32_t fallback=samples>=4?16:32;
|
||||
((NBRenderEncoder *)made)->_tileW=d.tileWidth?(uint32_t)d.tileWidth:fallback;
|
||||
((NBRenderEncoder *)made)->_tileH=d.tileHeight?(uint32_t)d.tileHeight:fallback;
|
||||
}
|
||||
const uint64_t big=t0 && (uint64_t)t0.width*t0.height>=2000000;
|
||||
if(big) {
|
||||
NBRenderEncoder *encoder=(NBRenderEncoder *)made;
|
||||
@@ -4682,6 +4717,23 @@ static void reportUnreadableLibrary(NSString *path) {
|
||||
const uint64_t pipelineBegan=nowUs();const NBCost costBegan=NBCost::now();
|
||||
uint64_t cacheBefore[10]={};if(!gJournalPath.empty())nmb_device_work_stats(gDevice,cacheBefore);
|
||||
NmbRenderPipeline *handle=nmb_render_pipeline_new_with_options(gDevice,p,&creation);gTimePipeline.add(pipelineBegan);
|
||||
// With diagnostics on: which Metal functions the program Turnip just built for this pipeline belongs to (tu_darwin_last_program gives
|
||||
// the vertex and fragment shader hashes of the last program this thread built; the pass records of the frame trace name a pass's
|
||||
// heaviest fragment shader by the same hash). One line per fragment hash, at most 512.
|
||||
if(handle && diagOn()) {
|
||||
static void (*lastProgram)(uint32_t *,uint32_t *)=nullptr;static std::atomic<bool> looked{false};
|
||||
if(!looked.load() && !gLoaderPath.empty()) {
|
||||
if(void *turnip=dlopen(gLoaderPath.c_str(),RTLD_NOW|RTLD_NOLOAD)){lastProgram=(void (*)(uint32_t *,uint32_t *))dlsym(turnip,"tu_darwin_last_program");looked.store(true);}
|
||||
}
|
||||
uint32_t vs=0,fs=0;
|
||||
if(lastProgram)lastProgram(&vs,&fs);
|
||||
if(fs) {
|
||||
static std::mutex seenLock;static std::set<uint32_t> seen;
|
||||
bool fresh=false;{std::lock_guard<std::mutex> lock(seenLock);fresh=seen.size()<512 && seen.insert(fs).second;}
|
||||
if(fresh)os_log(OS_LOG_DEFAULT,"NativeAdrenoMetal shader fs %{public}08x vs %{public}08x | frag %{public}@ vert %{public}@ | %llu us",fs,vs,
|
||||
d.fragmentFunction.name,d.vertexFunction.name,(unsigned long long)(nowUs()-pipelineBegan));
|
||||
}
|
||||
}
|
||||
if(handle && reflection && wantsReflection(options))*reflection=(MTLRenderPipelineReflection *)renderReflection(p);
|
||||
delete p;
|
||||
std::string cost=gJournalPath.empty()?std::string():NBCost::now().since(costBegan);
|
||||
@@ -4708,6 +4760,50 @@ static void reportUnreadableLibrary(NSString *path) {
|
||||
}
|
||||
// The asynchronous forms make the pipeline on the caller's thread and call the handler before returning, as the library's
|
||||
// newFunctionWithName:constantValues:completionHandler: does (RenderBox's pipeline loader, Core Image's bilateral solver).
|
||||
// A tile pipeline (MTLTileRenderPipelineDescriptor): the runtime compiles the tile function as a compute kernel under the descriptor's
|
||||
// colour formats; a render encoder dispatches it per tile of its pass.
|
||||
- (id<MTLRenderPipelineState>)newRenderPipelineStateWithTileDescriptor:(MTLTileRenderPipelineDescriptor *)d options:(MTLPipelineOption)options
|
||||
reflection:(MTLRenderPipelineReflection **)reflection error:(NSError **)error {
|
||||
if(reflection)*reflection=nil;
|
||||
NBFunction *tile=[(id)d.tileFunction isKindOfClass:[NBFunction class]]?(NBFunction *)d.tileFunction:nil;
|
||||
if(!tile){if(error)*error=nbError(@"tile pipeline: foreign or missing tile function");return nil;}
|
||||
NmbTilePipelineDescriptor p={};
|
||||
p.tile=tile.handle;
|
||||
for(unsigned i=0;i<8;i++)p.formats[i]=d.colorAttachments[i].pixelFormat;
|
||||
p.samples=singleSample()?1:d.rasterSampleCount;
|
||||
p.threadgroup_matches_tile=d.threadgroupSizeMatchesTileSize;
|
||||
p.max_threads=d.maxTotalThreadsPerThreadgroup;
|
||||
p.max_call_stack_depth=d.maxCallStackDepth;
|
||||
p.adding_binary_functions=d.supportAddingBinaryFunctions;
|
||||
for(unsigned i=0;i<31;i++)p.mutability[i]=d.tileBuffers[i].mutability;
|
||||
std::vector<NmbFunction *> linkedStore,privateStore;
|
||||
MTLLinkedFunctions *linked=d.linkedFunctions;
|
||||
if(!linkedHandles(linked.functions,linkedStore) || !linkedHandles(linked.privateFunctions,privateStore)) {
|
||||
if(error)*error=nbError(@"tile pipeline: foreign linked function");return nil;
|
||||
}
|
||||
p.linked=linkedStore.data();p.linked_count=linkedStore.size();p.private_linked=privateStore.data();p.private_linked_count=privateStore.size();
|
||||
std::vector<NmbBinaryArchive *> archives;
|
||||
if(!pipelineArchives(d.binaryArchives,archives,error))return nil;
|
||||
const NmbPipelineOptions creation={(uint64_t)options,archives.data(),archives.size()};
|
||||
const uint64_t began=nowUs();
|
||||
NmbRenderPipeline *handle=nmb_tile_pipeline_new(gDevice,&p,&creation);gTimePipeline.add(began);
|
||||
if(!handle) {
|
||||
NSString *why=[NSString stringWithFormat:@"%@ | tile %@",lastError(),tile.name];
|
||||
static std::atomic<int> told{0};
|
||||
if(told.fetch_add(1)<64)NBLOG(@"native-metal: tile pipeline refused: %@",why);else noteProblem([@"tile pipeline: " stringByAppendingString:why]);
|
||||
if(error)*error=nbError([@"tile pipeline: " stringByAppendingString:why]);
|
||||
return nil;
|
||||
}
|
||||
NBRenderPipeline *state=[NBRenderPipeline new];state.handle=handle;state.stateFormat=(uint32_t)d.colorAttachments[0].pixelFormat;
|
||||
gPipelines++;gTilePipelines++;if(error)*error=nil;
|
||||
return (id<MTLRenderPipelineState>)state;
|
||||
}
|
||||
- (void)newRenderPipelineStateWithTileDescriptor:(MTLTileRenderPipelineDescriptor *)d options:(MTLPipelineOption)options
|
||||
completionHandler:(MTLNewRenderPipelineStateWithReflectionCompletionHandler)handler {
|
||||
NSError *error=nil;MTLRenderPipelineReflection *reflection=nil;
|
||||
id<MTLRenderPipelineState> state=[(id<MTLDevice>)self newRenderPipelineStateWithTileDescriptor:d options:options reflection:&reflection error:&error];
|
||||
handler(state,reflection,error);
|
||||
}
|
||||
- (void)newRenderPipelineStateWithDescriptor:(MTLRenderPipelineDescriptor *)d completionHandler:(MTLNewRenderPipelineStateCompletionHandler)handler {
|
||||
NSError *error=nil;id<MTLRenderPipelineState> state=[(id<MTLDevice>)self newRenderPipelineStateWithDescriptor:d error:&error];
|
||||
handler(state,error);
|
||||
@@ -4717,23 +4813,6 @@ static void reportUnreadableLibrary(NSString *path) {
|
||||
NSError *error=nil;MTLRenderPipelineReflection *reflection=nil;
|
||||
id<MTLRenderPipelineState> state=[(id<MTLDevice>)self newRenderPipelineStateWithDescriptor:d options:options reflection:&reflection error:&error];
|
||||
handler(state,reflection,error);
|
||||
// With diagnostics on: which Metal functions the program Turnip just built for this pipeline belongs to (tu_darwin_last_program gives
|
||||
// the vertex and fragment shader hashes of the last program this thread built; the pass records of the frame trace name a pass's
|
||||
// heaviest fragment shader by the same hash). One line per fragment hash, at most 512.
|
||||
if(handle && diagOn()) {
|
||||
static void (*lastProgram)(uint32_t *,uint32_t *)=nullptr;static std::atomic<bool> looked{false};
|
||||
if(!looked.load() && !gLoaderPath.empty()) {
|
||||
if(void *turnip=dlopen(gLoaderPath.c_str(),RTLD_NOW|RTLD_NOLOAD)){lastProgram=(void (*)(uint32_t *,uint32_t *))dlsym(turnip,"tu_darwin_last_program");looked.store(true);}
|
||||
}
|
||||
uint32_t vs=0,fs=0;
|
||||
if(lastProgram)lastProgram(&vs,&fs);
|
||||
if(fs) {
|
||||
static std::mutex seenLock;static std::set<uint32_t> seen;
|
||||
bool fresh=false;{std::lock_guard<std::mutex> lock(seenLock);fresh=seen.size()<512 && seen.insert(fs).second;}
|
||||
if(fresh)os_log(OS_LOG_DEFAULT,"NativeAdrenoMetal shader fs %{public}08x vs %{public}08x | frag %{public}@ vert %{public}@ | %llu us",fs,vs,
|
||||
d.fragmentFunction.name,d.vertexFunction.name,(unsigned long long)(nowUs()-pipelineBegan));
|
||||
}
|
||||
}
|
||||
}
|
||||
- (void)newComputePipelineStateWithFunction:(id<MTLFunction>)function completionHandler:(MTLNewComputePipelineStateCompletionHandler)handler {
|
||||
NSError *error=nil;id<MTLComputePipelineState> state=[(id<MTLDevice>)self newComputePipelineStateWithFunction:function error:&error];
|
||||
@@ -5460,8 +5539,9 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) {
|
||||
(ull)gDispatches.load(),(ull)gBlits.load());
|
||||
// The screen-size passes that ended: how many, those whose draws' scissors reach 90 % of the target, those that drew nothing; then the
|
||||
// targets' megapixels and the megapixels inside the draws' scissors (a bound from above on what the passes drew on).
|
||||
row("SA big %llu whole %llu empty %llu | target %lluM scissors %lluM",(ull)gBigPassesEnded.load(),(ull)gBigPassesWhole.load(),
|
||||
(ull)gBigPassesEmpty.load(),(ull)(gBigPassTargetPixels.load()/1000000),(ull)(gBigPassScissorPixels.load()/1000000));
|
||||
row("SA big %llu whole %llu empty %llu | target %lluM scissors %lluM | tile pipelines %llu dispatches %llu",(ull)gBigPassesEnded.load(),
|
||||
(ull)gBigPassesWhole.load(),(ull)gBigPassesEmpty.load(),(ull)(gBigPassTargetPixels.load()/1000000),(ull)(gBigPassScissorPixels.load()/1000000),
|
||||
(ull)gTilePipelines.load(),(ull)gTileDispatches.load());
|
||||
row("G commit-to-complete n=%llu total=%llums max=%llums mean=%lluus",(ull)gTimeEncodeToCommit.count.load(),ms(gTimeEncodeToCommit.total.load()),
|
||||
ms(gTimeEncodeToCommit.slowest.load()),(ull)(gTimeEncodeToCommit.count.load()?gTimeEncodeToCommit.total.load()/gTimeEncodeToCommit.count.load():0));
|
||||
for(int k=0;k<3;k++) {
|
||||
|
||||
@@ -355,6 +355,7 @@ struct NmbRenderPassDescriptor {
|
||||
struct NmbPassAttachment colors[8], depth, stencil;
|
||||
uint64_t width, height, array_length, samples;
|
||||
NmbBuffer *visibility; /* the pass's visibility result buffer, or null */
|
||||
uint64_t tile_width, tile_height, imageblock_sample_length, threadgroup_memory_length; /* 0: Metal's defaults */
|
||||
};
|
||||
NmbEncoder *nmb_render_encoder_new(NmbCommandBuffer *, const struct NmbRenderPassDescriptor *);
|
||||
bool nmb_encoder_end(NmbEncoder *);
|
||||
@@ -366,6 +367,27 @@ bool nmb_encoder_set_buffer_offset(NmbEncoder *, uint32_t stage, uint64_t offset
|
||||
bool nmb_encoder_set_bytes(NmbEncoder *, uint32_t stage, const void *bytes, size_t length, uint32_t index);
|
||||
bool nmb_encoder_set_texture(NmbEncoder *, uint32_t stage, NmbTexture *, uint32_t index);
|
||||
bool nmb_encoder_set_sampler(NmbEncoder *, uint32_t stage, NmbSampler *, uint32_t index);
|
||||
// The tile stage of a render encoder is stage 3 of the nmb_encoder_set_* binders. A tile pipeline is a render pipeline state made
|
||||
// from a tile descriptor (newRenderPipelineStateWithTileDescriptor:): formats are MTLPixelFormat values, 0 for no attachment.
|
||||
typedef struct {
|
||||
NmbFunction *tile;
|
||||
uint64_t formats[8];
|
||||
uint64_t samples;
|
||||
uint64_t threadgroup_matches_tile;
|
||||
uint64_t max_threads;
|
||||
uint64_t max_call_stack_depth;
|
||||
uint64_t adding_binary_functions;
|
||||
uint64_t mutability[31];
|
||||
NmbFunction *const *linked;
|
||||
uint64_t linked_count;
|
||||
NmbFunction *const *private_linked;
|
||||
uint64_t private_linked_count;
|
||||
} NmbTilePipelineDescriptor;
|
||||
NmbRenderPipeline *nmb_tile_pipeline_new(NmbDevice *, const NmbTilePipelineDescriptor *, const struct NmbPipelineOptions *options);
|
||||
bool nmb_render_tile_size(NmbEncoder *, uint64_t *size); // width, height
|
||||
bool nmb_render_dispatch_threads_per_tile(NmbEncoder *, const uint64_t *threads); // width, height, depth
|
||||
bool nmb_render_dispatch_threads_per_tile_in_region(NmbEncoder *, const uint64_t *threads, const uint64_t *region);
|
||||
bool nmb_render_set_threadgroup_memory_length(NmbEncoder *, uint64_t length, uint64_t offset, uint32_t index);
|
||||
|
||||
bool nmb_compute_set_pipeline(NmbEncoder *, NmbComputePipeline *);
|
||||
bool nmb_compute_set_threadgroup_memory(NmbEncoder *, uint32_t length, uint32_t index);
|
||||
|
||||
@@ -15,7 +15,7 @@ use metal2vulkan::metal::{
|
||||
};
|
||||
use metal2vulkan::pipeline::{
|
||||
ColorAttachmentDescriptor, ComputePipelineDescriptor, LinkedFunction, LinkedFunctions, RenderPipelineDescriptor, StageSource,
|
||||
TessellationDescriptor, VertexAttributeDescriptor, VertexBufferLayoutDescriptor, VertexDescriptor,
|
||||
TessellationDescriptor, TileRenderPipelineDescriptor, VertexAttributeDescriptor, VertexBufferLayoutDescriptor, VertexDescriptor,
|
||||
};
|
||||
use metal2vulkan_runtime::{
|
||||
BinaryArchive, BinaryArchiveDescriptor, BlitCommandEncoder, FamilyRow, FeatureRow, GpuFamily, RowState, BlitOption, Buffer, ClearColor, CommandBuffer, CommandQueue, ComputeCommandEncoder,
|
||||
@@ -906,6 +906,16 @@ pub unsafe extern "C" fn nmb_device_answer_selector(index: usize, kind: *mut u32
|
||||
/// this device has none of (iconservicesagent crashed in `CI::GLContext::init`, int-155); QuartzCore splits a frame into command buffers of
|
||||
/// 128 draws where Apple3 is false (`MetalContext::flush`). Each waived row stays the runtime's own refusal where it is used, and
|
||||
/// `nmb_device_family_waivers` names them for the journal.
|
||||
/// True where `NATIVE_METAL_TILE=1` (the boot argument `metal-tile=1`): the private flag `supportsTileShaders` answers yes and
|
||||
/// `supportsFamily:` also answers Apple4, the family whose rows are imageblocks and tile shaders, so a client that has a tile
|
||||
/// path takes it. Off by default: the runtime runs a tile dispatch as a compute dispatch between two instances of the pass, with
|
||||
/// the attachments copied to a scratch image and back, where the GPU's own tile memory cannot be reached; a client's path without
|
||||
/// tile shaders draws the same at reduced sizes. What the path costs here is not measured.
|
||||
fn tile_shaders_offered() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| std::env::var_os("NATIVE_METAL_TILE").is_some_and(|value| value == "1"))
|
||||
}
|
||||
|
||||
const TARGET_FAMILIES: [GpuFamily; 5] =
|
||||
[GpuFamily::Apple1, GpuFamily::Apple2, GpuFamily::Apple3, GpuFamily::Common1, GpuFamily::Common2];
|
||||
|
||||
@@ -948,6 +958,11 @@ fn device_answer(device: &NmbDevice, selector: &str, arg: u64) -> Result<u64, St
|
||||
return device.private().count(c).value().ok_or_else(none);
|
||||
}
|
||||
if let Some(f) = PrivateFlag::ALL.into_iter().find(|f| f.selector() == selector) {
|
||||
// QuartzCore's render context reads `supportsTileShaders` once (its tile downsample and blur paths); the runtime answers no
|
||||
// because it refuses part of Metal's tile shading. `metal-tile=1` answers yes, to measure what the path costs here.
|
||||
if f == PrivateFlag::SupportsTileShaders && tile_shaders_offered() {
|
||||
return Ok(1);
|
||||
}
|
||||
return device.private().flag(f).value().map(u64::from).ok_or_else(none);
|
||||
}
|
||||
let gap = |r: Result<bool, metal2vulkan_runtime::RuntimeGap>| r.map(u64::from).map_err(|g| format!("{selector}: {g:?}"));
|
||||
@@ -988,7 +1003,11 @@ fn device_answer(device: &NmbDevice, selector: &str, arg: u64) -> Result<u64, St
|
||||
"supportsTextureSampleCount:" => gap(p.supports_texture_sample_count(arg))?,
|
||||
"supportsVertexAmplificationCount:" => gap(p.supports_vertex_amplification_count(arg))?,
|
||||
"supportsRasterizationRateMapWithLayerCount:" => gap(p.supports_rasterization_rate_map_with_layer_count(arg))?,
|
||||
"supportsFamily:" => (d.supports_family(arg as i64) || TARGET_FAMILIES.iter().any(|f| f.raw() == arg as i64)) as u64,
|
||||
"supportsFamily:" => {
|
||||
(d.supports_family(arg as i64)
|
||||
|| TARGET_FAMILIES.iter().any(|f| f.raw() == arg as i64)
|
||||
|| (tile_shaders_offered() && GpuFamily::Apple4.raw() == arg as i64)) as u64
|
||||
}
|
||||
"supportsPrimitiveType:" => p.supports_primitive_type(arg) as u64,
|
||||
"supportsTextureWriteRoundingMode:" => p.supports_texture_write_rounding_mode(arg) as u64,
|
||||
_ => return Err(format!("{selector} is not a device query of the bridge")),
|
||||
@@ -2169,6 +2188,107 @@ fn render_descriptor(descriptor: *const NmbRenderPipelineDescriptor) -> Result<R
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// `MTLTileRenderPipelineDescriptor`: the tile function, the colour attachment formats (0 for none), the sample count, whether
|
||||
/// the threadgroup is the tile, the threadgroup limit (0 for the pipeline's own) and the buffers' mutability.
|
||||
#[repr(C)]
|
||||
pub struct NmbTilePipelineDescriptor {
|
||||
pub tile: *mut NmbFunction,
|
||||
pub formats: [u64; 8],
|
||||
pub samples: u64,
|
||||
pub threadgroup_matches_tile: u64,
|
||||
pub max_threads: u64,
|
||||
pub max_call_stack_depth: u64,
|
||||
pub adding_binary_functions: u64,
|
||||
pub mutability: [u64; 31],
|
||||
pub linked: *const *mut NmbFunction,
|
||||
pub linked_count: u64,
|
||||
pub private_linked: *const *mut NmbFunction,
|
||||
pub private_linked_count: u64,
|
||||
}
|
||||
|
||||
fn tile_descriptor(descriptor: *const NmbTilePipelineDescriptor) -> Result<TileRenderPipelineDescriptor, String> {
|
||||
let d = get(descriptor)?;
|
||||
let mut linked = LinkedFunctions::new();
|
||||
linked.functions = linked_list(d.linked, d.linked_count)?;
|
||||
linked.private_functions = linked_list(d.private_linked, d.private_linked_count)?;
|
||||
let tile = get(d.tile)?.0.function_descriptor().with_linked(linked);
|
||||
let mut out = TileRenderPipelineDescriptor::new(StageSource::Function(tile));
|
||||
for (index, &format) in d.formats.iter().enumerate() {
|
||||
out.color_attachment_formats[index] = optional_format(format)?;
|
||||
}
|
||||
out.raster_sample_count = u32::try_from(d.samples.max(1)).map_err(e("tile sample count"))?;
|
||||
out.threadgroup_size_matches_tile_size = d.threadgroup_matches_tile != 0;
|
||||
out.max_total_threads_per_threadgroup = if d.max_threads == 0 { None } else { Some(u32::try_from(d.max_threads).map_err(e("tile max threads"))?) };
|
||||
out.max_call_stack_depth = u32::try_from(d.max_call_stack_depth.max(1)).map_err(e("tile stack depth"))?;
|
||||
out.support_adding_binary_functions = d.adding_binary_functions != 0;
|
||||
for (index, &mutability) in d.mutability.iter().enumerate() {
|
||||
if mutability != 0 {
|
||||
out.buffers.insert(index as u32, raw::<metal2vulkan::metal::Mutability>(mutability, "buffer mutability")?);
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// `newRenderPipelineStateWithTileDescriptor:options:reflection:error:` without reflection: a render pipeline state whose
|
||||
/// tile function a render encoder dispatches per tile.
|
||||
///
|
||||
/// # Safety
|
||||
/// `options` is null or a valid record with a counted array of live archive handles.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn nmb_tile_pipeline_new(device: *mut NmbDevice, descriptor: *const NmbTilePipelineDescriptor,
|
||||
options: *const NmbPipelineOptions) -> *mut NmbRenderPipeline {
|
||||
guard(std::ptr::null_mut(), || {
|
||||
let resolved = unsafe { pipeline_options(options)? };
|
||||
let state = get(device)?.0.new_tile_render_pipeline_state(&tile_descriptor(descriptor)?, &resolved).map_err(e("tile pipeline"))?;
|
||||
Ok(boxed(NmbRenderPipeline(state)))
|
||||
})
|
||||
}
|
||||
|
||||
/// `tileWidth` and `tileHeight` of the encoder's pass, into the caller's two words.
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn nmb_render_tile_size(encoder: *mut NmbEncoder, out: *mut u64) -> bool {
|
||||
guard(false, || {
|
||||
if out.is_null() {
|
||||
return Err("null tile size".into());
|
||||
}
|
||||
let r = render(encoder)?;
|
||||
let size = [r.tile_width().map_err(e("tile width"))?, r.tile_height().map_err(e("tile height"))?];
|
||||
// SAFETY: the header's two words.
|
||||
unsafe { std::slice::from_raw_parts_mut(out, 2) }.copy_from_slice(&size);
|
||||
Ok(true)
|
||||
})
|
||||
}
|
||||
|
||||
/// `dispatchThreadsPerTile:`: `threads` is width, height, depth.
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn nmb_render_dispatch_threads_per_tile(encoder: *mut NmbEncoder, threads: *const u64) -> bool {
|
||||
guard(false, || {
|
||||
render(encoder)?.dispatch_threads_per_tile(size(threads)?).map_err(e("dispatch threads per tile"))?;
|
||||
Ok(true)
|
||||
})
|
||||
}
|
||||
|
||||
/// `dispatchThreadsPerTile:inRegion:`: `region` is the header's six words.
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn nmb_render_dispatch_threads_per_tile_in_region(encoder: *mut NmbEncoder, threads: *const u64, region_words: *const u64)
|
||||
-> bool {
|
||||
guard(false, || {
|
||||
render(encoder)?
|
||||
.dispatch_threads_per_tile_in_region(size(threads)?, region(region_words)?, None)
|
||||
.map_err(e("dispatch threads per tile in region"))?;
|
||||
Ok(true)
|
||||
})
|
||||
}
|
||||
|
||||
/// `setThreadgroupMemoryLength:offset:atIndex:` of a render encoder (the tile stage's threadgroup memory).
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn nmb_render_set_threadgroup_memory_length(encoder: *mut NmbEncoder, length: u64, offset: u64, index: u32) -> bool {
|
||||
guard(false, || {
|
||||
render(encoder)?.set_threadgroup_memory_length(length, offset, index).map_err(e("threadgroup memory length"))?;
|
||||
Ok(true)
|
||||
})
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn nmb_render_pipeline_release(pipeline: *mut NmbRenderPipeline) {
|
||||
release(pipeline)
|
||||
@@ -2845,6 +2965,11 @@ pub struct NmbRenderPassDescriptor {
|
||||
pub array_length: u64,
|
||||
pub samples: u64,
|
||||
pub visibility: *const NmbBuffer,
|
||||
/// `tileWidth`, `tileHeight`, `imageblockSampleLength` and `threadgroupMemoryLength` of the pass (0 for Metal's defaults).
|
||||
pub tile_width: u64,
|
||||
pub tile_height: u64,
|
||||
pub imageblock_sample_length: u64,
|
||||
pub threadgroup_memory_length: u64,
|
||||
}
|
||||
|
||||
fn texture_of(a: &NmbPassAttachment) -> Result<Option<Texture>, String> {
|
||||
@@ -2900,6 +3025,10 @@ pub extern "C" fn nmb_render_encoder_new(command_buffer: *mut NmbCommandBuffer,
|
||||
pass.render_target_height = d.height;
|
||||
pass.render_target_array_length = d.array_length;
|
||||
pass.default_raster_sample_count = d.samples;
|
||||
pass.tile_width = d.tile_width;
|
||||
pass.tile_height = d.tile_height;
|
||||
pass.imageblock_sample_length = d.imageblock_sample_length;
|
||||
pass.threadgroup_memory_length = d.threadgroup_memory_length;
|
||||
if !d.visibility.is_null() {
|
||||
pass.visibility_result_buffer = Some(get(d.visibility)?.0.live()?.clone());
|
||||
}
|
||||
@@ -2959,6 +3088,8 @@ fn optional<'a, T>(handle: *const T) -> Result<Option<&'a T>, String> {
|
||||
const COMPUTE: u32 = 0;
|
||||
const VERTEX: u32 = 1;
|
||||
const FRAGMENT: u32 = 2;
|
||||
/// The tile stage of a render encoder (`setTileBuffer:` and its kin).
|
||||
const TILE: u32 = 3;
|
||||
|
||||
fn bad_stage(stage: u32) -> String {
|
||||
format!("stage {stage} does not fit this encoder")
|
||||
@@ -2973,6 +3104,7 @@ pub extern "C" fn nmb_encoder_set_buffer(encoder: *mut NmbEncoder, stage: u32, b
|
||||
(NmbEncoder::Compute(c), COMPUTE) => c.set_buffer(buffer, offset, index),
|
||||
(NmbEncoder::Render(r), VERTEX) => r.set_vertex_buffer(buffer, offset, index),
|
||||
(NmbEncoder::Render(r), FRAGMENT) => r.set_fragment_buffer(buffer, offset, index),
|
||||
(NmbEncoder::Render(r), TILE) => r.set_tile_buffer(buffer, offset, index),
|
||||
_ => return Err(bad_stage(stage)),
|
||||
}
|
||||
.map_err(e("set buffer"))?;
|
||||
@@ -2987,6 +3119,7 @@ pub extern "C" fn nmb_encoder_set_buffer_offset(encoder: *mut NmbEncoder, stage:
|
||||
(NmbEncoder::Compute(c), COMPUTE) => c.set_buffer_offset(offset, index),
|
||||
(NmbEncoder::Render(r), VERTEX) => r.set_vertex_buffer_offset(offset, index),
|
||||
(NmbEncoder::Render(r), FRAGMENT) => r.set_fragment_buffer_offset(offset, index),
|
||||
(NmbEncoder::Render(r), TILE) => r.set_tile_buffer_offset(offset, index),
|
||||
_ => return Err(bad_stage(stage)),
|
||||
}
|
||||
.map_err(e("set buffer offset"))?;
|
||||
@@ -3009,6 +3142,7 @@ pub unsafe extern "C" fn nmb_encoder_set_bytes(encoder: *mut NmbEncoder, stage:
|
||||
(NmbEncoder::Compute(c), COMPUTE) => c.set_bytes(bytes, index),
|
||||
(NmbEncoder::Render(r), VERTEX) => r.set_vertex_bytes(bytes, index),
|
||||
(NmbEncoder::Render(r), FRAGMENT) => r.set_fragment_bytes(bytes, index),
|
||||
(NmbEncoder::Render(r), TILE) => r.set_tile_bytes(bytes, index),
|
||||
_ => return Err(bad_stage(stage)),
|
||||
}
|
||||
.map_err(e("set bytes"))?;
|
||||
@@ -3025,6 +3159,7 @@ pub extern "C" fn nmb_encoder_set_texture(encoder: *mut NmbEncoder, stage: u32,
|
||||
(NmbEncoder::Compute(c), COMPUTE) => c.set_texture(texture, index),
|
||||
(NmbEncoder::Render(r), VERTEX) => r.set_vertex_texture(texture, index),
|
||||
(NmbEncoder::Render(r), FRAGMENT) => r.set_fragment_texture(texture, index),
|
||||
(NmbEncoder::Render(r), TILE) => r.set_tile_texture(texture, index),
|
||||
_ => return Err(bad_stage(stage)),
|
||||
}
|
||||
.map_err(e("set texture"))?;
|
||||
@@ -3040,6 +3175,7 @@ pub extern "C" fn nmb_encoder_set_sampler(encoder: *mut NmbEncoder, stage: u32,
|
||||
(NmbEncoder::Compute(c), COMPUTE) => c.set_sampler_state(sampler, index),
|
||||
(NmbEncoder::Render(r), VERTEX) => r.set_vertex_sampler_state(sampler, index),
|
||||
(NmbEncoder::Render(r), FRAGMENT) => r.set_fragment_sampler_state(sampler, index),
|
||||
(NmbEncoder::Render(r), TILE) => r.set_tile_sampler_state(sampler, index),
|
||||
_ => return Err(bad_stage(stage)),
|
||||
}
|
||||
.map_err(e("set sampler"))?;
|
||||
|
||||
Reference in New Issue
Block a user