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

#if !defined(D3DUTIL_H)
#define D3DUTIL_H

#include "StdAfx.h"


// Index to D3dErrorMessage
#define DRIVERNOTFOUND          0
#define CANNOTRENDERWINDOW      1
#define D3DNOTFOUND             2
#define CARDNOTSUPPORT          3
#define NEEDSWITCHMODE          4
#define DEVICENOTFOUND          5
#define CREATEDEVICEFAIL        6
#define CREATEREFDEVICEFAIL     7
#define VIEWPORTFAIL            8
#define GETBACKBUFFERFAIL       9
#define COLORMODENOTSUPPORT     10
#define OUTOFMEMORY             11
#define UNKNOWNDEVICE           12
#define HALDEVICENOTFOUND       13
#define TNLHALDEVICENOTFOUND    14
#define NOSTENCILBUFFER         15
#define NOTEXTUREMEMORY         16
#define OFFSCREENCREATEFAIL     17
#define CREATEVERTEXBUFFER      18
#define RESETFAIL               19
#define HALNOTCOMPATIBLE        20
#define DEPTHSTENCILNOTFOUND    21
#define LOCKVBFAIL              22
#define CREATEVBFAIL            23
#define CREATEINDEXVBFAIL       24
#define LOCKINDEXVBFAIL         25

#define PI  3.14159265f

// Same definition as in ImageComponent2DRetained
#define CLEAN   0
#define MODIFY  1
#define NOTLIVE 2

// If we want to synchrinized draw primitive, change the 
// following to D3DDP_WAIT
#define DEFAULTMODE 0

#define D3D_STEREO 1


// Macro
#define SafeRelease(p) if(p) {p->Release(); p = NULL;}
#define SafeDelete(p)  if(p) { delete p; p = NULL;}
#define SafeFree(p)    if(p) { free(p); p = NULL;}

#define GetCtx() \
         if (ctx == 0) return; \
         D3dCtx *d3dCtx = reinterpret_cast<D3dCtx*>(ctx); \

         
#define GetDevice()  \
         GetCtx() \
         LPDIRECT3DDEVICE9 device = d3dCtx->pDevice; \
         if (device == NULL)  return;

#define GetCtx2() \
         if (ctx == 0) return 0;\
         D3dCtx *d3dCtx = reinterpret_cast<D3dCtx*>(ctx); \

         
#define GetDevice2()  \
         GetCtx2(); \
         LPDIRECT3DDEVICE9 device = d3dCtx->pDevice; \
         if (device == NULL)  return 0;

#define CopyColor(c, red, green, blue, alpha)  \
               (c).a = alpha; (c).r = red; (c).g = green; (c).b = blue;


#define CopyTranspose(m, s) { \
      (m)._11 = (s)[0]; (m)._12 = (s)[4]; (m)._13 = (s)[8];  (m)._14 = (s)[12]; \
      (m)._21 = (s)[1]; (m)._22 = (s)[5]; (m)._23 = (s)[9];  (m)._24 = (s)[13]; \
      (m)._31 = (s)[2]; (m)._32 = (s)[6]; (m)._33 = (s)[10]; (m)._34 = (s)[14]; \
      (m)._41 = (s)[3]; (m)._42 = (s)[7]; (m)._43 = (s)[11]; (m)._44 = (s)[15]; }

// Note that v should not be one of the reference in m
#define MultiplyScaler(m, v) { \
     (m)._11 *= v;  (m)._12 *= v; (m)._13 *= v;  (m)._14 *= v; \
     (m)._21 *= v;  (m)._22 *= v; (m)._23 *= v;  (m)._24 *= v; \
     (m)._31 *= v;  (m)._32 *= v; (m)._33 *= v;  (m)._34 *= v; \
     (m)._41 *= v;  (m)._42 *= v; (m)._43 *= v;  (m)._44 *= v; }

#define Clamp(c) \
         if (c > 1.0f) { \
              c = 1.0f; \
         } else if (c < 0.0f) { \
              c = 0.0f; \
         }

#define Magitude(x, y, z, w) sqrt((x)*(x) + (y)*(y) + (z)*(z) + (w)*(w));

#define NORMALIZE(x, y, z, w) { \
    float d; \
    d = Magitude(x, y, z, w); \
    (x) /= d; (y) /= d;  (z) /= d;  (w) /= d; }

extern vector<void *> freePointerList0;
extern vector<void *> freePointerList1;
extern BOOL useFreePointerList0;

//ISSUE 135 a iterator to void *
typedef vector<void *>::iterator ITER_VOID;

extern HANDLE hSema; // handle to semaphore
extern BOOL firstError;
extern BOOL debug;

// use for VertexBuffer
extern OSVERSIONINFO osvi; // OS info

extern D3dCtx* findCtx(HWND hwnd);
extern VOID lock();
extern VOID unlock();
extern VOID lockGeometry();
extern VOID unlockGeometry();
extern VOID lockSurfaceList();
extern VOID unlockSurfaceList();
extern VOID freeSurface(LPDIRECT3DBASETEXTURE9 surf);
extern VOID freePointer(void* surf);
extern VOID freePointerList();
extern char* getErrorMessage(int idx);
extern HWND getTopWindow(HWND hwnd);

extern LPDIRECT3DTEXTURE9 createTextureSurface(D3dCtx *d3dCtx,
					       jint numLevels,
					       jint internalFormat,
					       jint width, 
					       jint height,
					       jboolean useAutoMipMap);


extern LPDIRECT3DVOLUMETEXTURE9 createVolumeTexture(D3dCtx *d3dCtx,
						    jint numLevels,
						    jint internalFormat,
						    jint width, 
						    jint height,
						    jint depth);


