aboutsummaryrefslogtreecommitdiffstats
path: root/src/native/d3d/D3dCtx.hpp
blob: 230f0990fbe8a60cc5d0ce0e35cc236873b9a60a (plain)
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
/*
 * $RCSfile$
 *
 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
 *
 * Use is subject to license terms.
 *
 * $Revision$
 * $Date$
 * $State$
 */

#if !defined(D3DCTX_H)
#define D3DCTX_H


#include "StdAfx.h"
#include "D3dVertexBuffer.hpp"
#include "D3dDisplayList.hpp"

#define TEXTURETABLESIZE     8   
#define TEXSTAGESUPPORT      8
#define DISPLAYLIST_INITSIZE 8
#define NOCHANGE             0
#define RESETSURFACE         1
#define RECREATEDDRAW        2
#define RECREATEDFAIL       -1


// Use in texCoordPosition[]
// Must be negative number
#define TEX_GEN_NONE             0
#define TEX_EYE_LINEAR          -1
#define TEX_SPHERE_MAP          -2
#define TEX_NORMAL_MAP          -3
#define TEX_REFLECT_MAP         -4
#define TEX_OBJ_LINEAR          -5
#define TEX_GEN_INVALID         -6
#define TEX_GEN_AUTO            -7

typedef struct _D3DVERTEX {
    float x, y, z;
} D3DVERTEX;

typedef struct _D3DTLVERTEX {
    float sx, sy, sz, rhw;
    float tu, tv;
} D3DTLVERTEX;

typedef vector<LPDIRECT3DRESOURCE8> LPDIRECT3DRESOURCE8Vector;
typedef vector<LPDIRECT3DVERTEXBUFFER8> LPDIRECT3DVERTEXBUFFER8Vector;

class D3dCtx {
public:

    HWND   hwnd;                  // window handle
    HWND   topHwnd;               // Top window handle
    D3dDriverInfo  *driverInfo;   // Driver use
    D3dDeviceInfo  *deviceInfo;   // Device use 
    
    LPDIRECT3D8       pD3D;       // Direct3D interface
    LPDIRECT3DDEVICE8 pDevice;    // Instance of D3D Device
    
    LPDIRECT3DSURFACE8 depthStencilSurface;

    // This is used for readRaster and offscreen rendering
    // Only allocate the memory area if necessary
    LPDIRECT3DSURFACE8 frontSurface;

    LPDIRECT3DSURFACE8 backSurface;

    // Parameters use for CreateDevice()
    D3DPRESENT_PARAMETERS d3dPresent; 
    DWORD          dwBehavior;
         
    BOOL           offScreen; // true if it is offScreen rendering
                              // in this case only backSurface is used
    DWORD          offScreenWidth;
    DWORD          offScreenHeight;
    
    BOOL           bFullScreen;  // true if in full screen mode
    BOOL           bFullScreenRequired; // true if must run in full
                                        // screen mode or die
    BOOL           inToggle;     // in toggle fullscreen/window mode 
    RECT           screenRect;   // coordinate of window relative to
                                 // the whole desktop in multiple monitor
    RECT           windowRect; // coordinate of window relative to
                               // the current monitor desktop only 
    INT            minZDepth;  // min Z depth set in NativeConfigTemplate

    DEVMODE        devmode;     // current display mode
    DWORD          antialiasing; // PREFERRED, REQUIRED or UNNECESSARY

    
    // Store current color as in OGL glColor()
    float currentColor_r;
    float currentColor_g;    
    float currentColor_b;    
    float currentColor_a;    

    // Two side light is used. Note that D3D don't support two side
    // lighting.
    BOOL twoSideLightingEnable;

    // True if lighting is currently enable
    // Save the current RenderingState to avoid GetRenderState()
    // call during Rendering.
    BOOL isLightEnable;
    DWORD cullMode;
    DWORD fillMode;
    DWORD softwareVertexProcessing;
    DWORD zWriteEnable;
    DWORD zEnable;

