tnl: Replace deprecated TexCoordPtr with AttribPtr[_TNL_ATTRIB_TEX*]
[mesa.git] / src / mesa / drivers / windows / gldirect / dx9 / gld_dx9.h
1 /****************************************************************************
2 *
3 * Mesa 3-D graphics library
4 * Direct3D Driver Interface
5 *
6 * ========================================================================
7 *
8 * Copyright (C) 1991-2004 SciTech Software, Inc. All rights reserved.
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * SCITECH SOFTWARE INC BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
25 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 *
28 * ======================================================================
29 *
30 * Language: ANSI C
31 * Environment: Windows 9x/2000/XP/XBox (Win32)
32 *
33 * Description: GLDirect Direct3D 9.0 header file
34 *
35 ****************************************************************************/
36
37 #ifndef _GLD_DX9_H
38 #define _GLD_DX9_H
39
40 //---------------------------------------------------------------------------
41 // Windows includes
42 //---------------------------------------------------------------------------
43
44 //#ifndef STRICT
45 //#define STRICT
46 //#endif
47
48 //#define WIN32_LEAN_AND_MEAN
49 //#include <windows.h>
50 #include <d3d9.h>
51 #include <d3dx9.h>
52
53 // MS screwed up with the DX8.1 SDK - there's no compile-time
54 // method of compiling for 8.0 via the 8.1 SDK unless you
55 // "make sure you don't use any 8.1 interfaces".
56 // We CAN use 8.1 D3DX static functions, though - just not new 8.1 interfaces.
57 //
58 // D3D_SDK_VERSION is 120 for 8.0 (supported by Windows 95).
59 // D3D_SDK_VERSION is 220 for 8.1 (NOT supported by Windows 95).
60 //
61 //#define D3D_SDK_VERSION_DX9_SUPPORT_WIN95 120
62 //#define D3D_SDK_VERSION_DX91 220
63
64 // Typedef for obtaining function from d3d8.dll
65 typedef IDirect3D9* (WINAPI *FNDIRECT3DCREATE9) (UINT);
66
67
68 //---------------------------------------------------------------------------
69 // Defines
70 //---------------------------------------------------------------------------
71
72 #ifdef _DEBUG
73 #define _GLD_TEST_HRESULT(h) \
74 { \
75 HRESULT _hr = (h); \
76 if (FAILED(_hr)) { \
77 gldLogError(GLDLOG_ERROR, #h, _hr); \
78 } \
79 }
80 #define _GLD_DX9(func) _GLD_TEST_HRESULT(IDirect3D9_##func##)
81 #define _GLD_DX9_DEV(func) _GLD_TEST_HRESULT(IDirect3DDevice9_##func##)
82 #define _GLD_DX9_VB(func) _GLD_TEST_HRESULT(IDirect3DVertexBuffer9_##func##)
83 #define _GLD_DX9_TEX(func) _GLD_TEST_HRESULT(IDirect3DTexture9_##func##)
84 #else
85 #define _GLD_DX9(func) IDirect3D9_##func
86 #define _GLD_DX9_DEV(func) IDirect3DDevice9_##func
87 #define _GLD_DX9_VB(func) IDirect3DVertexBuffer9_##func
88 #define _GLD_DX9_TEX(func) IDirect3DTexture9_##func
89 #endif
90
91 #define SAFE_RELEASE(p) \
92 { \
93 if (p) { \
94 (p)->lpVtbl->Release(p); \
95 (p) = NULL; \
96 } \
97 }
98
99 #define SAFE_RELEASE_VB9(p) \
100 { \
101 if (p) { \
102 IDirect3DVertexBuffer9_Release((p)); \
103 (p) = NULL; \
104 } \
105 }
106
107 #define SAFE_RELEASE_SURFACE9(p) \
108 { \
109 if (p) { \
110 IDirect3DSurface9_Release((p)); \
111 (p) = NULL; \
112 } \
113 }
114
115 // Setup index.
116 enum {
117 GLD_SI_FLAT = 0,
118 GLD_SI_SMOOTH = 1,
119 GLD_SI_FLAT_EXTRAS = 2,
120 GLD_SI_SMOOTH_EXTRAS = 3,
121 };
122 /*
123 // Internal pipeline
124 typedef enum {
125 GLD_PIPELINE_MESA = 0, // Mesa pipeline
126 GLD_PIPELINE_D3D_FVF = 1, // Direct3D Fixed-function pipeline
127 GLD_PIPELINE_D3D_VS_TWOSIDE = 2 // Direct3D two-sided-lighting vertex shader
128 } GLD_tnl_pipeline;
129 */
130 //---------------------------------------------------------------------------
131 // Vertex definitions for Fixed-Function pipeline
132 //---------------------------------------------------------------------------
133
134 //
135 // NOTE: If the number of texture units is altered then most of
136 // the texture code will need to be revised.
137 //
138
139 #define GLD_MAX_TEXTURE_UNITS_DX9 2
140
141 //
142 // 2D vertex transformed by Mesa
143 //
144 #define GLD_FVF_2D_VERTEX ( D3DFVF_XYZRHW | \
145 D3DFVF_DIFFUSE | \
146 D3DFVF_SPECULAR | \
147 D3DFVF_TEX2)
148 typedef struct {
149 FLOAT x, y; // 2D raster coords
150 FLOAT sz; // Screen Z (depth)
151 FLOAT rhw; // Reciprocal homogenous W
152 DWORD diffuse; // Diffuse colour
153 DWORD specular; // For separate-specular support
154 FLOAT t0_u, t0_v; // 1st set of texture coords
155 FLOAT t1_u, t1_v; // 2nd set of texture coords
156 } GLD_2D_VERTEX;
157
158
159 //
160 // 3D vertex transformed by Direct3D
161 //
162 #define GLD_FVF_3D_VERTEX ( D3DFVF_XYZ | \
163 D3DFVF_DIFFUSE | \
164 D3DFVF_TEX2)
165
166 typedef struct {
167 D3DXVECTOR3 Position; // XYZ Vector in object space
168 D3DCOLOR Diffuse; // Diffuse colour
169 D3DXVECTOR2 TexUnit0; // Texture unit 0
170 D3DXVECTOR2 TexUnit1; // Texture unit 1
171 } GLD_3D_VERTEX;
172
173 //---------------------------------------------------------------------------
174 // Vertex Shaders
175 //---------------------------------------------------------------------------
176 /*
177 // DX8 Vertex Shader
178 typedef struct {
179 DWORD hShader; // If NULL, shader is invalid and cannot be used
180 BOOL bHardware; // If TRUE then shader was created for hardware,
181 // otherwise shader was created for software.
182 } GLD_vertexShader;
183 */
184 //---------------------------------------------------------------------------
185 // Structs
186 //---------------------------------------------------------------------------
187
188 // This keeps a count of how many times we choose each individual internal
189 // pathway. Useful for seeing if a certain pathway was ever used by an app, and
190 // how much each pathway is biased.
191 // Zero the members at context creation and dump stats at context deletion.
192 typedef struct {
193 // Note: DWORD is probably too small
194 ULARGE_INTEGER qwMesa; // Mesa TnL pipeline
195 ULARGE_INTEGER qwD3DFVF; // Direct3D Fixed-Function pipeline
196 // ULARGE_INTEGER dwD3D2SVS; // Direct3D Two-Sided Vertex Shader pipeline
197 } GLD_pipeline_usage;
198
199 // GLDirect Primitive Buffer (points, lines, triangles and quads)
200 typedef struct {
201 // Data for IDirect3DDevice9::CreateVertexBuffer()
202 DWORD dwStride; // Stride of vertex
203 DWORD dwUsage; // Usage flags
204 DWORD dwFVF; // Direct3D Flexible Vertex Format
205 DWORD dwPool; // Pool flags
206
207 IDirect3DVertexBuffer9 *pVB; // Holds points, lines, tris and quads.
208
209 // Point list is assumed to be at start of buffer
210 DWORD iFirstLine; // Index of start of line list
211 DWORD iFirstTriangle; // Index of start of triangle list
212
213 BYTE *pPoints; // Pointer to next free point
214 BYTE *pLines; // Pointer to next free line
215 BYTE *pTriangles; // Pointer to next free triangle
216
217 DWORD nPoints; // Number of points ready to render
218 DWORD nLines; // Number of lines ready to render
219 DWORD nTriangles; // Number of triangles ready to render
220 } GLD_pb_dx9;
221
222 // GLDirect DX9 driver data
223 typedef struct {
224 // GLDirect vars
225 BOOL bDoublebuffer; // Doublebuffer (otherwise single-buffered)
226 BOOL bDepthStencil; // Depth buffer needed (stencil optional)
227 D3DFORMAT RenderFormat; // Format of back/front buffer
228 D3DFORMAT DepthFormat; // Format of depth/stencil
229 // float fFlipWindowY; // Value for flipping viewport Y coord
230
231 // Direct3D vars
232 D3DCAPS9 d3dCaps9;
233 BOOL bHasHWTnL; // Device has Hardware Transform/Light?
234 IDirect3D9 *pD3D; // Base Direct3D9 interface
235 IDirect3DDevice9 *pDev; // Direct3D9 Device interface
236 GLD_pb_dx9 PB2d; // Vertices transformed by Mesa
237 GLD_pb_dx9 PB3d; // Vertices transformed by Direct3D
238 D3DPRIMITIVETYPE d3dpt; // Current Direct3D primitive type
239 D3DXMATRIX matProjection; // Projection matrix for D3D TnL
240 D3DXMATRIX matModelView; // Model/View matrix for D3D TnL
241 int iSetupFunc; // Which setup functions to use
242 BOOL bUseMesaTnL; // Whether to use Mesa or D3D for TnL
243
244 // Direct3D vars for two-sided lighting
245 // GLD_vertexShader VStwosidelight; // Vertex Shader for two-sided lighting
246 // D3DXMATRIX matWorldViewProj;// World/View/Projection matrix for shaders
247
248
249 // GLD_tnl_pipeline TnLPipeline; // Index of current internal pipeline
250 GLD_pipeline_usage PipelineUsage;
251
252 BOOL bCanScissor; // Scissor test - new for DX9
253 } GLD_driver_dx9;
254
255 #define GLD_GET_DX9_DRIVER(c) (GLD_driver_dx9*)(c)->glPriv
256
257 //---------------------------------------------------------------------------
258 // Function prototypes
259 //---------------------------------------------------------------------------
260
261 PROC gldGetProcAddress_DX9(LPCSTR a);
262 void gldEnableExtensions_DX9(GLcontext *ctx);
263 void gldInstallPipeline_DX9(GLcontext *ctx);
264 void gldSetupDriverPointers_DX9(GLcontext *ctx);
265 //void gldResizeBuffers_DX9(GLcontext *ctx);
266 void gldResizeBuffers_DX9(GLframebuffer *fb);
267
268
269 // Texture functions
270
271 void gldCopyTexImage1D_DX9(GLcontext *ctx, GLenum target, GLint level, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLint border);
272 void gldCopyTexImage2D_DX9(GLcontext *ctx, GLenum target, GLint level, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border);
273 void gldCopyTexSubImage1D_DX9(GLcontext *ctx, GLenum target, GLint level, GLint xoffset, GLint x, GLint y, GLsizei width );
274 void gldCopyTexSubImage2D_DX9(GLcontext *ctx, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height );
275 void gldCopyTexSubImage3D_DX9(GLcontext *ctx, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height );
276
277 void gld_NEW_TEXTURE_DX9(GLcontext *ctx);
278 void gld_DrawPixels_DX9(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels);
279 void gld_ReadPixels_DX9(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, const struct gl_pixelstore_attrib *unpack, GLvoid *dest);
280 void gld_CopyPixels_DX9(GLcontext *ctx, GLint srcx, GLint srcy, GLsizei width, GLsizei height, GLint dstx, GLint dsty, GLenum type);
281 void gld_Bitmap_DX9(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height, const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap);
282 const struct gl_texture_format* gld_ChooseTextureFormat_DX9(GLcontext *ctx, GLint internalFormat, GLenum srcFormat, GLenum srcType);
283 void gld_TexImage2D_DX9(GLcontext *ctx, GLenum target, GLint level, GLint internalFormat, GLint width, GLint height, GLint border, GLenum format, GLenum type, const GLvoid *pixels, const struct gl_pixelstore_attrib *packing, struct gl_texture_object *tObj, struct gl_texture_image *texImage);
284 void gld_TexImage1D_DX9(GLcontext *ctx, GLenum target, GLint level, GLint internalFormat, GLint width, GLint border, GLenum format, GLenum type, const GLvoid *pixels, const struct gl_pixelstore_attrib *packing, struct gl_texture_object *texObj, struct gl_texture_image *texImage );
285 void gld_TexSubImage2D_DX9( GLcontext *ctx, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels, const struct gl_pixelstore_attrib *packing, struct gl_texture_object *texObj, struct gl_texture_image *texImage );
286 void gld_TexSubImage1D_DX9(GLcontext *ctx, GLenum target, GLint level, GLint xoffset, GLsizei width, GLenum format, GLenum type, const GLvoid *pixels, const struct gl_pixelstore_attrib *packing, struct gl_texture_object *texObj, struct gl_texture_image *texImage);
287 void gld_DeleteTexture_DX9(GLcontext *ctx, struct gl_texture_object *tObj);
288 void gld_ResetLineStipple_DX9(GLcontext *ctx);
289
290 // 2D primitive functions
291
292 void gld_Points2D_DX9(GLcontext *ctx, GLuint first, GLuint last);
293
294 void gld_Line2DFlat_DX9(GLcontext *ctx, GLuint v0, GLuint v1);
295 void gld_Line2DSmooth_DX9(GLcontext *ctx, GLuint v0, GLuint v1);
296
297 void gld_Triangle2DFlat_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2);
298 void gld_Triangle2DSmooth_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2);
299 void gld_Triangle2DFlatExtras_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2);
300 void gld_Triangle2DSmoothExtras_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2);
301
302 void gld_Quad2DFlat_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint v3);
303 void gld_Quad2DSmooth_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint v3);
304 void gld_Quad2DFlatExtras_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint v3);
305 void gld_Quad2DSmoothExtras_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint v3);
306
307 // 3D primitive functions
308
309 void gld_Points3D_DX9(GLcontext *ctx, GLuint first, GLuint last);
310 void gld_Line3DFlat_DX9(GLcontext *ctx, GLuint v0, GLuint v1);
311 void gld_Triangle3DFlat_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2);
312 void gld_Quad3DFlat_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint v3);
313 void gld_Line3DSmooth_DX9(GLcontext *ctx, GLuint v0, GLuint v1);
314 void gld_Triangle3DSmooth_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2);
315 void gld_Quad3DSmooth_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint v3);
316
317 // Primitive functions for Two-sided-lighting Vertex Shader
318
319 void gld_Points2DTwoside_DX9(GLcontext *ctx, GLuint first, GLuint last);
320 void gld_Line2DFlatTwoside_DX9(GLcontext *ctx, GLuint v0, GLuint v1);
321 void gld_Line2DSmoothTwoside_DX9(GLcontext *ctx, GLuint v0, GLuint v1);
322 void gld_Triangle2DFlatTwoside_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2);
323 void gld_Triangle2DSmoothTwoside_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2);
324 void gld_Quad2DFlatTwoside_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint v3);
325 void gld_Quad2DSmoothTwoside_DX9(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint v3);
326
327 #endif