viewport.c 36.5 KB
Newer Older
1
/* Direct3D Viewport
2
 * Copyright (c) 1998 Lionel ULMER
3
 * Copyright (c) 2006-2007 Stefan DÖSINGER
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This file contains the implementation of Direct3DViewport2.
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20
 */
21

22
#include "config.h"
23
#include "wine/port.h"
24

25
#include "ddraw_private.h"
26

27
WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
28

29 30 31
/*****************************************************************************
 * Helper functions
 *****************************************************************************/
32

33
static void update_clip_space(struct d3d_device *device,
34 35 36 37 38 39 40 41 42 43 44 45
        struct wined3d_vec3 *scale, struct wined3d_vec3 *offset)
{
    D3DMATRIX clip_space =
    {
        scale->x,  0.0f,      0.0f,      0.0f,
        0.0f,      scale->y,  0.0f,      0.0f,
        0.0f,      0.0f,      scale->z,  0.0f,
        offset->x, offset->y, offset->z, 1.0f,
    };
    D3DMATRIX projection;

    multiply_matrix(&projection, &clip_space, &device->legacy_projection);
46
    wined3d_device_set_transform(device->wined3d_device,
47
            WINED3D_TS_PROJECTION, (struct wined3d_matrix *)&projection);
48
    device->legacy_clipspace = clip_space;
49 50
}

51 52 53 54 55 56
/*****************************************************************************
 * viewport_activate
 *
 * activates the viewport using IDirect3DDevice7::SetViewport
 *
 *****************************************************************************/
57
void viewport_activate(struct d3d_viewport *This, BOOL ignore_lights)
58
{
59
    struct wined3d_vec3 scale, offset;
60
    D3DVIEWPORT7 vp;
61

62 63
    if (!ignore_lights)
    {
64
        struct d3d_light *light;
65

66
        /* Activate all the lights associated with this context */
67
        LIST_FOR_EACH_ENTRY(light, &This->light_list, struct d3d_light, entry)
68 69
        {
            light_activate(light);
70
        }
71
    }
72 73

    /* And copy the values in the structure used by the device */
74 75
    if (This->use_vp2)
    {
76
        vp.dwX = This->viewports.vp2.dwX;
77 78 79
        vp.dwY = This->viewports.vp2.dwY;
        vp.dwHeight = This->viewports.vp2.dwHeight;
        vp.dwWidth = This->viewports.vp2.dwWidth;
80 81 82 83 84 85 86 87 88
        vp.dvMinZ = 0.0f;
        vp.dvMaxZ = 1.0f;

        scale.x = 2.0f / This->viewports.vp2.dvClipWidth;
        scale.y = 2.0f / This->viewports.vp2.dvClipHeight;
        scale.z = 1.0f / (This->viewports.vp2.dvMaxZ - This->viewports.vp2.dvMinZ);
        offset.x = -2.0f * This->viewports.vp2.dvClipX / This->viewports.vp2.dvClipWidth - 1.0f;
        offset.y = -2.0f * This->viewports.vp2.dvClipY / This->viewports.vp2.dvClipHeight + 1.0f;
        offset.z = -This->viewports.vp2.dvMinZ / (This->viewports.vp2.dvMaxZ - This->viewports.vp2.dvMinZ);
89 90 91
    }
    else
    {
92
        vp.dwX = This->viewports.vp1.dwX;
93 94 95
        vp.dwY = This->viewports.vp1.dwY;
        vp.dwHeight = This->viewports.vp1.dwHeight;
        vp.dwWidth = This->viewports.vp1.dwWidth;
96 97 98 99 100 101 102 103 104
        vp.dvMinZ = 0.0f;
        vp.dvMaxZ = 1.0f;

        scale.x = 2.0f * This->viewports.vp1.dvScaleX / This->viewports.vp1.dwWidth;
        scale.y = 2.0f * This->viewports.vp1.dvScaleY / This->viewports.vp1.dwHeight;
        scale.z = 1.0f;
        offset.x = 0.0f;
        offset.y = 0.0f;
        offset.z = 0.0f;
105
    }
106

107
    update_clip_space(This->active_device, &scale, &offset);
108
    IDirect3DDevice7_SetViewport(&This->active_device->IDirect3DDevice7_iface, &vp);
109
}
110

111 112 113 114 115 116
/*****************************************************************************
 * _dump_D3DVIEWPORT, _dump_D3DVIEWPORT2
 *
 * Writes viewport information to TRACE
 *
 *****************************************************************************/
117
static void _dump_D3DVIEWPORT(const D3DVIEWPORT *lpvp)
118
{
119
    TRACE("    - dwSize = %d   dwX = %d   dwY = %d\n",
120
            lpvp->dwSize, lpvp->dwX, lpvp->dwY);
121
    TRACE("    - dwWidth = %d   dwHeight = %d\n",
122
            lpvp->dwWidth, lpvp->dwHeight);
123
    TRACE("    - dvScaleX = %f   dvScaleY = %f\n",
124
            lpvp->dvScaleX, lpvp->dvScaleY);
125
    TRACE("    - dvMaxX = %f   dvMaxY = %f\n",
126
            lpvp->dvMaxX, lpvp->dvMaxY);
127
    TRACE("    - dvMinZ = %f   dvMaxZ = %f\n",
128
            lpvp->dvMinZ, lpvp->dvMaxZ);
129 130
}

