d3d.c 147 KB
Newer Older
1 2 3 4
/*
 * Some unit tests for d3d functions
 *
 * Copyright (C) 2005 Antoine Chavasse
5
 * Copyright (C) 2006,2011 Stefan Dösinger for CodeWeavers
6
 * Copyright (C) 2008 Alexander Dorofeyev
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 22
 */

23 24
#define COBJMACROS

25
#include "wine/test.h"
26
#include <limits.h>
27
#include "initguid.h"
28 29
#include "ddraw.h"
#include "d3d.h"
30
#include "unknwn.h"
31

32
static IDirectDraw7 *lpDD;
33
static IDirect3D7 *lpD3D;
34 35
static IDirectDrawSurface7 *lpDDS;
static IDirectDrawSurface7 *lpDDSdepth;
36
static IDirect3DDevice7 *lpD3DDevice;
37
static IDirect3DVertexBuffer7 *lpVBufSrc;
38

39 40 41 42 43 44
static IDirectDraw *DirectDraw1 = NULL;
static IDirectDrawSurface *Surface1 = NULL;
static IDirect3D *Direct3D1 = NULL;
static IDirect3DDevice *Direct3DDevice1 = NULL;
static IDirect3DExecuteBuffer *ExecuteBuffer = NULL;
static IDirect3DViewport *Viewport = NULL;
45
static IDirect3DLight *Light = NULL;
46

47 48 49 50 51 52 53 54
typedef struct {
    int total;
    int rgb;
    int hal;
    int tnlhal;
    int unk;
} D3D7ETest;

55 56 57 58 59
typedef struct {
    HRESULT desired_ret;
    int total;
} D3D7ECancelTest;

60 61 62 63 64 65 66 67 68 69
#define MAX_ENUMERATION_COUNT 10
typedef struct
{
    unsigned int count;
    char *callback_description_ptrs[MAX_ENUMERATION_COUNT];
    char callback_description_strings[MAX_ENUMERATION_COUNT][100];
    char *callback_name_ptrs[MAX_ENUMERATION_COUNT];
    char callback_name_strings[MAX_ENUMERATION_COUNT][100];
} D3D7ELifetimeTest;

Henri Verbeet's avatar
Henri Verbeet committed
70 71
static HRESULT (WINAPI *pDirectDrawCreateEx)(GUID *driver_guid,
        void **ddraw, REFIID interface_iid, IUnknown *outer);
72 73 74 75

static void init_function_pointers(void)
{
    HMODULE hmod = GetModuleHandleA("ddraw.dll");
76
    pDirectDrawCreateEx = (void*)GetProcAddress(hmod, "DirectDrawCreateEx");
77 78 79
}


80 81 82 83 84 85
static ULONG getRefcount(IUnknown *iface)
{
    IUnknown_AddRef(iface);
    return IUnknown_Release(iface);
}

86 87 88 89 90 91 92
static HRESULT WINAPI SurfaceCounter(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
{
    UINT *num = context;
    (*num)++;
    IDirectDrawSurface_Release(surface);
    return DDENUMRET_OK;
}
93

94
static BOOL CreateDirect3D(void)
95 96 97
{
    HRESULT rc;
    DDSURFACEDESC2 ddsd;
98
    UINT num;
99

100
    rc = pDirectDrawCreateEx(NULL, (void**)&lpDD,
101
        &IID_IDirectDraw7, NULL);
102
    ok(rc==DD_OK || rc==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %x\n", rc);
103
    if (!lpDD) {
104
        trace("DirectDrawCreateEx() failed with an error %x\n", rc);
105 106
        return FALSE;
    }
107 108

    rc = IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_NORMAL);
109
    ok(rc==DD_OK, "SetCooperativeLevel returned: %x\n", rc);
110 111

    rc = IDirectDraw7_QueryInterface(lpDD, &IID_IDirect3D7, (void**) &lpD3D);
112
    if (rc == E_NOINTERFACE) return FALSE;
113
    ok(rc==DD_OK, "QueryInterface returned: %x\n", rc);
114 115 116 117 118 119 120 121

    memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
    ddsd.dwWidth = 256;
    ddsd.dwHeight = 256;
    rc = IDirectDraw7_CreateSurface(lpDD, &ddsd, &lpDDS, NULL);
122
    if (FAILED(rc))
123
        return FALSE;
124

125 126 127 128
    num = 0;
    IDirectDraw7_EnumSurfaces(lpDD, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST, NULL, &num, SurfaceCounter);
    ok(num == 1, "Has %d surfaces, expected 1\n", num);

129 130 131 132
    memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
133 134 135 136
    U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
    U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
    U1(U4(ddsd).ddpfPixelFormat).dwZBufferBitDepth = 16;
    U3(U4(ddsd).ddpfPixelFormat).dwZBitMask = 0x0000FFFF;
137 138 139 140
    ddsd.dwWidth = 256;
    ddsd.dwHeight = 256;
    rc = IDirectDraw7_CreateSurface(lpDD, &ddsd, &lpDDSdepth, NULL);
    ok(rc==DD_OK, "CreateSurface returned: %x\n", rc);
141
    if (FAILED(rc)) {
142 143 144 145
        lpDDSdepth = NULL;
    } else {
        rc = IDirectDrawSurface_AddAttachedSurface(lpDDS, lpDDSdepth);
        ok(rc == DD_OK, "IDirectDrawSurface_AddAttachedSurface returned %x\n", rc);
146
        if (FAILED(rc))
147 148 149
            return FALSE;
    }

150 151
    rc = IDirect3D7_CreateDevice(lpD3D, &IID_IDirect3DTnLHalDevice, lpDDS,
        &lpD3DDevice);
152
    ok(rc==D3D_OK || rc==DDERR_NOPALETTEATTACHED || rc==E_OUTOFMEMORY, "CreateDevice returned: %x\n", rc);
153
    if (!lpD3DDevice) {
154 155 156 157 158 159 160 161 162 163 164 165
        trace("IDirect3D7::CreateDevice() for a TnL Hal device failed with an error %x, trying HAL\n", rc);
        rc = IDirect3D7_CreateDevice(lpD3D, &IID_IDirect3DHALDevice, lpDDS,
            &lpD3DDevice);
        if (!lpD3DDevice) {
            trace("IDirect3D7::CreateDevice() for a HAL device failed with an error %x, trying RGB\n", rc);
            rc = IDirect3D7_CreateDevice(lpD3D, &IID_IDirect3DRGBDevice, lpDDS,
                &lpD3DDevice);
            if (!lpD3DDevice) {
                trace("IDirect3D7::CreateDevice() for a RGB device failed with an error %x, giving up\n", rc);
                return FALSE;
            }
        }
166 167 168
    }

    return TRUE;
169 170
}

171
static void ReleaseDirect3D(void)
172 173 174 175 176 177 178
{
    if (lpD3DDevice != NULL)
    {
        IDirect3DDevice7_Release(lpD3DDevice);
        lpD3DDevice = NULL;
    }

179 180 181 182 183 184
    if (lpDDSdepth != NULL)
    {
        IDirectDrawSurface_Release(lpDDSdepth);
        lpDDSdepth = NULL;
    }

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    if (lpDDS != NULL)
    {
        IDirectDrawSurface_Release(lpDDS);
        lpDDS = NULL;
    }

    if (lpD3D != NULL)
    {
        IDirect3D7_Release(lpD3D);
        lpD3D = NULL;
    }

    if (lpDD != NULL)
    {
        IDirectDraw_Release(lpDD);
        lpDD = NULL;
    }
}

204
static void LightTest(void)
205 206 207 208 209
{
    HRESULT rc;
    D3DLIGHT7 light;
    D3DLIGHT7 defaultlight;
    BOOL bEnabled = FALSE;
210 211
    float one = 1.0f;
    float zero= 0.0f;
212
    D3DMATERIAL7 mat;
213 214 215
    BOOL enabled;
    unsigned int i;
    D3DDEVICEDESC7 caps;
216 217 218 219 220 221 222 223 224 225

    /* Set a few lights with funky indices. */
    memset(&light, 0, sizeof(light));
    light.dltType = D3DLIGHT_DIRECTIONAL;
    U1(light.dcvDiffuse).r = 0.5f;
    U2(light.dcvDiffuse).g = 0.6f;
    U3(light.dcvDiffuse).b = 0.7f;
    U2(light.dvDirection).y = 1.f;

    rc = IDirect3DDevice7_SetLight(lpD3DDevice, 5, &light);
226
    ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
227
    rc = IDirect3DDevice7_SetLight(lpD3DDevice, 10, &light);
228
    ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
229
    rc = IDirect3DDevice7_SetLight(lpD3DDevice, 45, &light);
230
    ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
231 232 233 234 235


    /* Try to retrieve a light beyond the indices of the lights that have
       been set. */
    rc = IDirect3DDevice7_GetLight(lpD3DDevice, 50, &light);
236
    ok(rc==DDERR_INVALIDPARAMS, "GetLight returned: %x\n", rc);
237
    rc = IDirect3DDevice7_GetLight(lpD3DDevice, 2, &light);
238
    ok(rc==DDERR_INVALIDPARAMS, "GetLight returned: %x\n", rc);
239 240 241 242


    /* Try to retrieve one of the lights that have been set */
    rc = IDirect3DDevice7_GetLight(lpD3DDevice, 10, &light);
243
    ok(rc==D3D_OK, "GetLight returned: %x\n", rc);
244 245 246 247


    /* Enable a light that have been previously set. */
    rc = IDirect3DDevice7_LightEnable(lpD3DDevice, 10, TRUE);
248
    ok(rc==D3D_OK, "LightEnable returned: %x\n", rc);
249 250 251 252 253 254 255 256 257 258 259 260


    /* Enable some lights that have not been previously set, and verify that
       they have been initialized with proper default values. */
    memset(&defaultlight, 0, sizeof(D3DLIGHT7));
    defaultlight.dltType = D3DLIGHT_DIRECTIONAL;
    U1(defaultlight.dcvDiffuse).r = 1.f;
    U2(defaultlight.dcvDiffuse).g = 1.f;
    U3(defaultlight.dcvDiffuse).b = 1.f;
    U3(defaultlight.dvDirection).z = 1.f;

    rc = IDirect3DDevice7_LightEnable(lpD3DDevice, 20, TRUE);
261
    ok(rc==D3D_OK, "LightEnable returned: %x\n", rc);
262 263
    memset(&light, 0, sizeof(D3DLIGHT7));
    rc = IDirect3DDevice7_GetLight(lpD3DDevice, 20, &light);
264
    ok(rc==D3D_OK, "GetLight returned: %x\n", rc);
265 266 267 268
    ok(!memcmp(&light, &defaultlight, sizeof(D3DLIGHT7)),
        "light data doesn't match expected default values\n" );

    rc = IDirect3DDevice7_LightEnable(lpD3DDevice, 50, TRUE);
269
    ok(rc==D3D_OK, "LightEnable returned: %x\n", rc);
270 271
    memset(&light, 0, sizeof(D3DLIGHT7));
    rc = IDirect3DDevice7_GetLight(lpD3DDevice, 50, &light);
272
    ok(rc==D3D_OK, "GetLight returned: %x\n", rc);
273 274 275 276 277 278
    ok(!memcmp(&light, &defaultlight, sizeof(D3DLIGHT7)),
        "light data doesn't match expected default values\n" );


    /* Disable one of the light that have been previously enabled. */
    rc = IDirect3DDevice7_LightEnable(lpD3DDevice, 20, FALSE);
279
    ok(rc==D3D_OK, "LightEnable returned: %x\n", rc);
280 281 282 283

    /* Try to retrieve the enable status of some lights */
    /* Light 20 is supposed to be disabled */
    rc = IDirect3DDevice7_GetLightEnable(lpD3DDevice, 20, &bEnabled );
284
    ok(rc==D3D_OK, "GetLightEnable returned: %x\n", rc);
285 286 287 288 289
    ok(!bEnabled, "GetLightEnable says the light is enabled\n");

    /* Light 10 is supposed to be enabled */
    bEnabled = FALSE;
    rc = IDirect3DDevice7_GetLightEnable(lpD3DDevice, 10, &bEnabled );
290
    ok(rc==D3D_OK, "GetLightEnable returned: %x\n", rc);
291 292 293 294
    ok(bEnabled, "GetLightEnable says the light is disabled\n");

    /* Light 80 has not been set */
    rc = IDirect3DDevice7_GetLightEnable(lpD3DDevice, 80, &bEnabled );
295
    ok(rc==DDERR_INVALIDPARAMS, "GetLightEnable returned: %x\n", rc);
296 297 298

    /* Light 23 has not been set */
    rc = IDirect3DDevice7_GetLightEnable(lpD3DDevice, 23, &bEnabled );
299
    ok(rc==DDERR_INVALIDPARAMS, "GetLightEnable returned: %x\n", rc);
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

    /* Set some lights with invalid parameters */
    memset(&light, 0, sizeof(D3DLIGHT7));
    light.dltType = 0;
    U1(light.dcvDiffuse).r = 1.f;
    U2(light.dcvDiffuse).g = 1.f;
    U3(light.dcvDiffuse).b = 1.f;
    U3(light.dvDirection).z = 1.f;
    rc = IDirect3DDevice7_SetLight(lpD3DDevice, 100, &light);
    ok(rc==DDERR_INVALIDPARAMS, "SetLight returned: %x\n", rc);

    memset(&light, 0, sizeof(D3DLIGHT7));
    light.dltType = 12345;
    U1(light.dcvDiffuse).r = 1.f;
    U2(light.dcvDiffuse).g = 1.f;
    U3(light.dcvDiffuse).b = 1.f;
    U3(light.dvDirection).z = 1.f;
    rc = IDirect3DDevice7_SetLight(lpD3DDevice, 101, &light);
    ok(rc==DDERR_INVALIDPARAMS, "SetLight returned: %x\n", rc);

    rc = IDirect3DDevice7_SetLight(lpD3DDevice, 102, NULL);
    ok(rc==DDERR_INVALIDPARAMS, "SetLight returned: %x\n", rc);

    memset(&light, 0, sizeof(D3DLIGHT7));
    light.dltType = D3DLIGHT_SPOT;
    U1(light.dcvDiffuse).r = 1.f;
    U2(light.dcvDiffuse).g = 1.f;
    U3(light.dcvDiffuse).b = 1.f;
    U3(light.dvDirection).z = 1.f;

330
    light.dvAttenuation0 = -one / zero; /* -INFINITY */
331 332 333
    rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
    ok(rc==DDERR_INVALIDPARAMS, "SetLight returned: %x\n", rc);

334
    light.dvAttenuation0 = -1.0;
335 336 337
    rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
    ok(rc==DDERR_INVALIDPARAMS, "SetLight returned: %x\n", rc);

338
    light.dvAttenuation0 = 0.0;
339 340 341
    rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
    ok(rc==D3D_OK, "SetLight returned: %x\n", rc);

342
    light.dvAttenuation0 = 1.0;
343 344 345
    rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
    ok(rc==D3D_OK, "SetLight returned: %x\n", rc);

346
    light.dvAttenuation0 = one / zero; /* +INFINITY */
347 348 349
    rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
    ok(rc==D3D_OK, "SetLight returned: %x\n", rc);

350
    light.dvAttenuation0 = zero / zero; /* NaN */
351
    rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
352 353
    ok(rc==D3D_OK ||
       broken(rc==DDERR_INVALIDPARAMS), "SetLight returned: %x\n", rc);
354 355 356

    /* Directional light ignores attenuation */
    light.dltType = D3DLIGHT_DIRECTIONAL;
357
    light.dvAttenuation0 = -1.0;
358 359
    rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
    ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
360 361 362 363 364

    memset(&mat, 0, sizeof(mat));
    rc = IDirect3DDevice7_SetMaterial(lpD3DDevice, &mat);
    ok(rc == D3D_OK, "IDirect3DDevice7_SetMaterial returned: %x\n", rc);

365
    U4(mat).power = 129.0;
366 367 368 369 370
    rc = IDirect3DDevice7_SetMaterial(lpD3DDevice, &mat);
    ok(rc == D3D_OK, "IDirect3DDevice7_SetMaterial(power = 129.0) returned: %x\n", rc);
    memset(&mat, 0, sizeof(mat));
    rc = IDirect3DDevice7_GetMaterial(lpD3DDevice, &mat);
    ok(rc == D3D_OK, "IDirect3DDevice7_GetMaterial returned: %x\n", rc);
371
    ok(U4(mat).power == 129, "Returned power is %f\n", U4(mat).power);
372

373
    U4(mat).power = -1.0;
374 375 376 377 378
    rc = IDirect3DDevice7_SetMaterial(lpD3DDevice, &mat);
    ok(rc == D3D_OK, "IDirect3DDevice7_SetMaterial(power = -1.0) returned: %x\n", rc);
    memset(&mat, 0, sizeof(mat));
    rc = IDirect3DDevice7_GetMaterial(lpD3DDevice, &mat);
    ok(rc == D3D_OK, "IDirect3DDevice7_GetMaterial returned: %x\n", rc);
379
    ok(U4(mat).power == -1, "Returned power is %f\n", U4(mat).power);
380 381 382 383 384

    memset(&caps, 0, sizeof(caps));
    rc = IDirect3DDevice7_GetCaps(lpD3DDevice, &caps);
    ok(rc == D3D_OK, "IDirect3DDevice7_GetCaps failed with %x\n", rc);

385
    if ( caps.dwMaxActiveLights == (DWORD) -1) {
Austin English's avatar
Austin English committed
386
        /* Some cards without T&L Support return -1 (Examples: Voodoo Banshee, RivaTNT / NV4) */
387 388 389 390
        skip("T&L not supported\n");
        return;
    }

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    for(i = 1; i <= caps.dwMaxActiveLights; i++) {
        rc = IDirect3DDevice7_LightEnable(lpD3DDevice, i, TRUE);
        ok(rc == D3D_OK, "Enabling light %u failed with %x\n", i, rc);
        rc = IDirect3DDevice7_GetLightEnable(lpD3DDevice, i, &enabled);
        ok(rc == D3D_OK, "GetLightEnable on light %u failed with %x\n", i, rc);
        ok(enabled, "Light %d is %s\n", i, enabled ? "enabled" : "disabled");
    }

    /* TODO: Test the rendering results in this situation */
    rc = IDirect3DDevice7_LightEnable(lpD3DDevice, i + 1, TRUE);
    ok(rc == D3D_OK, "Enabling one light more than supported returned %x\n", rc);
    rc = IDirect3DDevice7_GetLightEnable(lpD3DDevice, i + 1, &enabled);
    ok(rc == D3D_OK, "GetLightEnable on light %u failed with %x\n", i + 1,  rc);
    ok(enabled, "Light %d is %s\n", i + 1, enabled ? "enabled" : "disabled");
    rc = IDirect3DDevice7_LightEnable(lpD3DDevice, i + 1, FALSE);
    ok(rc == D3D_OK, "Disabling the additional returned %x\n", rc);

    for(i = 1; i <= caps.dwMaxActiveLights; i++) {
        rc = IDirect3DDevice7_LightEnable(lpD3DDevice, i, FALSE);
        ok(rc == D3D_OK, "Disabling light %u failed with %x\n", i, rc);
    }
412 413
}

414 415 416 417
static void StateTest( void )
{
    HRESULT rc;

418
    /* The msdn says it's undocumented, does it return an error too? */
419 420 421 422 423 424
    rc = IDirect3DDevice7_SetRenderState(lpD3DDevice, D3DRENDERSTATE_ZVISIBLE, TRUE);
    ok(rc == D3D_OK, "IDirect3DDevice7_SetRenderState(D3DRENDERSTATE_ZVISIBLE, TRUE) returned %08x\n", rc);
    rc = IDirect3DDevice7_SetRenderState(lpD3DDevice, D3DRENDERSTATE_ZVISIBLE, FALSE);
    ok(rc == D3D_OK, "IDirect3DDevice7_SetRenderState(D3DRENDERSTATE_ZVISIBLE, FALSE) returned %08x\n", rc);
}

425 426 427 428 429

static void SceneTest(void)
{
    HRESULT                      hr;

430
    /* Test an EndScene without BeginScene. Should return an error */
431 432 433 434 435 436
    hr = IDirect3DDevice7_EndScene(lpD3DDevice);
    ok(hr == D3DERR_SCENE_NOT_IN_SCENE, "IDirect3DDevice7_EndScene returned %08x\n", hr);

    /* Test a normal BeginScene / EndScene pair, this should work */
    hr = IDirect3DDevice7_BeginScene(lpD3DDevice);
    ok(hr == D3D_OK, "IDirect3DDevice7_BeginScene failed with %08x\n", hr);
437 438 439 440 441 442 443
    if (SUCCEEDED(hr))
    {
        hr = IDirect3DDevice7_EndScene(lpD3DDevice);
        ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed with %08x\n", hr);
    }

    if (lpDDSdepth)
444
    {
445 446 447 448
        DDBLTFX fx;
        memset(&fx, 0, sizeof(fx));
        fx.dwSize = sizeof(fx);

449 450 451 452 453 454 455
        hr = IDirectDrawSurface7_Blt(lpDDSdepth, NULL, NULL, NULL, DDBLT_DEPTHFILL, &fx);
        ok(hr == D3D_OK, "Depthfill failed outside a BeginScene / EndScene pair, hr 0x%08x\n", hr);

        hr = IDirect3DDevice7_BeginScene(lpD3DDevice);
        ok(hr == D3D_OK, "IDirect3DDevice7_BeginScene failed with %08x\n", hr);
        if (SUCCEEDED(hr))
        {
456
            hr = IDirectDrawSurface7_Blt(lpDDSdepth, NULL, NULL, NULL, DDBLT_DEPTHFILL, &fx);
457 458 459 460
            ok(hr == D3D_OK || broken(hr == E_FAIL),
                    "Depthfill failed in a BeginScene / EndScene pair, hr 0x%08x\n", hr);
            hr = IDirect3DDevice7_EndScene(lpD3DDevice);
            ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed with %08x\n", hr);
461
        }
462 463 464 465
    }
    else
    {
        skip("Depth stencil creation failed at startup, skipping depthfill test\n");
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    }

    /* Test another EndScene without having begun a new scene. Should return an error */
    hr = IDirect3DDevice7_EndScene(lpD3DDevice);
    ok(hr == D3DERR_SCENE_NOT_IN_SCENE, "IDirect3DDevice7_EndScene returned %08x\n", hr);

    /* Two nested BeginScene and EndScene calls */
    hr = IDirect3DDevice7_BeginScene(lpD3DDevice);
    ok(hr == D3D_OK, "IDirect3DDevice7_BeginScene failed with %08x\n", hr);
    hr = IDirect3DDevice7_BeginScene(lpD3DDevice);
    ok(hr == D3DERR_SCENE_IN_SCENE, "IDirect3DDevice7_BeginScene returned %08x\n", hr);
    hr = IDirect3DDevice7_EndScene(lpD3DDevice);
    ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed with %08x\n", hr);
    hr = IDirect3DDevice7_EndScene(lpD3DDevice);
    ok(hr == D3DERR_SCENE_NOT_IN_SCENE, "IDirect3DDevice7_EndScene returned %08x\n", hr);

    /* TODO: Verify that blitting works in the same way as in d3d9 */
}

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
static void LimitTest(void)
{
    IDirectDrawSurface7 *pTexture = NULL;
    HRESULT hr;
    int i;
    DDSURFACEDESC2 ddsd;

    memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
    ddsd.dwWidth = 16;
    ddsd.dwHeight = 16;
    hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &pTexture, NULL);
    ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
    if(!pTexture) return;

    for(i = 0; i < 8; i++) {
        hr = IDirect3DDevice7_SetTexture(lpD3DDevice, i, pTexture);
        ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %08x\n", i, hr);
        hr = IDirect3DDevice7_SetTexture(lpD3DDevice, i, NULL);
        ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %08x\n", i, hr);
        hr = IDirect3DDevice7_SetTextureStageState(lpD3DDevice, i, D3DTSS_COLOROP, D3DTOP_ADD);
        ok(hr == D3D_OK, "IDirect3DDevice8_SetTextureStageState for texture %d failed with %08x\n", i, hr);
    }

    IDirectDrawSurface7_Release(pTexture);
}

