winhttp: Don't refill buffer after receiving server response.
[wine.git] / dlls / ddraw / viewport.c
blob83c88f48691b85ed8ad2cd029edb9bb1c80c39f6
1 /* Direct3D Viewport
2 * Copyright (c) 1998 Lionel ULMER
3 * Copyright (c) 2006-2007 Stefan DÖSINGER
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "ddraw_private.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
24 /*****************************************************************************
25 * Helper functions
26 *****************************************************************************/
28 static void update_clip_space(struct d3d_device *device,
29 struct wined3d_vec3 *scale, struct wined3d_vec3 *offset)
31 struct wined3d_matrix clip_space =
33 scale->x, 0.0f, 0.0f, 0.0f,
34 0.0f, scale->y, 0.0f, 0.0f,
35 0.0f, 0.0f, scale->z, 0.0f,
36 offset->x, offset->y, offset->z, 1.0f,
38 struct wined3d_matrix projection;
40 multiply_matrix(&projection, &clip_space, &device->legacy_projection);
41 wined3d_stateblock_set_transform(device->state, WINED3D_TS_PROJECTION, &projection);
42 device->legacy_clipspace = clip_space;
45 /*****************************************************************************
46 * viewport_activate
48 * activates the viewport using IDirect3DDevice7::SetViewport
50 *****************************************************************************/
51 void viewport_activate(struct d3d_viewport *This, BOOL ignore_lights)
53 struct wined3d_vec3 scale, offset;
54 D3DVIEWPORT7 vp;
56 if (!ignore_lights)
58 struct d3d_light *light;
60 /* Activate all the lights associated with this context */
61 LIST_FOR_EACH_ENTRY(light, &This->light_list, struct d3d_light, entry)
63 light_activate(light);
67 if (This->version == DDRAW_VIEWPORT_VERSION_NONE)
69 TRACE("Viewport data was not set.\n");
70 return;
73 /* And copy the values in the structure used by the device */
74 if (This->version == DDRAW_VIEWPORT_VERSION_2)
76 vp.dwX = This->viewports.vp2.dwX;
77 vp.dwY = This->viewports.vp2.dwY;
78 vp.dwHeight = This->viewports.vp2.dwHeight;
79 vp.dwWidth = This->viewports.vp2.dwWidth;
80 vp.dvMinZ = 0.0f;
81 vp.dvMaxZ = 1.0f;
83 scale.x = 2.0f / This->viewports.vp2.dvClipWidth;
84 scale.y = 2.0f / This->viewports.vp2.dvClipHeight;
85 scale.z = 1.0f / (This->viewports.vp2.dvMaxZ - This->viewports.vp2.dvMinZ);
86 offset.x = -2.0f * This->viewports.vp2.dvClipX / This->viewports.vp2.dvClipWidth - 1.0f;
87 offset.y = -2.0f * This->viewports.vp2.dvClipY / This->viewports.vp2.dvClipHeight + 1.0f;
88 offset.z = -This->viewports.vp2.dvMinZ / (This->viewports.vp2.dvMaxZ - This->viewports.vp2.dvMinZ);
90 else
92 vp.dwX = This->viewports.vp1.dwX;
93 vp.dwY = This->viewports.vp1.dwY;
94 vp.dwHeight = This->viewports.vp1.dwHeight;
95 vp.dwWidth = This->viewports.vp1.dwWidth;
96 vp.dvMinZ = 0.0f;
97 vp.dvMaxZ = 1.0f;
99 scale.x = 2.0f * This->viewports.vp1.dvScaleX / This->viewports.vp1.dwWidth;
100 scale.y = 2.0f * This->viewports.vp1.dvScaleY / This->viewports.vp1.dwHeight;
101 scale.z = 1.0f;
102 offset.x = 0.0f;
103 offset.y = 0.0f;
104 offset.z = 0.0f;
107 update_clip_space(This->active_device, &scale, &offset);
108 IDirect3DDevice7_SetViewport(&This->active_device->IDirect3DDevice7_iface, &vp);
111 void viewport_deactivate(struct d3d_viewport *viewport)
113 struct d3d_light *light;
115 LIST_FOR_EACH_ENTRY(light, &viewport->light_list, struct d3d_light, entry)
117 light_deactivate(light);
121 void viewport_alloc_active_light_index(struct d3d_light *light)
123 struct d3d_viewport *vp = light->active_viewport;
124 unsigned int i, map;
126 TRACE("vp %p, light %p, index %lu, active_lights_count %lu.\n",
127 vp, light, light->active_light_index, vp->active_lights_count);
129 if (light->active_light_index)
130 return;
132 if (vp->active_lights_count >= DDRAW_MAX_ACTIVE_LIGHTS)
134 struct d3d_light *l;
136 LIST_FOR_EACH_ENTRY(l, &vp->light_list, struct d3d_light, entry)
138 if (l->active_light_index)
140 WARN("Too many active lights, viewport %p, light %p, deactivating %p.\n", vp, light, l);
141 light_deactivate(l);
143 /* Recycle active lights in a FIFO way. */
144 list_remove(&light->entry);
145 list_add_tail(&vp->light_list, &light->entry);
146 break;
151 map = ~vp->map_lights;
152 assert(vp->active_lights_count < DDRAW_MAX_ACTIVE_LIGHTS && map);
153 i = wined3d_bit_scan(&map);
154 light->active_light_index = i + 1;
155 ++vp->active_lights_count;
156 vp->map_lights |= 1u << i;
159 void viewport_free_active_light_index(struct d3d_light *light)
161 struct d3d_viewport *vp = light->active_viewport;
163 TRACE("vp %p, light %p, index %lu, active_lights_count %lu, map_lights %#lx.\n",
164 vp, light, light->active_light_index, vp->active_lights_count, vp->map_lights);
166 if (!light->active_light_index)
167 return;
169 assert(vp->map_lights & (1u << (light->active_light_index - 1)));
171 --vp->active_lights_count;
172 vp->map_lights &= ~(1u << (light->active_light_index - 1));
173 light->active_light_index = 0;
176 /*****************************************************************************
177 * _dump_D3DVIEWPORT, _dump_D3DVIEWPORT2
179 * Writes viewport information to TRACE
181 *****************************************************************************/
182 static void _dump_D3DVIEWPORT(const D3DVIEWPORT *lpvp)
184 TRACE(" - dwSize = %lu dwX = %lu dwY = %lu\n",
185 lpvp->dwSize, lpvp->dwX, lpvp->dwY);
186 TRACE(" - dwWidth = %lu dwHeight = %lu\n",
187 lpvp->dwWidth, lpvp->dwHeight);
188 TRACE(" - dvScaleX = %f dvScaleY = %f\n",
189 lpvp->dvScaleX, lpvp->dvScaleY);
190 TRACE(" - dvMaxX = %f dvMaxY = %f\n",
191 lpvp->dvMaxX, lpvp->dvMaxY);
192 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
193 lpvp->dvMinZ, lpvp->dvMaxZ);
196 static void _dump_D3DVIEWPORT2(const D3DVIEWPORT2 *lpvp)
198 TRACE(" - dwSize = %lu dwX = %lu dwY = %lu\n",
199 lpvp->dwSize, lpvp->dwX, lpvp->dwY);
200 TRACE(" - dwWidth = %lu dwHeight = %lu\n",
201 lpvp->dwWidth, lpvp->dwHeight);
202 TRACE(" - dvClipX = %f dvClipY = %f\n",
203 lpvp->dvClipX, lpvp->dvClipY);
204 TRACE(" - dvClipWidth = %f dvClipHeight = %f\n",
205 lpvp->dvClipWidth, lpvp->dvClipHeight);
206 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
207 lpvp->dvMinZ, lpvp->dvMaxZ);
210 static inline struct d3d_viewport *impl_from_IDirect3DViewport3(IDirect3DViewport3 *iface)
212 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
215 /*****************************************************************************
216 * IUnknown Methods.
217 *****************************************************************************/
219 /*****************************************************************************
220 * IDirect3DViewport3::QueryInterface
222 * A normal QueryInterface. Can query all interface versions and the
223 * IUnknown interface. The VTables of the different versions
224 * are equal
226 * Params:
227 * refiid: Interface id queried for
228 * obj: Address to write the interface pointer to
230 * Returns:
231 * S_OK on success.
232 * E_NOINTERFACE if the requested interface wasn't found
234 *****************************************************************************/
235 static HRESULT WINAPI d3d_viewport_QueryInterface(IDirect3DViewport3 *iface, REFIID riid, void **object)
237 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
239 if (IsEqualGUID(&IID_IDirect3DViewport3, riid)
240 || IsEqualGUID(&IID_IDirect3DViewport2, riid)
241 || IsEqualGUID(&IID_IDirect3DViewport, riid)
242 || IsEqualGUID(&IID_IUnknown, riid))
244 IDirect3DViewport3_AddRef(iface);
245 *object = iface;
246 return S_OK;
249 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
251 *object = NULL;
252 return E_NOINTERFACE;
255 /*****************************************************************************
256 * IDirect3DViewport3::AddRef
258 * Increases the refcount.
260 * Returns:
261 * The new refcount
263 *****************************************************************************/
264 static ULONG WINAPI d3d_viewport_AddRef(IDirect3DViewport3 *iface)
266 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
267 ULONG ref = InterlockedIncrement(&viewport->ref);
269 TRACE("%p increasing refcount to %lu.\n", viewport, ref);
271 return ref;
274 /*****************************************************************************
275 * IDirect3DViewport3::Release
277 * Reduces the refcount. If it falls to 0, the interface is released
279 * Returns:
280 * The new refcount
282 *****************************************************************************/
283 static ULONG WINAPI d3d_viewport_Release(IDirect3DViewport3 *iface)
285 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
286 ULONG ref = InterlockedDecrement(&viewport->ref);
288 TRACE("%p decreasing refcount to %lu.\n", viewport, ref);
290 if (!ref)
291 heap_free(viewport);
293 return ref;
296 /*****************************************************************************
297 * IDirect3DViewport Methods.
298 *****************************************************************************/
300 /*****************************************************************************
301 * IDirect3DViewport3::Initialize
303 * No-op initialization.
305 * Params:
306 * Direct3D: The direct3D device this viewport is assigned to
308 * Returns:
309 * DDERR_ALREADYINITIALIZED
311 *****************************************************************************/
312 static HRESULT WINAPI d3d_viewport_Initialize(IDirect3DViewport3 *iface, IDirect3D *d3d)
314 TRACE("iface %p, d3d %p.\n", iface, d3d);
316 return DDERR_ALREADYINITIALIZED;
319 static HRESULT WINAPI d3d_viewport_GetViewport(IDirect3DViewport3 *iface, D3DVIEWPORT *vp)
321 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
322 DWORD size;
324 TRACE("iface %p, vp %p.\n", iface, vp);
326 if (!vp)
327 return DDERR_INVALIDPARAMS;
329 if (viewport->version == DDRAW_VIEWPORT_VERSION_NONE)
331 WARN("Viewport data was not set.\n");
332 return D3DERR_VIEWPORTDATANOTSET;
335 wined3d_mutex_lock();
337 size = vp->dwSize;
338 if (viewport->version == DDRAW_VIEWPORT_VERSION_1)
340 memcpy(vp, &viewport->viewports.vp1, size);
342 else
344 D3DVIEWPORT vp1;
346 vp1.dwSize = sizeof(vp1);
347 vp1.dwX = viewport->viewports.vp2.dwX;
348 vp1.dwY = viewport->viewports.vp2.dwY;
349 vp1.dwWidth = viewport->viewports.vp2.dwWidth;
350 vp1.dwHeight = viewport->viewports.vp2.dwHeight;
352 vp1.dvScaleX = vp1.dwWidth / viewport->viewports.vp2.dvClipWidth;
353 vp1.dvScaleY = vp1.dwHeight / viewport->viewports.vp2.dvClipHeight;
354 vp1.dvMaxX = viewport->viewports.vp2.dvClipWidth + viewport->viewports.vp2.dvClipX;
355 vp1.dvMaxY = viewport->viewports.vp2.dvClipY;
356 vp1.dvMinZ = 0.0f;
357 vp1.dvMaxZ = 1.0f;
358 memcpy(vp, &vp1, size);
361 if (TRACE_ON(ddraw))
363 TRACE(" returning D3DVIEWPORT :\n");
364 _dump_D3DVIEWPORT(vp);
367 wined3d_mutex_unlock();
369 return D3D_OK;
372 static HRESULT WINAPI d3d_viewport_SetViewport(IDirect3DViewport3 *iface, D3DVIEWPORT *vp)
374 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
375 struct d3d_device *device = viewport->active_device;
376 struct wined3d_sub_resource_desc rt_desc;
377 struct wined3d_rendertarget_view *rtv;
378 IDirect3DViewport3 *current_viewport;
379 struct ddraw_surface *surface;
381 TRACE("iface %p, vp %p.\n", iface, vp);
383 if (!vp)
384 return DDERR_INVALIDPARAMS;
386 if (vp->dwSize != sizeof(*vp))
388 WARN("Invalid D3DVIEWPORT size %lu.\n", vp->dwSize);
389 return DDERR_INVALIDPARAMS;
392 if (TRACE_ON(ddraw))
394 TRACE(" getting D3DVIEWPORT :\n");
395 _dump_D3DVIEWPORT(vp);
398 if (!device)
400 WARN("Viewport not bound to a device, returning D3DERR_VIEWPORTHASNODEVICE.\n");
401 return D3DERR_VIEWPORTHASNODEVICE;
404 wined3d_mutex_lock();
406 if (device->version > 1)
408 if (!(rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0)))
410 wined3d_mutex_unlock();
411 return DDERR_INVALIDCAPS;
413 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
414 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
416 if (!wined3d_bound_range(vp->dwX, vp->dwWidth, rt_desc.width)
417 || !wined3d_bound_range(vp->dwY, vp->dwHeight, rt_desc.height))
419 WARN("Invalid viewport, returning DDERR_INVALIDPARAMS.\n");
420 wined3d_mutex_unlock();
421 return DDERR_INVALIDPARAMS;
425 viewport->version = DDRAW_VIEWPORT_VERSION_1;
426 viewport->viewports.vp1 = *vp;
428 /* Empirical testing on a couple of d3d1 games showed that these values
429 * should be ignored. */
430 viewport->viewports.vp1.dvMinZ = 0.0f;
431 viewport->viewports.vp1.dvMaxZ = 1.0f;
433 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(&device->IDirect3DDevice3_iface, &current_viewport)))
435 if (current_viewport == iface)
436 viewport_activate(viewport, FALSE);
437 IDirect3DViewport3_Release(current_viewport);
440 wined3d_mutex_unlock();
442 return D3D_OK;
445 /*****************************************************************************
446 * IDirect3DViewport3::TransformVertices
448 * Transforms vertices by the transformation matrix.
450 * This function is pretty similar to IDirect3DVertexBuffer7::ProcessVertices,
451 * so it's tempting to forward it to there. However, there are some
452 * tiny differences. First, the lpOffscreen flag that is reported back,
453 * then there is the homogeneous vertex that is generated. Also there's a lack
454 * of FVFs, but still a custom stride. Last, the d3d1 - d3d3 viewport has some
455 * settings (scale) that d3d7 and wined3d do not have. All in all wrapping to
456 * ProcessVertices doesn't pay of in terms of wrapper code needed and code
457 * reused.
459 * Params:
460 * dwVertexCount: The number of vertices to be transformed
461 * data: Pointer to the vertex input / output data.
462 * dwFlags: D3DTRANSFORM_CLIPPED or D3DTRANSFORM_UNCLIPPED
463 * offscreen: Logical AND of the planes that clipped the vertices if clipping
464 * is on. 0 if clipping is off.
466 * Returns:
467 * D3D_OK on success
468 * D3DERR_VIEWPORTHASNODEVICE if the viewport is not assigned to a device
469 * DDERR_INVALIDPARAMS if no clipping flag is specified
471 *****************************************************************************/
472 struct transform_vertices_vertex
474 float x, y, z, w; /* w is unused in input data. */
475 struct
477 DWORD p[4];
478 } payload;
481 static HRESULT WINAPI d3d_viewport_TransformVertices(IDirect3DViewport3 *iface,
482 DWORD dwVertexCount, D3DTRANSFORMDATA *data, DWORD dwFlags, DWORD *offscreen)
484 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
485 const struct wined3d_stateblock_state *state;
486 D3DVIEWPORT vp = viewport->viewports.vp1;
487 struct transform_vertices_vertex *in, *out;
488 struct wined3d_matrix mat;
489 float x, y, z, w;
490 unsigned int i;
491 D3DHVERTEX *outH;
492 struct d3d_device *device = viewport->active_device;
493 BOOL activate = device->current_viewport != viewport;
495 TRACE("iface %p, vertex_count %lu, data %p, flags %#lx, offscreen %p.\n",
496 iface, dwVertexCount, data, dwFlags, offscreen);
498 /* Tests on windows show that Windows crashes when this occurs,
499 * so don't return the (intuitive) return value
500 if (!device)
502 WARN("No device active, returning D3DERR_VIEWPORTHASNODEVICE\n");
503 return D3DERR_VIEWPORTHASNODEVICE;
507 if (!data || data->dwSize != sizeof(*data))
509 WARN("Transform data is NULL or size is incorrect, returning DDERR_INVALIDPARAMS\n");
510 return DDERR_INVALIDPARAMS;
512 if (!(dwFlags & (D3DTRANSFORM_UNCLIPPED | D3DTRANSFORM_CLIPPED)))
514 WARN("No clipping flag passed, returning DDERR_INVALIDPARAMS\n");
515 return DDERR_INVALIDPARAMS;
518 wined3d_mutex_lock();
519 if (activate)
520 viewport_activate(viewport, TRUE);
522 state = device->stateblock_state;
523 multiply_matrix(&mat, &state->transforms[WINED3D_TS_VIEW], &state->transforms[WINED3D_TS_WORLD_MATRIX(0)]);
524 multiply_matrix(&mat, &state->transforms[WINED3D_TS_PROJECTION], &mat);
526 /* The pointer is not tested against NULL on Windows. */
527 if (dwFlags & D3DTRANSFORM_CLIPPED)
528 *offscreen = ~0U;
529 else
530 *offscreen = 0;
532 outH = data->lpHOut;
533 for(i = 0; i < dwVertexCount; i++)
535 in = (struct transform_vertices_vertex *)((char *)data->lpIn + data->dwInSize * i);
536 out = (struct transform_vertices_vertex *)((char *)data->lpOut + data->dwOutSize * i);
538 x = (in->x * mat._11) + (in->y * mat._21) + (in->z * mat._31) + mat._41;
539 y = (in->x * mat._12) + (in->y * mat._22) + (in->z * mat._32) + mat._42;
540 z = (in->x * mat._13) + (in->y * mat._23) + (in->z * mat._33) + mat._43;
541 w = (in->x * mat._14) + (in->y * mat._24) + (in->z * mat._34) + mat._44;
543 if(dwFlags & D3DTRANSFORM_CLIPPED)
545 /* If clipping is enabled, Windows assumes that outH is
546 * a valid pointer. */
547 outH[i].u1.hx = (x - device->legacy_clipspace._41 * w) / device->legacy_clipspace._11;
548 outH[i].u2.hy = (y - device->legacy_clipspace._42 * w) / device->legacy_clipspace._22;
549 outH[i].u3.hz = (z - device->legacy_clipspace._43 * w) / device->legacy_clipspace._33;
551 outH[i].dwFlags = 0;
552 if (x > w)
553 outH[i].dwFlags |= D3DCLIP_RIGHT;
554 if (x < -w)
555 outH[i].dwFlags |= D3DCLIP_LEFT;
556 if (y > w)
557 outH[i].dwFlags |= D3DCLIP_TOP;
558 if (y < -w)
559 outH[i].dwFlags |= D3DCLIP_BOTTOM;
560 if (z < 0.0f)
561 outH[i].dwFlags |= D3DCLIP_FRONT;
562 if (z > w)
563 outH[i].dwFlags |= D3DCLIP_BACK;
565 *offscreen &= outH[i].dwFlags;
567 if(outH[i].dwFlags)
569 /* Looks like native just drops the vertex, leaves whatever data
570 * it has in the output buffer and goes on with the next vertex.
571 * The exact scheme hasn't been figured out yet, but windows
572 * definitely writes something there.
574 out->x = x;
575 out->y = y;
576 out->z = z;
577 out->w = w;
578 continue;
582 w = 1 / w;
583 x *= w; y *= w; z *= w;
585 out->x = (x + 1.0f) * vp.dwWidth * 0.5 + vp.dwX;
586 out->y = (-y + 1.0f) * vp.dwHeight * 0.5 + vp.dwY;
587 out->z = z;
588 out->w = w;
589 out->payload = in->payload;
592 if (activate && device->current_viewport)
593 viewport_activate(device->current_viewport, TRUE);
595 wined3d_mutex_unlock();
597 TRACE("All done\n");
598 return DD_OK;
601 /*****************************************************************************
602 * IDirect3DViewport3::LightElements
604 * The DirectX 5.0 sdk says that it's not implemented
606 * Params:
609 * Returns:
610 * DDERR_UNSUPPORTED
612 *****************************************************************************/
613 static HRESULT WINAPI d3d_viewport_LightElements(IDirect3DViewport3 *iface,
614 DWORD element_count, D3DLIGHTDATA *data)
616 TRACE("iface %p, element_count %lu, data %p.\n", iface, element_count, data);
618 return DDERR_UNSUPPORTED;
621 static HRESULT WINAPI d3d_viewport_SetBackground(IDirect3DViewport3 *iface, D3DMATERIALHANDLE material)
623 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
624 struct d3d_material *m;
626 TRACE("iface %p, material %#lx.\n", iface, material);
628 wined3d_mutex_lock();
630 if (!(m = ddraw_get_object(&viewport->ddraw->d3ddevice->handle_table, material - 1, DDRAW_HANDLE_MATERIAL)))
632 WARN("Invalid material handle %#lx.\n", material);
633 wined3d_mutex_unlock();
634 return DDERR_INVALIDPARAMS;
637 TRACE("Setting background color : %.8e %.8e %.8e %.8e.\n",
638 m->mat.u.diffuse.u1.r, m->mat.u.diffuse.u2.g,
639 m->mat.u.diffuse.u3.b, m->mat.u.diffuse.u4.a);
640 viewport->background = m;
642 wined3d_mutex_unlock();
644 return D3D_OK;
647 /*****************************************************************************
648 * IDirect3DViewport3::GetBackground
650 * Returns the material handle assigned to the background of the viewport
652 * Params:
653 * lphMat: Address to store the handle
654 * lpValid: is set to FALSE if no background is set, TRUE if one is set
656 * Returns:
657 * D3D_OK
659 *****************************************************************************/
660 static HRESULT WINAPI d3d_viewport_GetBackground(IDirect3DViewport3 *iface,
661 D3DMATERIALHANDLE *material, BOOL *valid)
663 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
665 TRACE("iface %p, material %p, valid %p.\n", iface, material, valid);
667 wined3d_mutex_lock();
668 if (valid)
669 *valid = !!viewport->background;
670 if (material)
671 *material = viewport->background ? viewport->background->Handle : 0;
672 wined3d_mutex_unlock();
674 return D3D_OK;
677 /*****************************************************************************
678 * IDirect3DViewport3::SetBackgroundDepth
680 * Sets a surface that represents the background depth. Its contents are
681 * used to set the depth buffer in IDirect3DViewport3::Clear
683 * Params:
684 * lpDDSurface: Surface to set
686 * Returns: D3D_OK, because it's a stub
688 *****************************************************************************/
689 static HRESULT WINAPI d3d_viewport_SetBackgroundDepth(IDirect3DViewport3 *iface, IDirectDrawSurface *surface)
691 FIXME("iface %p, surface %p stub!\n", iface, surface);
693 return D3D_OK;
696 /*****************************************************************************
697 * IDirect3DViewport3::GetBackgroundDepth
699 * Returns the surface that represents the depth field
701 * Params:
702 * lplpDDSurface: Address to store the interface pointer
703 * lpValid: Set to TRUE if a depth is assigned, FALSE otherwise
705 * Returns:
706 * D3D_OK, because it's a stub
707 * (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
709 *****************************************************************************/
710 static HRESULT WINAPI d3d_viewport_GetBackgroundDepth(IDirect3DViewport3 *iface,
711 IDirectDrawSurface **surface, BOOL *valid)
713 FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
715 return DD_OK;
718 /*****************************************************************************
719 * IDirect3DViewport3::Clear
721 * Clears the render target and / or the z buffer
723 * Params:
724 * dwCount: The amount of rectangles to clear. If 0, the whole buffer is
725 * cleared
726 * lpRects: Pointer to the array of rectangles. If NULL, Count must be 0
727 * dwFlags: D3DCLEAR_ZBUFFER and / or D3DCLEAR_TARGET
729 * Returns:
730 * D3D_OK on success
731 * D3DERR_VIEWPORTHASNODEVICE if there's no active device
732 * The return value of IDirect3DDevice7::Clear
734 *****************************************************************************/
735 static HRESULT WINAPI d3d_viewport_Clear(IDirect3DViewport3 *iface,
736 DWORD rect_count, D3DRECT *rects, DWORD flags)
738 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
739 DWORD color = 0x00000000;
740 HRESULT hr;
741 IDirect3DViewport3 *current_viewport;
742 IDirect3DDevice3 *d3d_device3;
744 TRACE("iface %p, rect_count %lu, rects %p, flags %#lx.\n", iface, rect_count, rects, flags);
746 if (!rects || !rect_count)
748 WARN("rect_count = %lu, rects = %p, ignoring clear\n", rect_count, rects);
749 return D3D_OK;
752 if (This->active_device == NULL) {
753 ERR(" Trying to clear a viewport not attached to a device!\n");
754 return D3DERR_VIEWPORTHASNODEVICE;
756 d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
758 wined3d_mutex_lock();
760 if (flags & D3DCLEAR_TARGET)
762 if (!This->background)
763 WARN("No background material set.\n");
764 else
765 color = D3DRGBA(This->background->mat.u.diffuse.u1.r,
766 This->background->mat.u.diffuse.u2.g,
767 This->background->mat.u.diffuse.u3.b,
768 This->background->mat.u.diffuse.u4.a);
771 /* Need to temporarily activate the viewport to clear it. The previously
772 * active one will be restored afterwards. */
773 viewport_activate(This, TRUE);
775 hr = IDirect3DDevice7_Clear(&This->active_device->IDirect3DDevice7_iface, rect_count, rects,
776 flags & (D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET), color, 1.0, 0x00000000);
778 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
780 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
781 viewport_activate(vp, TRUE);
782 IDirect3DViewport3_Release(current_viewport);
785 wined3d_mutex_unlock();
787 return hr;
790 /*****************************************************************************
791 * IDirect3DViewport3::AddLight
793 * Adds an light to the viewport
795 * Params:
796 * lpDirect3DLight: Interface of the light to add
798 * Returns:
799 * D3D_OK on success
800 * DDERR_INVALIDPARAMS if Direct3DLight is NULL
801 * DDERR_INVALIDPARAMS if there are 8 lights or more
803 *****************************************************************************/
804 static HRESULT WINAPI d3d_viewport_AddLight(IDirect3DViewport3 *viewport, IDirect3DLight *light)
806 struct d3d_light *light_impl = unsafe_impl_from_IDirect3DLight(light);
807 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(viewport);
809 TRACE("viewport %p, light %p.\n", viewport, light);
811 wined3d_mutex_lock();
813 if (light_impl->active_viewport)
815 wined3d_mutex_unlock();
816 WARN("Light %p is active in viewport %p.\n", light_impl, light_impl->active_viewport);
817 return D3DERR_LIGHTHASVIEWPORT;
820 light_impl->active_viewport = vp;
822 /* Add the light in the 'linked' chain */
823 list_add_tail(&vp->light_list, &light_impl->entry);
824 IDirect3DLight_AddRef(light);
826 light_activate(light_impl);
828 wined3d_mutex_unlock();
830 return D3D_OK;
833 /*****************************************************************************
834 * IDirect3DViewport3::DeleteLight
836 * Deletes a light from the viewports' light list
838 * Params:
839 * lpDirect3DLight: Light to delete
841 * Returns:
842 * D3D_OK on success
843 * DDERR_INVALIDPARAMS if the light wasn't found
845 *****************************************************************************/
846 static HRESULT WINAPI d3d_viewport_DeleteLight(IDirect3DViewport3 *iface, IDirect3DLight *lpDirect3DLight)
848 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
849 struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
851 TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
853 wined3d_mutex_lock();
855 if (l->active_viewport != viewport)
857 WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
858 wined3d_mutex_unlock();
859 return DDERR_INVALIDPARAMS;
862 light_deactivate(l);
863 list_remove(&l->entry);
864 l->active_viewport = NULL;
865 IDirect3DLight_Release(lpDirect3DLight);
867 wined3d_mutex_unlock();
869 return D3D_OK;
872 /*****************************************************************************
873 * IDirect3DViewport::NextLight
875 * Enumerates the lights associated with the viewport
877 * Params:
878 * lpDirect3DLight: Light to start with
879 * lplpDirect3DLight: Address to store the successor to
881 * Returns:
882 * D3D_OK, because it's a stub
884 *****************************************************************************/
885 static HRESULT WINAPI d3d_viewport_NextLight(IDirect3DViewport3 *iface,
886 IDirect3DLight *lpDirect3DLight, IDirect3DLight **lplpDirect3DLight, DWORD flags)
888 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
889 struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
890 struct list *entry;
891 HRESULT hr;
893 TRACE("iface %p, light %p, next_light %p, flags %#lx.\n",
894 iface, lpDirect3DLight, lplpDirect3DLight, flags);
896 if (!lplpDirect3DLight)
897 return DDERR_INVALIDPARAMS;
899 wined3d_mutex_lock();
901 switch (flags)
903 case D3DNEXT_NEXT:
904 if (!l || l->active_viewport != viewport)
906 if (l)
907 WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
908 entry = NULL;
910 else
911 entry = list_next(&viewport->light_list, &l->entry);
912 break;
914 case D3DNEXT_HEAD:
915 entry = list_head(&viewport->light_list);
916 break;
918 case D3DNEXT_TAIL:
919 entry = list_tail(&viewport->light_list);
920 break;
922 default:
923 entry = NULL;
924 WARN("Invalid flags %#lx.\n", flags);
925 break;
928 if (entry)
930 *lplpDirect3DLight = (IDirect3DLight *)LIST_ENTRY(entry, struct d3d_light, entry);
931 IDirect3DLight_AddRef(*lplpDirect3DLight);
932 hr = D3D_OK;
934 else
936 *lplpDirect3DLight = NULL;
937 hr = DDERR_INVALIDPARAMS;
940 wined3d_mutex_unlock();
942 return hr;
945 /*****************************************************************************
946 * IDirect3DViewport2 Methods.
947 *****************************************************************************/
949 static HRESULT WINAPI d3d_viewport_GetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *vp)
951 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
952 DWORD size;
954 TRACE("iface %p, vp %p.\n", iface, vp);
956 if (!vp)
957 return DDERR_INVALIDPARAMS;
959 if (viewport->version == DDRAW_VIEWPORT_VERSION_NONE)
961 WARN("Viewport data was not set.\n");
962 return D3DERR_VIEWPORTDATANOTSET;
965 wined3d_mutex_lock();
966 size = vp->dwSize;
967 if (viewport->version == DDRAW_VIEWPORT_VERSION_2)
969 memcpy(vp, &viewport->viewports.vp2, size);
971 else
973 D3DVIEWPORT2 vp2;
975 vp2.dwSize = sizeof(vp2);
976 vp2.dwX = viewport->viewports.vp1.dwX;
977 vp2.dwY = viewport->viewports.vp1.dwY;
978 vp2.dwWidth = viewport->viewports.vp1.dwWidth;
979 vp2.dwHeight = viewport->viewports.vp1.dwHeight;
981 vp2.dvClipWidth = viewport->viewports.vp1.dwWidth / viewport->viewports.vp1.dvScaleX;
982 vp2.dvClipHeight = viewport->viewports.vp1.dwHeight / viewport->viewports.vp1.dvScaleY;
983 vp2.dvClipX = -vp2.dvClipWidth / 2.0f;
984 vp2.dvClipY = vp2.dvClipHeight / 2.0f;
985 vp2.dvMinZ = 0.0f;
986 vp2.dvMaxZ = 1.0f;
987 memcpy(vp, &vp2, size);
990 if (TRACE_ON(ddraw))
992 TRACE(" returning D3DVIEWPORT2 :\n");
993 _dump_D3DVIEWPORT2(vp);
996 wined3d_mutex_unlock();
998 return D3D_OK;
1001 static HRESULT WINAPI d3d_viewport_SetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *vp)
1003 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
1004 struct d3d_device *device = viewport->active_device;
1005 struct wined3d_sub_resource_desc rt_desc;
1006 struct wined3d_rendertarget_view *rtv;
1007 IDirect3DViewport3 *current_viewport;
1008 struct ddraw_surface *surface;
1010 TRACE("iface %p, vp %p.\n", iface, vp);
1012 if (!vp)
1013 return DDERR_INVALIDPARAMS;
1015 if (vp->dwSize != sizeof(*vp))
1017 WARN("Invalid D3DVIEWPORT2 size %lu.\n", vp->dwSize);
1018 return DDERR_INVALIDPARAMS;
1021 if (TRACE_ON(ddraw))
1023 TRACE(" getting D3DVIEWPORT2 :\n");
1024 _dump_D3DVIEWPORT2(vp);
1027 if (!device)
1029 WARN("Viewport not bound to a device, returning D3DERR_VIEWPORTHASNODEVICE.\n");
1030 return D3DERR_VIEWPORTHASNODEVICE;
1033 wined3d_mutex_lock();
1035 if (device->version > 1)
1037 if (!(rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0)))
1039 wined3d_mutex_unlock();
1040 return DDERR_INVALIDCAPS;
1042 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1043 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
1045 if (!wined3d_bound_range(vp->dwX, vp->dwWidth, rt_desc.width)
1046 || !wined3d_bound_range(vp->dwY, vp->dwHeight, rt_desc.height))
1048 WARN("Invalid viewport, returning DDERR_INVALIDPARAMS.\n");
1049 wined3d_mutex_unlock();
1050 return DDERR_INVALIDPARAMS;
1054 viewport->version = DDRAW_VIEWPORT_VERSION_2;
1055 viewport->viewports.vp2 = *vp;
1057 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(&device->IDirect3DDevice3_iface, &current_viewport)))
1059 if (current_viewport == iface)
1060 viewport_activate(viewport, FALSE);
1061 IDirect3DViewport3_Release(current_viewport);
1064 wined3d_mutex_unlock();
1066 return D3D_OK;
1069 /*****************************************************************************
1070 * IDirect3DViewport3 Methods.
1071 *****************************************************************************/
1073 /*****************************************************************************
1074 * IDirect3DViewport3::SetBackgroundDepth2
1076 * Sets a IDirectDrawSurface4 surface as the background depth surface
1078 * Params:
1079 * lpDDS: Surface to set
1081 * Returns:
1082 * D3D_OK, because it's stub
1084 *****************************************************************************/
1085 static HRESULT WINAPI d3d_viewport_SetBackgroundDepth2(IDirect3DViewport3 *iface,
1086 IDirectDrawSurface4 *surface)
1088 FIXME("iface %p, surface %p stub!\n", iface, surface);
1090 return D3D_OK;
1093 /*****************************************************************************
1094 * IDirect3DViewport3::GetBackgroundDepth2
1096 * Returns the IDirect3DSurface4 interface to the background depth surface
1098 * Params:
1099 * lplpDDS: Address to store the interface pointer at
1100 * lpValid: Set to true if a surface is assigned
1102 * Returns:
1103 * D3D_OK because it's a stub
1105 *****************************************************************************/
1106 static HRESULT WINAPI d3d_viewport_GetBackgroundDepth2(IDirect3DViewport3 *iface,
1107 IDirectDrawSurface4 **surface, BOOL *valid)
1109 FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
1111 return D3D_OK;
1114 /*****************************************************************************
1115 * IDirect3DViewport3::Clear2
1117 * Another clearing method
1119 * Params:
1120 * Count: Number of rectangles to clear
1121 * Rects: Rectangle array to clear
1122 * Flags: Some flags :)
1123 * Color: Color to fill the render target with
1124 * Z: Value to fill the depth buffer with
1125 * Stencil: Value to fill the stencil bits with
1127 * Returns:
1129 *****************************************************************************/
1130 static HRESULT WINAPI d3d_viewport_Clear2(IDirect3DViewport3 *iface, DWORD rect_count,
1131 D3DRECT *rects, DWORD flags, DWORD color, D3DVALUE depth, DWORD stencil)
1133 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
1134 HRESULT hr;
1135 IDirect3DViewport3 *current_viewport;
1136 IDirect3DDevice3 *d3d_device3;
1138 TRACE("iface %p, rect_count %lu, rects %p, flags %#lx, color 0x%08lx, depth %.8e, stencil %lu.\n",
1139 iface, rect_count, rects, flags, color, depth, stencil);
1141 if (!rects || !rect_count)
1143 WARN("rect_count = %lu, rects = %p, ignoring clear\n", rect_count, rects);
1144 return D3D_OK;
1147 wined3d_mutex_lock();
1149 if (!viewport->active_device)
1151 WARN("Trying to clear a viewport not attached to a device.\n");
1152 wined3d_mutex_unlock();
1153 return D3DERR_VIEWPORTHASNODEVICE;
1155 d3d_device3 = &viewport->active_device->IDirect3DDevice3_iface;
1156 /* Need to temporarily activate viewport to clear it. Previously active
1157 * one will be restored afterwards. */
1158 viewport_activate(viewport, TRUE);
1160 hr = IDirect3DDevice7_Clear(&viewport->active_device->IDirect3DDevice7_iface,
1161 rect_count, rects, flags, color, depth, stencil);
1162 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
1164 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
1165 viewport_activate(vp, TRUE);
1166 IDirect3DViewport3_Release(current_viewport);
1169 wined3d_mutex_unlock();
1171 return hr;
1174 /*****************************************************************************
1175 * The VTable
1176 *****************************************************************************/
1178 static const struct IDirect3DViewport3Vtbl d3d_viewport_vtbl =
1180 /*** IUnknown Methods ***/
1181 d3d_viewport_QueryInterface,
1182 d3d_viewport_AddRef,
1183 d3d_viewport_Release,
1184 /*** IDirect3DViewport Methods */
1185 d3d_viewport_Initialize,
1186 d3d_viewport_GetViewport,
1187 d3d_viewport_SetViewport,
1188 d3d_viewport_TransformVertices,
1189 d3d_viewport_LightElements,
1190 d3d_viewport_SetBackground,
1191 d3d_viewport_GetBackground,
1192 d3d_viewport_SetBackgroundDepth,
1193 d3d_viewport_GetBackgroundDepth,
1194 d3d_viewport_Clear,
1195 d3d_viewport_AddLight,
1196 d3d_viewport_DeleteLight,
1197 d3d_viewport_NextLight,
1198 /*** IDirect3DViewport2 Methods ***/
1199 d3d_viewport_GetViewport2,
1200 d3d_viewport_SetViewport2,
1201 /*** IDirect3DViewport3 Methods ***/
1202 d3d_viewport_SetBackgroundDepth2,
1203 d3d_viewport_GetBackgroundDepth2,
1204 d3d_viewport_Clear2,
1207 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport3(IDirect3DViewport3 *iface)
1209 if (!iface) return NULL;
1210 assert(iface->lpVtbl == &d3d_viewport_vtbl);
1211 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1214 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport2(IDirect3DViewport2 *iface)
1216 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1217 if (!iface) return NULL;
1218 assert(iface->lpVtbl == (IDirect3DViewport2Vtbl *)&d3d_viewport_vtbl);
1219 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1222 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport(IDirect3DViewport *iface)
1224 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1225 if (!iface) return NULL;
1226 assert(iface->lpVtbl == (IDirect3DViewportVtbl *)&d3d_viewport_vtbl);
1227 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1230 void d3d_viewport_init(struct d3d_viewport *viewport, struct ddraw *ddraw)
1232 viewport->IDirect3DViewport3_iface.lpVtbl = &d3d_viewport_vtbl;
1233 viewport->ref = 1;
1234 viewport->ddraw = ddraw;
1235 viewport->version = DDRAW_VIEWPORT_VERSION_NONE;
1236 list_init(&viewport->light_list);