131
static void _dump_D3DVIEWPORT2(const D3DVIEWPORT2 *lpvp)
132
{
133
    TRACE("    - dwSize = %d   dwX = %d   dwY = %d\n",
134
            lpvp->dwSize, lpvp->dwX, lpvp->dwY);
135
    TRACE("    - dwWidth = %d   dwHeight = %d\n",
136
            lpvp->dwWidth, lpvp->dwHeight);
137
    TRACE("    - dvClipX = %f   dvClipY = %f\n",
138
            lpvp->dvClipX, lpvp->dvClipY);
139
    TRACE("    - dvClipWidth = %f   dvClipHeight = %f\n",
140
            lpvp->dvClipWidth, lpvp->dvClipHeight);
141
    TRACE("    - dvMinZ = %f   dvMaxZ = %f\n",
142
            lpvp->dvMinZ, lpvp->dvMaxZ);
143 144
}

145
static inline struct d3d_viewport *impl_from_IDirect3DViewport3(IDirect3DViewport3 *iface)
146
{
147
    return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
148 149
}

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
/*****************************************************************************
 * IUnknown Methods.
 *****************************************************************************/

/*****************************************************************************
 * IDirect3DViewport3::QueryInterface
 *
 * A normal QueryInterface. Can query all interface versions and the
 * IUnknown interface. The VTables of the different versions
 * are equal
 *
 * Params:
 *  refiid: Interface id queried for
 *  obj: Address to write the interface pointer to
 *
 * Returns:
 *  S_OK on success.
 *  E_NOINTERFACE if the requested interface wasn't found
 *
 *****************************************************************************/
170
static HRESULT WINAPI d3d_viewport_QueryInterface(IDirect3DViewport3 *iface, REFIID riid, void **object)
171
{
172
    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
173

174 175 176 177 178
    if (IsEqualGUID(&IID_IDirect3DViewport3, riid)
            || IsEqualGUID(&IID_IDirect3DViewport2, riid)
            || IsEqualGUID(&IID_IDirect3DViewport, riid)
            || IsEqualGUID(&IID_IUnknown, riid))
    {
179
        IDirect3DViewport3_AddRef(iface);
180 181
        *object = iface;
        return S_OK;
182
    }
183 184 185 186

    WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));

    *object = NULL;
187
    return E_NOINTERFACE;
188 189
}

190 191 192 193 194 195 196 197 198
/*****************************************************************************
 * IDirect3DViewport3::AddRef
 *
 * Increases the refcount.
 *
 * Returns:
 *  The new refcount
 *
 *****************************************************************************/
199
static ULONG WINAPI d3d_viewport_AddRef(IDirect3DViewport3 *iface)
200
{
201 202
    struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
    ULONG ref = InterlockedIncrement(&viewport->ref);
203

204
    TRACE("%p increasing refcount to %u.\n", viewport, ref);
205 206

    return ref;
207 208
}

209 210 211 212 213 214 215 216 217
/*****************************************************************************
 * IDirect3DViewport3::Release
 *
 * Reduces the refcount. If it falls to 0, the interface is released
 *
 * Returns:
 *  The new refcount
 *
 *****************************************************************************/
218
static ULONG WINAPI d3d_viewport_Release(IDirect3DViewport3 *iface)
219
{
220 221
    struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
    ULONG ref = InterlockedDecrement(&viewport->ref);
222

223 224 225 226
    TRACE("%p decreasing refcount to %u.\n", viewport, ref);

    if (!ref)
        HeapFree(GetProcessHeap(), 0, viewport);
227 228

    return ref;
229 230
}

231 232 233
/*****************************************************************************
 * IDirect3DViewport Methods.
 *****************************************************************************/
234

235 236 237 238 239 240 241 242 243 244 245 246
/*****************************************************************************
 * IDirect3DViewport3::Initialize
 *
 * No-op initialization.
 *
 * Params:
 *  Direct3D: The direct3D device this viewport is assigned to
 *
 * Returns:
 *  DDERR_ALREADYINITIALIZED
 *
 *****************************************************************************/
247
static HRESULT WINAPI d3d_viewport_Initialize(IDirect3DViewport3 *iface, IDirect3D *d3d)
248
{
249
    TRACE("iface %p, d3d %p.\n", iface, d3d);
250

251
    return DDERR_ALREADYINITIALIZED;
252 253
}

254 255 256 257 258 259 260 261 262 263 264 265 266
/*****************************************************************************
 * IDirect3DViewport3::GetViewport
 *
 * Returns the viewport data assigned to this viewport interface
 *
 * Params:
 *  Data: Address to store the data
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Data is NULL
 *
 *****************************************************************************/
