jdk/src/windows/native/sun/java2d/d3d/D3DUtils.cpp
author ohair
Fri, 08 Aug 2008 08:52:18 -0700
changeset 921 85b4d3bded64
parent 2 90ce3da70b43
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "ddrawUtils.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "D3DUtils.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "D3DSurfaceData.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
// These strings must be in the same order as pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
// formats in D3DSurfaceData.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
char * TR_NAMES[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    "TR_OPAQUE",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    "TR_BITMASK",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    "TR_TRANSLUCENT"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
char * PF_NAMES[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    "PF_INVALID" ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    "PF_INT_ARGB" ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    "PF_INT_RGB" ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    "PF_INT_RGBX",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    "PF_INT_BGR" ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    "PF_USHORT_565_RGB" ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    "PF_USHORT_555_RGB" ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    "PF_USHORT_555_RGBX" ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    "PF_INT_ARGB_PRE" ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    "PF_USHORT_4444_ARGB"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#endif // DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * This structure could be used when searching for a pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * format with preferred bit depth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // Pointer to a DDPIXELFORMAT structure where the found pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // format will be copied to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    DDPIXELFORMAT *pddpf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // If TRUE, the search was successful, FALSE otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    BOOL bFoundFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // Preferred bit depth
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    int preferredDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
} PixelFormatSearchStruct;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
jint D3DUtils_GetPixelFormatType(DDPIXELFORMAT*lpddpf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
HRESULT WINAPI EnumAlphaTextureFormatsCallback(DDPIXELFORMAT* pddpf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                               VOID* pContext )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    J2dTraceLn(J2D_TRACE_VERBOSE, "EnumAlphaTextureFormatsCallback");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    DDPIXELFORMAT* pddpfOut = (DDPIXELFORMAT*)pContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // Looking for a 8-bit luminance texture (and probably not alpha-luminance)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    if((pddpf->dwFlags & DDPF_ALPHA) && (pddpf->dwAlphaBitDepth == 8))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        memcpy(pddpfOut, pddpf, sizeof(DDPIXELFORMAT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return D3DENUMRET_CANCEL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    return D3DENUMRET_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
HRESULT CALLBACK
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
D3DUtils_TextureSearchCallback(DDPIXELFORMAT *lpddpf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                               void *param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    J2dTraceLn(J2D_TRACE_VERBOSE, "D3DUtils_TextureSearchCallback");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    jint pfType = D3DUtils_GetPixelFormatType(lpddpf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    if (pfType == PF_INVALID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return DDENUMRET_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    DWORD dwAlphaBitCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    if (lpddpf->dwFlags & DDPF_ALPHAPIXELS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        DWORD dwMask = lpddpf->dwRGBAlphaBitMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        while( dwMask ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            dwMask = dwMask & ( dwMask - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            dwAlphaBitCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    DWORD dwRGBBitCount = lpddpf->dwRGBBitCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    WORD wDepthIndex = D3D_DEPTH_IDX(dwRGBBitCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    WORD wTransparencyIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        dwAlphaBitCount > 0 ? TR_TRANSLUCENT_IDX : TR_OPAQUE_IDX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    D3DTextureTable *table = (D3DTextureTable*)param;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    D3DTextureTableCell *cell = &(*table)[wTransparencyIndex][wDepthIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    if (cell->pfType == PF_INVALID || pfType < cell->pfType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // set only if it wasn't set or if current pfType is better than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // the one found previously: it's better to use 565 than 555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        memcpy(&cell->pddpf, lpddpf, sizeof(DDPIXELFORMAT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        cell->pfType = pfType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // continue for all pixel formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    return DDENUMRET_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
HRESULT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
WINAPI EnumZBufferFormatsCallback(DDPIXELFORMAT* pddpf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                  VOID* pContext )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    J2dTraceLn(J2D_TRACE_VERBOSE, "EnumZBufferFormatsCallback");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    PixelFormatSearchStruct *ppfss = (PixelFormatSearchStruct*)pContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    DDPIXELFORMAT* pddpfOut = ppfss->pddpf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    // if found a format with the exact depth, return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    if (pddpf->dwZBufferBitDepth == (DWORD)ppfss->preferredDepth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        ppfss->bFoundFormat = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        memcpy(pddpfOut, pddpf, sizeof(DDPIXELFORMAT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return D3DENUMRET_CANCEL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // If a format with exact depth can't be found, look for the best
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // available, preferring those with the lowest bit depth to save
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    // video memory. Also, prefer formats with no stencil bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    if (!ppfss->bFoundFormat ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        (pddpfOut->dwZBufferBitDepth > pddpf->dwZBufferBitDepth &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         !(pddpf->dwFlags & DDPF_STENCILBUFFER)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        ppfss->bFoundFormat = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        memcpy(pddpfOut, pddpf, sizeof(DDPIXELFORMAT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    return D3DENUMRET_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
HRESULT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
WINAPI DeviceEnumCallback(LPSTR strDesc, LPSTR strName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                          LPD3DDEVICEDESC7 pDesc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                          LPVOID pParentInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    J2dTraceLn(J2D_TRACE_VERBOSE, "DeviceEnumCallback");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    DEVICES_INFO *devinfo = (DEVICES_INFO*)pParentInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    if (pDesc->deviceGUID == IID_IDirect3DHALDevice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        devinfo->pGUIDs[HAL_IDX] = &IID_IDirect3DHALDevice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    } else if (pDesc->deviceGUID == IID_IDirect3DTnLHalDevice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        devinfo->pGUIDs[TNL_IDX] = &IID_IDirect3DTnLHalDevice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    } else if (pDesc->deviceGUID == IID_IDirect3DRGBDevice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        devinfo->pGUIDs[RGB_IDX] = &IID_IDirect3DRGBDevice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    } else if (pDesc->deviceGUID == IID_IDirect3DRefDevice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        devinfo->pGUIDs[REF_IDX] = &IID_IDirect3DRefDevice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    return D3DENUMRET_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
HRESULT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
D3DUtils_FindMaskTileTextureFormat(IDirect3DDevice7 *d3dDevice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                   DDPIXELFORMAT* pddpf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    J2dTraceLn(J2D_TRACE_INFO, "D3DUtils_FindMaskTileTextureFormat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    d3dDevice->EnumTextureFormats(EnumAlphaTextureFormatsCallback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                  (void*)pddpf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    if (pddpf->dwAlphaBitDepth == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return D3D_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    return DDERR_GENERIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
HRESULT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
D3DUtils_FindDepthBufferFormat(IDirect3D7 *d3dObject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                               int preferredDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                               DDPIXELFORMAT* pddpf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                               const GUID *pDeviceGUID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    J2dTraceLn(J2D_TRACE_INFO, "D3DUtils_FindDepthBufferFormat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    PixelFormatSearchStruct pfss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    pfss.pddpf = pddpf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    pfss.bFoundFormat = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    pfss.preferredDepth = preferredDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    d3dObject->EnumZBufferFormats(*pDeviceGUID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                  EnumZBufferFormatsCallback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                  (void*)&pfss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    return pfss.bFoundFormat ? D3D_OK : DDERR_GENERIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
jint D3DUtils_GetPixelFormatType(DDPIXELFORMAT*lpddpf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    J2dTraceLn(J2D_TRACE_VERBOSE, "D3DUtils_GetPixelFormatType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    if (lpddpf == NULL) return PF_INVALID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    DWORD dwFlags = lpddpf->dwFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    // skip weird formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    if (lpddpf->dwRGBBitCount < 16   ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        dwFlags & DDPF_ALPHA         || dwFlags & DDPF_ZBUFFER       ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        dwFlags & DDPF_ZPIXELS       || dwFlags & DDPF_LUMINANCE     ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        dwFlags & DDPF_FOURCC        || dwFlags & DDPF_STENCILBUFFER ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        dwFlags & DDPF_BUMPLUMINANCE || dwFlags & DDPF_BUMPDUDV)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return PF_INVALID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    jint pfType = PF_INVALID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    DWORD aMask = lpddpf->dwRGBAlphaBitMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    DWORD rMask = lpddpf->dwRBitMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    DWORD gMask = lpddpf->dwGBitMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    DWORD bMask = lpddpf->dwBBitMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    if (rMask == 0x0000f800 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        gMask == 0x000007e0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        bMask == 0x0000001f &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        aMask == 0x00000000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        pfType = PF_USHORT_565_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    } else if (rMask == 0x00007C00 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
               gMask == 0x000003E0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
               bMask == 0x0000001f &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
               aMask == 0x00000000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        pfType = PF_USHORT_555_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    } else if (rMask == 0x00000f00 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
               gMask == 0x000000f0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
               bMask == 0x0000000f &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
               aMask == 0x0000f000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // REMIND: we currently don't support this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // pixel format, since we don't have the loops for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // premultiplied version of it. So we'll just use INT_ARGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        pfType = PF_INVALID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // pfType = PF_USHORT_4444_ARGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    } else if (rMask == 0x00ff0000 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
               gMask == 0x0000ff00 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
               bMask == 0x000000ff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (lpddpf->dwRGBBitCount == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            pfType = (dwFlags & DDPF_ALPHAPIXELS) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                PF_INT_ARGB : PF_INT_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            // We currently don't support this format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            // pfType = PF_3BYTE_BGR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            pfType = PF_INVALID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    return pfType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
D3DUtils_SetupTextureFormats(IDirect3DDevice7 *d3dDevice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                             D3DTextureTable &table)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    J2dTraceLn(J2D_TRACE_INFO, "D3DUtils_SetupTextureFormats");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    if (d3dDevice == NULL || table == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    ZeroMemory(table, sizeof(D3DTextureTable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    int t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    for (t = TR_OPAQUE_IDX; t < TR_MAX_IDX; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        for (int d = DEPTH16_IDX; d < DEPTH_MAX_IDX; d++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            table[t][d].pfType = PF_INVALID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    d3dDevice->EnumTextureFormats(D3DUtils_TextureSearchCallback, table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    // We've retrieved the pixel formats for this device. The matrix may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    // look something like this, depending on the formats the device supports:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    // Transparency/Depth        Depth 16            Depth 24          Depth 32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    // ------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    //      TR_OPAQUE   PF_USHORT_565_RGB          PF_INVALID        PF_INT_RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    //     TR_BITMASK          PF_INVALID          PF_INVALID        PF_INVALID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    // TR_TRANSLUCENT          PF_INVALID          PF_INVALID       PF_INT_ARGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    // we'll be using translucent pixel formats for bitmask images
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    // for now, this may change later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    memcpy(&table[TR_BITMASK_IDX], &table[TR_TRANSLUCENT_IDX],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
           sizeof(D3DTextureTableCell[DEPTH_MAX_IDX]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    // Transparency/Depth        Depth 16            Depth 24          Depth 32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    // ------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    //      TR_OPAQUE   PF_USHORT_565_RGB          PF_INVALID        PF_INT_RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    //     TR_BITMASK          PF_INVALID          PF_INVALID       PF_INT_ARGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    // TR_TRANSLUCENT          PF_INVALID          PF_INVALID       PF_INT_ARGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    // REMIND: crude force
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    // Find substitutes for pixel formats which we didn't find.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    // For example, if we didn't find a 24-bit format, 32-bit will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    // a first choice for substitution. But if it wasn't found either,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    // then use 16-bit format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    D3DTextureTableCell *cell16, *cell24, *cell32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    for (t = TR_OPAQUE_IDX; t < TR_MAX_IDX; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        cell16 = &table[t][DEPTH16_IDX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        cell24 = &table[t][DEPTH24_IDX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        cell32 = &table[t][DEPTH32_IDX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (cell32->pfType == PF_INVALID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (cell24->pfType != PF_INVALID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                memcpy(cell32, cell24, sizeof(D3DTextureTableCell));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            } else if (cell16->pfType != PF_INVALID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                memcpy(cell32, cell16, sizeof(D3DTextureTableCell));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                // no valid pixel formats for this transparency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                // type were found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        // now we know that 32-bit is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (cell24->pfType == PF_INVALID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            // use 32-bit format as a substitution for 24-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            memcpy(cell24, cell32, sizeof(D3DTextureTableCell));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // now we know that 32- and 24-bit are valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (cell16->pfType == PF_INVALID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            // use 24-bit format as a substitution for 16-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            memcpy(cell16, cell24, sizeof(D3DTextureTableCell));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    // After this loop the matrix may look something like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    // Transparency/Depth        Depth 16            Depth 24          Depth 32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    // ------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    //      TR_OPAQUE   PF_USHORT_565_RGB          PF_INT_RGB        PF_INT_RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    //     TR_BITMASK         PF_INT_ARGB         PF_INT_ARGB       PF_INT_ARGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    // TR_TRANSLUCENT         PF_INT_ARGB         PF_INT_ARGB       PF_INT_ARGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    // Print out the matrix (should look something like the comment above)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    J2dTraceLn1(J2D_TRACE_INFO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                "Texutre formats table for device %x", d3dDevice);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    J2dTraceLn(J2D_TRACE_INFO, "Transparency/Depth     Depth 16            "\
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
               "Depth 24            Depth 32");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    J2dTraceLn(J2D_TRACE_INFO, "-------------------------------------------"\
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
               "----------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    for (t = TR_OPAQUE_IDX; t < TR_MAX_IDX; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        J2dTrace1(J2D_TRACE_INFO, "%15s", TR_NAMES[t]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        for (int d = DEPTH16_IDX; d < DEPTH_MAX_IDX; d++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            J2dTrace1(J2D_TRACE_INFO, "%20s",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                      PF_NAMES[table[t][d].pfType]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        J2dTrace(J2D_TRACE_INFO, "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
#endif // DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
const GUID *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
D3DUtils_SelectDeviceGUID(IDirect3D7 *d3dObject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    static char * RASTERIZER_NAMES[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        "TNL", "HAL", "REFERENCE", "RGB"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    // try to use TnL rasterizer by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    int defIndex = TNL_IDX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    J2dTraceLn(J2D_TRACE_INFO, "D3DUtils_SelectDeviceGUID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    // unless a different one was requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    char *pRasterizer = getenv("J2D_D3D_RASTERIZER");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    if (pRasterizer != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (strncmp(pRasterizer, "ref", 3) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            defIndex = REF_IDX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        } else if (strncmp(pRasterizer, "rgb", 3) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            defIndex = RGB_IDX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        } else if (strncmp(pRasterizer, "hal", 3) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            defIndex = HAL_IDX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        } else if (strncmp(pRasterizer, "tnl", 3) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            defIndex = TNL_IDX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        J2dTraceLn1(J2D_TRACE_VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    "  rasterizer requested: %s",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    RASTERIZER_NAMES[defIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    DEVICES_INFO devInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    memset(&devInfo, 0, sizeof(devInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    HRESULT res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    if (FAILED(res = d3dObject->EnumDevices(DeviceEnumCallback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                            (VOID*)&devInfo)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        DebugPrintDirectDrawError(res, "D3DUtils_SelectDeviceGUID: "\
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                  "EnumDevices failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    // return requested rasterizer's guid if it's present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    if (devInfo.pGUIDs[defIndex] != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        J2dRlsTraceLn1(J2D_TRACE_VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                       "D3DUtils_SelectDeviceGUID: using %s rasterizer",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                       RASTERIZER_NAMES[defIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return devInfo.pGUIDs[defIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    // if not, try to find one, starting with the best available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    defIndex = TNL_IDX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (devInfo.pGUIDs[defIndex] != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            J2dRlsTraceLn1(J2D_TRACE_VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                           "D3DUtils_SelectDeviceGUID: using %s rasterizer",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                           RASTERIZER_NAMES[defIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            return devInfo.pGUIDs[defIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        // While we could use the rgb and ref rasterizers if tnl and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // hal aren't present, it's not practical for performance purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        // so we just leave an opportunity to force them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    } while (++defIndex < REF_IDX /*DEV_IDX_MAX*/);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    J2dRlsTraceLn(J2D_TRACE_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                  "D3DUtils_SelectDeviceGUID: "\
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                  "No Accelerated Rasterizers Found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
 * This function sets passed matrix to be a custom left-hand off-center
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
 * orthogonal matrix. The output is identical to D3DX's function call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
 * D3DXMatrixOrthoOffCenterLH((D3DXMATRIX*)&tx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
 *                            0.0, width, height, 0.0, -1.0, 1.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
D3DUtils_SetOrthoMatrixOffCenterLH(D3DMATRIX *m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                   float width, float height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    DASSERT((m != NULL) && (width > 0.0f) && (height > 0.0f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    memset(m, 0, sizeof(D3DMATRIX));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    m->_11 =  2.0f/width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    m->_22 = -2.0f/height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    m->_33 =  0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    m->_44 =  1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    m->_41 = -1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    m->_42 =  1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    m->_43 =  0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
D3DUtils_SetIdentityMatrix(D3DMATRIX *m, BOOL adjust)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    DASSERT(m != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    m->_12 = m->_13 = m->_14 = m->_21 = m->_23 = m->_24 = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    m->_31 = m->_32 = m->_34 = m->_43 = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    m->_11 = m->_22 = m->_33 = m->_44 = 1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    if (adjust) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        // This is required for proper texel alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        m->_41 = m->_42 = -0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        m->_41 = m->_42 = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
DDrawSurface *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
D3DUtils_CreatePlainSurface(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                            DDraw *ddObject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                            D3DContext *d3dContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                            int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    DXSurface *dxSurface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    jint pType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    J2dTraceLn(J2D_TRACE_INFO, "D3DUtils_CreatePlainSurface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    if (FAILED(d3dContext->CreateSurface(env, w, h, 32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                                         TR_OPAQUE, D3D_PLAIN_SURFACE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                                         &dxSurface, &pType)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    return new DDrawSurface(ddObject, dxSurface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
DDrawSurface *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
D3DUtils_CreateTexture(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                       DDraw *ddObject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                       D3DContext *d3dContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                       int transparency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                       int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    J2dTraceLn(J2D_TRACE_INFO, "D3DUtils_CreateTexture");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    DXSurface *dxSurface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    jint pType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    if (FAILED(d3dContext->CreateSurface(env, w, h, 32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                         transparency, D3D_TEXTURE_SURFACE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                                         &dxSurface, &pType)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    return new DDrawSurface(ddObject, dxSurface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
HRESULT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
D3DUtils_UploadIntImageToXRGBTexture(DDrawSurface *lpTexture,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                                     int *pSrc, int width, int height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    HRESULT res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    int texW = lpTexture->GetDXSurface()->GetWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    int texH = lpTexture->GetDXSurface()->GetHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    int srcStride = width * 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    J2dTraceLn(J2D_TRACE_INFO, "D3DUtils_UploadIntImageToXRGBTexture");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    if (width > texW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        width = texW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    if (height > texH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        height = texH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    SurfaceDataRasInfo rasInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    if (SUCCEEDED(res = lpTexture->Lock(NULL, &rasInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                                        DDLOCK_WAIT|DDLOCK_NOSYSLOCK, NULL)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        void *pDstPixels = rasInfo.rasBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        void *pSrcPixels = (void*)pSrc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        // REMIND: clear the dest first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        memset(pDstPixels, 0, texH * rasInfo.scanStride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            memcpy(pDstPixels, pSrcPixels, width * 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            pSrcPixels = PtrAddBytes(pSrcPixels, srcStride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            pDstPixels = PtrAddBytes(pDstPixels, rasInfo.scanStride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        } while (--height > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        res = lpTexture->Unlock(NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
HRESULT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
D3DUtils_CheckD3DCaps(LPD3DDEVICEDESC7 lpDesc7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    // The device must support fast rasterization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    static DWORD dwDevCaps =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        (D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_HWRASTERIZATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    BOOL vt = lpDesc7->dwDevCaps & D3DDEVCAPS_DRAWPRIMTLVERTEX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    BOOL rz = lpDesc7->dwDevCaps & D3DDEVCAPS_HWRASTERIZATION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    J2dTraceLn(J2D_TRACE_INFO, "D3DUtils_CheckD3DCaps");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    return (lpDesc7->dwDevCaps & dwDevCaps) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        D3D_OK :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        DDERR_GENERIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
HRESULT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
D3DUtils_CheckTextureCaps(LPD3DDEVICEDESC7 lpDesc7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    J2dTraceLn(J2D_TRACE_INFO, "D3DUtils_CheckTextureCaps");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    // REMIND: we should really check both Tri and Lin caps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    // but hopefully we won't be using line strips soon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    LPD3DPRIMCAPS lpDpcTriCaps = &lpDesc7->dpcTriCaps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    // Filtering requirements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    static DWORD dwFilterCaps =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        (D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_NEAREST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    // Check for caps used for alpha compositing (implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    // Porter-Duff rules)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    static DWORD dwBlendCaps =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        (D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
         D3DPBLENDCAPS_SRCALPHA  | D3DPBLENDCAPS_INVSRCALPHA |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
         D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    if ((lpDesc7->dwTextureOpCaps & D3DTEXOPCAPS_MODULATE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        (lpDpcTriCaps->dwTextureFilterCaps & dwFilterCaps) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        (lpDpcTriCaps->dwSrcBlendCaps  & dwBlendCaps) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        (lpDpcTriCaps->dwDestBlendCaps & dwBlendCaps))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        return D3D_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    return DDERR_GENERIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
HRESULT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
D3DUtils_CheckDeviceCaps(LPD3DDEVICEDESC7 lpDesc7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    J2dTraceLn(J2D_TRACE_INFO, "D3DUtils_CheckDeviceCaps");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    if (SUCCEEDED(D3DUtils_CheckD3DCaps(lpDesc7)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        SUCCEEDED(D3DUtils_CheckTextureCaps(lpDesc7)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        SUCCEEDED(D3DUtils_CheckDepthCaps(lpDesc7)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        return D3D_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    return DDERR_GENERIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
HRESULT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
D3DUtils_CheckDepthCaps(LPD3DDEVICEDESC7 lpDesc7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    J2dTraceLn(J2D_TRACE_INFO, "D3DUtils_CheckDepthCaps");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    // Check for required depth-buffer operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    // (see D3DContext::SetClip() for more info).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    static DWORD dwZCmpCaps = (D3DPCMPCAPS_ALWAYS | D3DPCMPCAPS_LESS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    // D3DPMISCCAPS_MASKZ capability allows enabling/disabling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    // depth buffer updates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    if ((lpDesc7->dpcTriCaps.dwMiscCaps & D3DPMISCCAPS_MASKZ) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        (lpDesc7->dpcTriCaps.dwZCmpCaps & dwZCmpCaps))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        return D3D_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    return DDERR_GENERIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
}