aboutsummaryrefslogtreecommitdiff
path: root/dxg/include
diff options
context:
space:
mode:
Diffstat (limited to 'dxg/include')
-rw-r--r--dxg/include/dxg/dxcommon.h98
-rw-r--r--dxg/include/dxg/dxg.h4
-rw-r--r--dxg/include/dxg/imm.h13
3 files changed, 100 insertions, 15 deletions
diff --git a/dxg/include/dxg/dxcommon.h b/dxg/include/dxg/dxcommon.h
index bfcdbe8..96cf3a5 100644
--- a/dxg/include/dxg/dxcommon.h
+++ b/dxg/include/dxg/dxcommon.h
@@ -5,16 +5,57 @@
5#include <directx/d3dx12.h> 5#include <directx/d3dx12.h>
6 6
7#include <assert.h> 7#include <assert.h>
8#include <stdbool.h>
8#include <stdio.h> 9#include <stdio.h>
10#include <stdlib.h>
11
12#define COUNTOF(ARR) (sizeof(ARR) / sizeof(ARR[0]))
13
14// MSVC currently has neither aligned_alloc nor alignof.
15//#define ALLOC(TYPE, COUNT) (TYPE*)aligned_alloc(alignof(TYPE), sizeof(TYPE) * (COUNT))
16
17typedef struct alloc_t {
18 void* base; // The result of malloc(). Pass this to free().
19 void* ptr; // Aligned pointer within the allocation.
20} alloc_t;
21
22static inline bool is_pow2_or_0(size_t x) { return (x & (x - 1)) == 0; }
23
24static inline void* align(void* address, size_t alignment) {
25 assert(is_pow2_or_0(alignment));
26 const size_t mask = alignment - 1;
27 return (void*)(((uintptr_t)address + mask) & ~mask);
28}
29
30static inline alloc_t alloc_aligned(size_t size, size_t alignment) {
31 void* base = malloc(size + (alignment - 1));
32 return (alloc_t){.base = base, .ptr = align(base, alignment)};
33}
34
35static inline free_aligned(alloc_t* alloc) {
36 assert(alloc);
37 free(alloc->base);
38 *alloc = (alloc_t){0};
39}
40
41#define ALIGNOF(TYPE) _Alignof(TYPE)
42#define ALLOC(TYPE, COUNT) alloc_aligned(sizeof(TYPE) * (COUNT), ALIGNOF(TYPE))
43#define FREE(ALLOC) free_aligned(&(ALLOC))
44
45#ifndef NDEBUG
46#define DEBUG_PRINT OutputDebugStringA
47#else
48#define DEBUG_PRINT
49#endif
9 50
10#define TRAP(ERROR) { \ 51#define TRAP(ERROR) { \
11 fprintf(stderr, "Error in file:[%s] line:%d: %s", __FILE__, __LINE__, ERROR);\ 52 fprintf(stderr, "Error in file:[%s] line:%d: %s\n", __FILE__, __LINE__, ERROR);\
12 assert(false);\ 53 assert(false);\
13 __debugbreak();\ 54 __debugbreak();\
14} 55}
15 56
16#define TRAP_HRESULT(RESULT) { \ 57#define TRAP_HRESULT(RESULT) { \
17 fprintf(stderr, "HRESULT: %u", RESULT);\ 58 fprintf(stderr, "HRESULT: 0x%x - ", RESULT);\
18 TRAP("API call failed")\ 59 TRAP("API call failed")\
19} 60}
20 61
@@ -31,25 +72,52 @@
31 }\ 72 }\
32} 73}
33 74
75#define MIN(A, B) ((A) < (B) ? (A) : (B))
76
34#define CD3DX12_CPU_DESCRIPTOR_HANDLE(HANDLE, INDEX, SIZE) \ 77#define CD3DX12_CPU_DESCRIPTOR_HANDLE(HANDLE, INDEX, SIZE) \
35 (D3D12_CPU_DESCRIPTOR_HANDLE){HANDLE.ptr + (INDEX * SIZE)} 78 (D3D12_CPU_DESCRIPTOR_HANDLE){HANDLE.ptr + (INDEX * SIZE)}
36 79
37#define OFFSET_HANDLE(HANDLE, OFFSET, SIZE) \ 80#define OFFSET_HANDLE(HANDLE, OFFSET, SIZE) \
38 (D3D12_CPU_DESCRIPTOR_HANDLE){HANDLE.ptr + (OFFSET * SIZE)} 81 (D3D12_CPU_DESCRIPTOR_HANDLE){HANDLE.ptr + (OFFSET * SIZE)}
39 82
40static inline D3D12_RESOURCE_BARRIER CD3DX12_RESOURCE_BARRIER_Transition(
41 _In_ ID3D12Resource* pResource,
42 D3D12_RESOURCE_STATES stateBefore,
43 D3D12_RESOURCE_STATES stateAfter) {
44 return (D3D12_RESOURCE_BARRIER){
45 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
46 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
47 .Transition.pResource = pResource,
48 .Transition.StateBefore = stateBefore,
49 .Transition.StateAfter = stateAfter,
50 .Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES};
51}
52
53typedef enum SampleMask { 83typedef enum SampleMask {
54 PointSampling = 0xffffffff 84 PointSampling = 0xffffffff
55} SampleMask; 85} SampleMask;
86
87D3D12_RESOURCE_BARRIER CD3DX12_RESOURCE_BARRIER_Transition(
88 ID3D12Resource* pResource,
89 D3D12_RESOURCE_STATES stateBefore,
90 D3D12_RESOURCE_STATES stateAfter);
91D3D12_RASTERIZER_DESC CD3DX12_RASTERIZER_DESC_DEFAULT();
92D3D12_BLEND_DESC CD3DX12_BLEND_DESC_DEFAULT();
93
94void dxg_wait(ID3D12Fence*, HANDLE fenceEvent, UINT64 fenceValue);
95
96// -----------------------------------------------------------------------------
97// Command Recorder
98// -----------------------------------------------------------------------------
99
100// Currently handling graphics command lists only.
101// Add compute when needed.
102typedef struct CommandRecorder {
103 ID3D12GraphicsCommandList* pCmdList;
104 ID3D12CommandAllocator* pCmdAllocator;
105} CommandRecorder;
106
107HRESULT dxg_cmdrec_init(CommandRecorder*, ID3D12Device*);
108void dxg_cmdrec_destroy(CommandRecorder*);
109HRESULT dxg_cmdrec_reset(CommandRecorder*);
110
111// -----------------------------------------------------------------------------
112// Upload Buffer
113// -----------------------------------------------------------------------------
114
115typedef struct UploadBuffer {
116 ID3D12Resource* pUploadBuffer;
117 size_t size;
118} UploadBuffer;
119
120void dxg_upload_buffer_init(UploadBuffer*, ID3D12Device*, size_t size);
121void dxg_upload_buffer_destroy(UploadBuffer*, ID3D12Device*);
122void dxg_upload_buffer_load(UploadBuffer*, const void* pData, size_t bytes, ID3D12Resource* pDstBuffer);
123bool dxg_upload_buffer_done(UploadBuffer*);
diff --git a/dxg/include/dxg/dxg.h b/dxg/include/dxg/dxg.h
new file mode 100644
index 0000000..0982d9c
--- /dev/null
+++ b/dxg/include/dxg/dxg.h
@@ -0,0 +1,4 @@
1#pragma once
2
3#include <dxg/dxcommon.h>
4#include <dxg/imm.h>
diff --git a/dxg/include/dxg/imm.h b/dxg/include/dxg/imm.h
new file mode 100644
index 0000000..fdee725
--- /dev/null
+++ b/dxg/include/dxg/imm.h
@@ -0,0 +1,13 @@
1#pragma once
2
3#include <dxg/dxcommon.h>
4
5#include <stddef.h>
6
7typedef struct DxgImm DxgImm;
8
9DxgImm* dxg_imm_init(ID3D12Device* pDevice, ID3D12CommandQueue*, DXGI_FORMAT swapChainRtvFormat, DXGI_SAMPLE_DESC swapChainSampleDesc, size_t bufferSizeVerts);
10void dxg_imm_destroy(DxgImm**);
11void dxg_imm_set_graphics_state(DxgImm*, const D3D12_VIEWPORT*, D3D12_CPU_DESCRIPTOR_HANDLE hBackBufferView, D3D12_CPU_DESCRIPTOR_HANDLE hDepthStencilView);
12void dxg_imm_flush(DxgImm*);
13void dxg_imm_draw_triangles(DxgImm*, const float* pVerts, size_t numTris);