extern LPDIRECT3DCUBETEXTURE9 createCubeMapTexture(D3dCtx *d3dCtx,
						   jint numLevels,
						   jint internalFormat,
						   jint width, 
						   jint height);


extern void copyDataToSurface(jint format, 
			      jint internalFormat,
			      jint xoffset, jint yoffset,
			      jint imgXOffset, jint imgYOffset,
			      jint width, jint height,  jint tilew,
			      jshort *data, LPDIRECT3DTEXTURE9 surf,
			      jint level);

extern void copyDataToSurface(jint format,
			      jint internalFormat,
			      jint xoffset, jint yoffset,
			      jint imgXOffset, jint imgYOffset,
			      jint width, jint height, jint tilew,
			      jbyte* data, 
			      LPDIRECT3DTEXTURE9 surf,
			      jint level);

extern void copyDataToVolume(jint format, 
			     jint internalFormat,
			     jint xoffset, jint yoffset,
			     jint zoffset,
			     jint imgXOffset, jint imgYOffset,
			     jint imgZOffset,
			     jint width, jint height,  jint depth,
			     jint tilew, jint tileh,
			     jshort *data, LPDIRECT3DVOLUMETEXTURE9 surf,
			     jint level);


extern void copyDataToVolume(jint format,
			     jint internalFormat,
			     jint xoffset, jint yoffset,
			     jint zoffset,
			     jint imgXOffset, jint imgYOffset,
			     jint imgZOffset,
			     jint width, jint height, jint depth,
			     jint tilew, jint tileh,
			     jbyte* data, 
			     LPDIRECT3DVOLUMETEXTURE9 surf,
			     jint level);

extern void copyDataToCubeMap(jint format, 
			      jint internalFormat,
			      jint xoffset, jint yoffset,
			      jint imgXOffset, jint imgYOffset,
			      jint width, jint height,
			      jint tilew, 
			      jshort *data, LPDIRECT3DCUBETEXTURE9 surf,
			      jint level,
			      jint face);


extern void copyDataToCubeMap(jint format,
			      jint internalFormat,
			      jint xoffset, jint yoffset,
			      jint imgXOffset, jint imgYOffset,
			      jint width, jint height,
			      jint tilew, 
			      jbyte* data, 
			      LPDIRECT3DCUBETEXTURE9 surf,
			      jint level,
			      jint face);


extern void copyDepthFromSurface(jint xoffset, jint yoffset,
				 jint subWidth, jint subHeight,
				 jint *data, 
				 LPDIRECT3DSURFACE9 surf);

extern void copyDepthFromSurface(jint xoffset, jint yoffset,
				 jint subWidth, jint subHeight,
				 jfloat *data, 
				 LPDIRECT3DSURFACE9 surf);

extern void copyDepthToSurface(D3dCtx *d3dCtx,
			       LPDIRECT3DDEVICE9 device,
			       jint dst_xoffset, jint dst_yoffset,
			       jint src_xoffset, jint src_yoffset,
			       jint subWidth, jint subHeight,
			       jint src_width, jint src_height,
			       jint *data, 
			       LPDIRECT3DSURFACE9 surf);

extern void copyDepthToSurface(D3dCtx *d3dCtx,
			       LPDIRECT3DDEVICE9 device,
			       jint dst_xoffset, jint dst_yoffset,
			       jint src_xoffset, jint src_yoffset,
			       jint subWidth, jint subHeight,
			       jint src_width, jint src_height,
			       jfloat *data, 
			       LPDIRECT3DSURFACE9 surf);

extern void copyDataFromSurface(jint internalFormat,
				jint xoffset, jint yoffset,
				jint width, jint height, 
				jbyte *data, LPDIRECT3DSURFACE9 surf);

void compositeDataToSurface(jint px, jint py,
			    jint xoffset, jint yoffset,
			    jint subWidth, jint subHeight,
			    jint dataWidth, 
			    jbyte *data, 
			    LPDIRECT3DSURFACE9 surf);

// extern BOOL isIdentity(jdouble *matrix);

extern void CopyTextureStage(LPDIRECT3DDEVICE9 device, 
			     int fromLevel, int toLevel);

extern "C"
DWORD countBits(DWORD mask); // Define in MasterControl.c

extern void throwAssert(JNIEnv *env, char *str);
extern BOOL createQuadIndices(D3dCtx *d3dCtx, int vcount);
extern VOID createLineModeIndexBuffer(D3dCtx *d3dCtx);
extern char *getPixelFormatName(D3DFORMAT format);
extern char *getMultiSampleName(D3DMULTISAMPLE_TYPE mtype); 
extern char *getSwapEffectName(D3DSWAPEFFECT swapEffect);
extern int getPrimitiveNum(int primitive, int vcount);
extern int getMaxNumVertex(int primitive, int vcount);
extern void drawTextureRect(D3dCtx *d3dCtx,
			    LPDIRECT3DDEVICE9 device,
			    LPDIRECT3DTEXTURE9 tex,
			    D3DTLVERTEX screenCoord,
			    int startx, int starty,
			    int endx, int endy,
			    int scaleWidth, int scaleHeight,
			    boolean texModeRepeat);
extern int  setTextureStage(D3dCtx *d3dCtx,
			    LPDIRECT3DDEVICE9 device, 
			    int mapTexStage,
			    jint texStage);
extern void setTexTransformStageFlag(D3dCtx* d3dCtx,  
				     LPDIRECT3DDEVICE9 device, 
				     int tus, int ts, int genMode); 
DWORD ucountBits(DWORD mask) ;
#endif