Henri Verbeet's avatar
Henri Verbeet committed
514 515
static HRESULT WINAPI enumDevicesCallback(GUID *Guid, char *DeviceDescription,
        char *DeviceName, D3DDEVICEDESC *hal, D3DDEVICEDESC *hel, void *ctx)
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
{
    UINT ver = *((UINT *) ctx);
    if(IsEqualGUID(&IID_IDirect3DRGBDevice, Guid))
    {
        ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
           "RGB Device %d hal line caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
        ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
           "RGB Device %d hal tri caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
        ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
           "RGB Device %d hel line caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);
        ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
           "RGB Device %d hel tri caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);

        ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
           "RGB Device %d hal line caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
        ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
           "RGB Device %d hal tri caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
        ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
           "RGB Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
        ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
           "RGB Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
537 538 539

        ok(hal->dcmColorModel == 0, "RGB Device %u hal caps has colormodel %u\n", ver, hal->dcmColorModel);
        ok(hel->dcmColorModel == D3DCOLOR_RGB, "RGB Device %u hel caps has colormodel %u\n", ver, hel->dcmColorModel);
540 541 542

        ok(hal->dwFlags == 0, "RGB Device %u hal caps has hardware flags %x\n", ver, hal->dwFlags);
        ok(hel->dwFlags != 0, "RGB Device %u hel caps has hardware flags %x\n", ver, hel->dwFlags);
543 544 545
    }
    else if(IsEqualGUID(&IID_IDirect3DHALDevice, Guid))
    {
546
        trace("HAL Device %d\n", ver);
547 548
        ok(hal->dcmColorModel == D3DCOLOR_RGB, "HAL Device %u hal caps has colormodel %u\n", ver, hel->dcmColorModel);
        ok(hel->dcmColorModel == 0, "HAL Device %u hel caps has colormodel %u\n", ver, hel->dcmColorModel);
549 550 551

        ok(hal->dwFlags != 0, "HAL Device %u hal caps has hardware flags %x\n", ver, hal->dwFlags);
        ok(hel->dwFlags != 0, "HAL Device %u hel caps has hardware flags %x\n", ver, hel->dwFlags);
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
    }
    else if(IsEqualGUID(&IID_IDirect3DRefDevice, Guid))
    {
        ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
           "REF Device %d hal line caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
        ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
           "REF Device %d hal tri caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
        ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
           "REF Device %d hel line caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);
        ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
           "REF Device %d hel tri caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);

        ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
           "REF Device %d hal line caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
        ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
           "REF Device %d hal tri caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
        ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
           "REF Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
        ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
           "REF Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
    }
    else if(IsEqualGUID(&IID_IDirect3DRampDevice, Guid))
    {
        ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
           "Ramp Device %d hal line caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
        ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
           "Ramp Device %d hal tri caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
        ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
           "Ramp Device %d hel line caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);
        ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
           "Ramp Device %d hel tri caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);

        ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
           "Ramp Device %d hal line caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
        ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
           "Ramp Device %d hal tri caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
        ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
           "Ramp Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
        ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
           "Ramp Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
592 593 594 595

        ok(hal->dcmColorModel == 0, "Ramp Device %u hal caps has colormodel %u\n", ver, hal->dcmColorModel);
        ok(hel->dcmColorModel == D3DCOLOR_MONO, "Ramp Device %u hel caps has colormodel %u\n",
                ver, hel->dcmColorModel);
596 597 598

        ok(hal->dwFlags == 0, "Ramp Device %u hal caps has hardware flags %x\n", ver, hal->dwFlags);
        ok(hel->dwFlags != 0, "Ramp Device %u hel caps has hardware flags %x\n", ver, hel->dwFlags);
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
    }
    else if(IsEqualGUID(&IID_IDirect3DMMXDevice, Guid))
    {
        ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
           "MMX Device %d hal line caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
        ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
           "MMX Device %d hal tri caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
        ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
           "MMX Device %d hel line caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);
        ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
           "MMX Device %d hel tri caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);

        ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
           "MMX Device %d hal line caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
        ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
           "MMX Device %d hal tri caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
        ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
           "MMX Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
        ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
           "MMX Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
619 620 621

        ok(hal->dcmColorModel == 0, "MMX Device %u hal caps has colormodel %u\n", ver, hal->dcmColorModel);
        ok(hel->dcmColorModel == D3DCOLOR_RGB, "MMX Device %u hel caps has colormodel %u\n", ver, hel->dcmColorModel);
622 623 624

        ok(hal->dwFlags == 0, "MMX Device %u hal caps has hardware flags %x\n", ver, hal->dwFlags);
        ok(hel->dwFlags != 0, "MMX Device %u hel caps has hardware flags %x\n", ver, hel->dwFlags);
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
    }
    else
    {
        ok(FALSE, "Unexpected device enumerated: \"%s\" \"%s\"\n", DeviceDescription, DeviceName);
        if(hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) trace("hal line has pow2 set\n");
        else trace("hal line does NOT have pow2 set\n");
        if(hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) trace("hal tri has pow2 set\n");
        else trace("hal tri does NOT have pow2 set\n");
        if(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) trace("hel line has pow2 set\n");
        else trace("hel line does NOT have pow2 set\n");
        if(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) trace("hel tri has pow2 set\n");
        else trace("hel tri does NOT have pow2 set\n");
    }
    return DDENUMRET_OK;
}

641 642
static HRESULT WINAPI enumDevicesCallbackTest7(char *DeviceDescription, char *DeviceName,
        D3DDEVICEDESC7 *lpdd7, void *Context)
643
{
644
    D3D7ETest *d3d7et = Context;
645 646 647 648 649 650 651 652 653 654 655 656 657 658
    if(IsEqualGUID(&lpdd7->deviceGUID, &IID_IDirect3DRGBDevice))
        d3d7et->rgb++;
    else if(IsEqualGUID(&lpdd7->deviceGUID, &IID_IDirect3DHALDevice))
        d3d7et->hal++;
    else if(IsEqualGUID(&lpdd7->deviceGUID, &IID_IDirect3DTnLHalDevice))
        d3d7et->tnlhal++;
    else
        d3d7et->unk++;

    d3d7et->total++;

    return DDENUMRET_OK;
}

659 660
static HRESULT WINAPI enumDevicesCancelTest7(char *DeviceDescription, char *DeviceName,
        D3DDEVICEDESC7 *lpdd7, void *Context)
661 662 663 664 665 666 667 668
{
    D3D7ECancelTest *d3d7et = Context;

    d3d7et->total++;

    return d3d7et->desired_ret;
}

669 670
static HRESULT WINAPI enumDevicesLifetimeTest7(char *DeviceDescription, char *DeviceName,
        D3DDEVICEDESC7 *lpdd7, void *Context)
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
{
    D3D7ELifetimeTest *ctx = Context;

    if (ctx->count == MAX_ENUMERATION_COUNT)
    {
        ok(0, "Enumerated too many devices for context in callback\n");
        return DDENUMRET_CANCEL;
    }

    ctx->callback_description_ptrs[ctx->count] = DeviceDescription;
    strcpy(ctx->callback_description_strings[ctx->count], DeviceDescription);
    ctx->callback_name_ptrs[ctx->count] = DeviceName;
    strcpy(ctx->callback_name_strings[ctx->count], DeviceName);

    ctx->count++;
    return DDENUMRET_OK;
}
688 689 690 691 692

/*  Check the deviceGUID of devices enumerated by
    IDirect3D7_EnumDevices. */
static void D3D7EnumTest(void)
{
693
    HRESULT hr;
694
    D3D7ETest d3d7et;
695
    D3D7ECancelTest d3d7_cancel_test;
696

697 698 699
    hr = IDirect3D7_EnumDevices(lpD3D, NULL, NULL);
    ok(hr == DDERR_INVALIDPARAMS, "IDirect3D7_EnumDevices returned 0x%08x\n", hr);

700
    memset(&d3d7et, 0, sizeof(d3d7et));
701 702
    hr = IDirect3D7_EnumDevices(lpD3D, enumDevicesCallbackTest7, &d3d7et);
    ok(hr == D3D_OK, "IDirect3D7_EnumDevices returned 0x%08x\n", hr);
703 704 705 706 707 708 709 710 711

    /* A couple of games (Delta Force LW and TFD) rely on this behaviour */
    ok(d3d7et.tnlhal < d3d7et.total, "TnLHal device enumerated as only device.\n");

    /* We make two additional assumptions. */
    ok(d3d7et.rgb, "No RGB Device enumerated.\n");

    if(d3d7et.tnlhal)
        ok(d3d7et.hal, "TnLHal device enumerated, but no Hal device found.\n");
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

    d3d7_cancel_test.desired_ret = DDENUMRET_CANCEL;
    d3d7_cancel_test.total = 0;
    hr = IDirect3D7_EnumDevices(lpD3D, enumDevicesCancelTest7, &d3d7_cancel_test);
    ok(hr == D3D_OK, "IDirect3D7_EnumDevices returned 0x%08x\n", hr);

    ok(d3d7_cancel_test.total == 1, "Enumerated a total of %u devices\n",
       d3d7_cancel_test.total);

    /* An enumeration callback can return any value besides DDENUMRET_OK to stop enumeration. */
    d3d7_cancel_test.desired_ret = E_INVALIDARG;
    d3d7_cancel_test.total = 0;
    hr = IDirect3D7_EnumDevices(lpD3D, enumDevicesCancelTest7, &d3d7_cancel_test);
    ok(hr == D3D_OK, "IDirect3D7_EnumDevices returned 0x%08x\n", hr);

    ok(d3d7_cancel_test.total == 1, "Enumerated a total of %u devices\n",
       d3d7_cancel_test.total);
729 730
}

731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
static void D3D7EnumLifetimeTest(void)
{
    D3D7ELifetimeTest ctx, ctx2;
    HRESULT hr;
    unsigned int i;

    ctx.count = 0;
    hr = IDirect3D7_EnumDevices(lpD3D, enumDevicesLifetimeTest7, &ctx);
    ok(hr == D3D_OK, "IDirect3D7_EnumDevices returned 0x%08x\n", hr);

    /* The enumeration strings remain valid even after IDirect3D7_EnumDevices finishes. */
    for (i = 0; i < ctx.count; i++)
    {
        ok(!strcmp(ctx.callback_description_ptrs[i], ctx.callback_description_strings[i]),
           "Got '%s' and '%s'\n", ctx.callback_description_ptrs[i], ctx.callback_description_strings[i]);
        ok(!strcmp(ctx.callback_name_ptrs[i], ctx.callback_name_strings[i]),
           "Got '%s' and '%s'\n", ctx.callback_name_ptrs[i], ctx.callback_name_strings[i]);
    }

    ctx2.count = 0;
    hr = IDirect3D7_EnumDevices(lpD3D, enumDevicesLifetimeTest7, &ctx2);
    ok(hr == D3D_OK, "IDirect3D7_EnumDevices returned 0x%08x\n", hr);

    /* The enumeration strings and their order are identical across enumerations. */
    ok(ctx.count == ctx2.count, "Enumerated %u and %u devices\n", ctx.count, ctx2.count);
    if (ctx.count == ctx2.count)
    {
        for (i = 0; i < ctx.count; i++)
        {
            ok(ctx.callback_description_ptrs[i] == ctx2.callback_description_ptrs[i],
               "Unequal description pointers %p and %p\n", ctx.callback_description_ptrs[i], ctx2.callback_description_ptrs[i]);
            ok(!strcmp(ctx.callback_description_strings[i], ctx2.callback_description_strings[i]),
               "Got '%s' and '%s'\n", ctx.callback_description_strings[i], ctx2.callback_description_strings[i]);
            ok(ctx.callback_name_ptrs[i] == ctx2.callback_name_ptrs[i],
               "Unequal name pointers %p and %p\n", ctx.callback_name_ptrs[i], ctx2.callback_name_ptrs[i]);
            ok(!strcmp(ctx.callback_name_strings[i], ctx2.callback_name_strings[i]),
               "Got '%s' and '%s'\n", ctx.callback_name_strings[i], ctx2.callback_name_strings[i]);
        }
    }

    /* Try altering the contents of the enumeration strings. */
    for (i = 0; i < ctx2.count; i++)
    {
        strcpy(ctx2.callback_description_ptrs[i], "Fake Description");
        strcpy(ctx2.callback_name_ptrs[i], "Fake Device");
    }

    ctx2.count = 0;
    hr = IDirect3D7_EnumDevices(lpD3D, enumDevicesLifetimeTest7, &ctx2);
    ok(hr == D3D_OK, "IDirect3D7_EnumDevices returned 0x%08x\n", hr);

    /* The original contents of the enumeration strings are not restored. */
    ok(ctx.count == ctx2.count, "Enumerated %u and %u devices\n", ctx.count, ctx2.count);
    if (ctx.count == ctx2.count)
    {
        for (i = 0; i < ctx.count; i++)
        {
            ok(ctx.callback_description_ptrs[i] == ctx2.callback_description_ptrs[i],
               "Unequal description pointers %p and %p\n", ctx.callback_description_ptrs[i], ctx2.callback_description_ptrs[i]);
            ok(strcmp(ctx.callback_description_strings[i], ctx2.callback_description_strings[i]) != 0,
               "Got '%s' and '%s'\n", ctx.callback_description_strings[i], ctx2.callback_description_strings[i]);
            ok(ctx.callback_name_ptrs[i] == ctx2.callback_name_ptrs[i],
               "Unequal name pointers %p and %p\n", ctx.callback_name_ptrs[i], ctx2.callback_name_ptrs[i]);
            ok(strcmp(ctx.callback_name_strings[i], ctx2.callback_name_strings[i]) != 0,
               "Got '%s' and '%s'\n", ctx.callback_name_strings[i], ctx2.callback_name_strings[i]);
        }
    }
}