267
static HRESULT WINAPI d3d_viewport_GetViewport(IDirect3DViewport3 *iface, D3DVIEWPORT *lpData)
268
{
269
    struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
270
    DWORD dwSize;
271 272

    TRACE("iface %p, data %p.\n", iface, lpData);
273

274 275
    wined3d_mutex_lock();

276 277
    dwSize = lpData->dwSize;
    memset(lpData, 0, dwSize);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    if (!This->use_vp2)
        memcpy(lpData, &(This->viewports.vp1), dwSize);
    else {
        D3DVIEWPORT vp1;
        vp1.dwSize = sizeof(vp1);
        vp1.dwX = This->viewports.vp2.dwX;
        vp1.dwY = This->viewports.vp2.dwY;
        vp1.dwWidth = This->viewports.vp2.dwWidth;
        vp1.dwHeight = This->viewports.vp2.dwHeight;
        vp1.dvMaxX = 0.0;
        vp1.dvMaxY = 0.0;
        vp1.dvScaleX = 0.0;
        vp1.dvScaleY = 0.0;
        vp1.dvMinZ = This->viewports.vp2.dvMinZ;
        vp1.dvMaxZ = This->viewports.vp2.dvMaxZ;
        memcpy(lpData, &vp1, dwSize);
    }
295

296 297
    if (TRACE_ON(ddraw))
    {
298
        TRACE("  returning D3DVIEWPORT :\n");
299
        _dump_D3DVIEWPORT(lpData);
300
    }
301 302

    wined3d_mutex_unlock();
303

304
    return DD_OK;
305 306
}

307 308 309 310 311 312 313 314 315
/*****************************************************************************
 * IDirect3DViewport3::SetViewport
 *
 * Sets the viewport information for this interface
 *
 * Params:
 *  lpData: Viewport to set
 *
 * Returns:
316
 *  D3D_OK on success
317 318 319
 *  DDERR_INVALIDPARAMS if Data is NULL
 *
 *****************************************************************************/
320
static HRESULT WINAPI d3d_viewport_SetViewport(IDirect3DViewport3 *iface, D3DVIEWPORT *lpData)
321
{
322
    struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
323
    IDirect3DViewport3 *current_viewport;
324

325 326 327 328
    TRACE("iface %p, data %p.\n", iface, lpData);

    if (TRACE_ON(ddraw))
    {
329
        TRACE("  getting D3DVIEWPORT :\n");
330
        _dump_D3DVIEWPORT(lpData);
331
    }
332

333 334
    wined3d_mutex_lock();

335 336 337
    This->use_vp2 = 0;
    memset(&(This->viewports.vp1), 0, sizeof(This->viewports.vp1));
    memcpy(&(This->viewports.vp1), lpData, lpData->dwSize);
338

339
    /* Tests on two games show that these values are never used properly so override
340 341 342 343
       them with proper ones :-)
    */
    This->viewports.vp1.dvMinZ = 0.0;
    This->viewports.vp1.dvMaxZ = 1.0;
344

345 346
    if (This->active_device)
    {
347
        IDirect3DDevice3 *d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
348
        if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
349
        {
350
            if (current_viewport == iface) viewport_activate(This, FALSE);
351 352
            IDirect3DViewport3_Release(current_viewport);
        }
353
    }
354 355

    wined3d_mutex_unlock();
356

357 358
    return DD_OK;
}
359

360 361 362 363 364
/*****************************************************************************
 * IDirect3DViewport3::TransformVertices
 *
 * Transforms vertices by the transformation matrix.
 *
365 366
 * This function is pretty similar to IDirect3DVertexBuffer7::ProcessVertices,
 * so it's tempting to forward it to there. However, there are some
367
 * tiny differences. First, the lpOffscreen flag that is reported back,
368
 * then there is the homogeneous vertex that is generated. Also there's a lack
369
 * of FVFs, but still a custom stride. Last, the d3d1 - d3d3 viewport has some
370
 * settings (scale) that d3d7 and wined3d do not have. All in all wrapping to
371 372 373
 * ProcessVertices doesn't pay of in terms of wrapper code needed and code
 * reused.
 *
374 375 376 377
 * Params:
 *  dwVertexCount: The number of vertices to be transformed
 *  lpData: Pointer to the vertex data
 *  dwFlags: D3DTRANSFORM_CLIPPED or D3DTRANSFORM_UNCLIPPED
378 379
 *  lpOffScreen: Set to the clipping plane clipping the vertex, if only one
 *               vertex is transformed and clipping is on. 0 otherwise
380 381
 *
 * Returns:
382 383 384
 *  D3D_OK on success
 *  D3DERR_VIEWPORTHASNODEVICE if the viewport is not assigned to a device
 *  DDERR_INVALIDPARAMS if no clipping flag is specified
385 386
 *
 *****************************************************************************/
387 388
static HRESULT WINAPI d3d_viewport_TransformVertices(IDirect3DViewport3 *iface,
        DWORD dwVertexCount, D3DTRANSFORMDATA *lpData, DWORD dwFlags, DWORD *lpOffScreen)
