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

#include "Stdafx.h"

// return true if device is capable of hardware accelerated

D3dDeviceInfo::D3dDeviceInfo()
{
}

D3dDeviceInfo::~D3dDeviceInfo()
{
}

VOID D3dDeviceInfo::setCaps(D3DCAPS9 *d3dCaps) {
    
    BOOL supportNPOT;

    if (deviceType == D3DDEVTYPE_HAL ){
        isHardware = true;
        isHardwareTnL = (d3dCaps->DevCaps &  D3DDEVCAPS_HWTRANSFORMANDLIGHT);
    }
    else // D3DDEVTYPE_REF
    {
        isHardware = false;
        isHardwareTnL = false;
    }
    
    
    // D3DTEXTURECAPS_NONPOW2CONDITIONAL caps-bit indicates "conditional"
    // Non Power of Two (NPOT)
    // textures that only support CLAMP addressing and don't
    // support mipmaps or compressed textures.
    // But some new vcards supports NP2 unconditional (GF6 and above).
    // Correct test for any kind of NP2 support:
    // If both unconditional and conditional support is
    // unavailable then NP2 is not possible anyway.
    //  -------------------------------------------
    //  POW2 |  NP2_CONDITIONAL | result
    //  -------------------------------------------
    //  true |      true        | CONDITIONAL NPOT(*)
    //  true |      false       | POW2 Only
    //  false|      any         | UNConditional NPOT (**)	//
    // ---------------------------------------------
    // (**)OpenGL like,  Java3D preferred.
    // (*) below test:
    /*
     * if (((d3dCaps->TextureCaps & D3DPTEXTURECAPS_POW2) != 0) &&  // POW2 is true
     *     ((d3dCaps->TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) == 0)){ //NPOT_Cond is false
     *      //Pow2 Only
     *      supportNPOT = false;
     *   }
     * else{
     *      // both conditional and unconditional
     *      supportNPOT = true;
     * }
     */
    if(d3dCaps->TextureCaps & D3DPTEXTURECAPS_POW2){
        supportNPOT = false;
        if(d3dCaps->TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL){
            // NPOT conditionl But, in certain cases textures can ignore the power of 2 limitation
            // As OpenGL is UNCONDITIONAL, it is not used by Java3D
            //supportNPOT = true;
        }
    } else {
        //UNconditional: Textures do not need to be a power of 2 in size
        supportNPOT = true;
    } 
    
    // check if it supports at least vertex shader 1.1
    if(d3dCaps->VertexShaderVersion < D3DVS_VERSION(1, 1)) {
        supportShaders11 = false;
    }
    else {
        supportShaders11 = true;
    }
    DWORD vsVersion = d3dCaps->VertexShaderVersion;
    if (debug) {
        char* dt;
        if (isHardware)
            dt = "HAL";
        else
            dt ="REL";
        
        printf("Java3D: Supported Shaders = %d.%d in mode %s ",
        HIBYTE(LOWORD(vsVersion)),
        LOBYTE(LOWORD(vsVersion)),
        dt);        
    }
    
    //supportStreamOffset =
    
    supportDepthBias = (d3dCaps->RasterCaps & D3DPRASTERCAPS_DEPTHBIAS) != 0;
    
    maxTextureBlendStages = d3dCaps->MaxTextureBlendStages;
    maxSimultaneousTextures = d3dCaps->MaxSimultaneousTextures;
    
    maxTextureUnitStageSupport = min(maxTextureBlendStages,  maxSimultaneousTextures);
    
    supportMipmap = ((d3dCaps->TextureCaps & D3DPTEXTURECAPS_MIPMAP) != 0);
    
    texturePow2Only =  ((d3dCaps->TextureCaps &  D3DPTEXTURECAPS_POW2) != 0);
    
    textureSquareOnly = ((d3dCaps->TextureCaps &   D3DPTEXTURECAPS_SQUAREONLY) != 0);
    
    linePatternSupport = false; //((d3dCaps->PrimitiveMiscCaps &   D3DPMISCCAPS_LINEPATTERNREP) != 0);
    
    texBorderModeSupport = ((d3dCaps->TextureAddressCaps & D3DPTADDRESSCAPS_BORDER) != 0);
    
    texLerpSupport = ((d3dCaps->TextureOpCaps &    D3DTEXOPCAPS_LERP) != 0);

    canRenderWindowed = true;//((d3dCaps->Caps2 &  D3DCAPS2_CANRENDERWINDOWED) != 0);
    
    maxPrimitiveCount = d3dCaps->MaxPrimitiveCount;
    maxVertexIndex = min(vertexBufferMaxVertexLimit, d3dCaps->MaxVertexIndex);
    
    maxTextureHeight = d3dCaps->MaxTextureHeight;
    maxTextureWidth =  d3dCaps->MaxTextureWidth;
    maxTextureDepth =  d3dCaps->MaxVolumeExtent;
    
    maxActiveLights = d3dCaps->MaxActiveLights;
    maxPointSize = DWORD(d3dCaps->MaxPointSize);
    maxAnisotropy = d3dCaps->MaxAnisotropy;
    
    maxVertexCount[GEO_TYPE_QUAD_SET] = min(vertexBufferMaxVertexLimit,
    maxPrimitiveCount << 1);
    
    // Since index is used, we need to make sure than index range
    // is also support.
    maxVertexCount[GEO_TYPE_QUAD_SET] = min(maxVertexCount[GEO_TYPE_QUAD_SET],   maxVertexIndex);
    
    maxVertexCount[GEO_TYPE_TRI_SET] = min(vertexBufferMaxVertexLimit,   maxPrimitiveCount*3);
    
    maxVertexCount[GEO_TYPE_POINT_SET] = min(vertexBufferMaxVertexLimit,   maxPrimitiveCount);
    
    maxVertexCount[GEO_TYPE_LINE_SET] = min(vertexBufferMaxVertexLimit, maxPrimitiveCount << 1);
    
    maxVertexCount[GEO_TYPE_TRI_STRIP_SET] = min(vertexBufferMaxVertexLimit, maxPrimitiveCount + 2);
    
    maxVertexCount[GEO_TYPE_TRI_FAN_SET] = min(vertexBufferMaxVertexLimit,	 maxPrimitiveCount + 2);
    
    maxVertexCount[GEO_TYPE_LINE_STRIP_SET] = min(vertexBufferMaxVertexLimit,
    maxPrimitiveCount +1);
    maxVertexCount[GEO_TYPE_INDEXED_QUAD_SET] = maxVertexCount[GEO_TYPE_QUAD_SET];
    maxVertexCount[GEO_TYPE_INDEXED_TRI_SET] = maxVertexCount[GEO_TYPE_TRI_SET];
    maxVertexCount[GEO_TYPE_INDEXED_POINT_SET] = maxVertexCount[GEO_TYPE_POINT_SET];
    maxVertexCount[GEO_TYPE_INDEXED_LINE_SET] = maxVertexCount[GEO_TYPE_LINE_SET];
    maxVertexCount[GEO_TYPE_INDEXED_TRI_STRIP_SET] = maxVertexCount[GEO_TYPE_TRI_STRIP_SET];
    maxVertexCount[GEO_TYPE_INDEXED_TRI_FAN_SET] = maxVertexCount[GEO_TYPE_TRI_FAN_SET];
    maxVertexCount[GEO_TYPE_INDEXED_LINE_STRIP_SET] = maxVertexCount[GEO_TYPE_LINE_STRIP_SET];
    
    if ( (d3dCaps->PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) != 0)
        supportRasterPresImmediate = true;
    else
        supportRasterPresImmediate = false;
    
    if (((d3dCaps->RasterCaps & D3DPRASTERCAPS_FOGTABLE) != 0) &&
	((d3dCaps->RasterCaps & D3DPRASTERCAPS_WFOG) != 0)) {
        // use pixel w-fog
        fogMode = D3DRS_FOGTABLEMODE;
        rangeFogEnable = false;
    }
    else if (((d3dCaps->RasterCaps & D3DPRASTERCAPS_FOGVERTEX) != 0) &&
	     ((d3dCaps->RasterCaps & D3DPRASTERCAPS_FOGRANGE) != 0)) {
	// use vertex range based fog
	fogMode = D3DRS_FOGVERTEXMODE;
	rangeFogEnable = true;
    }
    else if ((d3dCaps->RasterCaps & D3DPRASTERCAPS_FOGTABLE) != 0) {
	// use pixel z-fog
	fogMode = D3DRS_FOGTABLEMODE;
	rangeFogEnable = false;
    }
    else if (D3DPRASTERCAPS_FOGVERTEX) {
	// use vertex z-fog
	fogMode = D3DRS_FOGVERTEXMODE;
	rangeFogEnable = false;
    }
    else {
	if (debug) {
	    printf("[Java 3D] Fog not support in this device !\n");
	}
    } 
    
    texMask = 0;

    if(supportNPOT){
        texMask |= javax_media_j3d_Canvas3D_TEXTURE_NON_POWER_OF_TWO;
    }

    if((d3dCaps->Caps2 & D3DCAPS2_CANAUTOGENMIPMAP) != 0) {
	texMask |= javax_media_j3d_Canvas3D_TEXTURE_AUTO_MIPMAP_GENERATION;
    }
    
    if ((d3dCaps->TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP) &&
    (maxTextureDepth > 0)) {
        texMask |= javax_media_j3d_Canvas3D_TEXTURE_3D;
    }
    
    if (d3dCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP) {
        texMask |= javax_media_j3d_Canvas3D_TEXTURE_CUBE_MAP;
    }
    
    if (maxTextureUnitStageSupport > 1) {
        texMask |= javax_media_j3d_Canvas3D_TEXTURE_MULTI_TEXTURE;
    }
    
    if (d3dCaps->TextureOpCaps & D3DTEXOPCAPS_DOTPRODUCT3) {
        texMask |= javax_media_j3d_Canvas3D_TEXTURE_COMBINE_DOT3;
    }
    
    if (d3dCaps->TextureOpCaps & D3DTEXOPCAPS_SUBTRACT) {
        texMask |= javax_media_j3d_Canvas3D_TEXTURE_COMBINE_SUBTRACT;
    }
    
    if (d3dCaps->TextureOpCaps & D3DTEXOPCAPS_LERP) {
        texMask |= (javax_media_j3d_Canvas3D_TEXTURE_LERP|
        javax_media_j3d_Canvas3D_TEXTURE_COMBINE);
    } else if (d3dCaps->TextureOpCaps &
    (D3DTEXOPCAPS_DOTPRODUCT3|D3DTEXOPCAPS_SUBTRACT|
    D3DTEXOPCAPS_MODULATE|D3DTEXOPCAPS_ADD|
    D3DTEXOPCAPS_ADDSIGNED)) {
        texMask |= javax_media_j3d_Canvas3D_TEXTURE_COMBINE;
    }
    
    
    if (maxAnisotropy > 1) {
        texMask |= javax_media_j3d_Canvas3D_TEXTURE_ANISOTROPIC_FILTER;
    }
}