800 801 802 803 804 805 806 807 808 809 810 811
static void CapsTest(void)
{
    IDirect3D3 *d3d3;
    IDirect3D3 *d3d2;
    IDirectDraw *dd1;
    HRESULT hr;
    UINT ver;

    hr = DirectDrawCreate(NULL, &dd1, NULL);
    ok(hr == DD_OK, "Cannot create a DirectDraw 1 interface, hr = %08x\n", hr);
    hr = IDirectDraw_QueryInterface(dd1, &IID_IDirect3D3, (void **) &d3d3);
    ok(hr == D3D_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
812 813 814 815

    hr = IDirect3D3_EnumDevices(d3d3, NULL, NULL);
    ok(hr == DDERR_INVALIDPARAMS, "IDirect3D3_EnumDevices returned 0x%08x\n", hr);

816 817 818 819 820 821 822 823 824 825
    ver = 3;
    IDirect3D3_EnumDevices(d3d3, enumDevicesCallback, &ver);

    IDirect3D3_Release(d3d3);
    IDirectDraw_Release(dd1);

    hr = DirectDrawCreate(NULL, &dd1, NULL);
    ok(hr == DD_OK, "Cannot create a DirectDraw 1 interface, hr = %08x\n", hr);
    hr = IDirectDraw_QueryInterface(dd1, &IID_IDirect3D2, (void **) &d3d2);
    ok(hr == D3D_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
826 827 828 829

    hr = IDirect3D2_EnumDevices(d3d2, NULL, NULL);
    ok(hr == DDERR_INVALIDPARAMS, "IDirect3D2_EnumDevices returned 0x%08x\n", hr);

830 831 832 833 834 835 836
    ver = 2;
    IDirect3D2_EnumDevices(d3d2, enumDevicesCallback, &ver);

    IDirect3D2_Release(d3d2);
    IDirectDraw_Release(dd1);
}

837 838 839 840 841 842 843
struct v_in {
    float x, y, z;
};
struct v_out {
    float x, y, z, rhw;
};

844
static BOOL D3D1_createObjects(void)
845 846 847 848 849 850 851
{
    HRESULT hr;
    DDSURFACEDESC ddsd;
    D3DEXECUTEBUFFERDESC desc;
    D3DVIEWPORT vp_data;

    /* An IDirect3DDevice cannot be queryInterfaced from an IDirect3DDevice7 on windows */
852
    hr = DirectDrawCreate(NULL, &DirectDraw1, NULL);
853
    ok(hr==DD_OK || hr==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreate returned: %x\n", hr);
854 855
    if (!DirectDraw1) {
        return FALSE;
856 857
    }

858
    hr = IDirectDraw_SetCooperativeLevel(DirectDraw1, NULL, DDSCL_NORMAL);
859 860
    ok(hr==DD_OK, "SetCooperativeLevel returned: %x\n", hr);

861
    hr = IDirectDraw_QueryInterface(DirectDraw1, &IID_IDirect3D, (void**) &Direct3D1);
862
    if (hr == E_NOINTERFACE) return FALSE;
863
    ok(hr==DD_OK, "QueryInterface returned: %x\n", hr);
864 865 866
    if (!Direct3D1) {
        return FALSE;
    }
867 868 869 870 871 872 873

    memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
    ddsd.dwWidth = 256;
    ddsd.dwHeight = 256;
874
    IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &Surface1, NULL);
875
    if (!Surface1) {
876
        skip("DDSCAPS_3DDEVICE surface not available\n");
877 878
        return FALSE;
    }
879

880
    hr = IDirectDrawSurface_QueryInterface(Surface1, &IID_IDirect3DRGBDevice, (void **) &Direct3DDevice1);
881
    ok(hr==D3D_OK || hr==DDERR_NOPALETTEATTACHED || hr==E_OUTOFMEMORY, "CreateDevice returned: %x\n", hr);
882 883 884
    if(!Direct3DDevice1) {
        return FALSE;
    }
885 886 887 888 889 890 891

    memset(&desc, 0, sizeof(desc));
    desc.dwSize = sizeof(desc);
    desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
    desc.dwCaps = D3DDEBCAPS_VIDEOMEMORY;
    desc.dwBufferSize = 128;
    desc.lpData = NULL;
892
    hr = IDirect3DDevice_CreateExecuteBuffer(Direct3DDevice1, &desc, &ExecuteBuffer, NULL);
893
    ok(hr == D3D_OK, "IDirect3DDevice_CreateExecuteBuffer failed: %08x\n", hr);
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
    if(!ExecuteBuffer) {
        return FALSE;
    }

    hr = IDirect3D_CreateViewport(Direct3D1, &Viewport, NULL);
    ok(hr == D3D_OK, "IDirect3D_CreateViewport failed: %08x\n", hr);
    if(!Viewport) {
        return FALSE;
    }

    hr = IDirect3DViewport_Initialize(Viewport, Direct3D1);
    ok(hr == DDERR_ALREADYINITIALIZED, "IDirect3DViewport_Initialize returned %08x\n", hr);

    hr = IDirect3DDevice_AddViewport(Direct3DDevice1, Viewport);
    ok(hr == D3D_OK, "IDirect3DDevice_AddViewport returned %08x\n", hr);
    vp_data.dwSize = sizeof(vp_data);
    vp_data.dwX = 0;
    vp_data.dwY = 0;
    vp_data.dwWidth = 256;
    vp_data.dwHeight = 256;
    vp_data.dvScaleX = 1;
    vp_data.dvScaleY = 1;
    vp_data.dvMaxX = 256;
    vp_data.dvMaxY = 256;
    vp_data.dvMinZ = 0;
    vp_data.dvMaxZ = 1;
    hr = IDirect3DViewport_SetViewport(Viewport, &vp_data);
    ok(hr == D3D_OK, "IDirect3DViewport_SetViewport returned %08x\n", hr);

923 924 925 926 927
    hr = IDirect3D_CreateLight(Direct3D1, &Light, NULL);
    ok(hr == D3D_OK, "IDirect3D_CreateLight failed: %08x\n", hr);
    if (!Light)
        return FALSE;

928 929 930 931 932
    return TRUE;
}

static void D3D1_releaseObjects(void)
{
933
    if (Light) IDirect3DLight_Release(Light);
934 935 936 937 938 939 940 941
    if (Viewport) IDirect3DViewport_Release(Viewport);
    if (ExecuteBuffer) IDirect3DExecuteBuffer_Release(ExecuteBuffer);
    if (Direct3DDevice1) IDirect3DDevice_Release(Direct3DDevice1);
    if (Surface1) IDirectDrawSurface_Release(Surface1);
    if (Direct3D1) IDirect3D_Release(Direct3D1);
    if (DirectDraw1) IDirectDraw_Release(DirectDraw1);
}

942 943 944
static void ViewportTest(void)
{
    HRESULT hr;
945
    IDirect3DViewport2 *Viewport2;
946
    IDirect3DViewport3 *Viewport3;
947 948 949 950 951 952 953 954 955 956 957
    D3DVIEWPORT vp1_data, ret_vp1_data;
    D3DVIEWPORT2 vp2_data, ret_vp2_data;
    float infinity;

    *(DWORD*)&infinity = 0x7f800000;

    hr = IDirect3DDevice_AddViewport(Direct3DDevice1, Viewport);
    ok(hr == D3D_OK, "IDirect3DDevice_AddViewport returned %08x\n", hr);

    hr = IDirect3DViewport_QueryInterface(Viewport, &IID_IDirect3DViewport2, (void**) &Viewport2);
    ok(hr==D3D_OK, "QueryInterface returned: %x\n", hr);
958
    ok(Viewport2 == (IDirect3DViewport2 *)Viewport, "IDirect3DViewport2 iface different from IDirect3DViewport\n");
959 960 961

    hr = IDirect3DViewport_QueryInterface(Viewport, &IID_IDirect3DViewport3, (void**) &Viewport3);
    ok(hr==D3D_OK, "QueryInterface returned: %x\n", hr);
962
    ok(Viewport3 == (IDirect3DViewport3 *)Viewport, "IDirect3DViewport3 iface different from IDirect3DViewport\n");
963
    IDirect3DViewport3_Release(Viewport3);
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114

    vp1_data.dwSize = sizeof(vp1_data);
    vp1_data.dwX = 0;
    vp1_data.dwY = 1;
    vp1_data.dwWidth = 256;
    vp1_data.dwHeight = 257;
    vp1_data.dvMaxX = 0;
    vp1_data.dvMaxY = 0;
    vp1_data.dvScaleX = 0;
    vp1_data.dvScaleY = 0;
    vp1_data.dvMinZ = 0.25;
    vp1_data.dvMaxZ = 0.75;

    vp2_data.dwSize = sizeof(vp2_data);
    vp2_data.dwX = 2;
    vp2_data.dwY = 3;
    vp2_data.dwWidth = 258;
    vp2_data.dwHeight = 259;
    vp2_data.dvClipX = 0;
    vp2_data.dvClipY = 0;
    vp2_data.dvClipWidth = 0;
    vp2_data.dvClipHeight = 0;
    vp2_data.dvMinZ = 0.1;
    vp2_data.dvMaxZ = 0.9;

    hr = IDirect3DViewport2_SetViewport(Viewport2, &vp1_data);
    ok(hr == D3D_OK, "IDirect3DViewport2_SetViewport returned %08x\n", hr);

    memset(&ret_vp1_data, 0xff, sizeof(ret_vp1_data));
    ret_vp1_data.dwSize = sizeof(vp1_data);

    hr = IDirect3DViewport2_GetViewport(Viewport2, &ret_vp1_data);
    ok(hr == D3D_OK, "IDirect3DViewport2_GetViewport returned %08x\n", hr);

    ok(ret_vp1_data.dwX == vp1_data.dwX, "dwX is %u, expected %u\n", ret_vp1_data.dwX, vp1_data.dwX);
    ok(ret_vp1_data.dwY == vp1_data.dwY, "dwY is %u, expected %u\n", ret_vp1_data.dwY, vp1_data.dwY);
    ok(ret_vp1_data.dwWidth == vp1_data.dwWidth, "dwWidth is %u, expected %u\n", ret_vp1_data.dwWidth, vp1_data.dwWidth);
    ok(ret_vp1_data.dwHeight == vp1_data.dwHeight, "dwHeight is %u, expected %u\n", ret_vp1_data.dwHeight, vp1_data.dwHeight);
    ok(ret_vp1_data.dvMaxX == vp1_data.dvMaxX, "dvMaxX is %f, expected %f\n", ret_vp1_data.dvMaxX, vp1_data.dvMaxX);
    ok(ret_vp1_data.dvMaxY == vp1_data.dvMaxY, "dvMaxY is %f, expected %f\n", ret_vp1_data.dvMaxY, vp1_data.dvMaxY);
    todo_wine ok(ret_vp1_data.dvScaleX == infinity, "dvScaleX is %f, expected %f\n", ret_vp1_data.dvScaleX, infinity);
    todo_wine ok(ret_vp1_data.dvScaleY == infinity, "dvScaleY is %f, expected %f\n", ret_vp1_data.dvScaleY, infinity);
    ok(ret_vp1_data.dvMinZ == 0.0, "dvMinZ is %f, expected 0.0\n", ret_vp1_data.dvMinZ);
    ok(ret_vp1_data.dvMaxZ == 1.0, "dvMaxZ is %f, expected 1.0\n", ret_vp1_data.dvMaxZ);

    hr = IDirect3DViewport2_SetViewport2(Viewport2, &vp2_data);
    ok(hr == D3D_OK, "IDirect3DViewport2_SetViewport2 returned %08x\n", hr);

    memset(&ret_vp2_data, 0xff, sizeof(ret_vp2_data));
    ret_vp2_data.dwSize = sizeof(vp2_data);

    hr = IDirect3DViewport2_GetViewport2(Viewport2, &ret_vp2_data);
    ok(hr == D3D_OK, "IDirect3DViewport2_GetViewport2 returned %08x\n", hr);

    ok(ret_vp2_data.dwX == vp2_data.dwX, "dwX is %u, expected %u\n", ret_vp2_data.dwX, vp2_data.dwX);
    ok(ret_vp2_data.dwY == vp2_data.dwY, "dwY is %u, expected %u\n", ret_vp2_data.dwY, vp2_data.dwY);
    ok(ret_vp2_data.dwWidth == vp2_data.dwWidth, "dwWidth is %u, expected %u\n", ret_vp2_data.dwWidth, vp2_data.dwWidth);
    ok(ret_vp2_data.dwHeight == vp2_data.dwHeight, "dwHeight is %u, expected %u\n", ret_vp2_data.dwHeight, vp2_data.dwHeight);
    ok(ret_vp2_data.dvClipX == vp2_data.dvClipX, "dvClipX is %f, expected %f\n", ret_vp2_data.dvClipX, vp2_data.dvClipX);
    ok(ret_vp2_data.dvClipY == vp2_data.dvClipY, "dvClipY is %f, expected %f\n", ret_vp2_data.dvClipY, vp2_data.dvClipY);
    ok(ret_vp2_data.dvClipWidth == vp2_data.dvClipWidth, "dvClipWidth is %f, expected %f\n",
        ret_vp2_data.dvClipWidth, vp2_data.dvClipWidth);
    ok(ret_vp2_data.dvClipHeight == vp2_data.dvClipHeight, "dvClipHeight is %f, expected %f\n",
        ret_vp2_data.dvClipHeight, vp2_data.dvClipHeight);
    ok(ret_vp2_data.dvMinZ == vp2_data.dvMinZ, "dvMinZ is %f, expected %f\n", ret_vp2_data.dvMinZ, vp2_data.dvMinZ);
    ok(ret_vp2_data.dvMaxZ == vp2_data.dvMaxZ, "dvMaxZ is %f, expected %f\n", ret_vp2_data.dvMaxZ, vp2_data.dvMaxZ);

    memset(&ret_vp1_data, 0xff, sizeof(ret_vp1_data));
    ret_vp1_data.dwSize = sizeof(vp1_data);

    hr = IDirect3DViewport2_GetViewport(Viewport2, &ret_vp1_data);
    ok(hr == D3D_OK, "IDirect3DViewport2_GetViewport returned %08x\n", hr);

    ok(ret_vp1_data.dwX == vp2_data.dwX, "dwX is %u, expected %u\n", ret_vp1_data.dwX, vp2_data.dwX);
    ok(ret_vp1_data.dwY == vp2_data.dwY, "dwY is %u, expected %u\n", ret_vp1_data.dwY, vp2_data.dwY);
    ok(ret_vp1_data.dwWidth == vp2_data.dwWidth, "dwWidth is %u, expected %u\n", ret_vp1_data.dwWidth, vp2_data.dwWidth);
    ok(ret_vp1_data.dwHeight == vp2_data.dwHeight, "dwHeight is %u, expected %u\n", ret_vp1_data.dwHeight, vp2_data.dwHeight);
    ok(ret_vp1_data.dvMaxX == vp1_data.dvMaxX, "dvMaxX is %f, expected %f\n", ret_vp1_data.dvMaxX, vp1_data.dvMaxX);
    ok(ret_vp1_data.dvMaxY == vp1_data.dvMaxY, "dvMaxY is %f, expected %f\n", ret_vp1_data.dvMaxY, vp1_data.dvMaxY);
    todo_wine ok(ret_vp1_data.dvScaleX == infinity, "dvScaleX is %f, expected %f\n", ret_vp1_data.dvScaleX, infinity);
    todo_wine ok(ret_vp1_data.dvScaleY == infinity, "dvScaleY is %f, expected %f\n", ret_vp1_data.dvScaleY, infinity);
    todo_wine ok(ret_vp1_data.dvMinZ == 0.0, "dvMinZ is %f, expected 0.0\n", ret_vp1_data.dvMinZ);
    todo_wine ok(ret_vp1_data.dvMaxZ == 1.0, "dvMaxZ is %f, expected 1.0\n", ret_vp1_data.dvMaxZ);

    hr = IDirect3DViewport2_SetViewport2(Viewport2, &vp2_data);
    ok(hr == D3D_OK, "IDirect3DViewport2_SetViewport2 returned %08x\n", hr);

    memset(&ret_vp2_data, 0xff, sizeof(ret_vp2_data));
    ret_vp2_data.dwSize = sizeof(vp2_data);

    hr = IDirect3DViewport2_GetViewport2(Viewport2, &ret_vp2_data);
    ok(hr == D3D_OK, "IDirect3DViewport2_GetViewport2 returned %08x\n", hr);

    ok(ret_vp2_data.dwX == vp2_data.dwX, "dwX is %u, expected %u\n", ret_vp2_data.dwX, vp2_data.dwX);
    ok(ret_vp2_data.dwY == vp2_data.dwY, "dwY is %u, expected %u\n", ret_vp2_data.dwY, vp2_data.dwY);
    ok(ret_vp2_data.dwWidth == vp2_data.dwWidth, "dwWidth is %u, expected %u\n", ret_vp2_data.dwWidth, vp2_data.dwWidth);
    ok(ret_vp2_data.dwHeight == vp2_data.dwHeight, "dwHeight is %u, expected %u\n", ret_vp2_data.dwHeight, vp2_data.dwHeight);
    ok(ret_vp2_data.dvClipX == vp2_data.dvClipX, "dvClipX is %f, expected %f\n", ret_vp2_data.dvClipX, vp2_data.dvClipX);
    ok(ret_vp2_data.dvClipY == vp2_data.dvClipY, "dvClipY is %f, expected %f\n", ret_vp2_data.dvClipY, vp2_data.dvClipY);
    ok(ret_vp2_data.dvClipWidth == vp2_data.dvClipWidth, "dvClipWidth is %f, expected %f\n",
        ret_vp2_data.dvClipWidth, vp2_data.dvClipWidth);
    ok(ret_vp2_data.dvClipHeight == vp2_data.dvClipHeight, "dvClipHeight is %f, expected %f\n",
        ret_vp2_data.dvClipHeight, vp2_data.dvClipHeight);
    ok(ret_vp2_data.dvMinZ == vp2_data.dvMinZ, "dvMinZ is %f, expected %f\n", ret_vp2_data.dvMinZ, vp2_data.dvMinZ);
    ok(ret_vp2_data.dvMaxZ == vp2_data.dvMaxZ, "dvMaxZ is %f, expected %f\n", ret_vp2_data.dvMaxZ, vp2_data.dvMaxZ);

    hr = IDirect3DViewport2_SetViewport(Viewport2, &vp1_data);
    ok(hr == D3D_OK, "IDirect3DViewport2_SetViewport returned %08x\n", hr);

    memset(&ret_vp1_data, 0xff, sizeof(ret_vp1_data));
    ret_vp1_data.dwSize = sizeof(vp1_data);

    hr = IDirect3DViewport2_GetViewport(Viewport2, &ret_vp1_data);
    ok(hr == D3D_OK, "IDirect3DViewport2_GetViewport returned %08x\n", hr);

    ok(ret_vp1_data.dwX == vp1_data.dwX, "dwX is %u, expected %u\n", ret_vp1_data.dwX, vp1_data.dwX);
    ok(ret_vp1_data.dwY == vp1_data.dwY, "dwY is %u, expected %u\n", ret_vp1_data.dwY, vp1_data.dwY);
    ok(ret_vp1_data.dwWidth == vp1_data.dwWidth, "dwWidth is %u, expected %u\n", ret_vp1_data.dwWidth, vp1_data.dwWidth);
    ok(ret_vp1_data.dwHeight == vp1_data.dwHeight, "dwHeight is %u, expected %u\n", ret_vp1_data.dwHeight, vp1_data.dwHeight);
    ok(ret_vp1_data.dvMaxX == vp1_data.dvMaxX, "dvMaxX is %f, expected %f\n", ret_vp1_data.dvMaxX, vp1_data.dvMaxX);
    ok(ret_vp1_data.dvMaxY == vp1_data.dvMaxY, "dvMaxY is %f, expected %f\n", ret_vp1_data.dvMaxY, vp1_data.dvMaxY);
    todo_wine ok(ret_vp1_data.dvScaleX == infinity, "dvScaleX is %f, expected %f\n", ret_vp1_data.dvScaleX, infinity);
    todo_wine ok(ret_vp1_data.dvScaleY == infinity, "dvScaleY is %f, expected %f\n", ret_vp1_data.dvScaleY, infinity);
    ok(ret_vp1_data.dvMinZ == 0.0, "dvMinZ is %f, expected 0.0\n", ret_vp1_data.dvMinZ);
    ok(ret_vp1_data.dvMaxZ == 1.0, "dvMaxZ is %f, expected 1.0\n", ret_vp1_data.dvMaxZ);

    memset(&ret_vp2_data, 0xff, sizeof(ret_vp2_data));
    ret_vp2_data.dwSize = sizeof(vp2_data);

    hr = IDirect3DViewport2_GetViewport2(Viewport2, &ret_vp2_data);
    ok(hr == D3D_OK, "IDirect3DViewport2_GetViewport2 returned %08x\n", hr);

    ok(ret_vp2_data.dwX == vp1_data.dwX, "dwX is %u, expected %u\n", ret_vp2_data.dwX, vp1_data.dwX);
    ok(ret_vp2_data.dwY == vp1_data.dwY, "dwY is %u, expected %u\n", ret_vp2_data.dwY, vp1_data.dwY);
    ok(ret_vp2_data.dwWidth == vp1_data.dwWidth, "dwWidth is %u, expected %u\n", ret_vp2_data.dwWidth, vp1_data.dwWidth);
    ok(ret_vp2_data.dwHeight == vp1_data.dwHeight, "dwHeight is %u, expected %u\n", ret_vp2_data.dwHeight, vp1_data.dwHeight);
    ok(ret_vp2_data.dvClipX == vp2_data.dvClipX, "dvClipX is %f, expected %f\n", ret_vp2_data.dvClipX, vp2_data.dvClipX);
    ok(ret_vp2_data.dvClipY == vp2_data.dvClipY, "dvClipY is %f, expected %f\n", ret_vp2_data.dvClipY, vp2_data.dvClipY);
    ok(ret_vp2_data.dvClipWidth == vp2_data.dvClipWidth, "dvClipWidth is %f, expected %f\n",
        ret_vp2_data.dvClipWidth, vp2_data.dvClipWidth);
    ok(ret_vp2_data.dvClipHeight == vp2_data.dvClipHeight, "dvClipHeight is %f, expected %f\n",
        ret_vp2_data.dvClipHeight, vp2_data.dvClipHeight);
    ok(ret_vp2_data.dvMinZ == 0.0, "dvMinZ is %f, expected 0.0\n", ret_vp2_data.dvMinZ);
    ok(ret_vp2_data.dvMaxZ == 1.0, "dvMaxZ is %f, expected 1.0\n", ret_vp2_data.dvMaxZ);

    IDirect3DViewport2_Release(Viewport2);

    hr = IDirect3DDevice_DeleteViewport(Direct3DDevice1, Viewport);
    ok(hr == D3D_OK, "IDirect3DDevice_DeleteViewport returned %08x\n", hr);
}

1115 1116 1117 1118 1119 1120
static void Direct3D1Test(void)
{
    HRESULT hr;
    D3DEXECUTEBUFFERDESC desc;
    D3DINSTRUCTION *instr;
    D3DBRANCH *branch;
1121
    IDirect3D *Direct3D_alt;
1122 1123
    IDirect3DLight *d3dlight;
    ULONG refcount;
1124
    unsigned int idx = 0;
1125

1126 1127 1128
    /* Interface consistency check. */
    hr = IDirect3DDevice_GetDirect3D(Direct3DDevice1, &Direct3D_alt);
    ok(hr == D3D_OK, "IDirect3DDevice_GetDirect3D failed: %08x\n", hr);
1129
    ok(Direct3D_alt == Direct3D1, "Direct3D1 struct pointer mismatch: %p != %p\n", Direct3D_alt, Direct3D1);
1130
    IDirect3D_Release(Direct3D_alt);
1131

1132 1133
    memset(&desc, 0, sizeof(desc));
    desc.dwSize = sizeof(desc);
1134
    hr = IDirect3DExecuteBuffer_Lock(ExecuteBuffer, &desc);
1135
    ok(hr == D3D_OK, "IDirect3DExecuteBuffer_Lock failed: %08x\n", hr);
1136 1137

    memset(desc.lpData, 0, 128);
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
    instr = desc.lpData;
    instr[idx].bOpcode = D3DOP_BRANCHFORWARD;
    instr[idx].bSize = sizeof(*branch);
    instr[idx].wCount = 1;
    idx++;
    branch = (D3DBRANCH *) &instr[idx];
    branch->dwMask = 0x0;
    branch->dwValue = 1;
    branch->bNegate = TRUE;
    branch->dwOffset = 0;
    idx += (sizeof(*branch) / sizeof(*instr));
    instr[idx].bOpcode = D3DOP_EXIT;
    instr[idx].bSize = 0;
1151
    instr[idx].wCount = 0;
1152
    hr = IDirect3DExecuteBuffer_Unlock(ExecuteBuffer);
1153 1154
    ok(hr == D3D_OK, "IDirect3DExecuteBuffer_Unlock failed: %08x\n", hr);

1155
    hr = IDirect3DDevice_Execute(Direct3DDevice1, ExecuteBuffer, Viewport, D3DEXECUTE_CLIPPED);
1156 1157
    ok(hr == D3D_OK, "IDirect3DDevice_Execute returned %08x\n", hr);

1158 1159 1160
    memset(&desc, 0, sizeof(desc));
    desc.dwSize = sizeof(desc);

1161
    hr = IDirect3DExecuteBuffer_Lock(ExecuteBuffer, &desc);
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
    ok(hr == D3D_OK, "IDirect3DExecuteBuffer_Lock failed: %08x\n", hr);

    memset(desc.lpData, 0, 128);
    instr = desc.lpData;
    idx = 0;
    instr[idx].bOpcode = D3DOP_BRANCHFORWARD;
    instr[idx].bSize = sizeof(*branch);
    instr[idx].wCount = 1;
    idx++;
    branch = (D3DBRANCH *) &instr[idx];
    branch->dwMask = 0x0;
    branch->dwValue = 1;
    branch->bNegate = TRUE;
    branch->dwOffset = 64;
    instr = (D3DINSTRUCTION*)((char*)desc.lpData + 64);
    instr[0].bOpcode = D3DOP_EXIT;
    instr[0].bSize = 0;
    instr[0].wCount = 0;
1180
    hr = IDirect3DExecuteBuffer_Unlock(ExecuteBuffer);
1181 1182
    ok(hr == D3D_OK, "IDirect3DExecuteBuffer_Unlock failed: %08x\n", hr);

1183
    hr = IDirect3DDevice_Execute(Direct3DDevice1, ExecuteBuffer, Viewport, D3DEXECUTE_CLIPPED);
1184 1185
    ok(hr == D3D_OK, "IDirect3DDevice_Execute returned %08x\n", hr);

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
    /* Test rendering 0 triangles */
    memset(&desc, 0, sizeof(desc));
    desc.dwSize = sizeof(desc);

    hr = IDirect3DExecuteBuffer_Lock(ExecuteBuffer, &desc);
    ok(hr == D3D_OK, "IDirect3DExecuteBuffer_Lock failed: %08x\n", hr);

    memset(desc.lpData, 0, 128);
    instr = desc.lpData;

    instr->bOpcode = D3DOP_TRIANGLE;
    instr->bSize = sizeof(D3DOP_TRIANGLE);
    instr->wCount = 0;
1199
    instr++;
1200 1201 1202 1203 1204 1205 1206 1207 1208
    instr->bOpcode = D3DOP_EXIT;
    instr->bSize = 0;
    instr->wCount = 0;
    hr = IDirect3DExecuteBuffer_Unlock(ExecuteBuffer);
    ok(hr == D3D_OK, "IDirect3DExecuteBuffer_Unlock failed: %08x\n", hr);

    hr = IDirect3DDevice_Execute(Direct3DDevice1, ExecuteBuffer, Viewport, D3DEXECUTE_CLIPPED);
    ok(hr == D3D_OK, "IDirect3DDevice_Execute returned %08x\n", hr);

1209
    hr = IDirect3DDevice_DeleteViewport(Direct3DDevice1, Viewport);
1210
    ok(hr == D3D_OK, "IDirect3DDevice_DeleteViewport returned %08x\n", hr);
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228

    hr = IDirect3DViewport_AddLight(Viewport, Light);
    ok(hr == D3D_OK, "IDirect3DViewport_AddLight returned %08x\n", hr);
    refcount = getRefcount((IUnknown*) Light);
    ok(refcount == 2, "Refcount should be 2, returned is %d\n", refcount);

    hr = IDirect3DViewport_NextLight(Viewport, NULL, &d3dlight, D3DNEXT_HEAD);
    ok(hr == D3D_OK, "IDirect3DViewport_AddLight returned %08x\n", hr);
    ok(d3dlight == Light, "Got different light returned %p, expected %p\n", d3dlight, Light);
    refcount = getRefcount((IUnknown*) Light);
    ok(refcount == 3, "Refcount should be 2, returned is %d\n", refcount);

    hr = IDirect3DViewport_DeleteLight(Viewport, Light);
    ok(hr == D3D_OK, "IDirect3DViewport_DeleteLight returned %08x\n", hr);
    refcount = getRefcount((IUnknown*) Light);
    ok(refcount == 2, "Refcount should be 2, returned is %d\n", refcount);

    IDirect3DLight_Release(Light);
1229 1230
}

1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
static BOOL colortables_check_equality(PALETTEENTRY table1[256], PALETTEENTRY table2[256])
{
    int i;

    for (i = 0; i < 256; i++) {
       if (table1[i].peRed != table2[i].peRed || table1[i].peGreen != table2[i].peGreen ||
           table1[i].peBlue != table2[i].peBlue) return FALSE;
    }

    return TRUE;
}

/* test palette handling in IDirect3DTexture_Load */
static void TextureLoadTest(void)
{
    IDirectDrawSurface *TexSurface = NULL;
    IDirect3DTexture *Texture = NULL;
    IDirectDrawSurface *TexSurface2 = NULL;
    IDirect3DTexture *Texture2 = NULL;
    IDirectDrawPalette *palette = NULL;
    IDirectDrawPalette *palette2 = NULL;
    IDirectDrawPalette *palette_tmp = NULL;
    PALETTEENTRY table1[256], table2[256], table_tmp[256];
    HRESULT hr;
    DDSURFACEDESC ddsd;
    int i;

    memset (&ddsd, 0, sizeof (ddsd));
    ddsd.dwSize = sizeof (ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
    ddsd.dwHeight = 128;
    ddsd.dwWidth = 128;
    ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
    ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
    ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_PALETTEINDEXED8;
    U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 8;

    hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &TexSurface, NULL);
    ok(hr==D3D_OK, "CreateSurface returned: %x\n", hr);
    if (FAILED(hr)) {
        skip("IDirectDraw_CreateSurface failed; skipping further tests\n");
        goto cleanup;
    }

    hr = IDirectDrawSurface_QueryInterface(TexSurface, &IID_IDirect3DTexture,
                (void *)&Texture);
    ok(hr==D3D_OK, "IDirectDrawSurface_QueryInterface returned: %x\n", hr);
    if (FAILED(hr)) {
        skip("Can't get IDirect3DTexture interface; skipping further tests\n");
        goto cleanup;
    }

    hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &TexSurface2, NULL);
    ok(hr==D3D_OK, "CreateSurface returned: %x\n", hr);
    if (FAILED(hr)) {
        skip("IDirectDraw_CreateSurface failed; skipping further tests\n");
        goto cleanup;
    }

    hr = IDirectDrawSurface_QueryInterface(TexSurface2, &IID_IDirect3DTexture,
                (void *)&Texture2);
    ok(hr==D3D_OK, "IDirectDrawSurface_QueryInterface returned: %x\n", hr);
    if (FAILED(hr)) {
        skip("Can't get IDirect3DTexture interface; skipping further tests\n");
        goto cleanup;
    }

1298 1299 1300 1301
    /* test load of Texture to Texture */
    hr = IDirect3DTexture_Load(Texture, Texture);
    ok(hr == DD_OK, "IDirect3DTexture_Load returned %08x\n", hr);

1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
    /* test Load when both textures have no palette */
    hr = IDirect3DTexture_Load(Texture2, Texture);
    ok(hr == DD_OK, "IDirect3DTexture_Load returned %08x\n", hr);

    for (i = 0; i < 256; i++) {
        table1[i].peRed = i;
        table1[i].peGreen = i;
        table1[i].peBlue = i;
        table1[i].peFlags = 0;
    }

    hr = IDirectDraw_CreatePalette(DirectDraw1, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, table1, &palette, NULL);
    ok(hr == DD_OK, "CreatePalette returned %08x\n", hr);
    if (FAILED(hr)) {
        skip("IDirectDraw_CreatePalette failed; skipping further tests\n");
        goto cleanup;
    }

    /* test Load when source texture has palette and destination has no palette */
    hr = IDirectDrawSurface_SetPalette(TexSurface, palette);
    ok(hr == DD_OK, "IDirectDrawSurface_SetPalette returned %08x\n", hr);
    hr = IDirect3DTexture_Load(Texture2, Texture);
    ok(hr == DDERR_NOPALETTEATTACHED, "IDirect3DTexture_Load returned %08x\n", hr);

    for (i = 0; i < 256; i++) {
        table2[i].peRed = 255 - i;
        table2[i].peGreen = 255 - i;
        table2[i].peBlue = 255 - i;
        table2[i].peFlags = 0;
    }

    hr = IDirectDraw_CreatePalette(DirectDraw1, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, table2, &palette2, NULL);
    ok(hr == DD_OK, "CreatePalette returned %08x\n", hr);
    if (FAILED(hr)) {
        skip("IDirectDraw_CreatePalette failed; skipping further tests\n");
        goto cleanup;
    }

    /* test Load when source has no palette and destination has a palette */
    hr = IDirectDrawSurface_SetPalette(TexSurface, NULL);
    ok(hr == DD_OK, "IDirectDrawSurface_SetPalette returned %08x\n", hr);
    hr = IDirectDrawSurface_SetPalette(TexSurface2, palette2);
    ok(hr == DD_OK, "IDirectDrawSurface_SetPalette returned %08x\n", hr);
    hr = IDirect3DTexture_Load(Texture2, Texture);
    ok(hr == DD_OK, "IDirect3DTexture_Load returned %08x\n", hr);
    hr = IDirectDrawSurface_GetPalette(TexSurface2, &palette_tmp);
    ok(hr == DD_OK, "IDirectDrawSurface_GetPalette returned %08x\n", hr);
    if (!palette_tmp) {
        skip("IDirectDrawSurface_GetPalette failed; skipping color table check\n");
        goto cleanup;
    } else {
        hr = IDirectDrawPalette_GetEntries(palette_tmp, 0, 0, 256, table_tmp);
        ok(hr == DD_OK, "IDirectDrawPalette_GetEntries returned %08x\n", hr);
        ok(colortables_check_equality(table2, table_tmp), "Unexpected palettized texture color table\n");
        IDirectDrawPalette_Release(palette_tmp);
    }

    /* test Load when both textures have palettes */
    hr = IDirectDrawSurface_SetPalette(TexSurface, palette);
    ok(hr == DD_OK, "IDirectDrawSurface_SetPalette returned %08x\n", hr);
    hr = IDirect3DTexture_Load(Texture2, Texture);
    ok(hr == DD_OK, "IDirect3DTexture_Load returned %08x\n", hr);
    hr = IDirect3DTexture_Load(Texture2, Texture);
    ok(hr == DD_OK, "IDirect3DTexture_Load returned %08x\n", hr);
    hr = IDirectDrawSurface_GetPalette(TexSurface2, &palette_tmp);
    ok(hr == DD_OK, "IDirectDrawSurface_GetPalette returned %08x\n", hr);
    if (!palette_tmp) {
        skip("IDirectDrawSurface_GetPalette failed; skipping color table check\n");
        goto cleanup;
    } else {
        hr = IDirectDrawPalette_GetEntries(palette_tmp, 0, 0, 256, table_tmp);
        ok(hr == DD_OK, "IDirectDrawPalette_GetEntries returned %08x\n", hr);
        ok(colortables_check_equality(table1, table_tmp), "Unexpected palettized texture color table\n");
        IDirectDrawPalette_Release(palette_tmp);
    }

    cleanup:

    if (palette) IDirectDrawPalette_Release(palette);
    if (palette2) IDirectDrawPalette_Release(palette2);
    if (TexSurface) IDirectDrawSurface_Release(TexSurface);
    if (Texture) IDirect3DTexture_Release(Texture);
    if (TexSurface2) IDirectDrawSurface_Release(TexSurface2);
    if (Texture2) IDirect3DTexture_Release(Texture2);
}

1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
static void VertexBufferDescTest(void)
{
    HRESULT rc;
    D3DVERTEXBUFFERDESC desc;
    union mem_t
    {
        D3DVERTEXBUFFERDESC desc2;
        unsigned char buffer[512];
    } mem;

    memset(&desc, 0, sizeof(desc));
    desc.dwSize = sizeof(desc);
    desc.dwCaps = 0;
    desc.dwFVF = D3DFVF_XYZ;
    desc.dwNumVertices = 1;
    rc = IDirect3D7_CreateVertexBuffer(lpD3D, &desc, &lpVBufSrc, 0);
    ok(rc==D3D_OK || rc==E_OUTOFMEMORY, "CreateVertexBuffer returned: %x\n", rc);
    if (!lpVBufSrc)
    {
        trace("IDirect3D7::CreateVertexBuffer() failed with an error %x\n", rc);
        goto out;
    }

    memset(mem.buffer, 0x12, sizeof(mem.buffer));
    mem.desc2.dwSize = sizeof(D3DVERTEXBUFFERDESC)*2;
    rc = IDirect3DVertexBuffer7_GetVertexBufferDesc(lpVBufSrc, &mem.desc2);
    if(rc != D3D_OK)
        skip("GetVertexBuffer Failed!\n");
    ok( mem.desc2.dwSize == sizeof(D3DVERTEXBUFFERDESC)*2, "Size returned from GetVertexBufferDesc does not match the value put in\n" );
    ok( mem.buffer[sizeof(D3DVERTEXBUFFERDESC)] == 0x12, "GetVertexBufferDesc cleared outside of the struct! (dwSize was double the size of the struct)\n");
1418 1419
    ok( mem.desc2.dwCaps == desc.dwCaps, "dwCaps returned differs. Got %x, expected %x\n", mem.desc2.dwCaps, desc.dwCaps);
    ok( mem.desc2.dwFVF == desc.dwFVF, "dwFVF returned differs. Got %x, expected %x\n", mem.desc2.dwFVF, desc.dwFVF);
1420 1421 1422 1423 1424 1425 1426 1427 1428
    ok (mem.desc2.dwNumVertices == desc.dwNumVertices, "dwNumVertices returned differs. Got %x, expected %x\n", mem.desc2.dwNumVertices, desc.dwNumVertices);

    memset(mem.buffer, 0x12, sizeof(mem.buffer));
    mem.desc2.dwSize = 0;
    rc = IDirect3DVertexBuffer7_GetVertexBufferDesc(lpVBufSrc, &mem.desc2);
    if(rc != D3D_OK)
        skip("GetVertexBuffer Failed!\n");
    ok( mem.desc2.dwSize == 0, "Size returned from GetVertexBufferDesc does not match the value put in\n" );
    ok( mem.buffer[sizeof(D3DVERTEXBUFFERDESC)] == 0x12, "GetVertexBufferDesc cleared outside of the struct! (dwSize was 0)\n");
1429 1430
    ok( mem.desc2.dwCaps == desc.dwCaps, "dwCaps returned differs. Got %x, expected %x\n", mem.desc2.dwCaps, desc.dwCaps);
    ok( mem.desc2.dwFVF == desc.dwFVF, "dwFVF returned differs. Got %x, expected %x\n", mem.desc2.dwFVF, desc.dwFVF);
1431 1432 1433 1434 1435 1436 1437 1438 1439
    ok (mem.desc2.dwNumVertices == desc.dwNumVertices, "dwNumVertices returned differs. Got %x, expected %x\n", mem.desc2.dwNumVertices, desc.dwNumVertices);

    memset(mem.buffer, 0x12, sizeof(mem.buffer));
    mem.desc2.dwSize = sizeof(D3DVERTEXBUFFERDESC);
    rc = IDirect3DVertexBuffer7_GetVertexBufferDesc(lpVBufSrc, &mem.desc2);
    if(rc != D3D_OK)
        skip("GetVertexBuffer Failed!\n");
    ok( mem.desc2.dwSize == sizeof(D3DVERTEXBUFFERDESC), "Size returned from GetVertexBufferDesc does not match the value put in\n" );
    ok( mem.buffer[sizeof(D3DVERTEXBUFFERDESC)] == 0x12, "GetVertexBufferDesc cleared outside of the struct! (dwSize was the size of the struct)\n");
1440 1441
    ok( mem.desc2.dwCaps == desc.dwCaps, "dwCaps returned differs. Got %x, expected %x\n", mem.desc2.dwCaps, desc.dwCaps);
    ok( mem.desc2.dwFVF == desc.dwFVF, "dwFVF returned differs. Got %x, expected %x\n", mem.desc2.dwFVF, desc.dwFVF);
1442 1443 1444 1445 1446 1447
    ok (mem.desc2.dwNumVertices == desc.dwNumVertices, "dwNumVertices returned differs. Got %x, expected %x\n", mem.desc2.dwNumVertices, desc.dwNumVertices);

out:
    IDirect3DVertexBuffer7_Release(lpVBufSrc);
}

1448 1449
static void D3D7_OldRenderStateTest(void)
{
1450
    HRESULT hr;
1451 1452
    DWORD val;

1453 1454 1455 1456 1457 1458 1459 1460 1461
    /* Test reaction to some deprecated states in D3D7. */
    hr = IDirect3DDevice7_SetRenderState(lpD3DDevice, D3DRENDERSTATE_TEXTUREHANDLE, 0);
    ok(hr == DDERR_INVALIDPARAMS, "IDirect3DDevice7_SetRenderState returned %#x.\n", hr);
    hr = IDirect3DDevice7_GetRenderState(lpD3DDevice, D3DRENDERSTATE_TEXTUREHANDLE, &val);
    ok(hr == DDERR_INVALIDPARAMS, "IDirect3DDevice7_GetRenderState returned %#x.\n", hr);
    hr = IDirect3DDevice7_SetRenderState(lpD3DDevice, D3DRENDERSTATE_TEXTUREMAPBLEND, D3DTBLEND_MODULATE);
    ok(hr == DDERR_INVALIDPARAMS, "IDirect3DDevice7_SetRenderState returned %#x.\n", hr);
    hr = IDirect3DDevice7_GetRenderState(lpD3DDevice, D3DRENDERSTATE_TEXTUREMAPBLEND, &val);
    ok(hr == DDERR_INVALIDPARAMS, "IDirect3DDevice7_GetRenderState returned %#x.\n", hr);
1462 1463
}

1464 1465 1466
#define IS_VALUE_NEAR(a, b)    ( ((a) == (b)) || ((a) == (b) - 1) || ((a) == (b) + 1) )
#define MIN(a, b)    ((a) < (b) ? (a) : (b))

1467
static void DeviceLoadTest(void)
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
{
    DDSURFACEDESC2 ddsd;
    IDirectDrawSurface7 *texture_levels[2][8];
    IDirectDrawSurface7 *cube_face_levels[2][6][8];
    DWORD flags;
    HRESULT hr;
    DDBLTFX ddbltfx;
    RECT loadrect;
    POINT loadpoint;
    int i, i1, i2;
    unsigned diff_count = 0, diff_count2 = 0;
    unsigned x, y;
    BOOL load_mip_subset_broken = FALSE;
    IDirectDrawPalette *palettes[5];
    PALETTEENTRY table1[256];
    DDCOLORKEY ddckey;
1484
    D3DDEVICEDESC7 d3dcaps;
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534

    /* Test loading of texture subrectangle with a mipmap surface. */
    memset(texture_levels, 0, sizeof(texture_levels));
    memset(cube_face_levels, 0, sizeof(cube_face_levels));
    memset(palettes, 0, sizeof(palettes));

    for (i = 0; i < 2; i++)
    {
        memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
        ddsd.dwSize = sizeof(ddsd);
        ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
        ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
        ddsd.dwWidth = 128;
        ddsd.dwHeight = 128;
        U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
        U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
        U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
        U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00FF0000;
        U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000FF00;
        U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000FF;
        hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &texture_levels[i][0], NULL);
        ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
        if (FAILED(hr)) goto out;

        /* Check the number of created mipmaps */
        memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
        ddsd.dwSize = sizeof(ddsd);
        hr = IDirectDrawSurface7_GetSurfaceDesc(texture_levels[i][0], &ddsd);
        ok(hr==DD_OK,"IDirectDrawSurface7_GetSurfaceDesc returned: %x\n",hr);
        ok(U2(ddsd).dwMipMapCount == 8, "unexpected mip count %u\n", U2(ddsd).dwMipMapCount);
        if (U2(ddsd).dwMipMapCount != 8) goto out;

        for (i1 = 1; i1 < 8; i1++)
        {
            hr = IDirectDrawSurface7_GetAttachedSurface(texture_levels[i][i1 - 1], &ddsd.ddsCaps, &texture_levels[i][i1]);
            ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
            if (FAILED(hr)) goto out;
        }
    }

    for (i1 = 0; i1 < 8; i1++)
    {
        memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
        ddsd.dwSize = sizeof(ddsd);
        hr = IDirectDrawSurface7_Lock(texture_levels[0][i1], NULL, &ddsd, DDLOCK_WAIT, NULL);
        ok(hr==DD_OK, "IDirectDrawSurface7_Lock returned: %x\n",hr);
        if (FAILED(hr)) goto out;

        for (y = 0 ; y < ddsd.dwHeight; y++)
        {
1535
            DWORD *textureRow = (DWORD*)((char*)ddsd.lpSurface + y * U1(ddsd).lPitch);
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559

            for (x = 0; x < ddsd.dwWidth;  x++)
            {
                /* x stored in green component, y in blue. */
                DWORD color = 0xff0000 | (x << 8)  | y;
                *textureRow++ = color;
            }
        }

        hr = IDirectDrawSurface7_Unlock(texture_levels[0][i1], NULL);
        ok(hr==DD_OK, "IDirectDrawSurface7_Unlock returned: %x\n",hr);
    }

    for (i1 = 0; i1 < 8; i1++)
    {
        memset(&ddbltfx, 0, sizeof(ddbltfx));
        ddbltfx.dwSize = sizeof(ddbltfx);
        U5(ddbltfx).dwFillColor = 0;
        hr = IDirectDrawSurface7_Blt(texture_levels[1][i1], NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
        ok(hr == DD_OK, "IDirectDrawSurface7_Blt failed with %08x\n", hr);
    }

    /* First test some broken coordinates. */
    loadpoint.x = loadpoint.y = 0;
1560
    SetRectEmpty(&loadrect);
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
    hr = IDirect3DDevice7_Load(lpD3DDevice, texture_levels[1][0], &loadpoint, texture_levels[0][0], &loadrect, 0);
    ok(hr==DDERR_INVALIDPARAMS, "IDirect3DDevice7_Load returned: %x\n",hr);

    loadpoint.x = loadpoint.y = 50;
    loadrect.left = 0;
    loadrect.top = 0;
    loadrect.right = 100;
    loadrect.bottom = 100;
    hr = IDirect3DDevice7_Load(lpD3DDevice, texture_levels[1][0], &loadpoint, texture_levels[0][0], &loadrect, 0);
    ok(hr==DDERR_INVALIDPARAMS, "IDirect3DDevice7_Load returned: %x\n",hr);

    /* Test actual loading. */
    loadpoint.x = loadpoint.y = 31;
    loadrect.left = 30;
    loadrect.top = 20;
    loadrect.right = 93;
    loadrect.bottom = 52;

    hr = IDirect3DDevice7_Load(lpD3DDevice, texture_levels[1][0], &loadpoint, texture_levels[0][0], &loadrect, 0);
    ok(hr==D3D_OK, "IDirect3DDevice7_Load returned: %x\n",hr);

    for (i1 = 0; i1 < 8; i1++)
    {
        diff_count = 0;
        diff_count2 = 0;

        memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
        ddsd.dwSize = sizeof(ddsd);
        hr = IDirectDrawSurface7_Lock(texture_levels[1][i1], NULL, &ddsd, DDLOCK_WAIT, NULL);
        ok(hr==DD_OK, "IDirectDrawSurface7_Lock returned: %x\n",hr);
        if (FAILED(hr)) goto out;

        for (y = 0 ; y < ddsd.dwHeight; y++)
        {
1595
            DWORD *textureRow = (DWORD*)((char*)ddsd.lpSurface + y * U1(ddsd).lPitch);
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687

            for (x = 0; x < ddsd.dwWidth;  x++)
            {
                DWORD color = *textureRow++;

                if (x < loadpoint.x || x >= loadpoint.x + loadrect.right - loadrect.left ||
                    y < loadpoint.y || y >= loadpoint.y + loadrect.bottom - loadrect.top)
                {
                    if (color & 0xffffff) diff_count++;
                }
                else
                {
                    DWORD r = (color & 0xff0000) >> 16;
                    DWORD g = (color & 0xff00) >> 8;
                    DWORD b = (color & 0xff);

                    if (r != 0xff || g != x + loadrect.left - loadpoint.x || b != y + loadrect.top - loadpoint.y) diff_count++;
                }

                /* This codepath is for software RGB device. It has what looks like some weird off by one errors, but may
                   technically be correct as it's not precisely defined by docs. */
                if (x < loadpoint.x || x >= loadpoint.x + loadrect.right - loadrect.left ||
                    y < loadpoint.y || y >= loadpoint.y + loadrect.bottom - loadrect.top + 1)
                {
                    if (color & 0xffffff) diff_count2++;
                }
                else
                {
                    DWORD r = (color & 0xff0000) >> 16;
                    DWORD g = (color & 0xff00) >> 8;
                    DWORD b = (color & 0xff);

                    if (r != 0xff || !IS_VALUE_NEAR(g, x + loadrect.left - loadpoint.x) ||
                        !IS_VALUE_NEAR(b, y + loadrect.top - loadpoint.y)) diff_count2++;
                }
            }
        }

        hr = IDirectDrawSurface7_Unlock(texture_levels[1][i1], NULL);
        ok(hr==DD_OK, "IDirectDrawSurface7_Unlock returned: %x\n",hr);

        ok(diff_count == 0 || diff_count2 == 0, "Unexpected destination texture level pixels; %u differences at %d level\n",
                MIN(diff_count, diff_count2), i1);

        loadpoint.x /= 2;
        loadpoint.y /= 2;
        loadrect.top /= 2;
        loadrect.left /= 2;
        loadrect.right = (loadrect.right + 1) / 2;
        loadrect.bottom = (loadrect.bottom + 1) / 2;
    }

    /* This crashes on native (tested on real windows XP / directx9 / nvidia and
     * qemu Win98 / directx7 / RGB software rasterizer):
     * passing non toplevel surfaces (sublevels) to Load (DX7 docs tell not to do this)
    hr = IDirect3DDevice7_Load(lpD3DDevice, texture_levels[1][1], NULL, texture_levels[0][1], NULL, 0);
    */

    /* Freed in reverse order as native seems to dislike and crash on freeing top level surface first. */
    for (i = 0; i < 2; i++)
    {
        for (i1 = 7; i1 >= 0; i1--)
        {
            if (texture_levels[i][i1]) IDirectDrawSurface7_Release(texture_levels[i][i1]);
        }
    }
    memset(texture_levels, 0, sizeof(texture_levels));

    /* Test texture size mismatch. */
    for (i = 0; i < 2; i++)
    {
        memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
        ddsd.dwSize = sizeof(ddsd);
        ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
        ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
        ddsd.dwWidth = i ? 256 : 128;
        ddsd.dwHeight = 128;
        hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &texture_levels[i][0], NULL);
        ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
        if (FAILED(hr)) goto out;
    }

    hr = IDirect3DDevice7_Load(lpD3DDevice, texture_levels[1][0], NULL, texture_levels[0][0], NULL, 0);
    ok(hr==DDERR_INVALIDPARAMS, "IDirect3DDevice7_Load returned: %x\n",hr);

    hr = IDirect3DDevice7_Load(lpD3DDevice, texture_levels[0][0], NULL, texture_levels[1][0], NULL, 0);
    ok(hr==DDERR_INVALIDPARAMS, "IDirect3DDevice7_Load returned: %x\n",hr);

    IDirectDrawSurface7_Release(texture_levels[0][0]);
    IDirectDrawSurface7_Release(texture_levels[1][0]);
    memset(texture_levels, 0, sizeof(texture_levels));

1688 1689 1690
    memset(&d3dcaps, 0, sizeof(d3dcaps));
    hr = IDirect3DDevice7_GetCaps(lpD3DDevice, &d3dcaps);
    ok(hr == D3D_OK, "IDirect3DDevice7_GetCaps returned %08x\n", hr);
1691

1692 1693 1694 1695 1696 1697 1698 1699
    if (!(d3dcaps.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_CUBEMAP))
    {
        skip("No cubemap support\n");
    }
    else
    {
        /* Test loading mipmapped cubemap texture subrectangle from another similar texture. */
        for (i = 0; i < 2; i++)
1700 1701 1702
        {
            memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
            ddsd.dwSize = sizeof(ddsd);
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
            ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
            ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
            ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALLFACES;
            ddsd.dwWidth = 128;
            ddsd.dwHeight = 128;
            U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
            U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
            U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
            U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00FF0000;
            U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000FF00;
            U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000FF;
            hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &cube_face_levels[i][0][0], NULL);
            ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
            if (FAILED(hr)) goto out;
1717

1718 1719
            flags = DDSCAPS2_CUBEMAP_NEGATIVEX;
            for (i1 = 1; i1 < 6; i1++, flags <<= 1)
1720
            {
1721 1722 1723
                ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
                ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP | flags;
                hr = IDirectDrawSurface7_GetAttachedSurface(cube_face_levels[i][0][0], &ddsd.ddsCaps, &cube_face_levels[i][i1][0]);
1724 1725 1726 1727
                ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
                if (FAILED(hr)) goto out;
            }

1728
            for (i1 = 0; i1 < 6; i1++)
1729
            {
1730 1731 1732 1733 1734 1735 1736 1737 1738
                /* Check the number of created mipmaps */
                memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
                ddsd.dwSize = sizeof(ddsd);
                hr = IDirectDrawSurface7_GetSurfaceDesc(cube_face_levels[i][i1][0], &ddsd);
                ok(hr==DD_OK,"IDirectDrawSurface7_GetSurfaceDesc returned: %x\n",hr);
                ok(U2(ddsd).dwMipMapCount == 8, "unexpected mip count %u\n", U2(ddsd).dwMipMapCount);
                if (U2(ddsd).dwMipMapCount != 8) goto out;

                for (i2 = 1; i2 < 8; i2++)
1739
                {
1740 1741 1742 1743 1744
                    ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_MIPMAP;
                    ddsd.ddsCaps.dwCaps2 = DDSCAPS2_MIPMAPSUBLEVEL;
                    hr = IDirectDrawSurface7_GetAttachedSurface(cube_face_levels[i][i1][i2 - 1], &ddsd.ddsCaps, &cube_face_levels[i][i1][i2]);
                    ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
                    if (FAILED(hr)) goto out;
1745 1746 1747 1748
                }
            }
        }

1749 1750 1751 1752 1753 1754 1755 1756
        for (i = 0; i < 6; i++)
            for (i1 = 0; i1 < 8; i1++)
            {
                memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
                ddsd.dwSize = sizeof(ddsd);
                hr = IDirectDrawSurface7_Lock(cube_face_levels[0][i][i1], NULL, &ddsd, DDLOCK_WAIT, NULL);
                ok(hr==DD_OK, "IDirectDrawSurface7_Lock returned: %x\n",hr);
                if (FAILED(hr)) goto out;
1757

1758 1759 1760
                for (y = 0 ; y < ddsd.dwHeight; y++)
                {
                    DWORD *textureRow = (DWORD*)((char*)ddsd.lpSurface + y * U1(ddsd).lPitch);
1761

1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
                    for (x = 0; x < ddsd.dwWidth;  x++)
                    {
                        /* face number in low 4 bits of red, x stored in green component, y in blue. */
                        DWORD color = 0xf00000 | (i << 16) | (x << 8)  | y;
                        *textureRow++ = color;
                    }
                }

                hr = IDirectDrawSurface7_Unlock(cube_face_levels[0][i][i1], NULL);
                ok(hr==DD_OK, "IDirectDrawSurface7_Unlock returned: %x\n",hr);
            }

        for (i = 0; i < 6; i++)
            for (i1 = 0; i1 < 8; i1++)
            {
                memset(&ddbltfx, 0, sizeof(ddbltfx));
                ddbltfx.dwSize = sizeof(ddbltfx);
                U5(ddbltfx).dwFillColor = 0;
                hr = IDirectDrawSurface7_Blt(cube_face_levels[1][i][i1], NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
                ok(hr == DD_OK, "IDirectDrawSurface7_Blt failed with %08x\n", hr);
            }
1783 1784 1785 1786 1787 1788 1789

        loadpoint.x = loadpoint.y = 10;
        loadrect.left = 30;
        loadrect.top = 20;
        loadrect.right = 93;
        loadrect.bottom = 52;

1790 1791 1792
        hr = IDirect3DDevice7_Load(lpD3DDevice, cube_face_levels[1][0][0], &loadpoint, cube_face_levels[0][0][0], &loadrect,
                                        DDSCAPS2_CUBEMAP_ALLFACES);
        ok(hr==D3D_OK, "IDirect3DDevice7_Load returned: %x\n",hr);
1793

1794 1795 1796 1797 1798 1799 1800
        for (i = 0; i < 6; i++)
        {
            loadpoint.x = loadpoint.y = 10;
            loadrect.left = 30;
            loadrect.top = 20;
            loadrect.right = 93;
            loadrect.bottom = 52;
1801

1802
            for (i1 = 0; i1 < 8; i1++)
1803
            {
1804 1805
                diff_count = 0;
                diff_count2 = 0;
1806

1807 1808 1809 1810 1811
                memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
                ddsd.dwSize = sizeof(ddsd);
                hr = IDirectDrawSurface7_Lock(cube_face_levels[1][i][i1], NULL, &ddsd, DDLOCK_WAIT, NULL);
                ok(hr==DD_OK, "IDirectDrawSurface7_Lock returned: %x\n",hr);
                if (FAILED(hr)) goto out;
1812

1813 1814 1815
                for (y = 0 ; y < ddsd.dwHeight; y++)
                {
                    DWORD *textureRow = (DWORD*)((char*)ddsd.lpSurface + y * U1(ddsd).lPitch);
1816

1817
                    for (x = 0; x < ddsd.dwWidth;  x++)
1818
                    {
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
                        DWORD color = *textureRow++;

                        if (x < loadpoint.x || x >= loadpoint.x + loadrect.right - loadrect.left ||
                            y < loadpoint.y || y >= loadpoint.y + loadrect.bottom - loadrect.top)
                        {
                            if (color & 0xffffff) diff_count++;
                        }
                        else
                        {
                            DWORD r = (color & 0xff0000) >> 16;
                            DWORD g = (color & 0xff00) >> 8;
                            DWORD b = (color & 0xff);

                            if (r != (0xf0 | i) || g != x + loadrect.left - loadpoint.x ||
                                b != y + loadrect.top - loadpoint.y) diff_count++;
                        }

                        /* This codepath is for software RGB device. It has what looks like some weird off by one errors, but may
                        technically be correct as it's not precisely defined by docs. */
                        if (x < loadpoint.x || x >= loadpoint.x + loadrect.right - loadrect.left ||
                            y < loadpoint.y || y >= loadpoint.y + loadrect.bottom - loadrect.top + 1)
                        {
                            if (color & 0xffffff) diff_count2++;
                        }
                        else
                        {
                            DWORD r = (color & 0xff0000) >> 16;
                            DWORD g = (color & 0xff00) >> 8;
                            DWORD b = (color & 0xff);

                            if (r != (0xf0 | i) || !IS_VALUE_NEAR(g, x + loadrect.left - loadpoint.x) ||
                                !IS_VALUE_NEAR(b, y + loadrect.top - loadpoint.y)) diff_count2++;
                        }
1852 1853 1854
                    }
                }

1855 1856
                hr = IDirectDrawSurface7_Unlock(cube_face_levels[1][i][i1], NULL);
                ok(hr==DD_OK, "IDirectDrawSurface7_Unlock returned: %x\n",hr);
1857

1858 1859 1860
                ok(diff_count == 0 || diff_count2 == 0,
                    "Unexpected destination texture level pixels; %u differences at face %x level %d\n",
                    MIN(diff_count, diff_count2), i, i1);
1861

1862 1863 1864 1865 1866 1867 1868
                loadpoint.x /= 2;
                loadpoint.y /= 2;
                loadrect.top /= 2;
                loadrect.left /= 2;
                loadrect.right = (loadrect.right + 1) / 2;
                loadrect.bottom = (loadrect.bottom + 1) / 2;
            }
1869 1870
        }

1871 1872 1873 1874 1875 1876 1877
        for (i = 0; i < 2; i++)
            for (i1 = 5; i1 >= 0; i1--)
                for (i2 = 7; i2 >= 0; i2--)
                {
                    if (cube_face_levels[i][i1][i2]) IDirectDrawSurface7_Release(cube_face_levels[i][i1][i2]);
                }
        memset(cube_face_levels, 0, sizeof(cube_face_levels));
1878

1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
        /* Test cubemap loading from regular texture. */
        memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
        ddsd.dwSize = sizeof(ddsd);
        ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
        ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX;
        ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALLFACES;
        ddsd.dwWidth = 128;
        ddsd.dwHeight = 128;
        hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &cube_face_levels[0][0][0], NULL);
        ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
        if (FAILED(hr)) goto out;
1890

1891 1892 1893 1894 1895 1896 1897 1898 1899
        memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
        ddsd.dwSize = sizeof(ddsd);
        ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
        ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
        ddsd.dwWidth = 128;
        ddsd.dwHeight = 128;
        hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &texture_levels[0][0], NULL);
        ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
        if (FAILED(hr)) goto out;