389
{
390 391
    struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
    D3DVIEWPORT vp = viewport->viewports.vp1;
392
    D3DMATRIX view_mat, world_mat, mat;
393 394 395 396 397
    float *in;
    float *out;
    float x, y, z, w;
    unsigned int i;
    D3DHVERTEX *outH;
398 399 400

    TRACE("iface %p, vertex_count %u, vertex_data %p, flags %#x, clip_plane %p.\n",
            iface, dwVertexCount, lpData, dwFlags, lpOffScreen);
401 402 403

    /* Tests on windows show that Windows crashes when this occurs,
     * so don't return the (intuitive) return value
404
    if (!viewport->active_device)
405 406 407 408 409 410 411 412 413 414 415 416 417
    {
        WARN("No device active, returning D3DERR_VIEWPORTHASNODEVICE\n");
        return D3DERR_VIEWPORTHASNODEVICE;
    }
     */

    if(!(dwFlags & (D3DTRANSFORM_UNCLIPPED | D3DTRANSFORM_CLIPPED)))
    {
        WARN("No clipping flag passed, returning DDERR_INVALIDPARAMS\n");
        return DDERR_INVALIDPARAMS;
    }


418
    wined3d_mutex_lock();
419
    wined3d_device_get_transform(viewport->active_device->wined3d_device,
420
            D3DTRANSFORMSTATE_VIEW, (struct wined3d_matrix *)&view_mat);
421
    wined3d_device_get_transform(viewport->active_device->wined3d_device,
422
            WINED3D_TS_WORLD_MATRIX(0), (struct wined3d_matrix *)&world_mat);
423
    multiply_matrix(&mat, &view_mat, &world_mat);
424
    multiply_matrix(&mat, &viewport->active_device->legacy_projection, &mat);
425

426 427
    in = lpData->lpIn;
    out = lpData->lpOut;
428 429 430
    outH = lpData->lpHOut;
    for(i = 0; i < dwVertexCount; i++)
    {
431 432 433 434
        x = (in[0] * mat._11) + (in[1] * mat._21) + (in[2] * mat._31) + mat._41;
        y = (in[0] * mat._12) + (in[1] * mat._22) + (in[2] * mat._32) + mat._42;
        z = (in[0] * mat._13) + (in[1] * mat._23) + (in[2] * mat._33) + mat._43;
        w = (in[0] * mat._14) + (in[1] * mat._24) + (in[2] * mat._34) + mat._44;
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

        if(dwFlags & D3DTRANSFORM_CLIPPED)
        {
            /* If clipping is enabled, Windows assumes that outH is
             * a valid pointer
             */
            outH[i].u1.hx = x; outH[i].u2.hy = y; outH[i].u3.hz = z;

            outH[i].dwFlags = 0;
            if(x * vp.dvScaleX > ((float) vp.dwWidth * 0.5))
                outH[i].dwFlags |= D3DCLIP_RIGHT;
            if(x * vp.dvScaleX <= -((float) vp.dwWidth) * 0.5)
                outH[i].dwFlags |= D3DCLIP_LEFT;
            if(y * vp.dvScaleY > ((float) vp.dwHeight * 0.5))
                outH[i].dwFlags |= D3DCLIP_TOP;
            if(y * vp.dvScaleY <= -((float) vp.dwHeight) * 0.5)
                outH[i].dwFlags |= D3DCLIP_BOTTOM;
            if(z < 0.0)
                outH[i].dwFlags |= D3DCLIP_FRONT;
            if(z > 1.0)
                outH[i].dwFlags |= D3DCLIP_BACK;

            if(outH[i].dwFlags)
            {
                /* Looks like native just drops the vertex, leaves whatever data
                 * it has in the output buffer and goes on with the next vertex.
                 * The exact scheme hasn't been figured out yet, but windows
462
                 * definitely writes something there.
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
                 */
                out[0] = x;
                out[1] = y;
                out[2] = z;
                out[3] = w;
                in = (float *) ((char *) in + lpData->dwInSize);
                out = (float *) ((char *) out + lpData->dwOutSize);
                continue;
            }
        }

        w = 1 / w;
        x *= w; y *= w; z *= w;

        out[0] = vp.dwWidth / 2 + vp.dwX + x * vp.dvScaleX;
        out[1] = vp.dwHeight / 2 + vp.dwY - y * vp.dvScaleY;
        out[2] = z;
        out[3] = w;
        in = (float *) ((char *) in + lpData->dwInSize);
        out = (float *) ((char *) out + lpData->dwOutSize);
    }

    /* According to the d3d test, the offscreen flag is set only
     * if exactly one vertex is transformed. Its not documented,
     * but the test shows that the lpOffscreen flag is set to the
     * flag combination of clipping planes that clips the vertex.
     *
     * If clipping is requested, Windows assumes that the offscreen
     * param is a valid pointer.
     */
    if(dwVertexCount == 1 && dwFlags & D3DTRANSFORM_CLIPPED)
    {
        *lpOffScreen = outH[0].dwFlags;
    }
    else if(*lpOffScreen)
    {
        *lpOffScreen = 0;
    }
501
    wined3d_mutex_unlock();
502 503

    TRACE("All done\n");
504
    return DD_OK;
505 506
}

507 508 509 510 511 512 513 514 515 516 517 518
/*****************************************************************************
 * IDirect3DViewport3::LightElements
 *
 * The DirectX 5.0 sdk says that it's not implemented
 *
 * Params:
 *  ?
 *
 * Returns:
 *  DDERR_UNSUPPORTED
 *
 *****************************************************************************/
519 520
static HRESULT WINAPI d3d_viewport_LightElements(IDirect3DViewport3 *iface,
        DWORD element_count, D3DLIGHTDATA *data)
521
{
522
    TRACE("iface %p, element_count %u, data %p.\n", iface, element_count, data);
523

524
    return DDERR_UNSUPPORTED;
525
}
526

