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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
#pragma once
#include <base/tl/threading.h>
#include <engine/graphics.h>
class CCommandBuffer
{
class CBuffer
{
unsigned char *m_pData;
unsigned m_Size;
unsigned m_Used;
public:
CBuffer(unsigned BufferSize)
{
m_Size = BufferSize;
m_pData = new unsigned char[m_Size];
m_Used = 0;
}
~CBuffer()
{
delete [] m_pData;
m_pData = 0x0;
m_Used = 0;
m_Size = 0;
}
void Reset()
{
m_Used = 0;
}
void *Alloc(unsigned Requested)
{
if(Requested + m_Used > m_Size)
return 0;
void *pPtr = &m_pData[m_Used];
m_Used += Requested;
return pPtr;
}
unsigned char *DataPtr() { return m_pData; }
unsigned DataSize() { return m_Size; }
unsigned DataUsed() { return m_Used; }
};
public:
CBuffer m_CmdBuffer;
CBuffer m_DataBuffer;
enum
{
MAX_TEXTURES=1024*4,
};
enum
{
// commadn groups
CMDGROUP_CORE = 0, // commands that everyone has to implement
CMDGROUP_PLATFORM = 10000, // commands specific to a platform
//
CMD_NOP = CMDGROUP_CORE,
//
CMD_RUNBUFFER,
// syncronization
CMD_SIGNAL,
// texture commands
CMD_TEXTURE_CREATE,
CMD_TEXTURE_DESTROY,
CMD_TEXTURE_UPDATE,
// rendering
CMD_CLEAR,
CMD_RENDER,
// swap
CMD_SWAP,
// misc
CMD_SCREENSHOT,
CMD_VIDEOMODES,
};
enum
{
TEXFORMAT_INVALID = 0,
TEXFORMAT_RGB,
TEXFORMAT_RGBA,
TEXFORMAT_ALPHA,
TEXFLAG_NOMIPMAPS = 1,
};
enum
{
INITFLAG_FULLSCREEN = 1,
INITFLAG_VSYNC = 2,
INITFLAG_RESIZABLE = 4,
};
enum
{
//
PRIMTYPE_INVALID = 0,
PRIMTYPE_LINES,
PRIMTYPE_QUADS,
};
enum
{
BLEND_NONE = 0,
BLEND_ALPHA,
BLEND_ADDITIVE,
};
struct SPoint { float x, y, z; };
struct STexCoord { float u, v; };
struct SColor { float r, g, b, a; };
struct SVertex
{
SPoint m_Pos;
STexCoord m_Tex;
SColor m_Color;
};
struct SCommand
{
public:
SCommand(unsigned Cmd) : m_Cmd(Cmd), m_Size(0) {}
unsigned m_Cmd;
unsigned m_Size;
};
struct SState
{
int m_BlendMode;
int m_Texture;
SPoint m_ScreenTL;
SPoint m_ScreenBR;
// clip
bool m_ClipEnable;
int m_ClipX;
int m_ClipY;
int m_ClipW;
int m_ClipH;
};
struct SCommand_Clear : public SCommand
{
SCommand_Clear() : SCommand(CMD_CLEAR) {}
SColor m_Color;
};
struct SCommand_Signal : public SCommand
{
SCommand_Signal() : SCommand(CMD_SIGNAL) {}
semaphore *m_pSemaphore;
};
struct SCommand_RunBuffer : public SCommand
{
SCommand_RunBuffer() : SCommand(CMD_RUNBUFFER) {}
CCommandBuffer *m_pOtherBuffer;
};
struct SCommand_Render : public SCommand
{
SCommand_Render() : SCommand(CMD_RENDER) {}
SState m_State;
unsigned m_PrimType;
unsigned m_PrimCount;
SVertex *m_pVertices; // you should use the command buffer data to allocate vertices for this command
};
struct SCommand_Screenshot : public SCommand
{
SCommand_Screenshot() : SCommand(CMD_SCREENSHOT) {}
CImageInfo *m_pImage; // processor will fill this out, the one who adds this command must free the data as well
};
struct SCommand_VideoModes : public SCommand
{
SCommand_VideoModes() : SCommand(CMD_VIDEOMODES) {}
CVideoMode *m_pModes; // processor will fill this in
int m_MaxModes; // maximum of modes the processor can write to the m_pModes
int *m_pNumModes; // processor will write to this pointer
};
struct SCommand_Swap : public SCommand
{
SCommand_Swap() : SCommand(CMD_SWAP) {}
int m_Finish;
};
struct SCommand_Texture_Create : public SCommand
{
SCommand_Texture_Create() : SCommand(CMD_TEXTURE_CREATE) {}
// texture information
int m_Slot;
int m_Width;
int m_Height;
int m_Format;
int m_StoreFormat;
int m_Flags;
void *m_pData; // will be freed by the command processor
};
struct SCommand_Texture_Update : public SCommand
{
SCommand_Texture_Update() : SCommand(CMD_TEXTURE_UPDATE) {}
// texture information
int m_Slot;
int m_X;
int m_Y;
int m_Width;
int m_Height;
int m_Format;
void *m_pData; // will be freed by the command processor
};
struct SCommand_Texture_Destroy : public SCommand
{
SCommand_Texture_Destroy() : SCommand(CMD_TEXTURE_DESTROY) {}
// texture information
int m_Slot;
};
//
CCommandBuffer(unsigned CmdBufferSize, unsigned DataBufferSize)
: m_CmdBuffer(CmdBufferSize), m_DataBuffer(DataBufferSize)
{
}
void *AllocData(unsigned WantedSize)
{
return m_DataBuffer.Alloc(WantedSize);
}
template<class T>
bool AddCommand(const T &Command)
{
// make sure that we don't do something stupid like ->AddCommand(&Cmd);
(void)static_cast<const SCommand *>(&Command);
// allocate and copy the command into the buffer
SCommand *pCmd = (SCommand *)m_CmdBuffer.Alloc(sizeof(Command));
if(!pCmd)
return false;
mem_copy(pCmd, &Command, sizeof(Command));
pCmd->m_Size = sizeof(Command);
return true;
}
SCommand *GetCommand(unsigned *pIndex)
{
if(*pIndex >= m_CmdBuffer.DataUsed())
return NULL;
SCommand *pCommand = (SCommand *)&m_CmdBuffer.DataPtr()[*pIndex];
*pIndex += pCommand->m_Size;
return pCommand;
}
void Reset()
{
m_CmdBuffer.Reset();
m_DataBuffer.Reset();
}
};
// interface for the graphics backend
// all these functions are called on the main thread
class IGraphicsBackend
{
public:
enum
{
INITFLAG_FULLSCREEN = 1,
INITFLAG_VSYNC = 2,
INITFLAG_RESIZABLE = 4,
};
virtual int Init(const char *pName, int Width, int Height, int FsaaSamples, int Flags) = 0;
virtual int Shutdown() = 0;
virtual void Minimize() = 0;
virtual void Maximize() = 0;
virtual int WindowActive() = 0;
virtual int WindowOpen() = 0;
virtual void RunBuffer(CCommandBuffer *pBuffer) = 0;
virtual bool IsIdle() const = 0;
virtual void WaitForIdle() = 0;
};
class CGraphics_Threaded : public IEngineGraphics
{
enum
{
NUM_CMDBUFFERS = 2,
MAX_VERTICES = 32*1024,
MAX_TEXTURES = 1024*4,
DRAWING_QUADS=1,
DRAWING_LINES=2
};
CCommandBuffer::SState m_State;
IGraphicsBackend *m_pBackend;
CCommandBuffer *m_apCommandBuffers[NUM_CMDBUFFERS];
CCommandBuffer *m_pCommandBuffer;
unsigned m_CurrentCommandBuffer;
//
class IStorage *m_pStorage;
class IConsole *m_pConsole;
CCommandBuffer::SVertex m_aVertices[MAX_VERTICES];
int m_NumVertices;
CCommandBuffer::SColor m_aColor[4];
CCommandBuffer::STexCoord m_aTexture[4];
bool m_RenderEnable;
float m_Rotation;
int m_Drawing;
bool m_DoScreenshot;
char m_aScreenshotName[128];
int m_InvalidTexture;
struct CTexture
{
int m_State;
int m_MemSize;
int m_Flags;
int m_Next;
};
CTexture m_aTextures[MAX_TEXTURES];
int m_FirstFreeTexture;
int m_TextureMemoryUsage;
void FlushVertices();
void AddVertices(int Count);
void Rotate4(const CCommandBuffer::SPoint &rCenter, CCommandBuffer::SVertex *pPoints);
static unsigned char Sample(int w, int h, const unsigned char *pData, int u, int v, int Offset, int ScaleW, int ScaleH, int Bpp);
static unsigned char *Rescale(int Width, int Height, int NewWidth, int NewHeight, int Format, const unsigned char *pData);
void KickCommandBuffer();
int IssueInit();
int InitWindow();
public:
CGraphics_Threaded();
virtual void ClipEnable(int x, int y, int w, int h);
virtual void ClipDisable();
virtual void BlendNone();
virtual void BlendNormal();
virtual void BlendAdditive();
virtual int MemoryUsage() const;
virtual void MapScreen(float TopLeftX, float TopLeftY, float BottomRightX, float BottomRightY);
virtual void GetScreen(float *pTopLeftX, float *pTopLeftY, float *pBottomRightX, float *pBottomRightY);
virtual void LinesBegin();
virtual void LinesEnd();
virtual void LinesDraw(const CLineItem *pArray, int Num);
virtual int UnloadTexture(int Index);
virtual int LoadTextureRaw(int Width, int Height, int Format, const void *pData, int StoreFormat, int Flags);
virtual int LoadTextureRawSub(int TextureID, int x, int y, int Width, int Height, int Format, const void *pData);
// simple uncompressed RGBA loaders
virtual int LoadTexture(const char *pFilename, int StorageType, int StoreFormat, int Flags);
virtual int LoadPNG(CImageInfo *pImg, const char *pFilename, int StorageType);
void ScreenshotDirect(const char *pFilename);
virtual void TextureSet(int TextureID);
virtual void Clear(float r, float g, float b);
virtual void QuadsBegin();
virtual void QuadsEnd();
virtual void QuadsSetRotation(float Angle);
virtual void SetColorVertex(const CColorVertex *pArray, int Num);
virtual void SetColor(float r, float g, float b, float a);
virtual void QuadsSetSubset(float TlU, float TlV, float BrU, float BrV);
virtual void QuadsSetSubsetFree(
float x0, float y0, float x1, float y1,
float x2, float y2, float x3, float y3);
virtual void QuadsDraw(CQuadItem *pArray, int Num);
virtual void QuadsDrawTL(const CQuadItem *pArray, int Num);
virtual void QuadsDrawFreeform(const CFreeformItem *pArray, int Num);
virtual void QuadsText(float x, float y, float Size, float r, float g, float b, float a, const char *pText);
virtual void Minimize();
virtual void Maximize();
virtual int WindowActive();
virtual int WindowOpen();
virtual int Init();
virtual void Shutdown();
virtual void TakeScreenshot(const char *pFilename);
virtual void Swap();
virtual int GetVideoModes(CVideoMode *pModes, int MaxModes);
// syncronization
virtual void InsertSignal(semaphore *pSemaphore);
virtual bool IsIdle();
virtual void WaitForIdle();
};
extern IGraphicsBackend *CreateGraphicsBackend();
|