1900

1901 1902 1903
        hr = IDirect3DDevice7_Load(lpD3DDevice, cube_face_levels[0][0][0], NULL, texture_levels[0][0], NULL,
                                        DDSCAPS2_CUBEMAP_ALLFACES);
        ok(hr==DDERR_INVALIDPARAMS, "IDirect3DDevice7_Load returned: %x\n",hr);
1904

1905 1906 1907 1908
        IDirectDrawSurface7_Release(cube_face_levels[0][0][0]);
        memset(cube_face_levels, 0, sizeof(cube_face_levels));
        IDirectDrawSurface7_Release(texture_levels[0][0]);
        memset(texture_levels, 0, sizeof(texture_levels));
1909

1910 1911 1912 1913
        /* Partial cube maps(e.g. created with an explicitly set DDSCAPS2_CUBEMAP_POSITIVEX flag)
         * BSOD some Windows machines when an app tries to create them(Radeon X1600, Windows XP,
         * Catalyst 10.2 driver, 6.14.10.6925)
         */
1914
    }
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924

    /* Test texture loading with different mip level count (larger levels match, smaller levels missing in destination. */
    for (i = 0; i < 2; i++)
    {
        memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
        ddsd.dwSize = sizeof(ddsd);
        ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_MIPMAPCOUNT;
        ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
        ddsd.dwWidth = 128;
        ddsd.dwHeight = 128;
1925
        U2(ddsd).dwMipMapCount = i ? 4 : 8;
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
        U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
        U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
        U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
        U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00FF0000;
        U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000FF00;
        U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000FF;
        hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &texture_levels[i][0], NULL);
        ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
        if (FAILED(hr)) goto out;

        /* Check the number of created mipmaps */
        memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
        ddsd.dwSize = sizeof(ddsd);
        hr = IDirectDrawSurface7_GetSurfaceDesc(texture_levels[i][0], &ddsd);
        ok(hr==DD_OK,"IDirectDrawSurface7_GetSurfaceDesc returned: %x\n",hr);
        ok(U2(ddsd).dwMipMapCount == (i ? 4 : 8), "unexpected mip count %u\n", U2(ddsd).dwMipMapCount);
        if (U2(ddsd).dwMipMapCount != (i ? 4 : 8)) goto out;

        for (i1 = 1; i1 < (i ? 4 : 8); i1++)
        {
            hr = IDirectDrawSurface7_GetAttachedSurface(texture_levels[i][i1 - 1], &ddsd.ddsCaps, &texture_levels[i][i1]);
            ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
            if (FAILED(hr)) goto out;
        }
    }

    for (i1 = 0; i1 < 8; i1++)
    {
        memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
        ddsd.dwSize = sizeof(ddsd);
        hr = IDirectDrawSurface7_Lock(texture_levels[0][i1], NULL, &ddsd, DDLOCK_WAIT, NULL);
        ok(hr==DD_OK, "IDirectDrawSurface7_Lock returned: %x\n",hr);
        if (FAILED(hr)) goto out;

        for (y = 0 ; y < ddsd.dwHeight; y++)
        {
1962
            DWORD *textureRow = (DWORD*)((char*)ddsd.lpSurface + y * U1(ddsd).lPitch);
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007

            for (x = 0; x < ddsd.dwWidth;  x++)
            {
                /* x stored in green component, y in blue. */
                DWORD color = 0xf00000 | (i1 << 16) | (x << 8)  | y;
                *textureRow++ = color;
            }
        }

        hr = IDirectDrawSurface7_Unlock(texture_levels[0][i1], NULL);
        ok(hr==DD_OK, "IDirectDrawSurface7_Unlock returned: %x\n",hr);
    }

    for (i1 = 0; i1 < 4; i1++)
    {
        memset(&ddbltfx, 0, sizeof(ddbltfx));
        ddbltfx.dwSize = sizeof(ddbltfx);
        U5(ddbltfx).dwFillColor = 0;
        hr = IDirectDrawSurface7_Blt(texture_levels[1][i1], NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
        ok(hr == DD_OK, "IDirectDrawSurface7_Blt failed with %08x\n", hr);
    }

    loadpoint.x = loadpoint.y = 31;
    loadrect.left = 30;
    loadrect.top = 20;
    loadrect.right = 93;
    loadrect.bottom = 52;

    /* Destination mip levels are a subset of source mip levels. */
    hr = IDirect3DDevice7_Load(lpD3DDevice, texture_levels[1][0], &loadpoint, texture_levels[0][0], &loadrect, 0);
    ok(hr==D3D_OK, "IDirect3DDevice7_Load returned: %x\n",hr);

    for (i1 = 0; i1 < 4; i1++)
    {
        diff_count = 0;
        diff_count2 = 0;

        memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
        ddsd.dwSize = sizeof(ddsd);
        hr = IDirectDrawSurface7_Lock(texture_levels[1][i1], NULL, &ddsd, DDLOCK_WAIT, NULL);
        ok(hr==DD_OK, "IDirectDrawSurface7_Lock returned: %x\n",hr);
        if (FAILED(hr)) goto out;

        for (y = 0 ; y < ddsd.dwHeight; y++)
        {
2008
            DWORD *textureRow = (DWORD*)((char*)ddsd.lpSurface + y * U1(ddsd).lPitch);
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124

            for (x = 0; x < ddsd.dwWidth;  x++)
            {
                DWORD color = *textureRow++;

                if (x < loadpoint.x || x >= loadpoint.x + loadrect.right - loadrect.left ||
                    y < loadpoint.y || y >= loadpoint.y + loadrect.bottom - loadrect.top)
                {
                    if (color & 0xffffff) diff_count++;
                }
                else
                {
                    DWORD r = (color & 0xff0000) >> 16;
                    DWORD g = (color & 0xff00) >> 8;
                    DWORD b = (color & 0xff);

                    if (r != (0xf0 | i1) || g != x + loadrect.left - loadpoint.x ||
                        b != y + loadrect.top - loadpoint.y) diff_count++;
                }

                /* This codepath is for software RGB device. It has what looks like some weird off by one errors, but may
                technically be correct as it's not precisely defined by docs. */
                if (x < loadpoint.x || x >= loadpoint.x + loadrect.right - loadrect.left ||
                    y < loadpoint.y || y >= loadpoint.y + loadrect.bottom - loadrect.top + 1)
                {
                    if (color & 0xffffff) diff_count2++;
                }
                else
                {
                    DWORD r = (color & 0xff0000) >> 16;
                    DWORD g = (color & 0xff00) >> 8;
                    DWORD b = (color & 0xff);

                    if (r != (0xf0 | i1) || !IS_VALUE_NEAR(g, x + loadrect.left - loadpoint.x) ||
                        !IS_VALUE_NEAR(b, y + loadrect.top - loadpoint.y)) diff_count2++;
                }
            }
        }

        hr = IDirectDrawSurface7_Unlock(texture_levels[1][i1], NULL);
        ok(hr==DD_OK, "IDirectDrawSurface7_Unlock returned: %x\n",hr);

        ok(diff_count == 0 || diff_count2 == 0, "Unexpected destination texture level pixels; %u differences at %d level\n",
             MIN(diff_count, diff_count2), i1);

        loadpoint.x /= 2;
        loadpoint.y /= 2;
        loadrect.top /= 2;
        loadrect.left /= 2;
        loadrect.right = (loadrect.right + 1) / 2;
        loadrect.bottom = (loadrect.bottom + 1) / 2;
    }

    /* Destination mip levels are a superset of source mip levels (should fail). */
    hr = IDirect3DDevice7_Load(lpD3DDevice, texture_levels[0][0], &loadpoint, texture_levels[1][0], &loadrect, 0);
    ok(hr==DDERR_INVALIDPARAMS, "IDirect3DDevice7_Load returned: %x\n",hr);

    for (i = 0; i < 2; i++)
    {
        for (i1 = 7; i1 >= 0; i1--)
        {
            if (texture_levels[i][i1]) IDirectDrawSurface7_Release(texture_levels[i][i1]);
        }
    }
    memset(texture_levels, 0, sizeof(texture_levels));

    /* Test loading from mipmap texture to a regular texture that matches one sublevel in size. */
    memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
    ddsd.dwWidth = 128;
    ddsd.dwHeight = 128;
    U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
    U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
    U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
    U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00FF0000;
    U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000FF00;
    U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000FF;
    hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &texture_levels[0][0], NULL);
    ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
    if (FAILED(hr)) goto out;

    memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
    ddsd.dwWidth = 32;
    ddsd.dwHeight = 32;
    U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
    U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
    U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
    U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00FF0000;
    U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000FF00;
    U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000FF;
    hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &texture_levels[1][0], NULL);
    ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
    if (FAILED(hr)) goto out;

    for (i1 = 1; i1 < 8; i1++)
    {
        hr = IDirectDrawSurface7_GetAttachedSurface(texture_levels[0][i1 - 1], &ddsd.ddsCaps, &texture_levels[0][i1]);
        ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
        if (FAILED(hr)) goto out;
    }

    for (i1 = 0; i1 < 8; i1++)
    {
        memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
        ddsd.dwSize = sizeof(ddsd);
        hr = IDirectDrawSurface7_Lock(texture_levels[0][i1], NULL, &ddsd, DDLOCK_WAIT, NULL);
        ok(hr==DD_OK, "IDirectDrawSurface7_Lock returned: %x\n",hr);
        if (FAILED(hr)) goto out;

        for (y = 0 ; y < ddsd.dwHeight; y++)
        {
2125
            DWORD *textureRow = (DWORD*)((char*)ddsd.lpSurface + y * U1(ddsd).lPitch);
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178

            for (x = 0; x < ddsd.dwWidth;  x++)
            {
                /* x stored in green component, y in blue. */
                DWORD color = 0xf00000 | (i1 << 16) | (x << 8)  | y;
                *textureRow++ = color;
            }
        }

        hr = IDirectDrawSurface7_Unlock(texture_levels[0][i1], NULL);
        ok(hr==DD_OK, "IDirectDrawSurface7_Unlock returned: %x\n",hr);
    }

    memset(&ddbltfx, 0, sizeof(ddbltfx));
    ddbltfx.dwSize = sizeof(ddbltfx);
    U5(ddbltfx).dwFillColor = 0;
    hr = IDirectDrawSurface7_Blt(texture_levels[1][0], NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
    ok(hr == DD_OK, "IDirectDrawSurface7_Blt failed with %08x\n", hr);

    loadpoint.x = loadpoint.y = 32;
    loadrect.left = 32;
    loadrect.top = 32;
    loadrect.right = 96;
    loadrect.bottom = 96;

    hr = IDirect3DDevice7_Load(lpD3DDevice, texture_levels[1][0], &loadpoint, texture_levels[0][0], &loadrect, 0);
    ok(hr==D3D_OK, "IDirect3DDevice7_Load returned: %x\n",hr);

    loadpoint.x /= 4;
    loadpoint.y /= 4;
    loadrect.top /= 4;
    loadrect.left /= 4;
    loadrect.right = (loadrect.right + 3) / 4;
    loadrect.bottom = (loadrect.bottom + 3) / 4;

    /* NOTE: something in either nvidia driver or directx9 on WinXP appears to be broken:
     * this kind of Load calls (to subset with smaller surface(s)) produces wrong results with
     * copied subrectangles divided more than needed, without apparent logic. But it works
     * as expected on qemu / Win98 / directx7 / RGB device. Some things are broken on XP, e.g.
     * some games don't work that worked in Win98, so it is assumed here XP results are wrong.
     * The following code attempts to detect broken results, actual tests will then be skipped
     */
    load_mip_subset_broken = TRUE;
    diff_count = 0;

    memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
    ddsd.dwSize = sizeof(ddsd);
    hr = IDirectDrawSurface7_Lock(texture_levels[1][0], NULL, &ddsd, DDLOCK_WAIT, NULL);
    ok(hr==DD_OK, "IDirectDrawSurface7_Lock returned: %x\n",hr);
    if (FAILED(hr)) goto out;

    for (y = 0 ; y < ddsd.dwHeight; y++)
    {
2179
        DWORD *textureRow = (DWORD*)((char*)ddsd.lpSurface + y * U1(ddsd).lPitch);
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207

        for (x = 0; x < ddsd.dwWidth;  x++)
        {
            DWORD color = *textureRow++;

            if (x < 2 || x >= 2 + 4 ||
                y < 2 || y >= 2 + 4)
            {
                if (color & 0xffffff) diff_count++;
            }
            else
            {
                DWORD r = (color & 0xff0000) >> 16;

                if ((r & (0xf0)) != 0xf0) diff_count++;
            }
        }
    }

    if (diff_count) load_mip_subset_broken = FALSE;

    if (load_mip_subset_broken) {
        skip("IDirect3DDevice7_Load is broken (happens on some modern Windows installations like XP). Skipping affected tests.\n");
    } else {
        diff_count = 0;

        for (y = 0 ; y < ddsd.dwHeight; y++)
        {
2208
            DWORD *textureRow = (DWORD*)((char*)ddsd.lpSurface + y * U1(ddsd).lPitch);
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259

            for (x = 0; x < ddsd.dwWidth;  x++)
            {
                DWORD color = *textureRow++;

                if (x < loadpoint.x || x >= loadpoint.x + loadrect.right - loadrect.left ||
                    y < loadpoint.y || y >= loadpoint.y + loadrect.bottom - loadrect.top)
                {
                    if (color & 0xffffff) diff_count++;
                }
                else
                {
                    DWORD r = (color & 0xff0000) >> 16;
                    DWORD g = (color & 0xff00) >> 8;
                    DWORD b = (color & 0xff);

                    if (r != (0xf0 | 2) || g != x + loadrect.left - loadpoint.x ||
                        b != y + loadrect.top - loadpoint.y) diff_count++;
                }
            }
        }
    }

    hr = IDirectDrawSurface7_Unlock(texture_levels[1][0], NULL);
    ok(hr==DD_OK, "IDirectDrawSurface7_Unlock returned: %x\n",hr);

    ok(diff_count == 0, "Unexpected destination texture level pixels; %u differences\n", diff_count);

    for (i = 0; i < 2; i++)
    {
        for (i1 = 7; i1 >= 0; i1--)
        {
            if (texture_levels[i][i1]) IDirectDrawSurface7_Release(texture_levels[i][i1]);
        }
    }
    memset(texture_levels, 0, sizeof(texture_levels));

    if (!load_mip_subset_broken)
    {
        /* Test loading when destination mip levels are a subset of source mip levels and start from smaller
        * surface (than first source mip level)
        */
        for (i = 0; i < 2; i++)
        {
            memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
            ddsd.dwSize = sizeof(ddsd);
            ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
            if (i) ddsd.dwFlags |= DDSD_MIPMAPCOUNT;
            ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
            ddsd.dwWidth = i ? 32 : 128;
            ddsd.dwHeight = i ? 32 : 128;
2260
            if (i) U2(ddsd).dwMipMapCount = 4;
2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
            U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
            U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
            U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
            U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00FF0000;
            U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000FF00;
            U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000FF;
            hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &texture_levels[i][0], NULL);
            ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
            if (FAILED(hr)) goto out;

            /* Check the number of created mipmaps */
            memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
            ddsd.dwSize = sizeof(ddsd);
            hr = IDirectDrawSurface7_GetSurfaceDesc(texture_levels[i][0], &ddsd);
            ok(hr==DD_OK,"IDirectDrawSurface7_GetSurfaceDesc returned: %x\n",hr);
            ok(U2(ddsd).dwMipMapCount == (i ? 4 : 8), "unexpected mip count %u\n", U2(ddsd).dwMipMapCount);
            if (U2(ddsd).dwMipMapCount != (i ? 4 : 8)) goto out;

            for (i1 = 1; i1 < (i ? 4 : 8); i1++)
            {
                hr = IDirectDrawSurface7_GetAttachedSurface(texture_levels[i][i1 - 1], &ddsd.ddsCaps, &texture_levels[i][i1]);
                ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
                if (FAILED(hr)) goto out;
            }
        }

        for (i1 = 0; i1 < 8; i1++)
        {
            memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
            ddsd.dwSize = sizeof(ddsd);
            hr = IDirectDrawSurface7_Lock(texture_levels[0][i1], NULL, &ddsd, DDLOCK_WAIT, NULL);
            ok(hr==DD_OK, "IDirectDrawSurface7_Lock returned: %x\n",hr);
            if (FAILED(hr)) goto out;

            for (y = 0 ; y < ddsd.dwHeight; y++)
            {
2297
                DWORD *textureRow = (DWORD*)((char*)ddsd.lpSurface + y * U1(ddsd).lPitch);
2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336

                for (x = 0; x < ddsd.dwWidth;  x++)
                {
                    /* x stored in green component, y in blue. */
                    DWORD color = 0xf00000 | (i1 << 16) | (x << 8)  | y;
                    *textureRow++ = color;
                }
            }

            hr = IDirectDrawSurface7_Unlock(texture_levels[0][i1], NULL);
            ok(hr==DD_OK, "IDirectDrawSurface7_Unlock returned: %x\n",hr);
        }

        for (i1 = 0; i1 < 4; i1++)
        {
            memset(&ddbltfx, 0, sizeof(ddbltfx));
            ddbltfx.dwSize = sizeof(ddbltfx);
            U5(ddbltfx).dwFillColor = 0;
            hr = IDirectDrawSurface7_Blt(texture_levels[1][i1], NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
            ok(hr == DD_OK, "IDirectDrawSurface7_Blt failed with %08x\n", hr);
        }

        loadpoint.x = loadpoint.y = 0;
        loadrect.left = 0;
        loadrect.top = 0;
        loadrect.right = 64;
        loadrect.bottom = 64;

        hr = IDirect3DDevice7_Load(lpD3DDevice, texture_levels[1][0], &loadpoint, texture_levels[0][0], &loadrect, 0);
        ok(hr==D3D_OK, "IDirect3DDevice7_Load returned: %x\n",hr);

        i = 0;
        for (i1 = 0; i1 < 8 && i < 4; i1++)
        {
            DDSURFACEDESC2 ddsd2;

            memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
            ddsd.dwSize = sizeof(ddsd);
            hr = IDirectDrawSurface7_GetSurfaceDesc(texture_levels[0][i1], &ddsd);
2337
            ok(SUCCEEDED(hr), "IDirectDrawSurface7_GetSurfaceDesc returned %#x.\n", hr);
2338 2339 2340 2341

            memset(&ddsd2, 0, sizeof(DDSURFACEDESC2));
            ddsd2.dwSize = sizeof(ddsd2);
            hr = IDirectDrawSurface7_GetSurfaceDesc(texture_levels[1][i], &ddsd2);
2342
            ok(SUCCEEDED(hr), "IDirectDrawSurface7_GetSurfaceDesc returned %#x.\n", hr);
2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355

            if (ddsd.dwWidth == ddsd2.dwWidth && ddsd.dwHeight == ddsd2.dwHeight)
            {
                diff_count = 0;

                memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
                ddsd.dwSize = sizeof(ddsd);
                hr = IDirectDrawSurface7_Lock(texture_levels[1][i], NULL, &ddsd, DDLOCK_WAIT, NULL);
                ok(hr==DD_OK, "IDirectDrawSurface7_Lock returned: %x\n",hr);
                if (FAILED(hr)) goto out;

                for (y = 0 ; y < ddsd.dwHeight; y++)
                {
2356
                    DWORD *textureRow = (DWORD*)((char*)ddsd.lpSurface + y * U1(ddsd).lPitch);
2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455

                    for (x = 0; x < ddsd.dwWidth;  x++)
                    {
                        DWORD color = *textureRow++;

                        if (x < loadpoint.x || x >= loadpoint.x + loadrect.right - loadrect.left ||
                            y < loadpoint.y || y >= loadpoint.y + loadrect.bottom - loadrect.top)
                        {
                            if (color & 0xffffff) diff_count++;
                        }
                        else
                        {
                            DWORD r = (color & 0xff0000) >> 16;
                            DWORD g = (color & 0xff00) >> 8;
                            DWORD b = (color & 0xff);

                            if (r != (0xf0 | i1) || g != x + loadrect.left - loadpoint.x ||
                                b != y + loadrect.top - loadpoint.y) diff_count++;
                        }
                    }
                }

                hr = IDirectDrawSurface7_Unlock(texture_levels[1][i], NULL);
                ok(hr==DD_OK, "IDirectDrawSurface7_Unlock returned: %x\n",hr);

                ok(diff_count == 0, "Unexpected destination texture level pixels; %u differences at %d level\n", diff_count, i1);

                i++;
            }

            loadpoint.x /= 2;
            loadpoint.y /= 2;
            loadrect.top /= 2;
            loadrect.left /= 2;
            loadrect.right = (loadrect.right + 1) / 2;
            loadrect.bottom = (loadrect.bottom + 1) / 2;
        }

        for (i = 0; i < 2; i++)
        {
            for (i1 = 7; i1 >= 0; i1--)
            {
                if (texture_levels[i][i1]) IDirectDrawSurface7_Release(texture_levels[i][i1]);
            }
        }
        memset(texture_levels, 0, sizeof(texture_levels));
    }

    /* Test palette copying. */
    for (i = 0; i < 2; i++)
    {
        memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
        ddsd.dwSize = sizeof(ddsd);
        ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
        ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
        ddsd.dwWidth = 128;
        ddsd.dwHeight = 128;
        U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
        U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_PALETTEINDEXED8;
        U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 8;
        hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &texture_levels[i][0], NULL);
        ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
        if (FAILED(hr)) goto out;

        /* Check the number of created mipmaps */
        memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
        ddsd.dwSize = sizeof(ddsd);
        hr = IDirectDrawSurface7_GetSurfaceDesc(texture_levels[i][0], &ddsd);
        ok(hr==DD_OK,"IDirectDrawSurface7_GetSurfaceDesc returned: %x\n",hr);
        ok(U2(ddsd).dwMipMapCount == 8, "unexpected mip count %u\n", U2(ddsd).dwMipMapCount);
        if (U2(ddsd).dwMipMapCount != 8) goto out;

        for (i1 = 1; i1 < 8; i1++)
        {
            hr = IDirectDrawSurface7_GetAttachedSurface(texture_levels[i][i1 - 1], &ddsd.ddsCaps, &texture_levels[i][i1]);
            ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
            if (FAILED(hr)) goto out;
        }
    }

    memset(table1, 0, sizeof(table1));
    for (i = 0; i < 3; i++)
    {
        table1[0].peBlue = i + 1;
        hr = IDirectDraw7_CreatePalette(lpDD, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, table1, &palettes[i], NULL);
        ok(hr == DD_OK, "CreatePalette returned %08x\n", hr);
        if (FAILED(hr))
        {
            skip("IDirectDraw7_CreatePalette failed; skipping further tests\n");
            goto out;
        }
    }

    hr = IDirectDrawSurface7_SetPalette(texture_levels[0][0], palettes[0]);
    ok(hr==DD_OK, "IDirectDrawSurface7_SetPalette returned: %x\n", hr);

    hr = IDirect3DDevice7_Load(lpD3DDevice, texture_levels[1][0], NULL, texture_levels[0][0], NULL, 0);
    ok(hr==D3D_OK, "IDirect3DDevice7_Load returned: %x\n",hr);