527
static HRESULT WINAPI d3d_viewport_SetBackground(IDirect3DViewport3 *iface, D3DMATERIALHANDLE material)
528
{
529
    struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
530
    struct d3d_material *m;
531

532
    TRACE("iface %p, material %#x.\n", iface, material);
533

534
    wined3d_mutex_lock();
535

536
    if (!(m = ddraw_get_object(&viewport->ddraw->d3ddevice->handle_table, material - 1, DDRAW_HANDLE_MATERIAL)))
537
    {
538
        WARN("Invalid material handle %#x.\n", material);
539
        wined3d_mutex_unlock();
540 541 542
        return DDERR_INVALIDPARAMS;
    }

543 544 545
    TRACE("Setting background color : %.8e %.8e %.8e %.8e.\n",
            m->mat.u.diffuse.u1.r, m->mat.u.diffuse.u2.g,
            m->mat.u.diffuse.u3.b, m->mat.u.diffuse.u4.a);
546
    viewport->background = m;
547

548 549
    wined3d_mutex_unlock();

550
    return D3D_OK;
551
}
552

553 554 555 556 557 558 559 560 561 562
/*****************************************************************************
 * IDirect3DViewport3::GetBackground
 *
 * Returns the material handle assigned to the background of the viewport
 *
 * Params:
 *  lphMat: Address to store the handle
 *  lpValid: is set to FALSE if no background is set, TRUE if one is set
 *
 * Returns:
563
 *  D3D_OK
564 565
 *
 *****************************************************************************/
566 567
static HRESULT WINAPI d3d_viewport_GetBackground(IDirect3DViewport3 *iface,
        D3DMATERIALHANDLE *material, BOOL *valid)
568
{
569
    struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
570

571
    TRACE("iface %p, material %p, valid %p.\n", iface, material, valid);
572

573
    wined3d_mutex_lock();
574 575 576 577
    if (valid)
        *valid = !!viewport->background;
    if (material)
        *material = viewport->background ? viewport->background->Handle : 0;
578
    wined3d_mutex_unlock();
579

580
    return D3D_OK;
581
}
582

583 584 585 586 587 588 589 590 591 592 593 594
/*****************************************************************************
 * IDirect3DViewport3::SetBackgroundDepth
 *
 * Sets a surface that represents the background depth. It's contents are
 * used to set the depth buffer in IDirect3DViewport3::Clear
 *
 * Params:
 *  lpDDSurface: Surface to set
 *
 * Returns: D3D_OK, because it's a stub
 *
 *****************************************************************************/
595
static HRESULT WINAPI d3d_viewport_SetBackgroundDepth(IDirect3DViewport3 *iface, IDirectDrawSurface *surface)
596
{
597
    FIXME("iface %p, surface %p stub!\n", iface, surface);
598

599
    return D3D_OK;
600
}
601

602 603 604 605 606 607 608
/*****************************************************************************
 * IDirect3DViewport3::GetBackgroundDepth
 *
 * Returns the surface that represents the depth field
 *
 * Params:
 *  lplpDDSurface: Address to store the interface pointer
Austin English's avatar
Austin English committed
609
 *  lpValid: Set to TRUE if a depth is assigned, FALSE otherwise
610 611 612 613 614 615
 *
 * Returns:
 *  D3D_OK, because it's a stub
 *  (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
 *
 *****************************************************************************/
616 617
static HRESULT WINAPI d3d_viewport_GetBackgroundDepth(IDirect3DViewport3 *iface,
        IDirectDrawSurface **surface, BOOL *valid)
618
{
619
    FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
620

621
    return DD_OK;
622 623
}

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
/*****************************************************************************
 * IDirect3DViewport3::Clear
 *
 * Clears the render target and / or the z buffer
 *
 * Params:
 *  dwCount: The amount of rectangles to clear. If 0, the whole buffer is
 *           cleared
 *  lpRects: Pointer to the array of rectangles. If NULL, Count must be 0
 *  dwFlags: D3DCLEAR_ZBUFFER and / or D3DCLEAR_TARGET
 *
 * Returns:
 *  D3D_OK on success
 *  D3DERR_VIEWPORTHASNODEVICE if there's no active device
 *  The return value of IDirect3DDevice7::Clear
 *
 *****************************************************************************/
641 642
static HRESULT WINAPI d3d_viewport_Clear(IDirect3DViewport3 *iface,
        DWORD rect_count, D3DRECT *rects, DWORD flags)
643
{
644
    struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
645
    DWORD color = 0x00000000;
646
    HRESULT hr;
647
    IDirect3DViewport3 *current_viewport;
648
    IDirect3DDevice3 *d3d_device3;
649

650
    TRACE("iface %p, rect_count %u, rects %p, flags %#x.\n", iface, rect_count, rects, flags);
651

652 653 654 655 656 657
    if (!rects || !rect_count)
    {
        WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count, rects);
        return D3D_OK;
    }

658
    if (This->active_device == NULL) {
659
        ERR(" Trying to clear a viewport not attached to a device!\n");
660
        return D3DERR_VIEWPORTHASNODEVICE;
661
    }
662
    d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
663

664 665
    wined3d_mutex_lock();

666 667
    if (flags & D3DCLEAR_TARGET)
    {
668 669
        if (!This->background)
            WARN("No background material set.\n");
670
        else
671 672 673 674
            color = D3DRGBA(This->background->mat.u.diffuse.u1.r,
                    This->background->mat.u.diffuse.u2.g,
                    This->background->mat.u.diffuse.u3.b,
                    This->background->mat.u.diffuse.u4.a);
675
    }
