Browse Source

added compute shader api (#638)

Nicolas Cannasse 1 year ago
parent
commit
f84d80bb01
2 changed files with 153 additions and 1 deletions
  1. 96 1
      libs/directx/dx/Dx12.hx
  2. 57 0
      libs/directx/dx12.cpp

+ 96 - 1
libs/directx/dx/Dx12.hx

@@ -190,6 +190,7 @@ abstract CommandList(Resource) {
 	public function setGraphicsRootConstantBufferView( index : Int, address : Address ) {}
 	public function setGraphicsRootDescriptorTable( index : Int, address : Address ) {}
 	public function setGraphicsRootShaderResourceView( index : Int, address : Address ) {}
+	public function setGraphicsRootUnorderedAccessView( index : Int, address : Address ) {}
 
 	public function iaSetPrimitiveTopology( top : PrimitiveTopology ) {}
 	public function iaSetVertexBuffers( startSlot : Int, numViews : Int, views : VertexBufferView /* hl.CArray */ ) {}
@@ -211,6 +212,14 @@ abstract CommandList(Resource) {
 
 	public function setPredication( res : Resource, offset : Int64, op : PredicationOp ) {}
 
+	public function setComputeRootSignature( sign : RootSignature ) {}
+	public function setComputeRoot32BitConstants( index : Int, numValues : Int, data : hl.Bytes, dstOffset : Int ) {}
+	public function setComputeRootConstantBufferView( index : Int, address : Address ) {}
+	public function setComputeRootDescriptorTable( index : Int, address : Address ) {}
+	public function setComputeRootShaderResourceView( index : Int, address : Address ) {}
+	public function setComputeRootUnorderedAccessView( index : Int, address : Address ) {}
+	public function dispatch( x : Int, y : Int, z : Int ) {}
+
 	static function create( type : CommandListType, alloc : CommandAllocator, state : PipelineState ) : Resource { return null; }
 }
 
@@ -655,6 +664,12 @@ abstract GraphicsPipelineState(Resource) {
 	}
 }
 
+abstract ComputePipelineState(Resource) {
+	@:to inline function toPS():PipelineState {
+		return cast this;
+	}
+}
+
 abstract Bool32(Int) {
 	@:to inline function toBool() return this != 1;
 	@:from static inline function fromBool(b:Bool) : Bool32 { return cast b?1:0; };
@@ -784,7 +799,7 @@ enum abstract DxgiFormat(Int) {
 
 @:struct class ShaderBytecode {
 	public var shaderBytecode : hl.Bytes;
-	public var bytecodeLength : Int;
+	public var bytecodeLength : hl.I64;
 }
 
 @:struct class SoDeclarationEntry {
@@ -1099,6 +1114,16 @@ enum abstract PipelineStateFlags(Int) {
 	}
 }
 
+@:struct class ComputePipelineStateDesc {
+	public var rootSignature : RootSignature;
+	@:packed public var cs(default,null) : ShaderBytecode;
+	public var nodeMask : Int;
+	@:packed public var cachedPSO(default,null) : CachedPipelineState;
+	public var flags : PipelineStateFlags;
+	public function new() {
+	}
+}
+
 enum abstract HeapType(Int) {
 	var DEFAULT = 1;
 	var UPLOAD = 2;
@@ -1408,6 +1433,69 @@ enum abstract IndirectArgumentType(Int) {
 	}
 }
 
+enum abstract UAVDimension(Int) {
+	public var UNKNOWN = 0;
+	public var BUFFER = 1;
+	public var TEXTURE1D = 2;
+	public var TEXTURE1DARRAY = 3;
+	public var TEXTURE2D = 4;
+	public var TEXTURE2DARRAY = 5;
+	public var TEXTURE3D = 8;
+}
+
+@:struct class UnorderedAccessViewDesc {
+	public var format : Format;
+	public var viewDimension : UAVDimension;
+}
+
+enum UAVBufferFlags {
+	RAW;
+}
+
+@:struct class UAVBufferViewDesc extends UnorderedAccessViewDesc {
+	public var firstElement : hl.I64;
+	public var numElements : Int;
+	public var structureSizeInBytes : Int;
+	public var counterOffsetInBytes : hl.I64;
+	public var flags : haxe.EnumFlags<UAVBufferFlags>;
+	public function new() {
+		viewDimension = BUFFER;
+	}
+}
+
+@:struct class UAVTextureViewDesc extends UnorderedAccessViewDesc {
+	var int0 : Int;
+	var int1 : Int;
+	var int2 : Int;
+	var int3 : Int;
+	var padding1 : hl.I64;
+	var padding2 : Int;
+	public function new(dim) {
+		viewDimension = dim;
+	}
+	public var mipSlice(get,set) : Int;
+	public var firstArraySlice(get,set) : Int;
+	public var firstWSlice(get,set) : Int;
+	public var arraySize(get,set) : Int;
+	public var planeSlice(get,set) : Int;
+	public var wSlice(get,set) : Int;
+	inline function get_mipSlice() return int0;
+	inline function set_mipSlice(v) return int0 = v;
+
+	inline function get_planeSlice() return switch( viewDimension ) { case TEXTURE2DARRAY: int3; default: int1; }
+	inline function set_planeSlice(v) return switch( viewDimension ) { case TEXTURE2DARRAY: int3 = v; default: int1 = v; }
+
+	inline function get_firstArraySlice() return int1;
+	inline function set_firstArraySlice(v) return int1 = v;
+	inline function get_arraySize() return int2;
+	inline function set_arraySize(v) return int2 = v;
+
+	inline function get_firstWSlice() return int1;
+	inline function set_firstWSlice(v) return int1 = v;
+	inline function get_wSlice() return int2;
+	inline function set_wSlice(v) return int2 = v;
+}
+
 enum abstract QueryType(Int) {
 	var OCCLUSION = 0;
 	var BINARY_OCCLUSION = 1;
@@ -1465,6 +1553,10 @@ class Dx12 {
 		return null;
 	}
 
+	public static function createComputePipelineState( desc : ComputePipelineStateDesc ) : ComputePipelineState {
+		return null;
+	}
+
 	public static function serializeRootSignature( desc : RootSignatureDesc, version : Int, size : hl.Ref<Int> ) : hl.Bytes {
 		return null;
 	}
@@ -1486,6 +1578,9 @@ class Dx12 {
 	public static function createConstantBufferView( desc : ConstantBufferViewDesc, target : Address ) {
 	}
 
+	public static function createUnorderedAccessView( res : Resource, counter : Resource, desc : UnorderedAccessViewDesc, target : Address ) {
+	}
+
 	public static function createShaderResourceView( resource : Resource, desc : ShaderResourceViewDesc, target : Address ) {
 	}
 

+ 57 - 0
libs/directx/dx12.cpp

@@ -546,6 +546,10 @@ void HL_NAME(create_constant_buffer_view)( D3D12_CONSTANT_BUFFER_VIEW_DESC *desc
 	static_driver->device->CreateConstantBufferView(desc,descriptor);
 }
 
+void HL_NAME(create_unordered_access_view)( ID3D12Resource *res, ID3D12Resource *counter, D3D12_UNORDERED_ACCESS_VIEW_DESC *desc, D3D12_CPU_DESCRIPTOR_HANDLE descriptor ) {
+	static_driver->device->CreateUnorderedAccessView(res,counter,desc,descriptor);
+}
+
 void HL_NAME(create_sampler)( D3D12_SAMPLER_DESC *desc, D3D12_CPU_DESCRIPTOR_HANDLE descriptor ) {
 	static_driver->device->CreateSampler(desc,descriptor);
 }
@@ -604,6 +608,7 @@ DEFINE_PRIM(_VOID, create_render_target_view, _RES _STRUCT _I64);
 DEFINE_PRIM(_VOID, create_depth_stencil_view, _RES _STRUCT _I64);
 DEFINE_PRIM(_VOID, create_shader_resource_view, _RES _STRUCT _I64);
 DEFINE_PRIM(_VOID, create_constant_buffer_view, _STRUCT _I64);
+DEFINE_PRIM(_VOID, create_unordered_access_view, _RES _RES _STRUCT _I64);
 DEFINE_PRIM(_VOID, create_sampler, _STRUCT _I64);
 DEFINE_PRIM(_RES, create_committed_resource, _STRUCT _I32 _STRUCT _I32 _STRUCT);
 DEFINE_PRIM(_RES, get_back_buffer, _I32);
@@ -711,6 +716,13 @@ ID3D12PipelineState *HL_NAME(create_graphics_pipeline_state)( D3D12_GRAPHICS_PIP
 	return state;
 }
 
+ID3D12PipelineState *HL_NAME(create_compute_pipeline_state)( D3D12_COMPUTE_PIPELINE_STATE_DESC *desc ) {
+	ID3D12PipelineState *state = NULL;
+	// if shader is considered invalid, maybe you're missing dxil.dll
+	DXERR(static_driver->device->CreateComputePipelineState(desc,IID_PPV_ARGS(&state)));
+	return state;
+}
+
 ID3D12CommandSignature *HL_NAME(create_command_signature)( D3D12_COMMAND_SIGNATURE_DESC *desc, ID3D12RootSignature *rootSign ) {
 	ID3D12CommandSignature *sign = NULL;
 	DXERR(static_driver->device->CreateCommandSignature(desc,rootSign,IID_PPV_ARGS(&sign)));
@@ -723,6 +735,7 @@ DEFINE_PRIM(_BYTES, compiler_compile, _COMPILER _BYTES _BYTES _ARR _REF(_I32));
 DEFINE_PRIM(_BYTES, serialize_root_signature, _STRUCT _I32 _REF(_I32));
 DEFINE_PRIM(_RES, rootsignature_create, _BYTES _I32);
 DEFINE_PRIM(_RES, create_graphics_pipeline_state, _STRUCT);
+DEFINE_PRIM(_RES, create_compute_pipeline_state, _STRUCT);
 DEFINE_PRIM(_RES, create_command_signature, _STRUCT _RES);
 
 // ---- HEAPS
@@ -900,6 +913,10 @@ void HL_NAME(command_list_set_graphics_root_shader_resource_view)( ID3D12Graphic
 	l->SetGraphicsRootShaderResourceView(index,handle);
 }
 
+void HL_NAME(command_list_set_graphics_root_unordered_access_view)( ID3D12GraphicsCommandList *l, int index, D3D12_GPU_VIRTUAL_ADDRESS handle ) {
+	l->SetGraphicsRootUnorderedAccessView(index,handle);
+}
+
 void HL_NAME(command_list_execute_indirect)( ID3D12GraphicsCommandList *l, ID3D12CommandSignature *sign, int maxCommandCount, ID3D12Resource *args, int64 argsOffset, ID3D12Resource *count, int64 countOffset  ) {
 	l->ExecuteIndirect(sign, maxCommandCount, args, argsOffset, count, countOffset);
 }
@@ -920,6 +937,34 @@ void HL_NAME(command_list_set_predication)( ID3D12GraphicsCommandList *l, ID3D12
 	l->SetPredication(res,offset,op);
 }
 
+void HL_NAME(command_list_set_compute_root_signature)( ID3D12GraphicsCommandList *l, ID3D12RootSignature *sign ) {
+	l->SetComputeRootSignature(sign);
+}
+
+void HL_NAME(command_list_set_compute_root32_bit_constants)( ID3D12GraphicsCommandList *l, int index, int numValues, void *data, int destOffset ) {
+	l->SetComputeRoot32BitConstants(index, numValues, data, destOffset);
+}
+
+void HL_NAME(command_list_set_compute_root_constant_buffer_view)( ID3D12GraphicsCommandList *l, int index, D3D12_GPU_VIRTUAL_ADDRESS address ) {
+	l->SetComputeRootConstantBufferView(index,address);
+}
+
+void HL_NAME(command_list_set_compute_root_descriptor_table)( ID3D12GraphicsCommandList *l, int index, D3D12_GPU_DESCRIPTOR_HANDLE handle ) {
+	l->SetComputeRootDescriptorTable(index,handle);
+}
+
+void HL_NAME(command_list_set_compute_root_shader_resource_view)( ID3D12GraphicsCommandList *l, int index, D3D12_GPU_VIRTUAL_ADDRESS handle ) {
+	l->SetComputeRootShaderResourceView(index,handle);
+}
+
+void HL_NAME(command_list_set_compute_root_unordered_access_view)( ID3D12GraphicsCommandList *l, int index, D3D12_GPU_VIRTUAL_ADDRESS handle ) {
+	l->SetComputeRootUnorderedAccessView(index,handle);
+}
+
+void HL_NAME(command_list_dispatch)( ID3D12GraphicsCommandList *l, int x, int y, int z ) {
+	l->Dispatch(x,y,z);
+}
+
 DEFINE_PRIM(_RES, command_allocator_create, _I32);
 DEFINE_PRIM(_VOID, command_allocator_reset, _RES);
 DEFINE_PRIM(_RES, command_list_create, _I32 _RES _RES);
@@ -936,6 +981,7 @@ DEFINE_PRIM(_VOID, command_list_set_graphics_root32_bit_constants, _RES _I32 _I3
 DEFINE_PRIM(_VOID, command_list_set_graphics_root_constant_buffer_view, _RES _I32 _I64);
 DEFINE_PRIM(_VOID, command_list_set_graphics_root_descriptor_table, _RES _I32 _I64);
 DEFINE_PRIM(_VOID, command_list_set_graphics_root_shader_resource_view, _RES _I32 _I64);
+DEFINE_PRIM(_VOID, command_list_set_graphics_root_unordered_access_view, _RES _I32 _I64);
 DEFINE_PRIM(_VOID, command_list_set_descriptor_heaps, _RES _ARR);
 DEFINE_PRIM(_VOID, command_list_set_pipeline_state, _RES _RES);
 DEFINE_PRIM(_VOID, command_list_ia_set_vertex_buffers, _RES _I32 _I32 _STRUCT);
@@ -952,3 +998,14 @@ DEFINE_PRIM(_VOID, command_list_begin_query, _RES _RES _I32 _I32);
 DEFINE_PRIM(_VOID, command_list_end_query, _RES _RES _I32 _I32);
 DEFINE_PRIM(_VOID, command_list_resolve_query_data, _RES _RES _I32 _I32 _I32 _RES _I64);
 DEFINE_PRIM(_VOID, command_list_set_predication, _RES _RES _I64 _I32);
+
+DEFINE_PRIM(_VOID, command_list_set_compute_root_signature, _RES _RES);
+DEFINE_PRIM(_VOID, command_list_set_compute_root32_bit_constants, _RES _I32 _I32 _BYTES _I32);
+DEFINE_PRIM(_VOID, command_list_set_compute_root_constant_buffer_view, _RES _I32 _I64);
+DEFINE_PRIM(_VOID, command_list_set_compute_root_descriptor_table, _RES _I32 _I64);
+DEFINE_PRIM(_VOID, command_list_set_compute_root_shader_resource_view, _RES _I32 _I64);
+DEFINE_PRIM(_VOID, command_list_set_compute_root_unordered_access_view, _RES _I32 _I64);
+DEFINE_PRIM(_VOID, command_list_dispatch, _RES _I32 _I32 _I32);
+
+//command_list_clear_unordered_access_view_float,
+//command_list_clear_unordered_access_view_uint,