2456 2457 2458
    hr = IDirectDrawSurface7_GetPalette(texture_levels[0][1], &palettes[4]);
    ok(hr==DDERR_NOPALETTEATTACHED, "IDirectDrawSurface7_GetPalette returned: %x\n", hr);

2459 2460 2461 2462
    hr = IDirectDrawSurface7_GetPalette(texture_levels[1][0], &palettes[4]);
    ok(hr==DDERR_NOPALETTEATTACHED, "IDirectDrawSurface7_GetPalette returned: %x\n", hr);

    hr = IDirectDrawSurface7_SetPalette(texture_levels[0][1], palettes[1]);
2463
    ok(hr==DDERR_NOTONMIPMAPSUBLEVEL, "IDirectDrawSurface7_SetPalette returned: %x\n", hr);
2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484
    hr = IDirectDrawSurface7_SetPalette(texture_levels[1][0], palettes[2]);
    ok(hr==DD_OK, "IDirectDrawSurface7_SetPalette returned: %x\n", hr);

    hr = IDirect3DDevice7_Load(lpD3DDevice, texture_levels[1][0], NULL, texture_levels[0][0], NULL, 0);
    ok(hr==D3D_OK, "IDirect3DDevice7_Load returned: %x\n",hr);

    memset(table1, 0, sizeof(table1));
    hr = IDirectDrawSurface7_GetPalette(texture_levels[1][0], &palettes[4]);
    ok(hr==DD_OK, "IDirectDrawSurface7_GetPalette returned: %x\n", hr);
    if (SUCCEEDED(hr))
    {
        hr = IDirectDrawPalette_GetEntries(palettes[4], 0, 0, 256, table1);
        ok(hr == DD_OK, "IDirectDrawPalette_GetEntries returned %08x\n", hr);
        ok(table1[0].peBlue == 1, "Unexpected palette color after load: %u\n", (unsigned)table1[0].peBlue);
    }

    /* Test colorkey copying. */
    ddckey.dwColorSpaceLowValue = ddckey.dwColorSpaceHighValue = 64;
    hr = IDirectDrawSurface7_SetColorKey(texture_levels[0][0], DDCKEY_SRCBLT, &ddckey);
    ok(hr==DD_OK, "IDirectDrawSurface7_SetColorKey returned: %x\n", hr);
    hr = IDirectDrawSurface7_SetColorKey(texture_levels[0][1], DDCKEY_SRCBLT, &ddckey);