676

677 678
    /* Need to temporarily activate viewport to clear it. Previously active one will be restored
        afterwards. */
679
    viewport_activate(This, TRUE);
680

681 682
    hr = IDirect3DDevice7_Clear(&This->active_device->IDirect3DDevice7_iface, rect_count, rects,
            flags & (D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET), color, 1.0, 0x00000000);
683

684
    if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
685 686
    {
        struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
687
        viewport_activate(vp, TRUE);
688 689 690
        IDirect3DViewport3_Release(current_viewport);
    }

691 692
    wined3d_mutex_unlock();

693
    return hr;
694
}
695

696 697 698 699 700 701 702 703 704 705 706 707 708 709
/*****************************************************************************
 * IDirect3DViewport3::AddLight
 *
 * Adds an light to the viewport
 *
 * Params:
 *  lpDirect3DLight: Interface of the light to add
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Direct3DLight is NULL
 *  DDERR_INVALIDPARAMS if there are 8 lights or more
 *
 *****************************************************************************/
710
static HRESULT WINAPI d3d_viewport_AddLight(IDirect3DViewport3 *iface, IDirect3DLight *lpDirect3DLight)
711
{
712
    struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
713
    struct d3d_light *light_impl = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
714 715
    DWORD i = 0;
    DWORD map = This->map_lights;
716

717
    TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
718

719 720
    wined3d_mutex_lock();

721
    if (This->num_lights >= 8)
722
    {
723
        wined3d_mutex_unlock();
724
        return DDERR_INVALIDPARAMS;
725
    }
726 727

    /* Find a light number and update both light and viewports objects accordingly */
728 729 730 731
    while (map & 1)
    {
        map >>= 1;
        ++i;
732
    }
733
    light_impl->dwLightIndex = i;
734 735 736
    This->num_lights++;
    This->map_lights |= 1<<i;

737
    /* Add the light in the 'linked' chain */
738
    list_add_head(&This->light_list, &light_impl->entry);
739
    IDirect3DLight_AddRef(lpDirect3DLight);
740

741
    /* Attach the light to the viewport */
742
    light_impl->active_viewport = This;
743

744
    /* If active, activate the light */
745
    if (This->active_device)
746
        light_activate(light_impl);
747

748 749
    wined3d_mutex_unlock();

750
    return D3D_OK;
751 752
}

753 754 755 756 757 758 759 760 761 762 763 764 765
/*****************************************************************************
 * IDirect3DViewport3::DeleteLight
 *
 * Deletes a light from the viewports' light list
 *
 * Params:
 *  lpDirect3DLight: Light to delete
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if the light wasn't found
 *
 *****************************************************************************/
766
static HRESULT WINAPI d3d_viewport_DeleteLight(IDirect3DViewport3 *iface, IDirect3DLight *lpDirect3DLight)
767
{
768
    struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
769
    struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
770

771
    TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
772

773
    wined3d_mutex_lock();
774

775
    if (l->active_viewport != viewport)
776 777
    {
        WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
778
        wined3d_mutex_unlock();
779
        return DDERR_INVALIDPARAMS;
780
    }
781 782 783 784 785

    light_deactivate(l);
    list_remove(&l->entry);
    l->active_viewport = NULL;
    IDirect3DLight_Release(lpDirect3DLight);
786 787
    --viewport->num_lights;
    viewport->map_lights &= ~(1 << l->dwLightIndex);
788

789
    wined3d_mutex_unlock();
790

791
    return D3D_OK;
792
}
793

794 795 796 797 798 799 800 801 802 803 804 805 806
/*****************************************************************************
 * IDirect3DViewport::NextLight
 *
 * Enumerates the lights associated with the viewport
 *
 * Params:
 *  lpDirect3DLight: Light to start with
 *  lplpDirect3DLight: Address to store the successor to
 *
 * Returns:
 *  D3D_OK, because it's a stub
 *
 *****************************************************************************/
807 808
static HRESULT WINAPI d3d_viewport_NextLight(IDirect3DViewport3 *iface,
        IDirect3DLight *lpDirect3DLight, IDirect3DLight **lplpDirect3DLight, DWORD flags)
809
{
810
    struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
811
    struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
812 813
    struct list *entry;
    HRESULT hr;
814

815
    TRACE("iface %p, light %p, next_light %p, flags %#x.\n",
816
            iface, lpDirect3DLight, lplpDirect3DLight, flags);
817 818 819 820

    if (!lplpDirect3DLight)
        return DDERR_INVALIDPARAMS;

821
    wined3d_mutex_lock();
822

823
    switch (flags)
824
    {
825
        case D3DNEXT_NEXT:
826
            if (!l || l->active_viewport != viewport)
827 828 829 830
            {
                if (l)
                    WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
                entry = NULL;
831
            }
832
            else
833
                entry = list_next(&viewport->light_list, &l->entry);
834
            break;
835

836
        case D3DNEXT_HEAD:
837
            entry = list_head(&viewport->light_list);
838
            break;
839

840
        case D3DNEXT_TAIL:
841
            entry = list_tail(&viewport->light_list);
842
            break;
843

844
        default:
845
            entry = NULL;
846
            WARN("Invalid flags %#x.\n", flags);
847 848 849
            break;
    }

850 851
    if (entry)
    {
852
        *lplpDirect3DLight = (IDirect3DLight *)LIST_ENTRY(entry, struct d3d_light, entry);
853
        IDirect3DLight_AddRef(*lplpDirect3DLight);
854 855 856 857 858 859 860
        hr = D3D_OK;
    }
    else
    {
        *lplpDirect3DLight = NULL;
        hr = DDERR_INVALIDPARAMS;
    }
861

862
    wined3d_mutex_unlock();
863

864
    return hr;
865 866
}