    // Ambient material used when coloring Attributes
    D3DMATERIAL8 ambientMaterial;

    // temporary variables for ambient light setting
    D3DLIGHT8 savedLight;
    D3DMATERIAL8 savedMaterial;
    BOOL savedLightEnable;

    // temporary variables used for building VertexBuffer
    LPD3DVERTEXBUFFER pVB;     // point to the current VB being update
    DWORD  texSetUsed;  
    DWORD  texStride[TEXSTAGESUPPORT];

    // true when in toggle mode
    BOOL forceResize;

    // Texture related variables
    INT *bindTextureId;
    DWORD bindTextureIdLen;

    LPDIRECT3DTEXTURE8 *textureTable;
    DWORD textureTableLen;

    // Volume Texture related variables
    // Since 2d & 3d texture ID can't be the same from Java3D.
    // We don't need bindVolumeId
    LPDIRECT3DVOLUMETEXTURE8 *volumeTable;
    DWORD volumeTableLen;

    // Texture Cube Mapping related variables
    LPDIRECT3DCUBETEXTURE8 *cubeMapTable;
    DWORD cubeMapTableLen;
    
    // true if hardware support MultiTexture
    BOOL multiTextureSupport;

    // handle to monitor that this ctx belongs to. This is equal to
    // NULL if this window is a primary display screen or it covers
    // more than one screen. 
    HMONITOR monitor;

    // D3D don't have concept of current texture unit stage,
    // instead, the texture unit stage is pass in as argument
    // for all texture call.
    INT texUnitStage;

    // true if linear filtering is to be used
    BOOL texLinearMode;
    

    // This is used temporary to store the blend function
    // when two pass texture is used to simulate BLEND mode
    DWORD srcBlendFunc;
    DWORD dstBlendFunc;
    DWORD blendEnable;


    // This is used for to transform vertex 
    // from world to screen coordinate
    LPDIRECT3DVERTEXBUFFER8 srcVertexBuffer;
    LPDIRECT3DVERTEXBUFFER8 dstVertexBuffer;

    // For Rect of texture map in Raster write
    D3DTLVERTEX rasterRect[4];

    // Set automatic Texture coordinate generation type
    // TEX_xxx_xxx as defined in GeometryArrayRetained.cpp
    INT texGenMode[TEXSTAGESUPPORT];

    // Whether TEXTURE_COORDINATE_2/3/4 is used in this state
    INT texCoordFormat[TEXSTAGESUPPORT];

    // Whether texture transform matrix is set in this state or not
    BOOL texTransformSet[TEXSTAGESUPPORT];

    // Remember the last Texture Transform pass down, since
    // TexCoordGen may destroy it in some mode so we have to 
    // restore it later manually.
    D3DXMATRIX texTransform[TEXSTAGESUPPORT];

    // True if we copy m._41, m._42 elment to m._31, m._32
    // as a workaround that 2D texture translation did not work.
    BOOL texTranslateSet[TEXSTAGESUPPORT];

    float planeS[TEXSTAGESUPPORT][4];
    float planeT[TEXSTAGESUPPORT][4];
    float planeR[TEXSTAGESUPPORT][4];
    float planeQ[TEXSTAGESUPPORT][4];

    // Display List ID (start from 1) => VertexBuffer pointer table
    LPD3DDISPLAYLIST *displayListTable;
    int dlTableSize;

    // For immediate mode rendering, we save the vertexBuffer pointer
    // in variable pVertexBuffer of GeometryArrayRetained to reuse it.
    D3dVertexBuffer vertexBufferTable;

    int currDisplayListID;

    // True if colorTarget need to reset
    BOOL resetColorTarget;

    // Use for QuadArray
    LPDIRECT3DINDEXBUFFER8 quadIndexBuffer;
    DWORD quadIndexBufferSize;

    // Use for Quad Polygon Line mode
    LPDIRECT3DINDEXBUFFER8 lineModeIndexBuffer;

    // Use temporary for reindexing
    DWORD *reIndexifyTable;