2485
    ok(hr == DDERR_NOTONMIPMAPSUBLEVEL, "Got unexpected hr %#x.\n", hr);
2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519

    hr = IDirectDrawSurface7_GetColorKey(texture_levels[1][0], DDCKEY_SRCBLT, &ddckey);
    ok(hr==DDERR_NOCOLORKEY, "IDirectDrawSurface7_GetColorKey returned: %x\n", hr);

    hr = IDirect3DDevice7_Load(lpD3DDevice, texture_levels[1][0], NULL, texture_levels[0][0], NULL, 0);
    ok(hr==D3D_OK, "IDirect3DDevice7_Load returned: %x\n",hr);

    hr = IDirectDrawSurface7_GetColorKey(texture_levels[1][0], DDCKEY_SRCBLT, &ddckey);
    ok(hr==DD_OK, "IDirectDrawSurface7_GetColorKey returned: %x\n", hr);
    ok(ddckey.dwColorSpaceLowValue == ddckey.dwColorSpaceHighValue && ddckey.dwColorSpaceLowValue == 64,
        "Unexpected color key values: %u - %u\n", ddckey.dwColorSpaceLowValue, ddckey.dwColorSpaceHighValue);

    out:

    for (i = 0; i < 5; i++)
    {
        if (palettes[i]) IDirectDrawPalette_Release(palettes[i]);
    }

    for (i = 0; i < 2; i++)
    {
        for (i1 = 7; i1 >= 0; i1--)
        {
            if (texture_levels[i][i1]) IDirectDrawSurface7_Release(texture_levels[i][i1]);
        }
    }

    for (i = 0; i < 2; i++)
        for (i1 = 5; i1 >= 0; i1--)
            for (i2 = 7; i2 >= 0; i2--)
            {
                if (cube_face_levels[i][i1][i2]) IDirectDrawSurface7_Release(cube_face_levels[i][i1][i2]);
            }
}
2520

2521 2522 2523 2524 2525 2526 2527 2528
static void SetMaterialTest(void)
{
    HRESULT rc;

    rc =IDirect3DDevice7_SetMaterial(lpD3DDevice, NULL);
    ok(rc == DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, got %x\n", rc);
}

2529 2530 2531
static void SetRenderTargetTest(void)
{
    HRESULT hr;
2532
    IDirectDrawSurface7 *newrt, *failrt, *oldrt, *temprt;
2533
    D3DVIEWPORT7 vp;
2534
    DDSURFACEDESC2 ddsd, ddsd2;
2535
    DWORD stateblock;
2536
    ULONG refcount;
2537 2538 2539 2540 2541 2542 2543

    memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE;
    ddsd.dwWidth = 64;
    ddsd.dwHeight = 64;
2544

2545 2546 2547 2548 2549 2550 2551 2552
    hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &newrt, NULL);
    ok(hr == DD_OK, "IDirectDraw7_CreateSurface failed, hr=0x%08x\n", hr);
    if(FAILED(hr))
    {
        skip("Skipping SetRenderTarget test\n");
        return;
    }

2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566
    memset(&ddsd2, 0, sizeof(ddsd2));
    ddsd2.dwSize = sizeof(ddsd2);
    ddsd2.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
    ddsd2.ddsCaps.dwCaps = DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER;
    ddsd2.dwWidth = 64;
    ddsd2.dwHeight = 64;
    U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
    U4(ddsd2).ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
    U1(U4(ddsd2).ddpfPixelFormat).dwZBufferBitDepth = 16;
    U3(U4(ddsd2).ddpfPixelFormat).dwZBitMask = 0x0000FFFF;

    hr = IDirectDraw7_CreateSurface(lpDD, &ddsd2, &failrt, NULL);
    ok(hr == DD_OK, "IDirectDraw7_CreateSurface failed, hr=0x%08x\n", hr);

2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579
    memset(&vp, 0, sizeof(vp));
    vp.dwX = 10;
    vp.dwY = 10;
    vp.dwWidth = 246;
    vp.dwHeight = 246;
    vp.dvMinZ = 0.25;
    vp.dvMaxZ = 0.75;
    hr = IDirect3DDevice7_SetViewport(lpD3DDevice, &vp);
    ok(hr == D3D_OK, "IDirect3DDevice7_SetViewport failed, hr=0x%08x\n", hr);

    hr = IDirect3DDevice7_GetRenderTarget(lpD3DDevice, &oldrt);
    ok(hr == DD_OK, "IDirect3DDevice7_GetRenderTarget failed, hr=0x%08x\n", hr);

2580
    refcount = getRefcount((IUnknown*) oldrt);
2581
    ok(refcount == 3, "Refcount should be 3, returned is %d\n", refcount);
2582 2583 2584 2585

    refcount = getRefcount((IUnknown*) failrt);
    ok(refcount == 1, "Refcount should be 1, returned is %d\n", refcount);

2586
    hr = IDirect3DDevice7_SetRenderTarget(lpD3DDevice, failrt, 0);
2587
    ok(hr != D3D_OK, "IDirect3DDevice7_SetRenderTarget succeeded\n");
2588

2589
    refcount = getRefcount((IUnknown*) oldrt);
2590
    ok(refcount == 2, "Refcount should be 2, returned is %d\n", refcount);
2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601

    refcount = getRefcount((IUnknown*) failrt);
    ok(refcount == 2, "Refcount should be 2, returned is %d\n", refcount);

    hr = IDirect3DDevice7_GetRenderTarget(lpD3DDevice, &temprt);
    ok(hr == DD_OK, "IDirect3DDevice7_GetRenderTarget failed, hr=0x%08x\n", hr);
    ok(failrt == temprt, "Wrong iface returned\n");

    refcount = getRefcount((IUnknown*) failrt);
    ok(refcount == 3, "Refcount should be 3, returned is %d\n", refcount);

2602 2603
    hr = IDirect3DDevice7_SetRenderTarget(lpD3DDevice, newrt, 0);
    ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderTarget failed, hr=0x%08x\n", hr);
2604 2605 2606 2607

    refcount = getRefcount((IUnknown*) failrt);
    ok(refcount == 2, "Refcount should be 2, returned is %d\n", refcount);

2608 2609 2610 2611 2612 2613 2614
    memset(&vp, 0xff, sizeof(vp));
    hr = IDirect3DDevice7_GetViewport(lpD3DDevice, &vp);
    ok(hr == D3D_OK, "IDirect3DDevice7_GetViewport failed, hr=0x%08x\n", hr);
    ok(vp.dwX == 10, "vp.dwX is %u, expected 10\n", vp.dwX);
    ok(vp.dwY == 10, "vp.dwY is %u, expected 10\n", vp.dwY);
    ok(vp.dwWidth == 246, "vp.dwWidth is %u, expected 246\n", vp.dwWidth);
    ok(vp.dwHeight == 246, "vp.dwHeight is %u, expected 246\n", vp.dwHeight);
2615 2616
    ok(vp.dvMinZ == 0.25, "vp.dvMinZ is %f, expected 0.25\n", vp.dvMinZ);
    ok(vp.dvMaxZ == 0.75, "vp.dvMaxZ is %f, expected 0.75\n", vp.dvMaxZ);
2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671

    memset(&vp, 0, sizeof(vp));
    vp.dwX = 0;
    vp.dwY = 0;
    vp.dwWidth = 64;
    vp.dwHeight = 64;
    vp.dvMinZ = 0.0;
    vp.dvMaxZ = 1.0;
    hr = IDirect3DDevice7_SetViewport(lpD3DDevice, &vp);
    ok(hr == D3D_OK, "IDirect3DDevice7_SetViewport failed, hr=0x%08x\n", hr);

    hr = IDirect3DDevice7_BeginStateBlock(lpD3DDevice);
    ok(hr == D3D_OK, "IDirect3DDevice7_BeginStateblock failed, hr=0x%08x\n", hr);
    hr = IDirect3DDevice7_SetRenderTarget(lpD3DDevice, oldrt, 0);
    ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderTarget failed, hr=0x%08x\n", hr);

    /* Check this twice, before and after ending the stateblock */
    memset(&vp, 0xff, sizeof(vp));
    hr = IDirect3DDevice7_GetViewport(lpD3DDevice, &vp);
    ok(hr == D3D_OK, "IDirect3DDevice7_GetViewport failed, hr=0x%08x\n", hr);
    ok(vp.dwX == 0, "vp.dwX is %u, expected 0\n", vp.dwX);
    ok(vp.dwY == 0, "vp.dwY is %u, expected 0\n", vp.dwY);
    ok(vp.dwWidth == 64, "vp.dwWidth is %u, expected 64\n", vp.dwWidth);
    ok(vp.dwHeight == 64, "vp.dwHeight is %u, expected 64\n", vp.dwHeight);
    ok(vp.dvMinZ == 0.0, "vp.dvMinZ is %f, expected 0.0\n", vp.dvMinZ);
    ok(vp.dvMaxZ == 1.0, "vp.dvMaxZ is %f, expected 1.0\n", vp.dvMaxZ);

    hr = IDirect3DDevice7_EndStateBlock(lpD3DDevice, &stateblock);
    ok(hr == D3D_OK, "IDirect3DDevice7_EndStateblock failed, hr=0x%08x\n", hr);

    memset(&vp, 0xff, sizeof(vp));
    hr = IDirect3DDevice7_GetViewport(lpD3DDevice, &vp);
    ok(hr == D3D_OK, "IDirect3DDevice7_GetViewport failed, hr=0x%08x\n", hr);
    ok(vp.dwX == 0, "vp.dwX is %u, expected 0\n", vp.dwX);
    ok(vp.dwY == 0, "vp.dwY is %u, expected 0\n", vp.dwY);
    ok(vp.dwWidth == 64, "vp.dwWidth is %u, expected 64\n", vp.dwWidth);
    ok(vp.dwHeight == 64, "vp.dwHeight is %u, expected 64\n", vp.dwHeight);
    ok(vp.dvMinZ == 0.0, "vp.dvMinZ is %f, expected 0.0\n", vp.dvMinZ);
    ok(vp.dvMaxZ == 1.0, "vp.dvMaxZ is %f, expected 1.0\n", vp.dvMaxZ);

    hr = IDirect3DDevice7_DeleteStateBlock(lpD3DDevice, stateblock);
    ok(hr == D3D_OK, "IDirect3DDevice7_DeleteStateblock failed, hr=0x%08x\n", hr);

    memset(&vp, 0, sizeof(vp));
    vp.dwX = 0;
    vp.dwY = 0;
    vp.dwWidth = 256;
    vp.dwHeight = 256;
    vp.dvMinZ = 0.0;
    vp.dvMaxZ = 0.0;
    hr = IDirect3DDevice7_SetViewport(lpD3DDevice, &vp);
    ok(hr == D3D_OK, "IDirect3DDevice7_SetViewport failed, hr=0x%08x\n", hr);

    IDirectDrawSurface7_Release(oldrt);
    IDirectDrawSurface7_Release(newrt);
2672
    IDirectDrawSurface7_Release(failrt);
2673
    IDirectDrawSurface7_Release(failrt);
2674 2675
}