867 868 869 870 871 872 873 874
/*****************************************************************************
 * IDirect3DViewport2 Methods.
 *****************************************************************************/

/*****************************************************************************
 * IDirect3DViewport3::GetViewport2
 *
 * Returns the currently set viewport in a D3DVIEWPORT2 structure.
875
 * Similar to IDirect3DViewport3::GetViewport
876 877 878 879 880 881 882 883 884 885 886
 *
 * Params:
 *  lpData: Pointer to the structure to fill
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if the viewport was set with
 *                      IDirect3DViewport3::SetViewport
 *  DDERR_INVALIDPARAMS if Data is NULL
 *
 *****************************************************************************/
887
static HRESULT WINAPI d3d_viewport_GetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *lpData)
888
{
889
    struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
890
    DWORD dwSize;
891 892

    TRACE("iface %p, data %p.\n", iface, lpData);
893

894
    wined3d_mutex_lock();
895 896
    dwSize = lpData->dwSize;
    memset(lpData, 0, dwSize);
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
    if (This->use_vp2)
        memcpy(lpData, &(This->viewports.vp2), dwSize);
    else {
        D3DVIEWPORT2 vp2;
        vp2.dwSize = sizeof(vp2);
        vp2.dwX = This->viewports.vp1.dwX;
        vp2.dwY = This->viewports.vp1.dwY;
        vp2.dwWidth = This->viewports.vp1.dwWidth;
        vp2.dwHeight = This->viewports.vp1.dwHeight;
        vp2.dvClipX = 0.0;
        vp2.dvClipY = 0.0;
        vp2.dvClipWidth = 0.0;
        vp2.dvClipHeight = 0.0;
        vp2.dvMinZ = This->viewports.vp1.dvMinZ;
        vp2.dvMaxZ = This->viewports.vp1.dvMaxZ;
        memcpy(lpData, &vp2, dwSize);
    }
914

915 916
    if (TRACE_ON(ddraw))
    {
917
        TRACE("  returning D3DVIEWPORT2 :\n");
918
        _dump_D3DVIEWPORT2(lpData);
919
    }
920

921 922
    wined3d_mutex_unlock();

923
    return D3D_OK;
924 925
}

926 927 928 929 930 931 932 933 934 935 936 937
/*****************************************************************************
 * IDirect3DViewport3::SetViewport2
 *
 * Sets the viewport from a D3DVIEWPORT2 structure
 *
 * Params:
 *  lpData: Viewport to set
 *
 * Returns:
 *  D3D_OK on success
 *
 *****************************************************************************/
938
static HRESULT WINAPI d3d_viewport_SetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *lpData)
939
{
940
    struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
941
    IDirect3DViewport3 *current_viewport;
942

943 944 945 946
    TRACE("iface %p, data %p.\n", iface, lpData);

    if (TRACE_ON(ddraw))
    {
947
        TRACE("  getting D3DVIEWPORT2 :\n");
948
        _dump_D3DVIEWPORT2(lpData);
949 950
    }

951 952
    wined3d_mutex_lock();

953 954 955
    This->use_vp2 = 1;
    memset(&(This->viewports.vp2), 0, sizeof(This->viewports.vp2));
    memcpy(&(This->viewports.vp2), lpData, lpData->dwSize);
956

957 958
    if (This->active_device)
    {
959
        IDirect3DDevice3 *d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
960
        if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
961
        {
962
            if (current_viewport == iface) viewport_activate(This, FALSE);
963 964
            IDirect3DViewport3_Release(current_viewport);
        }
965
    }
966 967

    wined3d_mutex_unlock();
968

969
    return D3D_OK;
970 971
}

972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
/*****************************************************************************
 * IDirect3DViewport3 Methods.
 *****************************************************************************/

/*****************************************************************************
 * IDirect3DViewport3::SetBackgroundDepth2
 *
 * Sets a IDirectDrawSurface4 surface as the background depth surface
 *
 * Params:
 *  lpDDS: Surface to set
 *
 * Returns:
 *  D3D_OK, because it's stub
 *
 *****************************************************************************/
988 989
static HRESULT WINAPI d3d_viewport_SetBackgroundDepth2(IDirect3DViewport3 *iface,
        IDirectDrawSurface4 *surface)
990
{
991
    FIXME("iface %p, surface %p stub!\n", iface, surface);
992

993
    return D3D_OK;
994 995
}

996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
/*****************************************************************************
 * IDirect3DViewport3::GetBackgroundDepth2
 *
 * Returns the IDirect3DSurface4 interface to the background depth surface
 *
 * Params:
 *  lplpDDS: Address to store the interface pointer at
 *  lpValid: Set to true if a surface is assigned
 *
 * Returns:
 *  D3D_OK because it's a stub
 *
 *****************************************************************************/
