Skip to content

Commit

Permalink
Added: Debug API to metal
Browse files Browse the repository at this point in the history
  • Loading branch information
MrcSnm committed Dec 5, 2023
1 parent 1895bf7 commit 21299bf
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 11 deletions.
172 changes: 172 additions & 0 deletions source/metal/commandbuffer.d
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ enum MTLCommandBufferStatus : NSUInteger
Error = 5
}

///Error codes that indicate why a command buffer is unable to finish its execution.
enum MTLCommandBufferError : NSUInteger
{
///An error code that represents the absence of any problems.
None = 0,
///An error code that indicates the system interrupted and terminated the command buffer because it took more time to execute than the system allows.
Timeout = 2,
///An error code that indicates the command buffer generated a page fault the GPU can’t service.
PageFault = 3,
///An error code that indicates a process doesn’t have access to a GPU device.
NotPermitted = 7,
///An error code that indicates the GPU device doesn’t have sufficient memory to execute a command buffer.
OutOfMemory = 8,
///An error code that indicates the command buffer has an invalid reference to resource.
InvalidResource = 9,
///An error code that indicates the GPU ran out of one or more of its internal resources that support memoryless render pass attachments.
Memoryless = 10,
///An error code that indicates a person physically removed the GPU device before the command buffer finished running.
DeviceRemoved = 11,
///An error code that indicates the GPU terminated the command buffer because a kernel function of tile shader used too many stack frames.
StackOverflow = 12,
///An error code that indicates the system has revoked the Metal device’s access because it’s responsible for too many timeouts or hangs.
AccessRevoked = 4,
///An error code that indicates the Metal framework has an internal problem.
ErrorInternal = 1

}