2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719
static void VertexBufferLockRest(void)
{
    D3DVERTEXBUFFERDESC desc;
    IDirect3DVertexBuffer7 *buffer;
    HRESULT hr;
    unsigned int i;
    void *data;
    const struct
    {
        DWORD flags;
        const char *debug_string;
        HRESULT result;
    }
    test_data[] =
    {
        {0,                                         "(none)",                                       D3D_OK },
        {DDLOCK_WAIT,                               "DDLOCK_WAIT",                                  D3D_OK },
        {DDLOCK_EVENT,                              "DDLOCK_EVENT",                                 D3D_OK },
        {DDLOCK_READONLY,                           "DDLOCK_READONLY",                              D3D_OK },
        {DDLOCK_WRITEONLY,                          "DDLOCK_WRITEONLY",                             D3D_OK },
        {DDLOCK_NOSYSLOCK,                          "DDLOCK_NOSYSLOCK",                             D3D_OK },
        {DDLOCK_NOOVERWRITE,                        "DDLOCK_NOOVERWRITE",                           D3D_OK },
        {DDLOCK_DISCARDCONTENTS,                    "DDLOCK_DISCARDCONTENTS",                       D3D_OK },

        {DDLOCK_READONLY | DDLOCK_WRITEONLY,        "DDLOCK_READONLY | DDLOCK_WRITEONLY",           D3D_OK },
        {DDLOCK_READONLY | DDLOCK_DISCARDCONTENTS,  "DDLOCK_READONLY | DDLOCK_DISCARDCONTENTS",     D3D_OK },
        {0xdeadbeef,                                "0xdeadbeef",                                   D3D_OK },
    };

    memset(&desc, 0 , sizeof(desc));
    desc.dwSize = sizeof(desc);
    desc.dwCaps = 0;
    desc.dwFVF = D3DFVF_XYZ;
    desc.dwNumVertices = 64;
    hr = IDirect3D7_CreateVertexBuffer(lpD3D, &desc, &buffer, 0);
    ok(hr == D3D_OK, "IDirect3D7_CreateVertexBuffer failed, 0x%08x\n", hr);

    for(i = 0; i < (sizeof(test_data) / sizeof(*test_data)); i++)
    {
        hr = IDirect3DVertexBuffer7_Lock(buffer, test_data[i].flags, &data, NULL);
        ok(hr == test_data[i].result, "Lock flags %s returned 0x%08x, expected 0x%08x\n",
            test_data[i].debug_string, hr, test_data[i].result);
        if(SUCCEEDED(hr))
        {
2720
            ok(data != NULL, "The data pointer returned by Lock is NULL\n");
2721 2722 2723 2724 2725 2726 2727 2728
            hr = IDirect3DVertexBuffer7_Unlock(buffer);
            ok(hr == D3D_OK, "IDirect3DVertexBuffer7_Unlock failed, 0x%08x\n", hr);
        }
    }

    IDirect3DVertexBuffer7_Release(buffer);
}

2729 2730
static void FindDevice(void)
{
2731 2732 2733
    static const struct
    {
        const GUID *guid;
2734
        BOOL todo;
2735 2736
    } deviceGUIDs[] =
    {
2737 2738
        {&IID_IDirect3DRampDevice, TRUE},
        {&IID_IDirect3DRGBDevice,  FALSE},
2739 2740
    };

2741 2742 2743 2744 2745
    static const GUID *nonexistent_deviceGUIDs[] = {&IID_IDirect3DMMXDevice,
                                                    &IID_IDirect3DRefDevice,
                                                    &IID_IDirect3DTnLHalDevice,
                                                    &IID_IDirect3DNullDevice};

2746 2747
    D3DFINDDEVICESEARCH search = {0};
    D3DFINDDEVICERESULT result = {0};
2748
    IDirect3DDevice *d3dhal;
2749
    HRESULT hr;
2750
    int i;
2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777

    /* Test invalid parameters. */
    hr = IDirect3D_FindDevice(Direct3D1, NULL, NULL);
    ok(hr == DDERR_INVALIDPARAMS,
       "Expected IDirect3D1::FindDevice to return DDERR_INVALIDPARAMS, got 0x%08x\n", hr);

    hr = IDirect3D_FindDevice(Direct3D1, NULL, &result);
    ok(hr == DDERR_INVALIDPARAMS,
       "Expected IDirect3D1::FindDevice to return DDERR_INVALIDPARAMS, got 0x%08x\n", hr);

    hr = IDirect3D_FindDevice(Direct3D1, &search, NULL);
    ok(hr == DDERR_INVALIDPARAMS,
       "Expected IDirect3D1::FindDevice to return DDERR_INVALIDPARAMS, got 0x%08x\n", hr);

    search.dwSize = 0;
    result.dwSize = 0;

    hr = IDirect3D_FindDevice(Direct3D1, &search, &result);
    ok(hr == DDERR_INVALIDPARAMS,
       "Expected IDirect3D1::FindDevice to return DDERR_INVALIDPARAMS, got 0x%08x\n", hr);

    search.dwSize = sizeof(search) + 1;
    result.dwSize = sizeof(result) + 1;

    hr = IDirect3D_FindDevice(Direct3D1, &search, &result);
    ok(hr == DDERR_INVALIDPARAMS,
       "Expected IDirect3D1::FindDevice to return DDERR_INVALIDPARAMS, got 0x%08x\n", hr);
2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797

    /* Specifying no flags is permitted. */
    search.dwSize = sizeof(search);
    search.dwFlags = 0;
    result.dwSize = sizeof(result);

    hr = IDirect3D_FindDevice(Direct3D1, &search, &result);
    ok(hr == D3D_OK,
       "Expected IDirect3D1::FindDevice to return D3D_OK, got 0x%08x\n", hr);

    /* Try an arbitrary non-device GUID. */
    search.dwSize = sizeof(search);
    search.dwFlags = D3DFDS_GUID;
    search.guid = IID_IDirect3D;
    result.dwSize = sizeof(result);

    hr = IDirect3D_FindDevice(Direct3D1, &search, &result);
    ok(hr == DDERR_NOTFOUND,
       "Expected IDirect3D1::FindDevice to return DDERR_NOTFOUND, got 0x%08x\n", hr);

2798 2799 2800 2801 2802 2803 2804
    /* These GUIDs appear to be never present. */
    for (i = 0; i < sizeof(nonexistent_deviceGUIDs)/sizeof(nonexistent_deviceGUIDs[0]); i++)
    {
        search.dwSize = sizeof(search);
        search.dwFlags = D3DFDS_GUID;
        search.guid = *nonexistent_deviceGUIDs[i];
        result.dwSize = sizeof(result);
2805

2806 2807 2808 2809
        hr = IDirect3D_FindDevice(Direct3D1, &search, &result);
        ok(hr == DDERR_NOTFOUND,
           "[%d] Expected IDirect3D1::FindDevice to return DDERR_NOTFOUND, got 0x%08x\n", i, hr);
    }
2810

2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823
    /* The HAL device can only be enumerated if hardware acceleration is present. */
    search.dwSize = sizeof(search);
    search.dwFlags = D3DFDS_GUID;
    search.guid = IID_IDirect3DHALDevice;
    result.dwSize = sizeof(result);

    hr = IDirect3D_FindDevice(Direct3D1, &search, &result);
    trace("IDirect3D::FindDevice returned 0x%08x for the HAL device GUID\n", hr);
    if (SUCCEEDED(hr))
    {
        hr = IDirectDrawSurface_QueryInterface(Surface1, &IID_IDirect3DHALDevice, (void **)&d3dhal);
        /* Currently Wine only supports the creation of one Direct3D device
         * for a given DirectDraw instance. */
2824 2825
        ok(SUCCEEDED(hr) || broken(hr == DDERR_INVALIDPIXELFORMAT) /* XP/Win2003 Wow64 on VMware */,
           "Expected IDirectDrawSurface::QueryInterface to succeed, got 0x%08x\n", hr);
2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838

        if (SUCCEEDED(hr))
            IDirect3DDevice_Release(d3dhal);
    }
    else
    {
        hr = IDirectDrawSurface_QueryInterface(Surface1, &IID_IDirect3DHALDevice, (void **)&d3dhal);
        ok(FAILED(hr), "Expected IDirectDrawSurface::QueryInterface to fail, got 0x%08x\n", hr);

        if (SUCCEEDED(hr))
            IDirect3DDevice_Release(d3dhal);
    }

2839 2840 2841 2842 2843 2844 2845 2846 2847 2848
    /* These GUIDs appear to be always present. */
    for (i = 0; i < sizeof(deviceGUIDs)/sizeof(deviceGUIDs[0]); i++)
    {
        search.dwSize = sizeof(search);
        search.dwFlags = D3DFDS_GUID;
        search.guid = *deviceGUIDs[i].guid;
        result.dwSize = sizeof(result);

        hr = IDirect3D_FindDevice(Direct3D1, &search, &result);

2849
        todo_wine_if (deviceGUIDs[i].todo)
2850 2851 2852
            ok(hr == D3D_OK,
               "[%d] Expected IDirect3D1::FindDevice to return D3D_OK, got 0x%08x\n", i, hr);
    }
2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863

    /* Curiously the color model criteria seem to be ignored. */
    search.dwSize = sizeof(search);
    search.dwFlags = D3DFDS_COLORMODEL;
    search.dcmColorModel = 0xdeadbeef;
    result.dwSize = sizeof(result);

    hr = IDirect3D_FindDevice(Direct3D1, &search, &result);
    todo_wine
    ok(hr == D3D_OK,
       "Expected IDirect3D1::FindDevice to return D3D_OK, got 0x%08x\n", hr);
2864 2865
}

2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886
static void BackBuffer3DCreateSurfaceTest(void)
{
    DDSURFACEDESC ddsd;
    DDSURFACEDESC created_ddsd;
    DDSURFACEDESC2 ddsd2;
    IDirectDrawSurface *surf;
    IDirectDrawSurface4 *surf4;
    IDirectDrawSurface7 *surf7;
    HRESULT hr;
    IDirectDraw2 *dd2;
    IDirectDraw4 *dd4;
    IDirectDraw7 *dd7;
    DDCAPS ddcaps;
    IDirect3DDevice *d3dhal;

    const DWORD caps = DDSCAPS_BACKBUFFER | DDSCAPS_3DDEVICE;
    const DWORD expected_caps = DDSCAPS_BACKBUFFER | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM;

    memset(&ddcaps, 0, sizeof(ddcaps));
    ddcaps.dwSize = sizeof(DDCAPS);
    hr = IDirectDraw_GetCaps(DirectDraw1, &ddcaps, NULL);
2887
    ok(SUCCEEDED(hr), "DirectDraw_GetCaps failed: 0x%08x\n", hr);
2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909
    if (!(ddcaps.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
    {
        skip("DDraw reported no VIDEOMEMORY cap. Broken video driver? Skipping surface caps tests.\n");
        return ;
    }

    memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
    ddsd.dwWidth = 64;
    ddsd.dwHeight = 64;
    ddsd.ddsCaps.dwCaps = caps;
    memset(&ddsd2, 0, sizeof(ddsd2));
    ddsd2.dwSize = sizeof(ddsd2);
    ddsd2.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
    ddsd2.dwWidth = 64;
    ddsd2.dwHeight = 64;
    ddsd2.ddsCaps.dwCaps = caps;
    memset(&created_ddsd, 0, sizeof(created_ddsd));
    created_ddsd.dwSize = sizeof(DDSURFACEDESC);

    hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &surf, NULL);
2910
    ok(SUCCEEDED(hr), "IDirectDraw_CreateSurface failed: 0x%08x\n", hr);
2911 2912 2913 2914
    if (surf != NULL)
    {
        hr = IDirectDrawSurface_GetSurfaceDesc(surf, &created_ddsd);
        ok(SUCCEEDED(hr), "IDirectDraw_GetSurfaceDesc failed: 0x%08x\n", hr);
2915
        ok(created_ddsd.ddsCaps.dwCaps == expected_caps,
2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958
           "GetSurfaceDesc returned caps %x, expected %x\n", created_ddsd.ddsCaps.dwCaps,
           expected_caps);

        hr = IDirectDrawSurface_QueryInterface(surf, &IID_IDirect3DHALDevice, (void **)&d3dhal);
        /* Currently Wine only supports the creation of one Direct3D device
           for a given DirectDraw instance. It has been created already
           in D3D1_createObjects() - IID_IDirect3DRGBDevice */
        todo_wine ok(SUCCEEDED(hr), "Expected IDirectDrawSurface::QueryInterface to succeed, got 0x%08x\n", hr);

        if (SUCCEEDED(hr))
            IDirect3DDevice_Release(d3dhal);

        IDirectDrawSurface_Release(surf);
    }

    hr = IDirectDraw_QueryInterface(DirectDraw1, &IID_IDirectDraw2, (void **) &dd2);
    ok(SUCCEEDED(hr), "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);

    hr = IDirectDraw2_CreateSurface(dd2, &ddsd, &surf, NULL);
    ok(hr == DDERR_INVALIDCAPS, "IDirectDraw2_CreateSurface didn't return %x08x, but %x08x\n",
       DDERR_INVALIDCAPS, hr);

    IDirectDraw2_Release(dd2);

    hr = IDirectDraw_QueryInterface(DirectDraw1, &IID_IDirectDraw4, (void **) &dd4);
    ok(SUCCEEDED(hr), "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);

    hr = IDirectDraw4_CreateSurface(dd4, &ddsd2, &surf4, NULL);
    ok(hr == DDERR_INVALIDCAPS, "IDirectDraw4_CreateSurface didn't return %x08x, but %x08x\n",
       DDERR_INVALIDCAPS, hr);

    IDirectDraw4_Release(dd4);

    hr = IDirectDraw_QueryInterface(DirectDraw1, &IID_IDirectDraw7, (void **) &dd7);
    ok(SUCCEEDED(hr), "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);

    hr = IDirectDraw7_CreateSurface(dd7, &ddsd2, &surf7, NULL);
    ok(hr == DDERR_INVALIDCAPS, "IDirectDraw7_CreateSurface didn't return %x08x, but %x08x\n",
       DDERR_INVALIDCAPS, hr);

    IDirectDraw7_Release(dd7);
}

2959 2960 2961 2962 2963
static void BackBuffer3DAttachmentTest(void)
{
    HRESULT hr;
    IDirectDrawSurface *surface1, *surface2, *surface3, *surface4;
    DDSURFACEDESC ddsd;
2964 2965
    HWND window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
            100, 100, 160, 160, NULL, NULL, NULL, NULL);
2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977

    hr = IDirectDraw_SetCooperativeLevel(DirectDraw1, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
    ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);

    /* Perform attachment tests on a back-buffer */
    memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER | DDSCAPS_3DDEVICE;
    ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
    ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
    hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &surface2, NULL);
2978
    ok(SUCCEEDED(hr), "CreateSurface returned: %x\n",hr);
2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056

    if (surface2 != NULL)
    {
        /* Try a single primary and a two back buffers */
        memset(&ddsd, 0, sizeof(ddsd));
        ddsd.dwSize = sizeof(ddsd);
        ddsd.dwFlags = DDSD_CAPS;
        ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
        hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &surface1, NULL);
        ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);

        memset(&ddsd, 0, sizeof(ddsd));
        ddsd.dwSize = sizeof(ddsd);
        ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
        ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER | DDSCAPS_3DDEVICE;
        ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
        ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
        hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &surface3, NULL);
        ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);

        /* This one has a different size */
        memset(&ddsd, 0, sizeof(ddsd));
        ddsd.dwSize = sizeof(ddsd);
        ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
        ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER | DDSCAPS_3DDEVICE;
        ddsd.dwWidth = 128;
        ddsd.dwHeight = 128;
        hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &surface4, NULL);
        ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);

        hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
        todo_wine ok(hr == DD_OK || broken(hr == DDERR_CANNOTATTACHSURFACE),
           "Attaching a back buffer to a front buffer returned %08x\n", hr);
        if(SUCCEEDED(hr))
        {
            /* Try the reverse without detaching first */
            hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
            ok(hr == DDERR_SURFACEALREADYATTACHED, "Attaching an attached surface to its attachee returned %08x\n", hr);
            hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
            ok(hr == DD_OK, "DeleteAttachedSurface failed with %08x\n", hr);
        }
        hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
        todo_wine ok(hr == DD_OK || broken(hr == DDERR_CANNOTATTACHSURFACE),
           "Attaching a front buffer to a back buffer returned %08x\n", hr);
        if(SUCCEEDED(hr))
        {
            /* Try to detach reversed */
            hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
            ok(hr == DDERR_CANNOTDETACHSURFACE, "DeleteAttachedSurface returned %08x\n", hr);
            /* Now the proper detach */
            hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface1);
            ok(hr == DD_OK, "DeleteAttachedSurface failed with %08x\n", hr);
        }
        hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface3);
        todo_wine ok(hr == DD_OK || broken(hr == DDERR_CANNOTATTACHSURFACE),
           "Attaching a back buffer to another back buffer returned %08x\n", hr);
        if(SUCCEEDED(hr))
        {
            hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface3);
            ok(hr == DD_OK, "DeleteAttachedSurface failed with %08x\n", hr);
        }
        hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface4);
        ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a back buffer to a front buffer of different size returned %08x\n", hr);
        hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface1);
        ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a front buffer to a back buffer of different size returned %08x\n", hr);

        IDirectDrawSurface_Release(surface4);
        IDirectDrawSurface_Release(surface3);
        IDirectDrawSurface_Release(surface2);
        IDirectDrawSurface_Release(surface1);
    }

    hr =IDirectDraw_SetCooperativeLevel(DirectDraw1, NULL, DDSCL_NORMAL);
    ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);

    DestroyWindow(window);
}

3057 3058 3059
static void dump_format(const DDPIXELFORMAT *fmt)
{
    trace("dwFlags %08x, FourCC %08x, dwZBufferBitDepth %u, stencil %08x\n", fmt->dwFlags, fmt->dwFourCC,
3060 3061 3062
          U1(*fmt).dwZBufferBitDepth, U2(*fmt).dwStencilBitDepth);
    trace("dwZBitMask %08x, dwStencilBitMask %08x, dwRGBZBitMask %08x\n", U3(*fmt).dwZBitMask,
          U4(*fmt).dwStencilBitMask, U5(*fmt).dwRGBZBitMask);
3063 3064
}

