forked from KD-lab-Open-Source/Perimeter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIRenderDevice.h
393 lines (313 loc) · 14.6 KB
/
IRenderDevice.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
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
#pragma once
#include <cstdint>
#include "Unknown.h"
#include "RenderTypes.h"
#include "MemoryResource.h"
struct DrawRange {
size_t offset = 0;
size_t len = 0;
};
enum eRenderDeviceSelection {
DEVICE_D3D9,
DEVICE_HEADLESS,
DEVICE_SOKOL
};
//Возвращает минимальное число,являющееся степенью двойки и не меньше, чем n
inline int Power2up(int n)
{
int i=1;
while(i<n)
i=i<<1;
return i;
}
class VertexBuffer: public MemoryResource {
public:
union {
void* ptr = nullptr;
#ifdef PERIMETER_D3D9
struct IDirect3DVertexBuffer9* d3d;
#endif
#ifdef PERIMETER_SOKOL
struct SokolBuffer* sg;
#endif
};
bool dynamic = false;
vertex_fmt_t fmt = 0;
uint16_t VertexSize = 0;
uint32_t NumberVertex = 0;
VertexBuffer();
~VertexBuffer() {
Destroy();
}
NO_COPY_CONSTRUCTOR(VertexBuffer)
void Destroy();
};
class IndexBuffer: public MemoryResource {
public:
union {
void* ptr = nullptr;
#ifdef PERIMETER_D3D9
struct IDirect3DIndexBuffer9* d3d;
#endif
#ifdef PERIMETER_SOKOL
struct SokolBuffer* sg;
#endif
};
bool dynamic = false;
uint32_t NumberIndices = 0;
IndexBuffer();
~IndexBuffer() {
Destroy();
}
NO_COPY_CONSTRUCTOR(IndexBuffer)
void Destroy();
};
struct sTextureFormatData
{
int rBitCount,gBitCount,bBitCount,aBitCount;
int rBitShift,gBitShift,bBitShift,aBitShift;
int BytePerPixel;
uint32_t FormatID;
sTextureFormatData& Set(int bpp,int rcount,int gcount,int bcount,int acount,int rshift,int gshift,int bshift,int ashift, uint32_t formatID=-1)
{
BytePerPixel=bpp;
rBitCount=rcount; gBitCount=gcount; bBitCount=bcount; aBitCount=acount;
rBitShift=rshift; gBitShift=gshift; bBitShift=bshift; aBitShift=ashift;
FormatID = formatID;
return *this;
}
void Get(int &bpp,int &rcount,int &gcount,int &bcount,int &acount,int &rshift,int &gshift,int &bshift,int &ashift)
{
bpp=BytePerPixel;
rcount=rBitCount; gcount=gBitCount; bcount=bBitCount; acount=aBitCount;
rshift=rBitShift; gshift=gBitShift; bshift=bBitShift; ashift=aBitShift;
}
};
class cIUnkClass;
class cObjMesh;
unsigned int ColorByNormal(Vect3f n);
Vect3f NormalByColor(uint32_t d);
void BuildMipMap(int x,int y,int bpp,int bplSrc,const void *pSrc,int bplDst,void *pDst,
int rc,int gc,int bc,int ac,int rs,int gs,int bs,int as,int Blur=0);
struct sPolygon {
static const size_t PN = 3;
indices_t p1, p2, p3;
inline void set(indices_t i1,indices_t i2,indices_t i3) { p1=i1; p2=i2; p3=i3; }
};
class cFont;
class cTexture;
class cTexLibrary;
class cCamera;
struct sDataRenderMaterial
{
sColor4f Ambient;
sColor4f Diffuse;
sColor4f Specular;
sColor4f Emissive;
float Power = 0.0f;
float Phase = 0.0f;
uint32_t mat = 0;
cTexture* Tex[2] = {};
MatXf TexMatrix;
float MaterialAnimPhase = 0.0f;
sDataRenderMaterial() = default;
};
using ColorConversionFunc = uint32_t (*)(const sColor4c&);
class cInterfaceRenderDevice : public cUnknownClass
{
protected:
struct SDL_Window* sdl_window = nullptr;
cTexLibrary* TexLibrary = nullptr;
cFont* CurrentFont = nullptr;
cFont* DefaultFont = nullptr;
cCamera *DrawNode = nullptr;
Vect2i ScreenSize = { 0, 0 };
Vect2i MaxScreenSize = { 0, 0 };
uint32_t ScreenHZ = 0;
uint32_t RenderMode = 0;
class DrawBuffer* activeDrawBuffer = nullptr;
std::unordered_map<uint64_t, class DrawBuffer*> drawBuffers;
Mat4f orthoVP;
eCullMode CameraCullMode = CULL_NONE;
bool debugUIEnabled = false;
virtual void DrawFieldDispatcher(class FieldDispatcher* ffd, uint8_t transparent);
public:
cInterfaceRenderDevice();
~cInterfaceRenderDevice() override;
// Runtime set methods
ColorConversionFunc ConvertColor = nullptr;
// Helper methods
inline cCamera* GetDrawNode() { return DrawNode; }
inline cTexLibrary* GetTexLibrary() { return TexLibrary; }
inline eModeRenderDevice GetRenderMode() { return static_cast<eModeRenderDevice>(RenderMode); }
cTexture* GetTexture(int n);
void DrawFieldDispatcher(class FieldDispatcher *ffd);
void DrawSprite3(int x, int y, int dx, int dy, float u, float v, float du, float dv,
cTexture *Tex1, cTexture *Tex2, const sColor4c& ColorMul=sColor4c(255,255,255,255), float phase=0);
// Common methods
virtual uint32_t GetWindowCreationFlags() const { return 0; }
virtual int Init(int xScr,int yScr,int mode, SDL_Window* wnd = nullptr, int RefreshRateInHz=0);
virtual int Done();
virtual int BeginScene();
virtual int EndScene();
virtual bool IsFullScreen() { return (RenderMode&RENDERDEVICE_MODE_WINDOW) == 0; }
virtual int GetSizeX() { return ScreenSize.x; };
virtual int GetSizeY() { return ScreenSize.y; };
virtual int GetMaxSizeX() { return MaxScreenSize.x; }
virtual int GetMaxSizeY() { return MaxScreenSize.y; }
virtual void SetTexture(uint32_t slot, class cTexture* texture, float Phase);
virtual void SetDrawNode(cCamera* node) { DrawNode = node; };
virtual void SetWorldMatXf(const MatXf& matrix);
virtual void SetFont(cFont *pFont);
virtual void SetDefaultFont(cFont *pFont);
virtual float GetFontLength(const char *string, size_t* count = nullptr);
virtual float GetCharLength(const char c);
//Back in the good-ish old days a line was just one pixel width and everybody was happy, over years due to
//technological progress the screen resolutions have become higher and higher each year such as a 2004 1 pixel line
//is too thin to be even noticed in super pro whatever 4k screen unless you stare close enough to feel the screen
//warmth in your face
//Therefore this function returns what width a thin line should have based on 2004 era screen and 1~px ratio
float getThinLineWidth() const;
virtual size_t GetSizeFromFormat(vertex_fmt_t fmt) const;
virtual void CreateVertexBuffer(class VertexBuffer &vb, uint32_t NumberVertex, vertex_fmt_t fmt, bool dynamic);
virtual void DeleteVertexBuffer(class VertexBuffer &vb);
virtual void CreateIndexBuffer(class IndexBuffer& ib, uint32_t NumberIndices, bool dynamic);
virtual void DeleteIndexBuffer(class IndexBuffer &ib);
virtual void* LockVertexBuffer(class VertexBuffer &vb);
virtual void* LockVertexBufferRange(class VertexBuffer &vb, uint32_t Start, uint32_t Amount);
virtual void UnlockVertexBuffer(class VertexBuffer &vb);
virtual indices_t* LockIndexBuffer(class IndexBuffer &ib);
virtual indices_t* LockIndexBufferRange(class IndexBuffer &ib, uint32_t Start, uint32_t Amount);
virtual void UnlockIndexBuffer(class IndexBuffer &ib);
virtual class DrawBuffer* GetDrawBuffer(vertex_fmt_t fmt, ePrimitiveType primitive, size_t vertices = 0);
virtual void SetActiveDrawBuffer(class DrawBuffer*);
/*Внутренний мтод. Использовать с крайней осторожностью.
Перед использованием посмотреть как используется внутри Render.
*/
virtual void ChangeTextColor(const char* &str,sColor4c& diffuse);
virtual void OutTextRect(int x,int y,const char *string,int align,Vect2f& bmin,Vect2f& bmax);
virtual void OutText(int x,int y,const char *string,const sColor4f& color,int align=-1,eBlendMode blend_mode=ALPHA_BLEND);
virtual void OutText(int x,int y,const char *string,const sColor4f& color,int align,eBlendMode blend_mode,
cTexture* pTexture,eColorMode mode,
Vect2f uv,//Координаты текстуры в точке x,y
Vect2f duv,//du,dv на один логический пиксель
//(лог пиксель равен графическому в разрешении 1024x768)
float phase=0,
float lerp_factor=1//0..1 Насколько сильно влияет pTexture
);
virtual void CreateFFDData(class FieldDispatcher *rd);
virtual void DeleteFFDData(class FieldDispatcher *rd);
virtual int CreateTilemap(class cTileMap *TileMap);
virtual int DeleteTilemap(class cTileMap *TileMap);
virtual void DrawElasticSphere(class ElasticSphere *es);
virtual void DrawScene(class cScene *Scene);
virtual void DrawBound(const Vect3f &min, const Vect3f &max, const sColor4c& diffuse=sColor4c(255,255,255,255));
virtual void DrawBound(const MatXf &Matrix, const Vect3f &min, const Vect3f &max, bool wireframe=false, const sColor4c& diffuse=sColor4c(255,255,255,255));
virtual void DrawSprite(int x,int y,int dx,int dy,float u,float v,float du,float dv,
cTexture *Texture,const sColor4c& ColorMul=sColor4c(255,255,255,255),float phase=0,eBlendMode mode=ALPHA_NONE);
virtual void DrawSprite3(int x1, int y1, int dx, int dy, float u0, float v0, float du0, float dv0, float u1, float v1, float du1, float dv1,
cTexture *Tex1, cTexture *Tex2, const sColor4c& ColorMul=sColor4c(255,255,255,255), float phase=0, eColorMode mode=COLOR_MOD, eBlendMode blend_mode=ALPHA_NONE);
virtual void DrawSprite2(int x,int y,int dx,int dy,float u,float v,float du,float dv,float u1,float v1,float du1,float dv1,
cTexture *Tex1,cTexture *Tex2,float lerp_factor,float alpha=1,float phase=0,eColorMode mode=COLOR_MOD,eBlendMode blend_mode=ALPHA_NONE);
virtual void DrawLine(int x1,int y1,int x2,int y2,const sColor4c& color, float width = 1.0);
virtual void DrawPixel(int x1,int y1, const sColor4c& color);
virtual void DrawRectangle(int x,int y,int dx,int dy,const sColor4c& color, float outline = 0.0);
virtual void FlushPrimitive2D();
virtual void DrawLine(const Vect3f &v1,const Vect3f &v2,const sColor4c& color);
virtual void DrawPoint(const Vect3f &v1, const sColor4c& color);
virtual void FlushPrimitive3D();
virtual void DebugUISetEnable(bool enabled);
virtual bool DebugUIIsEnabled();
virtual bool DebugUIMouseMove(const Vect2f& pos);
virtual bool DebugUIMousePress(const Vect2f& pos, uint8_t button, bool pressed);
virtual bool DebugUIKeyPress(struct sKey* key, bool pressed);
#ifdef PERIMETER_DEBUG
virtual void StartCaptureFrame();
#endif
// Decl only methods
virtual eRenderDeviceSelection GetRenderSelection() const = 0;
virtual bool ChangeSize(int xScr,int yScr,int mode) = 0;
virtual int GetClipRect(int *xmin,int *ymin,int *xmax,int *ymax) = 0;
virtual int SetClipRect(int xmin,int ymin,int xmax,int ymax) = 0;
virtual void UseOrthographicProjection() = 0;
virtual void SetDrawTransform(class cCamera *pDrawNode) = 0;
virtual void SetWorldMat4f(const Mat4f* matrix) = 0;
virtual int Fill(int r,int g,int b,int a=255) = 0;
virtual void ClearZBuffer() = 0;
virtual int Flush(bool wnd=false) = 0;
virtual int SetGamma(float fGamma,float fStart=0.f,float fFinish=1.f) = 0;
virtual bool SetScreenShot(const char *fname) = 0;
virtual uint32_t GetRenderState(eRenderStateOption option) = 0;
virtual int SetRenderState(eRenderStateOption option,uint32_t value) = 0;
virtual int CreateTexture(class cTexture *Texture,class cFileImage *FileImage,bool enable_assert=true) = 0;
virtual int DeleteTexture(class cTexture *Texture) = 0;
virtual void* LockTexture(class cTexture *Texture, int& Pitch) = 0;
virtual void* LockTextureRect(class cTexture* Texture, int& Pitch, Vect2i pos, Vect2i size) = 0;
virtual void UnlockTexture(class cTexture *Texture) = 0;
virtual void SetTextureImage(uint32_t slot, struct TextureImage* texture_image) = 0;
virtual void SetTextureTransform(uint32_t slot, const Mat4f& transform) = 0;
virtual uint32_t GetMaxTextureSlots() = 0;
virtual void SetGlobalFog(const sColor4f &color,const Vect2f &v) = 0;
virtual void SetGlobalLight(Vect3f *vLight, sColor4f *Ambient = nullptr,
sColor4f *Diffuse = nullptr, sColor4f *Specular = nullptr) = 0;
virtual bool IsEnableSelfShadow() = 0;
virtual void SetNoMaterial(eBlendMode blend,float Phase=0,cTexture *Texture0=0,cTexture *Texture1=0,eColorMode color_mode=COLOR_MOD) = 0;
virtual void SetBlendState(eBlendMode blend) = 0;
virtual void SubmitDrawBuffer(class DrawBuffer* db, DrawRange* range) = 0;
virtual void SubmitBuffers(ePrimitiveType primitive, class VertexBuffer* vb, size_t vertices, class IndexBuffer* ib, size_t indices, DrawRange* range) = 0;
virtual void BeginDrawMesh(bool obj_mesh, bool use_shadow) = 0;
virtual void EndDrawMesh() = 0;
virtual void SetSimplyMaterialMesh(cObjMesh* mesh, sDataRenderMaterial* data) = 0;
virtual void DrawNoMaterialMesh(cObjMesh* mesh, sDataRenderMaterial* data) = 0;
virtual void BeginDrawShadow(bool shadow_map) = 0;
virtual void EndDrawShadow() = 0;
virtual void SetSimplyMaterialShadow(cObjMesh* mesh, cTexture* texture) = 0;
virtual void DrawNoMaterialShadow(cObjMesh* mesh) = 0;
virtual union SurfaceImage GetShadowZBuffer() = 0;
virtual void SetRenderTarget(cTexture* target, union SurfaceImage zbuffer) = 0;
virtual void RestoreRenderTarget() = 0;
virtual void SetMaterialTilemap(cTileMap *TileMap) = 0;
virtual void SetMaterialTilemapShadow() = 0;
virtual void SetTileColor(sColor4f color) = 0;
virtual bool CreateShadowTexture(int xysize) = 0;
virtual void DeleteShadowTexture() = 0;
virtual cTexture* GetShadowMap() = 0;
virtual cTexture* GetLightMap() = 0;
// Decl only methods end
};
cInterfaceRenderDevice* CreateIRenderDevice(eRenderDeviceSelection selection);
extern cInterfaceRenderDevice* gb_RenderDevice;
//Из RenderDevice.h
enum eSurfaceFormat
{
SURFMT_COLOR = 0,
SURFMT_COLORALPHA,
SURFMT_RENDERMAP16,
SURFMT_RENDERMAP32,
SURFMT_BUMP,
SURFMT_COLOR32,
SURFMT_COLORALPHA32,
SURFMT_RENDERMAP_DEPTH,
SURFMT_GRAYALPHA,//A8L8 - в лудшем случае
SURFMT_UV,
SURFMT_U16V16,
SURFMT_NUMBER
};
enum eMaterialMode
{
MAT_COLOR_ADD_SPECULAR =1<<4,
MAT_ALPHA_ADDBLENDALPHA =1<<5,
MAT_ALPHA_BLEND =1<<6,
MAT_ALPHA_ADDBLEND =1<<7,
MAT_ALPHA_SUBBLEND =1<<29,
MAT_BUMP =1<<9,
MAT_NORMAL =1<<10,
MAT_ALPHA_TEST =1<<8,
MAT_IS_BLEND = MAT_ALPHA_ADDBLENDALPHA|MAT_ALPHA_BLEND|MAT_ALPHA_ADDBLEND|MAT_ALPHA_SUBBLEND,
// only d3d version
// render type
MAT_TEXMATRIX_STAGE1 =1<<16,
MAT_TEXNORMAL_STAGE2 =1<<18,
MAT_RENDER_SPHEREMAP =1<<22,
MAT_LIGHT =1<<31
};