-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRenderDevice.h
362 lines (295 loc) · 11.1 KB
/
RenderDevice.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
#pragma once
#include <map>
#include <d3d9.h>
#include <d3dx9.h>
#include "Material.h"
#include "Texture.h"
#define CHECKD3D(s) { HRESULT hr = (s); if (FAILED(hr)) ReportError(hr, __FILE__, __LINE__); }
void ReportError(HRESULT hr, const char *file, int line);
typedef IDirect3DTexture9 D3DTexture;
typedef D3DVIEWPORT9 ViewPort;
typedef IDirect3DSurface9 RenderTarget;
typedef D3DVERTEXELEMENT9 VertexElement;
typedef IDirect3DVertexDeclaration9 VertexDeclaration;
struct VideoMode
{
int width;
int height;
int depth;
int refreshrate;
};
void EnumerateDisplayModes(int adapter, std::vector<VideoMode>& modes);
enum TransformStateType {
TRANSFORMSTATE_WORLD = D3DTS_WORLD,
TRANSFORMSTATE_VIEW = D3DTS_VIEW,
TRANSFORMSTATE_PROJECTION = D3DTS_PROJECTION
};
enum UsageFlags {
USAGE_DYNAMIC = D3DUSAGE_DYNAMIC // to be used for vertex/index buffers which are locked every frame
};
class TextureManager
{
public:
TextureManager(RenderDevice& rd) : m_rd(rd)
{ }
~TextureManager()
{
UnloadAll();
}
D3DTexture* LoadTexture(MemTexture* memtex);
void SetDirty(MemTexture* memtex);
void UnloadTexture(MemTexture* memtex);
void UnloadAll();
private:
struct TexInfo
{
D3DTexture* d3dtex;
int texWidth;
int texHeight;
bool dirty;
};
RenderDevice& m_rd;
std::map<MemTexture*, TexInfo> m_map;
typedef std::map<MemTexture*, TexInfo>::iterator Iter;
};
// adds simple setters and getters on top of D3DLIGHT9, for compatibility
struct BaseLight : public D3DLIGHT9
{
BaseLight()
{
ZeroMemory(this, sizeof(*this));
}
D3DLIGHTTYPE getType() { return Type; }
void setType(D3DLIGHTTYPE lt) { Type = lt; }
const D3DCOLORVALUE& getDiffuse() { return Diffuse; }
const D3DCOLORVALUE& getSpecular() { return Specular; }
const D3DCOLORVALUE& getAmbient() { return Ambient; }
void setDiffuse(float r, float g, float b)
{
Diffuse.r = r;
Diffuse.g = g;
Diffuse.b = b;
}
void setSpecular(float r, float g, float b)
{
Specular.r = r;
Specular.g = g;
Specular.b = b;
}
void setAmbient(float r, float g, float b)
{
Ambient.r = r;
Ambient.g = g;
Ambient.b = b;
}
void setPosition(float x, float y, float z)
{
Position.x = x;
Position.y = y;
Position.z = z;
}
void setDirection(float x, float y, float z)
{
Direction.x = x;
Direction.y = y;
Direction.z = z;
}
void setRange(float r) { Range = r; }
void setFalloff(float r) { Falloff = r; }
void setAttenuation0(float r) { Attenuation0 = r; }
void setAttenuation1(float r) { Attenuation1 = r; }
void setAttenuation2(float r) { Attenuation2 = r; }
void setTheta(float r) { Theta = r; }
void setPhi(float r) { Phi = r; }
};
class VertexBuffer : public IDirect3DVertexBuffer9
{
public:
enum LockFlags
{
WRITEONLY = 0, // in DX9, this is specified during VB creation
NOOVERWRITE = D3DLOCK_NOOVERWRITE, // meaning: no recently drawn vertices are overwritten. only works with dynamic VBs.
// it's only needed for VBs which are locked several times per frame
DISCARDCONTENTS = D3DLOCK_DISCARD // discard previous contents; only works with dynamic VBs
};
void lock( unsigned int offsetToLock, unsigned int sizeToLock, void **dataBuffer, DWORD flags )
{
CHECKD3D(this->Lock(offsetToLock, sizeToLock, dataBuffer, flags));
}
void unlock(void)
{
CHECKD3D(this->Unlock());
}
void release(void)
{
while ( this->Release()!=0 );
}
private:
VertexBuffer(); // disable default constructor
};
class IndexBuffer : public IDirect3DIndexBuffer9
{
public:
enum Format {
FMT_INDEX16 = D3DFMT_INDEX16,
FMT_INDEX32 = D3DFMT_INDEX32
};
enum LockFlags
{
WRITEONLY = 0, // in DX9, this is specified during VB creation
NOOVERWRITE = D3DLOCK_NOOVERWRITE, // meaning: no recently drawn vertices are overwritten. only works with dynamic VBs.
// it's only needed for VBs which are locked several times per frame
DISCARD = D3DLOCK_DISCARD // discard previous contents; only works with dynamic VBs
};
void lock( unsigned int offsetToLock, unsigned int sizeToLock, void **dataBuffer, DWORD flags )
{
CHECKD3D(this->Lock(offsetToLock, sizeToLock, dataBuffer, flags) );
}
void unlock(void)
{
CHECKD3D(this->Unlock());
}
void release(void)
{
while ( this->Release()!=0 );
}
private:
IndexBuffer(); // disable default constructor
};
class RenderDevice
{
public:
typedef enum RenderStates
{
ALPHABLENDENABLE = D3DRS_ALPHABLENDENABLE,
ALPHATESTENABLE = D3DRS_ALPHATESTENABLE,
ALPHAREF = D3DRS_ALPHAREF,
ALPHAFUNC = D3DRS_ALPHAFUNC,
CLIPPING = D3DRS_CLIPPING,
CLIPPLANEENABLE = D3DRS_CLIPPLANEENABLE,
CULLMODE = D3DRS_CULLMODE,
DESTBLEND = D3DRS_DESTBLEND,
LIGHTING = D3DRS_LIGHTING,
SPECULARENABLE = D3DRS_SPECULARENABLE,
SRCBLEND = D3DRS_SRCBLEND,
ZENABLE = D3DRS_ZENABLE,
ZFUNC = D3DRS_ZFUNC,
ZWRITEENABLE = D3DRS_ZWRITEENABLE,
NORMALIZENORMALS = D3DRS_NORMALIZENORMALS,
TEXTUREFACTOR = D3DRS_TEXTUREFACTOR,
DEPTHBIAS = D3DRS_DEPTHBIAS,
COLORWRITEENABLE = D3DRS_COLORWRITEENABLE
};
enum TextureAddressMode {
TEX_WRAP = D3DTADDRESS_WRAP,
TEX_CLAMP = D3DTADDRESS_CLAMP,
TEX_MIRROR = D3DTADDRESS_MIRROR
};
RenderDevice(HWND hwnd, int width, int height, bool fullscreen, int screenWidth, int screenHeight, int colordepth, int &refreshrate, int VSync, bool useAA, bool stereo3DFXAA);
~RenderDevice();
void BeginScene();
void EndScene();
void Clear(DWORD numRects, D3DRECT* rects, DWORD flags, D3DCOLOR color, D3DVALUE z, DWORD stencil);
void Flip(bool vsync);
RenderTarget* GetBackBuffer() { return m_pBackBuffer; }
RenderTarget* DuplicateRenderTarget(RenderTarget* src);
D3DTexture* DuplicateTexture(RenderTarget* src);
D3DTexture* DuplicateDepthTexture(RenderTarget* src);
void SetRenderTarget( RenderTarget* );
void SetZBuffer( RenderTarget* );
RenderTarget* AttachZBufferTo(RenderTarget* surf);
void CopySurface(RenderTarget* dest, RenderTarget* src);
void CopySurface(D3DTexture* dest, RenderTarget* src);
void CopyDepth(D3DTexture* dest, RenderTarget* src);
D3DTexture* CreateSystemTexture(MemTexture* surf);
D3DTexture* UploadTexture(MemTexture* surf, int *pTexWidth=NULL, int *pTexHeight=NULL);
void UpdateTexture(D3DTexture* tex, MemTexture* surf);
void SetRenderState( const RenderStates p1, const DWORD p2 );
void SetTexture( DWORD, D3DTexture* );
void SetTextureFilter(DWORD texUnit, DWORD mode);
void SetTextureAddressMode(DWORD texUnit, TextureAddressMode mode);
void SetTextureStageState(DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value);
void SetMaterial( const BaseMaterial * const material );
void SetMaterial( const Material & material ) { SetMaterial(&material.getBaseMaterial()); }
void CreateVertexBuffer( unsigned int numVerts, DWORD usage, DWORD fvf, VertexBuffer **vBuffer );
void CreateIndexBuffer(unsigned int numIndices, DWORD usage, IndexBuffer::Format format, IndexBuffer **idxBuffer);
IndexBuffer* CreateAndFillIndexBuffer(unsigned int numIndices, const WORD * indices);
IndexBuffer* CreateAndFillIndexBuffer(const std::vector<WORD>& indices);
void DrawPrimitive(D3DPRIMITIVETYPE type, DWORD fvf, const void* vertices, DWORD vertexCount);
void DrawIndexedPrimitive(D3DPRIMITIVETYPE type, DWORD fvf, const void* vertices, DWORD vertexCount, const WORD* indices, DWORD indexCount);
void DrawPrimitiveVB(D3DPRIMITIVETYPE type, VertexBuffer* vb, DWORD startVertex, DWORD vertexCount);
void DrawIndexedPrimitiveVB(D3DPRIMITIVETYPE type, VertexBuffer* vb, DWORD startVertex, DWORD vertexCount, const WORD* indices, DWORD indexCount);
void DrawIndexedPrimitiveVB(D3DPRIMITIVETYPE type, VertexBuffer* vb, DWORD startVertex, DWORD vertexCount, IndexBuffer* ib, DWORD startIndex, DWORD indexCount);
void GetMaterial( BaseMaterial *_material );
void LightEnable( DWORD, BOOL );
void SetLight( DWORD, BaseLight* );
void GetLight( DWORD, BaseLight* );
void SetViewport( ViewPort* );
void GetViewport( ViewPort* );
void SetTransform( TransformStateType, D3DMATRIX* );
void GetTransform( TransformStateType, D3DMATRIX* );
void CreatePixelShader( const char* shader );
void SetPixelShaderConstants(const float* constantData, const unsigned int numFloat4s);
void RevertPixelShaderToFixedFunction();
void ForceAnisotropicFiltering( const bool enable ) { m_force_aniso = enable; }
// performance counters
unsigned Perf_GetNumDrawCalls() { return m_frameDrawCalls; }
unsigned Perf_GetNumStateChanges() { return m_frameStateChanges; }
unsigned Perf_GetNumTextureChanges() { return m_frameTextureChanges; }
inline void CreateVertexDeclaration( const VertexElement *element, VertexDeclaration **declaration )
{
m_pD3DDevice->CreateVertexDeclaration( element, declaration );
}
inline void SetVertexDeclaration( VertexDeclaration *declaration )
{
m_pD3DDevice->SetVertexDeclaration( declaration );
}
inline IDirect3DDevice9* GetCoreDevice()
{
return m_pD3DDevice;
}
private:
#ifdef USE_D3D9EX
IDirect3D9Ex* m_pD3D;
IDirect3DDevice9Ex* m_pD3DDevice;
#else
IDirect3D9* m_pD3D;
IDirect3DDevice9* m_pD3DDevice;
#endif
IDirect3DSurface9* m_pBackBuffer;
CComPtr<IndexBuffer> m_dynIndexBuffer; // workaround for DrawIndexedPrimitiveVB
UINT m_adapter; // index of the display adapter to use
static const DWORD RENDER_STATE_CACHE_SIZE=256;
static const DWORD TEXTURE_STATE_CACHE_SIZE=256;
DWORD renderStateCache[RENDER_STATE_CACHE_SIZE];
DWORD textureStateCache[8][TEXTURE_STATE_CACHE_SIZE];
BaseMaterial materialStateCache;
VertexBuffer* m_curVertexBuffer; // for caching
IndexBuffer* m_curIndexBuffer; // for caching
D3DTexture* m_curTexture[8]; // for caching
DWORD m_maxaniso;
bool m_mag_aniso;
bool m_autogen_mipmap;
bool m_force_aniso;
// performance counters
unsigned m_curDrawCalls, m_frameDrawCalls;
unsigned m_curStateChanges, m_frameStateChanges;
unsigned m_curTextureChanges, m_frameTextureChanges;
public:
TextureManager m_texMan;
};
class Shader
{
public:
Shader(RenderDevice *renderDevice);
~Shader();
bool Load( char* shaderName, const bool fromFile );
void Unload();
ID3DXEffect *Core()
{
return m_shader;
}
private:
ID3DXEffect* m_shader;
RenderDevice *m_renderDevice;
};