BOOL D3dDeviceInfo::supportAntialiasing() {
    return (multiSampleSupport != 0);
}


void D3dDeviceInfo::findDepthStencilFormat(int minZDepth, int minZDepthStencil)
{ 	
    // Fix to Issue 226 : D3D - fail on stress test for the creation and destruction of Canvases
    //sanity check  of stencil and depth
    minZDepthStencil = min(minZDepthStencil, 8);
    minZDepth = min(minZDepth, 32);
    
    depthStencilFormat = D3DFMT_UNKNOWN;
   
    for (int i=0; i < D3DDEPTHFORMATSIZE; i++) {
        //printf("\ndepthFormatSupport %s, %b",getPixelFormatName(d3dDepthFormat[i]), depthFormatSupport[i]);
        if (depthFormatSupport[i]){
            // prefer one with stencil buffer, follow by D3DFMT_D16_LOCKABLE,
            // printf("\n ZDepth %d, Stencil %d ",d3dDepthTable[i],d3dStencilDepthTable[i]);
            if (d3dDepthTable[i] >= minZDepth && d3dStencilDepthTable[i] >= minZDepthStencil ) {
                depthStencilFormat = (D3DFORMAT) d3dDepthFormat[i];
                break;
            }
        }//if
    }// for
    // if none suitable found    
}


D3DMULTISAMPLE_TYPE D3dDeviceInfo::getBestMultiSampleType()
{
    DWORD bitmask = 0;
    UINT i;

    // Fix to Issue 226 : D3D - fail on stress test for the creation and destruction of Canvases
     // start with 4 and up, if none found, try 3 and 2
    for (i=4; i <= 16; i++) {
	bitmask = (1 << i);
	if (multiSampleSupport & bitmask) {
	    return (D3DMULTISAMPLE_TYPE) i;
	}
    }

    // try 3 and 2  
    for (i=3; i >= 2; i--) {
	bitmask = (1 << i);
	if (multiSampleSupport & bitmask) {
	    return (D3DMULTISAMPLE_TYPE) i;
	}
    }

    // Should not happen
    return D3DMULTISAMPLE_NONE;
}

int D3dDeviceInfo::getTextureFeaturesMask()
{
    return texMask;
}