3065
static HRESULT WINAPI enum_z_fmt_cb(DDPIXELFORMAT *fmt, void *ctx)
3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107
{
    static const DDPIXELFORMAT formats[] =
    {
        {
            sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0,
            {16}, {0}, {0x0000ffff}, {0x00000000}, {0x00000000}
        },
        {
            sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0,
            {32}, {0}, {0xffffff00}, {0x00000000}, {0x00000000}
        },
        {
            sizeof(DDPIXELFORMAT), DDPF_ZBUFFER | DDPF_STENCILBUFFER, 0,
            {32}, {8}, {0xffffff00}, {0x000000ff}, {0x00000000}
        },
        {
            sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0,
            {32}, {0}, {0x00ffffff}, {0x00000000}, {0x00000000}
        },
        {
            sizeof(DDPIXELFORMAT), DDPF_ZBUFFER | DDPF_STENCILBUFFER, 0,
            {32}, {8}, {0x00ffffff}, {0xff000000}, {0x00000000}
        },
        {
            sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0,
            {24}, {0}, {0x00ffffff}, {0x00000000}, {0x00000000}
        },
        {
            sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0,
            {32}, {0}, {0xffffffff}, {0x00000000}, {0x00000000}
        },
    };
    unsigned int *count = ctx, i, expected_pitch;
    DDSURFACEDESC2 ddsd;
    IDirectDrawSurface7 *surface;
    HRESULT hr;
    (*count)++;

    memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
3108
    U4(ddsd).ddpfPixelFormat = *fmt;
3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123
    ddsd.dwWidth = 1024;
    ddsd.dwHeight = 1024;
    hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &surface, NULL);
    ok(SUCCEEDED(hr), "IDirectDraw7_CreateSurface failed, hr %#x.\n", hr);
    memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd);
    ok(SUCCEEDED(hr), "IDirectDrawSurface7_GetSurfaceDesc failed, hr %#x.\n", hr);
    IDirectDrawSurface7_Release(surface);

    ok(ddsd.dwFlags & DDSD_PIXELFORMAT, "DDSD_PIXELFORMAT is not set\n");
    ok(!(ddsd.dwFlags & DDSD_ZBUFFERBITDEPTH), "DDSD_ZBUFFERBITDEPTH is set\n");

    /* 24 bit unpadded depth buffers are actually padded(Geforce 9600, Win7,
     * Radeon 9000M WinXP) */
3124 3125
    if (U1(*fmt).dwZBufferBitDepth == 24) expected_pitch = ddsd.dwWidth * 4;
    else expected_pitch = ddsd.dwWidth * U1(*fmt).dwZBufferBitDepth / 8;
3126

3127 3128 3129 3130 3131 3132
    /* Some formats(16 bit depth without stencil) return pitch 0
     *
     * The Radeon X1600 Catalyst 10.2 Windows XP driver returns an otherwise sane
     * pitch with an extra 128 bytes, regardless of the format and width */
    if (U1(ddsd).lPitch != 0 && U1(ddsd).lPitch != expected_pitch
            && !broken(U1(ddsd).lPitch == expected_pitch + 128))
3133
    {
3134
        ok(0, "Z buffer pitch is %u, expected %u\n", U1(ddsd).lPitch, expected_pitch);
3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164
        dump_format(fmt);
    }

    for (i = 0; i < (sizeof(formats)/sizeof(*formats)); i++)
    {
        if (memcmp(&formats[i], fmt, fmt->dwSize) == 0) return DDENUMRET_OK;
    }

    ok(0, "Unexpected Z format enumerated\n");
    dump_format(fmt);

    return DDENUMRET_OK;
}

static void z_format_test(void)
{
    unsigned int count = 0;
    HRESULT hr;

    hr = IDirect3D7_EnumZBufferFormats(lpD3D, &IID_IDirect3DHALDevice, enum_z_fmt_cb, &count);
    if (hr == DDERR_NOZBUFFERHW)
    {
        skip("Z buffers not supported, skipping Z buffer format test\n");
        return;
    }

    ok(SUCCEEDED(hr), "IDirect3D7_EnumZBufferFormats failed, hr %#x.\n", hr);
    ok(count, "Expected at least one supported Z Buffer format\n");
}

3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249
static void test_get_caps1(void)
{
    D3DDEVICEDESC hw_caps, hel_caps;
    HRESULT hr;
    unsigned int i;

    memset(&hw_caps, 0, sizeof(hw_caps));
    hw_caps.dwSize = sizeof(hw_caps);
    hw_caps.dwFlags = 0xdeadbeef;
    memset(&hel_caps, 0, sizeof(hel_caps));
    hel_caps.dwSize = sizeof(hel_caps);
    hel_caps.dwFlags = 0xdeadc0de;

    /* NULL pointers */
    hr = IDirect3DDevice_GetCaps(Direct3DDevice1, &hw_caps, NULL);
    ok(hr == DDERR_INVALIDPARAMS, "GetCaps with NULL hel caps returned hr %#x, expected INVALIDPARAMS.\n", hr);
    ok(hw_caps.dwFlags == 0xdeadbeef, "hw_caps.dwFlags was modified: %#x.\n", hw_caps.dwFlags);
    hr = IDirect3DDevice_GetCaps(Direct3DDevice1, NULL, &hel_caps);
    ok(hr == DDERR_INVALIDPARAMS, "GetCaps with NULL hw caps returned hr %#x, expected INVALIDPARAMS.\n", hr);
    ok(hel_caps.dwFlags == 0xdeadc0de, "hel_caps.dwFlags was modified: %#x.\n", hel_caps.dwFlags);

    /* Successful call: Both are modified */
    hr = IDirect3DDevice_GetCaps(Direct3DDevice1, &hw_caps, &hel_caps);
    ok(hr == D3D_OK, "GetCaps with correct size returned hr %#x, expected D3D_OK.\n", hr);
    ok(hw_caps.dwFlags != 0xdeadbeef, "hw_caps.dwFlags was not modified: %#x.\n", hw_caps.dwFlags);
    ok(hel_caps.dwFlags != 0xdeadc0de, "hel_caps.dwFlags was not modified: %#x.\n", hel_caps.dwFlags);

    memset(&hw_caps, 0, sizeof(hw_caps));
    hw_caps.dwSize = sizeof(hw_caps);
    hw_caps.dwFlags = 0xdeadbeef;
    memset(&hel_caps, 0, sizeof(hel_caps));
    /* Keep dwSize at 0 */
    hel_caps.dwFlags = 0xdeadc0de;

    /* If one is invalid the call fails */
    hr = IDirect3DDevice_GetCaps(Direct3DDevice1, &hw_caps, &hel_caps);
    ok(hr == DDERR_INVALIDPARAMS, "GetCaps with invalid hel_caps size returned hr %#x, expected INVALIDPARAMS.\n", hr);
    ok(hw_caps.dwFlags == 0xdeadbeef, "hw_caps.dwFlags was modified: %#x.\n", hw_caps.dwFlags);
    ok(hel_caps.dwFlags == 0xdeadc0de, "hel_caps.dwFlags was modified: %#x.\n", hel_caps.dwFlags);
    hel_caps.dwSize = sizeof(hel_caps);
    hw_caps.dwSize = sizeof(hw_caps) + 1;
    hr = IDirect3DDevice_GetCaps(Direct3DDevice1, &hw_caps, &hel_caps);
    ok(hr == DDERR_INVALIDPARAMS, "GetCaps with invalid hw_caps size returned hr %#x, expected INVALIDPARAMS.\n", hr);
    ok(hw_caps.dwFlags == 0xdeadbeef, "hw_caps.dwFlags was modified: %#x.\n", hw_caps.dwFlags);
    ok(hel_caps.dwFlags == 0xdeadc0de, "hel_caps.dwFlags was modified: %#x.\n", hel_caps.dwFlags);

    for (i = 0; i < 1024; i++)
    {
        memset(&hw_caps, 0xfe, sizeof(hw_caps));
        memset(&hel_caps, 0xfe, sizeof(hel_caps));
        hw_caps.dwSize = hel_caps.dwSize = i;
        hr = IDirect3DDevice_GetCaps(Direct3DDevice1, &hw_caps, &hel_caps);
        switch (i)
        {
            /* D3DDEVICEDESCSIZE in old sdk versions */
            case FIELD_OFFSET(D3DDEVICEDESC, dwMinTextureWidth): /* 172, DirectX 3, IDirect3DDevice1 */
                ok(hw_caps.dwMinTextureWidth == 0xfefefefe, "hw_caps.dwMinTextureWidth was modified: %#x.\n",
                        hw_caps.dwMinTextureWidth);
                ok(hel_caps.dwMinTextureWidth == 0xfefefefe, "hel_caps.dwMinTextureWidth was modified: %#x.\n",
                        hel_caps.dwMinTextureWidth);
                /* drop through */
            case FIELD_OFFSET(D3DDEVICEDESC, dwMaxTextureRepeat): /* 204, DirectX 5, IDirect3DDevice2 */
                ok(hw_caps.dwMaxTextureRepeat == 0xfefefefe, "hw_caps.dwMaxTextureRepeat was modified: %#x.\n",
                        hw_caps.dwMaxTextureRepeat);
                ok(hel_caps.dwMaxTextureRepeat == 0xfefefefe, "hel_caps.dwMaxTextureRepeat was modified: %#x.\n",
                        hel_caps.dwMaxTextureRepeat);
                /* drop through */
            case sizeof(D3DDEVICEDESC): /* 252, DirectX 6, IDirect3DDevice3 */
                ok(hr == D3D_OK, "GetCaps with size %u returned hr %#x, expected D3D_OK.\n", i, hr);
                break;

            default:
                ok(hr == DDERR_INVALIDPARAMS,
                        "GetCaps with size %u returned hr %#x, expected DDERR_INVALIDPARAMS.\n", i, hr);
                break;
        }
    }

    /* Different valid sizes are OK */
    hw_caps.dwSize = 172;
    hel_caps.dwSize = sizeof(D3DDEVICEDESC);
    hr = IDirect3DDevice_GetCaps(Direct3DDevice1, &hw_caps, &hel_caps);
    ok(hr == D3D_OK, "GetCaps with different sizes returned hr %#x, expected D3D_OK.\n", hr);
}

3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264
static void test_get_caps7(void)
{
    HRESULT hr;
    D3DDEVICEDESC7 desc;

    hr = IDirect3DDevice7_GetCaps(lpD3DDevice, NULL);
    ok(hr == DDERR_INVALIDPARAMS, "IDirect3DDevice7::GetCaps(NULL) returned hr %#x, expected INVALIDPARAMS.\n", hr);

    memset(&desc, 0, sizeof(desc));
    hr = IDirect3DDevice7_GetCaps(lpD3DDevice, &desc);
    ok(hr == D3D_OK, "IDirect3DDevice7::GetCaps(non-NULL) returned hr %#x, expected D3D_OK.\n", hr);

    /* There's no dwSize in D3DDEVICEDESC7 */
}

3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456
struct d3d2_test_context
{
    IDirectDraw *ddraw;
    IDirect3D2 *d3d;
    IDirectDrawSurface *surface;
    IDirect3DDevice2 *device;
    IDirect3DViewport2 *viewport;
};

static void d3d2_release_objects(struct d3d2_test_context *context)
{
    LONG ref;
    HRESULT hr;

    if (context->viewport)
    {
        hr = IDirect3DDevice2_DeleteViewport(context->device, context->viewport);
        ok(hr == D3D_OK, "DeleteViewport returned %08x.\n", hr);
        ref = IDirect3DViewport2_Release(context->viewport);
        ok(ref == 0, "Viewport has reference count %d, expected 0.\n", ref);
    }
    if (context->device)
    {
        ref = IDirect3DDevice2_Release(context->device);
        ok(ref == 0, "Device has reference count %d, expected 0.\n", ref);
    }
    if (context->surface)
    {
        ref = IDirectDrawSurface_Release(context->surface);
        ok(ref == 0, "Surface has reference count %d, expected 0.\n", ref);
    }
    if (context->d3d)
    {
        ref = IDirect3D2_Release(context->d3d);
        ok(ref == 1, "IDirect3D2 has reference count %d, expected 1.\n", ref);
    }
    if (context->ddraw)
    {
        ref = IDirectDraw_Release(context->ddraw);
        ok(ref == 0, "DDraw has reference count %d, expected 0.\n", ref);
    }
}

static BOOL d3d2_create_objects(struct d3d2_test_context *context)
{
    HRESULT hr;
    DDSURFACEDESC ddsd;
    D3DVIEWPORT vp_data;

    memset(context, 0, sizeof(*context));

    hr = DirectDrawCreate(NULL, &context->ddraw, NULL);
    ok(hr == DD_OK || hr == DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreate failed: %08x.\n", hr);
    if (!context->ddraw) goto error;

    hr = IDirectDraw_SetCooperativeLevel(context->ddraw, NULL, DDSCL_NORMAL);
    ok(hr == DD_OK, "SetCooperativeLevel failed: %08x.\n", hr);
    if (FAILED(hr)) goto error;

    hr = IDirectDraw_QueryInterface(context->ddraw, &IID_IDirect3D2, (void**) &context->d3d);
    ok(hr == DD_OK || hr == E_NOINTERFACE, "QueryInterface failed: %08x.\n", hr);
    if (!context->d3d) goto error;

    memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
    ddsd.dwWidth = 256;
    ddsd.dwHeight = 256;
    IDirectDraw_CreateSurface(context->ddraw, &ddsd, &context->surface, NULL);
    if (!context->surface)
    {
        skip("DDSCAPS_3DDEVICE surface not available.\n");
        goto error;
    }

    hr = IDirect3D2_CreateDevice(context->d3d, &IID_IDirect3DHALDevice, context->surface, &context->device);
    ok(hr == D3D_OK  || hr == E_OUTOFMEMORY || hr == E_NOINTERFACE, "CreateDevice failed: %08x.\n", hr);
    if (!context->device) goto error;

    hr = IDirect3D2_CreateViewport(context->d3d, &context->viewport, NULL);
    ok(hr == D3D_OK, "CreateViewport failed: %08x.\n", hr);
    if (!context->viewport) goto error;

    hr = IDirect3DDevice2_AddViewport(context->device, context->viewport);
    ok(hr == D3D_OK, "AddViewport returned %08x.\n", hr);
    vp_data.dwSize = sizeof(vp_data);
    vp_data.dwX = 0;
    vp_data.dwY = 0;
    vp_data.dwWidth = 256;
    vp_data.dwHeight = 256;
    vp_data.dvScaleX = 1;
    vp_data.dvScaleY = 1;
    vp_data.dvMaxX = 256;
    vp_data.dvMaxY = 256;
    vp_data.dvMinZ = 0;
    vp_data.dvMaxZ = 1;
    hr = IDirect3DViewport2_SetViewport(context->viewport, &vp_data);
    ok(hr == D3D_OK, "SetViewport returned %08x.\n", hr);

    return TRUE;

error:
    d3d2_release_objects(context);
    return FALSE;
}

static void test_get_caps2(const struct d3d2_test_context *context)
{
    D3DDEVICEDESC hw_caps, hel_caps;
    HRESULT hr;
    unsigned int i;

    memset(&hw_caps, 0, sizeof(hw_caps));
    hw_caps.dwSize = sizeof(hw_caps);
    hw_caps.dwFlags = 0xdeadbeef;
    memset(&hel_caps, 0, sizeof(hel_caps));
    hel_caps.dwSize = sizeof(hel_caps);
    hel_caps.dwFlags = 0xdeadc0de;

    /* NULL pointers */
    hr = IDirect3DDevice2_GetCaps(context->device, &hw_caps, NULL);
    ok(hr == DDERR_INVALIDPARAMS, "GetCaps with NULL hel caps returned hr %#x, expected INVALIDPARAMS.\n", hr);
    ok(hw_caps.dwFlags == 0xdeadbeef, "hw_caps.dwFlags was modified: %#x.\n", hw_caps.dwFlags);
    hr = IDirect3DDevice2_GetCaps(context->device, NULL, &hel_caps);
    ok(hr == DDERR_INVALIDPARAMS, "GetCaps with NULL hw caps returned hr %#x, expected INVALIDPARAMS.\n", hr);
    ok(hel_caps.dwFlags == 0xdeadc0de, "hel_caps.dwFlags was modified: %#x.\n", hel_caps.dwFlags);

    /* Successful call: Both are modified */
    hr = IDirect3DDevice2_GetCaps(context->device, &hw_caps, &hel_caps);
    ok(hr == D3D_OK, "GetCaps with correct size returned hr %#x, expected D3D_OK.\n", hr);
    ok(hw_caps.dwFlags != 0xdeadbeef, "hw_caps.dwFlags was not modified: %#x.\n", hw_caps.dwFlags);
    ok(hel_caps.dwFlags != 0xdeadc0de, "hel_caps.dwFlags was not modified: %#x.\n", hel_caps.dwFlags);

    memset(&hw_caps, 0, sizeof(hw_caps));
    hw_caps.dwSize = sizeof(hw_caps);
    hw_caps.dwFlags = 0xdeadbeef;
    memset(&hel_caps, 0, sizeof(hel_caps));
    /* Keep dwSize at 0 */
    hel_caps.dwFlags = 0xdeadc0de;

    /* If one is invalid the call fails */
    hr = IDirect3DDevice2_GetCaps(context->device, &hw_caps, &hel_caps);
    ok(hr == DDERR_INVALIDPARAMS, "GetCaps with invalid hel_caps size returned hr %#x, expected INVALIDPARAMS.\n", hr);
    ok(hw_caps.dwFlags == 0xdeadbeef, "hw_caps.dwFlags was modified: %#x.\n", hw_caps.dwFlags);
    ok(hel_caps.dwFlags == 0xdeadc0de, "hel_caps.dwFlags was modified: %#x.\n", hel_caps.dwFlags);
    hel_caps.dwSize = sizeof(hel_caps);
    hw_caps.dwSize = sizeof(hw_caps) + 1;
    hr = IDirect3DDevice2_GetCaps(context->device, &hw_caps, &hel_caps);
    ok(hr == DDERR_INVALIDPARAMS, "GetCaps with invalid hw_caps size returned hr %#x, expected INVALIDPARAMS.\n", hr);
    ok(hw_caps.dwFlags == 0xdeadbeef, "hw_caps.dwFlags was modified: %#x.\n", hw_caps.dwFlags);
    ok(hel_caps.dwFlags == 0xdeadc0de, "hel_caps.dwFlags was modified: %#x.\n", hel_caps.dwFlags);

    for (i = 0; i < 1024; i++)
    {
        memset(&hw_caps, 0xfe, sizeof(hw_caps));
        memset(&hel_caps, 0xfe, sizeof(hel_caps));
        hw_caps.dwSize = hel_caps.dwSize = i;
        hr = IDirect3DDevice2_GetCaps(context->device, &hw_caps, &hel_caps);
        switch (i)
        {
            /* D3DDEVICEDESCSIZE in old sdk versions */
            case FIELD_OFFSET(D3DDEVICEDESC, dwMinTextureWidth): /* 172, DirectX 3, IDirect3DDevice1 */
                ok(hw_caps.dwMinTextureWidth == 0xfefefefe, "dwMinTextureWidth was modified: %#x.\n",
                        hw_caps.dwMinTextureWidth);
                ok(hel_caps.dwMinTextureWidth == 0xfefefefe, "dwMinTextureWidth was modified: %#x.\n",
                        hel_caps.dwMinTextureWidth);
                /* drop through */
            case FIELD_OFFSET(D3DDEVICEDESC, dwMaxTextureRepeat): /* 204, DirectX 5, IDirect3DDevice2 */
                ok(hw_caps.dwMaxTextureRepeat == 0xfefefefe, "dwMaxTextureRepeat was modified: %#x.\n",
                        hw_caps.dwMaxTextureRepeat);
                ok(hel_caps.dwMaxTextureRepeat == 0xfefefefe, "dwMaxTextureRepeat was modified: %#x.\n",
                        hel_caps.dwMaxTextureRepeat);
                /* drop through */
            case sizeof(D3DDEVICEDESC): /* 252, DirectX 6, IDirect3DDevice3 */
                ok(hr == D3D_OK, "GetCaps with size %u returned hr %#x, expected D3D_OK.\n", i, hr);
                break;

            default:
                ok(hr == DDERR_INVALIDPARAMS,
                        "GetCaps with size %u returned hr %#x, expected DDERR_INVALIDPARAMS.\n", i, hr);
                break;
        }
    }

    /* Different valid sizes are OK */
    hw_caps.dwSize = 172;
    hel_caps.dwSize = sizeof(D3DDEVICEDESC);
    hr = IDirect3DDevice2_GetCaps(context->device, &hw_caps, &hel_caps);
    ok(hr == D3D_OK, "GetCaps with different sizes returned hr %#x, expected D3D_OK.\n", hr);
}

3457 3458
START_TEST(d3d)
{
3459 3460 3461 3462 3463 3464 3465
    struct d3d2_test_context d3d2_context;
    void (* const d3d2_tests[])(const struct d3d2_test_context *) =
    {
        test_get_caps2
    };
    unsigned int i;

3466 3467
    init_function_pointers();
    if(!pDirectDrawCreateEx) {
3468
        win_skip("function DirectDrawCreateEx not available\n");
3469 3470 3471
        return;
    }

3472
    if(!CreateDirect3D()) {
3473 3474 3475 3476 3477 3478 3479
        skip("Skipping d3d7 tests\n");
    } else {
        LightTest();
        StateTest();
        SceneTest();
        LimitTest();
        D3D7EnumTest();
3480
        D3D7EnumLifetimeTest();
3481
        SetMaterialTest();
3482
        CapsTest();
3483
        VertexBufferDescTest();
3484
        D3D7_OldRenderStateTest();
3485
        DeviceLoadTest();
3486
        SetRenderTargetTest();
3487
        VertexBufferLockRest();
3488
        z_format_test();
3489
        test_get_caps7();
3490 3491 3492
        ReleaseDirect3D();
    }

3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504
    for (i = 0; i < (sizeof(d3d2_tests) / sizeof(*d3d2_tests)); i++)
    {
        if (!d3d2_create_objects(&d3d2_context))
        {
            ok(!i, "Unexpected d3d2 initialization failure.\n");
            skip("Skipping d3d2 tests.\n");
            break;
        }
        d3d2_tests[i](&d3d2_context);
        d3d2_release_objects(&d3d2_context);
    }

3505 3506 3507 3508
    if (!D3D1_createObjects()) {
        skip("Skipping d3d1 tests\n");
    } else {
        Direct3D1Test();
3509
        TextureLoadTest();
3510
        ViewportTest();
3511
        FindDevice();
3512
        BackBuffer3DCreateSurfaceTest();
3513
        BackBuffer3DAttachmentTest();
3514
        test_get_caps1();
3515
        D3D1_releaseObjects();
3516
    }
3517
}