-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgraphics.h
338 lines (260 loc) · 7.84 KB
/
graphics.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//
// Copyright (C) Tammo Hinrichs 2021. All rights reserved.
// Licensed under the MIT License. See LICENSE.md file for full license information
//
#pragma once
#include "types.h"
#include "system.h"
#include "math3d.h"
struct IDXGIAdapter;
struct ID3D11Texture2D;
struct ID3D11Buffer;
//---------------------------------------------------------------------------
// shaders
//---------------------------------------------------------------------------
class Shader : public RCObj
{
public:
enum class Type { None, Compute, Domain, Geometry, Hull, Pixel, Vertex };
Shader(Type t, const void* buf, size_t size);
~Shader();
struct Priv;
Priv* P = nullptr;
};
class ShaderResource : public RCObj
{
public:
virtual ~ShaderResource() {}
struct SR;
virtual SR& GetSR(bool write) = 0;
};
//---------------------------------------------------------------------------
// buffers
//---------------------------------------------------------------------------
class GpuBuffer : public ShaderResource
{
public:
enum class Usage { Immutable, Dynamic, GpuOnly, };
virtual ~GpuBuffer();
SR& GetSR(bool write) override { return GetSR(write, 0); }
RCPtr<ID3D11Buffer> GetBuffer() const;
struct Priv;
Priv* P = nullptr;
protected:
enum class Type { Vertex, Index, Constant, Structured, ByteBuffer };
GpuBuffer(Type type, Usage usage);
virtual void Commit() = 0;
void Upload(const void* data, uint size, uint stride=0, uint totalsize=0);
void Reset();
SR& GetSR(bool write, uint count);
};
template <typename T> class TypedBuffer : public GpuBuffer
{
protected:
uint num = 0;
uint cur = 0;
T* data = nullptr;
TypedBuffer(Type type, uint n, GpuBuffer::Usage usage) : GpuBuffer(type, usage), num(n)
{
if (usage != Usage::Immutable)
Commit();
}
public:
void Clear()
{
cur = 0;
Reset();
}
T* BeginLoad()
{
if (!data && num) data = new T[num];
return data + cur;
}
uint EndLoad(int n=-1)
{
if (n < 0) n = num;
ASSERT(!num || data);
ASSERT(cur + n <= num);
cur += n;
return cur - n;
}
uint Stride() const { return sizeof(T); }
uint Count() const { return cur; }
SR& GetSR(bool write) override { return GpuBuffer::GetSR(write,num); }
void Commit() override { Upload(data, cur * sizeof(T), sizeof(T), num * sizeof(T)); }
};
template <typename TV> class VertexBuffer : public TypedBuffer<TV>
{
public:
explicit VertexBuffer(int nv, GpuBuffer::Usage usage = GpuBuffer::Usage::Immutable)
: TypedBuffer<TV>(GpuBuffer::Type::Vertex, nv, usage) {}
};
class IndexBuffer : public TypedBuffer<uint16>
{
public:
explicit IndexBuffer(int ni, Usage usage = Usage::Immutable)
: TypedBuffer<uint16>(GpuBuffer::Type::Index, ni, usage) {}
};
template <typename TS> class StructuredBuffer : public TypedBuffer<TS>
{
public:
explicit StructuredBuffer(int nr, GpuBuffer::Usage usage = GpuBuffer::Usage::Immutable)
: TypedBuffer<TS>(GpuBuffer::Type::Structured, nr, usage) {}
};
class GpuByteBuffer : public TypedBuffer<uint8>
{
public:
explicit GpuByteBuffer(int size, GpuBuffer::Usage usage = GpuBuffer::Usage::Immutable)
: TypedBuffer<uint8>(GpuBuffer::Type::ByteBuffer, size, usage) {}
};
template<class TCB> class CBuffer : public GpuBuffer
{
protected:
void Commit() override { Upload(&data, sizeof(TCB)); }
public:
TCB data = {};
CBuffer() : GpuBuffer(Type::Constant, Usage::Immutable) {}
CBuffer(const TCB& cb) : GpuBuffer(Type::Constant), data(cb) {}
TCB* operator -> () { return &data; }
};
//---------------------------------------------------------------------------
// textures
//---------------------------------------------------------------------------
enum class PixelFormat : uint
{
None,
R8, R16, R16F, R16I, R32F, R32I,
RG8, RG16, RG16F, RG16I, RG32F, RG32I,
RGBA8, RGBA8sRGB, RGBA16, RGBA16F, RGBA16I, RGBA32F, RGBA32I,
BGRA8, BGRA8sRGB,
RGB10A2,
MAX_FMT,
// depth formats come last
D32F, D24S8,
};
struct TexturePara
{
uint sizeX = 0;
uint sizeY = 0;
uint mipmaps = 1;
PixelFormat format = PixelFormat::RGBA8;
bool Equals(const TexturePara& p) const;
bool operator ==(const TexturePara& p) const { return Equals(p); }
bool operator !=(const TexturePara& p) const { return !Equals(p); }
};
class Texture : public ShaderResource
{
public:
struct Priv;
Priv* P;
TexturePara para;
Texture();
virtual ~Texture();
void CopyFrom(RCPtr<Texture> texture) const;
RCPtr<ID3D11Texture2D> GetTex2D() const;
protected:
SR& GetSR(bool write) override;
};
class RenderTarget : public Texture
{
public:
~RenderTarget() override;
};
//---------------------------------------------------------------------------
// state and bindings
//---------------------------------------------------------------------------
// graphics
struct GState
{
RCPtr<Shader> VS;
RCPtr<Shader> PS;
};
struct GBindings {
GpuBuffer* vscb[4] = {};
ShaderResource* vsres[16] = {};
GpuBuffer* pscb[4] = {};
ShaderResource* psres[16] = {};
RenderTarget* target[4] = {};
RenderTarget* depth = 0;
};
// compute
struct CBindings {
ShaderResource* res[16] = {};
GpuBuffer* cb[4] = {};
ShaderResource* uav[16] = {};
};
//---------------------------------------------------------------------------
// vertex formats and geometry
//---------------------------------------------------------------------------
struct VertexC
{
Vec3 pos;
uint color;
};
struct VertexCT
{
Vec3 pos;
uint color;
Vec2 uv;
};
struct CbVSBasic
{
Mat44 mvp;
};
template<typename TV> class Geometry : public RCObj
{
VertexBuffer<TV> vb;
IndexBuffer ib;
public:
explicit Geometry(int nv, int ni = 0) : vb(nv), ib(ni) {}
void BeginLoad(TV*& vp, uint16*& ip)
{
vp = vb.BeginLoad();
ip = ib.BeginLoad();
}
void EndLoad(uint nv = -1, uint ni = -1)
{
vb.EndLoad(nv);
ib.EndLoad(ni);
}
void Draw(GState &state, const GBindings& binds, int instances=1);
};
//---------------------------------------------------------------------------
// screen capturing
//---------------------------------------------------------------------------
struct CaptureInfo
{
RCPtr<Texture> tex;
uint sizeX;
uint sizeY;
bool isHdr;
uint rateNum;
uint rateDen;
uint64 frameCount;
double time;
};
bool CaptureFrame(int timeoutMs, CaptureInfo &info);
void ReleaseFrame();
//---------------------------------------------------------------------------
// functions
//---------------------------------------------------------------------------
void GfxInit();
void GetVideoOutputs(Array<String>& into);
void InitD3D(int outputIndex);
void ExitD3D();
RCPtr<IDXGIAdapter> GetAdapter();
RCPtr<Texture> LoadImg(const char *filename);
RCPtr<Texture> CreateTexture(const TexturePara& para, const void* data);
struct ShaderDefine
{
String name, value;
};
RCPtr<Shader> CompileShader(Shader::Type type, const Buffer* code, const char* entryPoint, const char* name = nullptr);
RCPtr<Shader> CompileShader(Shader::Type type, const String& code, const char* entryPoint, const char* name = nullptr);
RCPtr<Shader> CompileShader(Shader::Type type, const Buffer* code, const char* entryPoint, const Array<ShaderDefine> ¯os, const char* name = nullptr);
RCPtr<Shader> CompileShader(Shader::Type type, const String& code, const char* entryPoint, const Array<ShaderDefine> ¯os, const char* name = nullptr);
RCPtr<RenderTarget> AcquireRenderTarget(TexturePara para);
RCPtr<RenderTarget> AcquireBackBuffer();
void Clear(RenderTarget* rt, Vec4 color);
void ClearDepth(RenderTarget* rt, float d=1.0f);
void Dispatch(Shader* shader, const CBindings& binds, uint gx=1, uint gy=1, uint gz=1);