1009 1010
static HRESULT WINAPI d3d_viewport_GetBackgroundDepth2(IDirect3DViewport3 *iface,
        IDirectDrawSurface4 **surface, BOOL *valid)
1011
{
1012
    FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
1013

1014
    return D3D_OK;
1015
}
1016

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
/*****************************************************************************
 * IDirect3DViewport3::Clear2
 *
 * Another clearing method
 *
 * Params:
 *  Count: Number of rectangles to clear
 *  Rects: Rectangle array to clear
 *  Flags: Some flags :)
 *  Color: Color to fill the render target with
 *  Z: Value to fill the depth buffer with
 *  Stencil: Value to fill the stencil bits with
 *
 * Returns:
 *
 *****************************************************************************/
1033 1034
static HRESULT WINAPI d3d_viewport_Clear2(IDirect3DViewport3 *iface, DWORD rect_count,
        D3DRECT *rects, DWORD flags, DWORD color, D3DVALUE depth, DWORD stencil)
1035
{
1036
    struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
1037
    HRESULT hr;
1038
    IDirect3DViewport3 *current_viewport;
1039
    IDirect3DDevice3 *d3d_device3;
1040 1041

    TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, depth %.8e, stencil %u.\n",
1042
            iface, rect_count, rects, flags, color, depth, stencil);
1043

1044 1045 1046 1047 1048 1049
    if (!rects || !rect_count)
    {
        WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count, rects);
        return D3D_OK;
    }

1050 1051
    wined3d_mutex_lock();

1052 1053 1054
    if (!viewport->active_device)
    {
        WARN("Trying to clear a viewport not attached to a device.\n");
1055
        wined3d_mutex_unlock();
1056
        return D3DERR_VIEWPORTHASNODEVICE;
1057
    }
1058
    d3d_device3 = &viewport->active_device->IDirect3DDevice3_iface;
1059 1060
    /* Need to temporarily activate viewport to clear it. Previously active
     * one will be restored afterwards. */
1061
    viewport_activate(viewport, TRUE);
1062

1063 1064
    hr = IDirect3DDevice7_Clear(&viewport->active_device->IDirect3DDevice7_iface,
            rect_count, rects, flags, color, depth, stencil);
1065
    if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
1066 1067
    {
        struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
1068
        viewport_activate(vp, TRUE);
1069 1070
        IDirect3DViewport3_Release(current_viewport);
    }
1071 1072 1073

    wined3d_mutex_unlock();

1074
    return hr;
1075 1076
}

1077 1078 1079
/*****************************************************************************
 * The VTable
 *****************************************************************************/
1080

1081
static const struct IDirect3DViewport3Vtbl d3d_viewport_vtbl =
1082
{
1083
    /*** IUnknown Methods ***/
1084 1085 1086
    d3d_viewport_QueryInterface,
    d3d_viewport_AddRef,
    d3d_viewport_Release,
1087
    /*** IDirect3DViewport Methods */
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
    d3d_viewport_Initialize,
    d3d_viewport_GetViewport,
    d3d_viewport_SetViewport,
    d3d_viewport_TransformVertices,
    d3d_viewport_LightElements,
    d3d_viewport_SetBackground,
    d3d_viewport_GetBackground,
    d3d_viewport_SetBackgroundDepth,
    d3d_viewport_GetBackgroundDepth,
    d3d_viewport_Clear,
    d3d_viewport_AddLight,
    d3d_viewport_DeleteLight,
    d3d_viewport_NextLight,
1101
    /*** IDirect3DViewport2 Methods ***/
1102 1103
    d3d_viewport_GetViewport2,
    d3d_viewport_SetViewport2,
1104
    /*** IDirect3DViewport3 Methods ***/
1105 1106 1107
    d3d_viewport_SetBackgroundDepth2,
    d3d_viewport_GetBackgroundDepth2,
    d3d_viewport_Clear2,
1108
};
1109

1110
struct d3d_viewport *unsafe_impl_from_IDirect3DViewport3(IDirect3DViewport3 *iface)
1111 1112 1113
{
    if (!iface) return NULL;
    assert(iface->lpVtbl == &d3d_viewport_vtbl);
1114
    return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1115 1116
}

1117
struct d3d_viewport *unsafe_impl_from_IDirect3DViewport2(IDirect3DViewport2 *iface)
1118 1119 1120 1121
{
    /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
    if (!iface) return NULL;
    assert(iface->lpVtbl == (IDirect3DViewport2Vtbl *)&d3d_viewport_vtbl);
1122
    return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1123 1124
}

1125
struct d3d_viewport *unsafe_impl_from_IDirect3DViewport(IDirect3DViewport *iface)
1126 1127 1128 1129
{
    /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
    if (!iface) return NULL;
    assert(iface->lpVtbl == (IDirect3DViewportVtbl *)&d3d_viewport_vtbl);
1130
    return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1131 1132
}

1133
void d3d_viewport_init(struct d3d_viewport *viewport, struct ddraw *ddraw)
1134
{
1135
    viewport->IDirect3DViewport3_iface.lpVtbl = &d3d_viewport_vtbl;
1136 1137 1138
    viewport->ref = 1;
    viewport->ddraw = ddraw;
    viewport->use_vp2 = 0xff;
1139
    list_init(&viewport->light_list);
1140
}