    // True if Direcct Draw context is being destroy and recreate
    // again during resize/toggle
    BOOL recreateDDraw;

    // Screen coordinate of current monitor in use
    // When hardware accleerated mode is used. For Emulation mode
    // they are always zero;
    INT monitorLeft;
    INT monitorTop;
    float pointSize;

    // Use to free resource surface in swap()
    BOOL useFreeList0;
    LPDIRECT3DRESOURCE8Vector freeResourceList0;
    LPDIRECT3DRESOURCE8Vector freeResourceList1;
    D3dVertexBufferVector freeVBList0;
    D3dVertexBufferVector freeVBList1;

    D3dCtx(JNIEnv *env, jobject obj, HWND hwnd, BOOL offScreen, jint vid);
    ~D3dCtx();

    BOOL initialize(JNIEnv *env, jobject obj);
    INT resize(JNIEnv *env, jobject obj);
    VOID error(char *s, HRESULT hr);
    VOID error(int idx, HRESULT hr);
    VOID error(int idx);
    VOID error(char *s);
    VOID warning(int idx, HRESULT hr);
    VOID warning(int idx);

    static VOID d3dError(int idx);
    static VOID d3dError(char *s);
    static VOID d3dWarning(int idx, HRESULT hr);
    static VOID d3dWarning(int idx);

    INT  toggleMode(BOOL fullScreen, JNIEnv *env, jobject obj);
    DWORD getWidth();
    DWORD getHeight();

    VOID release();
    VOID releaseTexture();
    VOID releaseVB();
    VOID setViewport();
    VOID transform(D3DVERTEX *worldCoord, D3DTLVERTEX *screenCoord);
    VOID getScreenRect(HWND hwnd, RECT *rect);
    HMONITOR findMonitor();
    VOID setDriverInfo();
    static D3dDeviceInfo* setDeviceInfo(D3dDriverInfo *driverInfo,
					BOOL *bFullScreen,
					int minZDepth);
    DWORD findBehavior();
    VOID setPresentParams();
    INT resetSurface(JNIEnv *env, jobject obj);
    VOID setPresentParams(JNIEnv *env, jobject obj);
    VOID setAmbientLightMaterial();
    VOID restoreDefaultLightMaterial();
    VOID freeResource(LPDIRECT3DRESOURCE8 surf);
    VOID freeVB(LPD3DVERTEXBUFFER vb);
    
    VOID freeList();
    VOID freeResourceList(LPDIRECT3DRESOURCE8Vector *v);
    VOID freeVBList(D3dVertexBufferVector *v); 
    BOOL createFrontBuffer();
 
    static D3dDeviceInfo* selectDevice(int deviceID,
				       D3dDriverInfo *driverInfo,
				       BOOL *bFullScreen,
				       int minZDepth);
    static D3dDeviceInfo* selectBestDevice(D3dDriverInfo *driverInfo,
					   BOOL *bFullScreen,
					   int minZDepth);
    static VOID setDeviceFromProperty(JNIEnv *env);
    static VOID setDebugProperty(JNIEnv *env);
    static VOID setVBLimitProperty(JNIEnv *env);
    static VOID setImplicitMultisamplingProperty(JNIEnv *env);    

private:

    RECT savedTopRect;        // for toggle between fullscreen mode
    RECT savedClientRect;  
    DWORD winStyle;

    VOID createVertexBuffer();

    VOID setCanvasProperty(JNIEnv *env, jobject obj);
    VOID setFullScreenFromProperty(JNIEnv *env);
    VOID enumDisplayMode(DEVMODE *devmode);

    static VOID printWarningMessage(D3dDeviceInfo *deviceInfo);
    static VOID showError(HWND hwnd, char *s, BOOL bFullScreen);
    VOID setDefaultAttributes();
    VOID printInfo(D3DPRESENT_PARAMETERS *d3dPresent); 
    VOID setWindowMode();
};

typedef vector<D3dCtx *> D3dCtxVector;
extern D3dCtxVector d3dCtxList;
const extern D3DXMATRIX identityMatrix;
#endif