///A configuration you create to customize a blit command encoder, which affects the runtime behavior of the blit pass you encode with it.
class MTLBlitPassDescriptor
{
Expand All @@ -65,6 +93,98 @@ class MTLBlitPassDescriptor
///A completion handler signature a GPU device calls when it finishes scheduling a command buffer, or when the GPU finishes running it.
alias MTLCommandBufferHandler = extern(C) void function(MTLCommandBuffer);



///Possible error conditions for the command encoder’s commands.
enum MTLCommandEncoderErrorState : NSInteger
{
///A state that indicates the GPU successfully executed the commands without any errors.
Completed = 1,
///An error state that indicates the GPU didn’t execute the commands.
Pending = 3,
///An error state that indicates the GPU failed to fully execute the commands because of an error.
Affected = 2,
///An error state that indicates the commands in the command buffer are the cause of an error.
Faulted = 4,
///An error state that indicates the command buffer doesn’t know the state of its commands on the GPU.
Unknown = 0
}


///A container that provides additional information about a runtime failure a GPU encounters as it runs the commands in a command buffer.
interface MTLCommandBufferEncoderInfo
{
///The name of the encoder that generates the error information
@selector("label")
NSString label();

///An array of debug signposts that Metal records as the GPU executes the commands of the encoder’s pass.
@selector("debugSignposts")
NSArray_!NSString debugSignposts();

///The execution status of the command encoder.
@selector("errorState")
MTLCommandEncoderErrorState errorState();



}

///Options for different kinds of function logs.
enum MTLFunctionLogType : NSUInteger
{
///A message related to usage validation.
Validation = 0
}


///The source code that logged a debug message.
interface MTLFunctionLogDebugLocation
{
///The name of the shader function.
@selector("functionName")
NSString functionName();

///The URL of the file that contains the shader function.
@selector("URL")
NSURL URL();

///The line that the log message appears on.
@selector("line")
NSUInteger line();

///The column where the log message appears.
@selector("column")
NSUInteger column();
}


///A log entry a Metal device generates when the it runs a command buffer.
interface MTLFunctionLog
{
///The type of message that was logged.
@selector("type")
MTLFunctionLogType type();

///If known, the location of the logging command within a shader source file.
@selector("debugLocation")
MTLFunctionLogDebugLocation debugLocation();

///The label for the encoder that logged the message.
@selector("encoderLabel")
NSString encoderLabel();

///When known, the function object corresponding to the logged message.
@selector("function")
MTLFunction function_();
}

///A collection of logged messages, created when a Metal device runs a command buffer.
interface MTLLogContainer
{
mixin ObjcExtend!NSFastEnumeration;
}

///A container that stores a sequence of GPU commands that you encode into it.
interface MTLCommandBuffer
{
Expand Down Expand Up @@ -133,4 +253,56 @@ interface MTLCommandBuffer
NSString label();
@selector("setLabel:")
NSString label(NSString);


///The command queue that creates the command buffer.
@selector("commandQueue")
MTLCommandQueue commandQueue();
@selector("setCommandQueue:")
MTLCommandQueue commandQueue(MTLCommandQueue);

///Marks the beginning of a debug group and gives it an identifying label, which temporarily replaces the previous group, if applicable.
@selector("pushDebugGroup")
void pushDebugGroup(NSString);


///Marks the end of a debug group and, if applicable, restores the previous group from a stack.
@selector("popDebugGroup")
void popDebugGroup();


///A description of an error when the GPU encounters an issue as it runs the command buffer.
@selector("error")
NSError error();
@selector("setError:")
NSError error(NSError);

/// Settings that determine which information the command buffer records about execution errors, and how it does it.
@selector("errorOptions")
MTLCommandBufferErrorOption errorOptions();
@selector("setErrorOptions:")
MTLCommandBufferErrorOption errorOptions(MTLCommandBufferErrorOption);

///The messages the command buffer records as the GPU runs its commands.

@selector("logs")
MTLLogContainer logs();


///The host time, in seconds, when the CPU begins to schedule the command buffer.
@selector("kernelStartTime")
CFTimeInterval kernelStartTime();

///The host time, in seconds, when the CPU finishes scheduling the command buffer.
@selector("kernelEndTime")
CFTimeInterval kernelEndTime();

///The host time, in seconds, when the GPU starts command buffer execution.
@selector("GPUStartTime")
CFTimeInterval GPUStartTime();

///The host time, in seconds, when the GPU finishes execution of the command buffer.
@selector("GPUEndTime")
CFTimeInterval GPUEndTime();

}
23 changes: 12 additions & 11 deletions source/objc/meta.d
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,18 @@ mixin template ObjcLink(Class)
@selector(__traits(getAttributes, ov)[0].sel)
mixin("__gshared SEL ",selToIdent(__traits(getAttributes, ov)[0].sel),";");
}
static if(hasUDA!(ov, Super))
{
pragma(mangle, ov.mangleof)
mixin("auto ",mixin(_metaGensym!()), " (void* self, Parameters!ov)",
"{",
"alias fn = extern(C) ReturnType!ov function (objc_super*, SEL, Parameters!ov);",
"objc_super superData = objc_super(self, ", __traits(getAttributes, ov)[2].stringof, "_);",
_ObjcGetMsgSuperSend!(ov, "&superData", true),
"}");
}
else static if(__traits(isStaticFunction, ov))
// static if(hasUDA!(ov, Super))
// {
// pragma(mangle, ov.mangleof)
// mixin("auto ",mixin(_metaGensym!()), " (void* self, Parameters!ov)",
// "{",
// "alias fn = extern(C) ReturnType!ov function (objc_super*, SEL, Parameters!ov);",
// "objc_super superData = objc_super(self, ", __traits(getAttributes, ov)[2].stringof, "_);",
// _ObjcGetMsgSuperSend!(ov, "&superData", true),
// "}");
// }
// else
static if(__traits(isStaticFunction, ov))
{
pragma(mangle, ov.mangleof)
mixin("auto ",mixin(_metaGensym!()), " (Parameters!ov)",
Expand Down
28 changes: 28 additions & 0 deletions source/objc/runtime.d
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,32 @@ struct NSRange
class NSData
{
mixin ObjcExtend!NSObject;
}

///An object that represents the location of a resource, such as an item on a remote server or the path to a local file.
class NSURL
{
mixin ObjcExtend!NSObject;

}

///This defines the structure used as contextual information in the NSFastEnumeration protocol.
struct NSFastEnumerationState
{
import core.stdc.config;
///A C array that you can use to hold returned values.
c_ulong[5] extra;
///A C array of objects.
void* itemsPtr;
///Arbitrary state information used to detect whether the collection has been mutated.
c_ulong mutationsPtr;
///Arbitrary state information used by the iterator. Typically this is set to 0 at the beginning of the iteration.
c_ulong state;
}
///A protocol that objects adopt to support fast enumeration.
interface NSFastEnumeration
{
///Returns by reference a C array of objects over which the sender should iterate, and as the return value the number of objects in the array.
@selector("countByEnumeratingWithState:objects:count:")
NSUInteger countByEnumeratingWithState(NSFastEnumerationState* state, void* objects, NSUInteger count);
}

0 comments on commit 21299bf

Please sign in to comment.