VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine_new/wined3d/device.c@ 49419

Last change on this file since 49419 was 49170, checked in by vboxsync, 11 years ago

wined3d: always emit zero vertattr

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 200.5 KB
Line 
1/*
2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27/*
28 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
29 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
30 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
31 * a choice of LGPL license versions is made available with the language indicating
32 * that LGPLv2 or any later version may be used, or where a choice of which version
33 * of the LGPL is applied is otherwise unspecified.
34 */
35
36#include "config.h"
37#include "wine/port.h"
38
39#include <stdio.h>
40#ifdef HAVE_FLOAT_H
41# include <float.h>
42#endif
43
44#include "wined3d_private.h"
45
46WINE_DEFAULT_DEBUG_CHANNEL(d3d);
47WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
48
49/* Define the default light parameters as specified by MSDN. */
50const struct wined3d_light WINED3D_default_light =
51{
52 WINED3D_LIGHT_DIRECTIONAL, /* Type */
53 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
54 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
55 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
56 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
57 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
58 0.0f, /* Range */
59 0.0f, /* Falloff */
60 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
61 0.0f, /* Theta */
62 0.0f /* Phi */
63};
64
65/* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
66 * actually have the same values in GL and D3D. */
67GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
68{
69 switch(primitive_type)
70 {
71 case WINED3D_PT_POINTLIST:
72 return GL_POINTS;
73
74 case WINED3D_PT_LINELIST:
75 return GL_LINES;
76
77 case WINED3D_PT_LINESTRIP:
78 return GL_LINE_STRIP;
79
80 case WINED3D_PT_TRIANGLELIST:
81 return GL_TRIANGLES;
82
83 case WINED3D_PT_TRIANGLESTRIP:
84 return GL_TRIANGLE_STRIP;
85
86 case WINED3D_PT_TRIANGLEFAN:
87 return GL_TRIANGLE_FAN;
88
89 case WINED3D_PT_LINELIST_ADJ:
90 return GL_LINES_ADJACENCY_ARB;
91
92 case WINED3D_PT_LINESTRIP_ADJ:
93 return GL_LINE_STRIP_ADJACENCY_ARB;
94
95 case WINED3D_PT_TRIANGLELIST_ADJ:
96 return GL_TRIANGLES_ADJACENCY_ARB;
97
98 case WINED3D_PT_TRIANGLESTRIP_ADJ:
99 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
100
101 default:
102 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
103 return GL_NONE;
104 }
105}
106
107static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
108{
109 switch(primitive_type)
110 {
111 case GL_POINTS:
112 return WINED3D_PT_POINTLIST;
113
114 case GL_LINES:
115 return WINED3D_PT_LINELIST;
116
117 case GL_LINE_STRIP:
118 return WINED3D_PT_LINESTRIP;
119
120 case GL_TRIANGLES:
121 return WINED3D_PT_TRIANGLELIST;
122
123 case GL_TRIANGLE_STRIP:
124 return WINED3D_PT_TRIANGLESTRIP;
125
126 case GL_TRIANGLE_FAN:
127 return WINED3D_PT_TRIANGLEFAN;
128
129 case GL_LINES_ADJACENCY_ARB:
130 return WINED3D_PT_LINELIST_ADJ;
131
132 case GL_LINE_STRIP_ADJACENCY_ARB:
133 return WINED3D_PT_LINESTRIP_ADJ;
134
135 case GL_TRIANGLES_ADJACENCY_ARB:
136 return WINED3D_PT_TRIANGLELIST_ADJ;
137
138 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
139 return WINED3D_PT_TRIANGLESTRIP_ADJ;
140
141 default:
142 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
143 return WINED3D_PT_UNDEFINED;
144 }
145}
146
147static BOOL fixed_get_input(BYTE usage, BYTE usage_idx, unsigned int *regnum)
148{
149 if ((usage == WINED3D_DECL_USAGE_POSITION || usage == WINED3D_DECL_USAGE_POSITIONT) && !usage_idx)
150 *regnum = WINED3D_FFP_POSITION;
151 else if (usage == WINED3D_DECL_USAGE_BLEND_WEIGHT && !usage_idx)
152 *regnum = WINED3D_FFP_BLENDWEIGHT;
153 else if (usage == WINED3D_DECL_USAGE_BLEND_INDICES && !usage_idx)
154 *regnum = WINED3D_FFP_BLENDINDICES;
155 else if (usage == WINED3D_DECL_USAGE_NORMAL && !usage_idx)
156 *regnum = WINED3D_FFP_NORMAL;
157 else if (usage == WINED3D_DECL_USAGE_PSIZE && !usage_idx)
158 *regnum = WINED3D_FFP_PSIZE;
159 else if (usage == WINED3D_DECL_USAGE_COLOR && !usage_idx)
160 *regnum = WINED3D_FFP_DIFFUSE;
161 else if (usage == WINED3D_DECL_USAGE_COLOR && usage_idx == 1)
162 *regnum = WINED3D_FFP_SPECULAR;
163 else if (usage == WINED3D_DECL_USAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
164 *regnum = WINED3D_FFP_TEXCOORD0 + usage_idx;
165 else
166 {
167 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n", debug_d3ddeclusage(usage), usage_idx);
168 *regnum = ~0U;
169 return FALSE;
170 }
171
172 return TRUE;
173}
174
175/* Context activation is done by the caller. */
176static void device_stream_info_from_declaration(struct wined3d_device *device, struct wined3d_stream_info *stream_info)
177{
178 const struct wined3d_state *state = &device->stateBlock->state;
179 /* We need to deal with frequency data! */
180 struct wined3d_vertex_declaration *declaration = state->vertex_declaration;
181 BOOL use_vshader;
182 unsigned int i;
183 WORD map;
184
185 stream_info->use_map = 0;
186 stream_info->swizzle_map = 0;
187 stream_info->all_vbo = 1;
188
189 /* Check for transformed vertices, disable vertex shader if present. */
190 stream_info->position_transformed = declaration->position_transformed;
191 use_vshader = state->vertex_shader && !declaration->position_transformed;
192
193 /* Translate the declaration into strided data. */
194 for (i = 0; i < declaration->element_count; ++i)
195 {
196 const struct wined3d_vertex_declaration_element *element = &declaration->elements[i];
197 const struct wined3d_stream_state *stream = &state->streams[element->input_slot];
198 struct wined3d_buffer *buffer = stream->buffer;
199 struct wined3d_bo_address data;
200 BOOL stride_used;
201 unsigned int idx;
202 DWORD stride;
203
204 TRACE("%p Element %p (%u of %u)\n", declaration->elements,
205 element, i + 1, declaration->element_count);
206
207 if (!buffer) continue;
208
209 stride = stream->stride;
210
211 TRACE("Stream %u, buffer %p.\n", element->input_slot, buffer);
212 buffer_get_memory(buffer, &device->adapter->gl_info, &data);
213
214 /* We can't use VBOs if the base vertex index is negative. OpenGL
215 * doesn't accept negative offsets (or rather offsets bigger than the
216 * VBO, because the pointer is unsigned), so use system memory
217 * sources. In most sane cases the pointer - offset will still be > 0,
218 * otherwise it will wrap around to some big value. Hope that with the
219 * indices, the driver wraps it back internally. If not,
220 * drawStridedSlow() is needed, including a vertex buffer path. */
221
222 if (state->load_base_vertex_index < 0)
223 {
224 WARN_(d3d_perf)("load_base_vertex_index is < 0 (%d), not using VBOs.\n", state->load_base_vertex_index);
225 data.buffer_object = 0;
226 data.addr = buffer_get_sysmem(buffer, &device->adapter->gl_info);
227 if ((UINT_PTR)data.addr < -state->load_base_vertex_index * stride)
228 FIXME("System memory vertex data load offset is negative!\n");
229 }
230 data.addr += element->offset;
231
232 TRACE("offset %u input_slot %u usage_idx %d\n", element->offset, element->input_slot, element->usage_idx);
233
234 if (use_vshader)
235 {
236 if (element->output_slot == ~0U)
237 {
238 /* TODO: Assuming vertexdeclarations are usually used with the
239 * same or a similar shader, it might be worth it to store the
240 * last used output slot and try that one first. */
241 stride_used = vshader_get_input(state->vertex_shader,
242 element->usage, element->usage_idx, &idx);
243 }
244 else
245 {
246 idx = element->output_slot;
247 stride_used = TRUE;
248 }
249 }
250 else
251 {
252 if (!element->ffp_valid)
253 {
254 WARN("Skipping unsupported fixed function element of format %s and usage %s\n",
255 debug_d3dformat(element->format->id), debug_d3ddeclusage(element->usage));
256 stride_used = FALSE;
257 }
258 else
259 {
260 stride_used = fixed_get_input(element->usage, element->usage_idx, &idx);
261 }
262 }
263
264 if (stride_used)
265 {
266 TRACE("Load %s array %u [usage %s, usage_idx %u, "
267 "input_slot %u, offset %u, stride %u, format %s, buffer_object %u]\n",
268 use_vshader ? "shader": "fixed function", idx,
269 debug_d3ddeclusage(element->usage), element->usage_idx, element->input_slot,
270 element->offset, stride, debug_d3dformat(element->format->id), data.buffer_object);
271
272 data.addr += stream->offset;
273
274 stream_info->elements[idx].format = element->format;
275 stream_info->elements[idx].data = data;
276#ifdef VBOX_WITH_WINE_FIX_BUFOFFSET
277 stream_info->elements[idx].offset = element->offset;
278#endif
279 stream_info->elements[idx].stride = stride;
280 stream_info->elements[idx].stream_idx = element->input_slot;
281
282 if (!device->adapter->gl_info.supported[ARB_VERTEX_ARRAY_BGRA]
283 && element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
284 {
285 stream_info->swizzle_map |= 1 << idx;
286 }
287 stream_info->use_map |= 1 << idx;
288 }
289 }
290
291 /* Preload the vertex buffers. */
292 device->num_buffer_queries = 0;
293 for (i = 0, map = stream_info->use_map; map; map >>= 1, ++i)
294 {
295 struct wined3d_stream_info_element *element;
296 struct wined3d_buffer *buffer;
297
298 if (!(map & 1))
299 continue;
300
301 element = &stream_info->elements[i];
302 buffer = state->streams[element->stream_idx].buffer;
303 wined3d_buffer_preload(buffer);
304
305 /* If the preload dropped the buffer object, update the stream info. */
306 if (
307#ifdef VBOX_WITH_WINE_FIX_STRINFOBUF
308 element->data.buffer_object &&
309#endif
310 buffer->buffer_object != element->data.buffer_object)
311 {
312 element->data.buffer_object = 0;
313 element->data.addr = buffer_get_sysmem(buffer, &device->adapter->gl_info) + (ptrdiff_t)element->data.addr;
314 }
315
316 if (!buffer->buffer_object)
317 stream_info->all_vbo = 0;
318
319 if (buffer->query)
320 device->buffer_queries[device->num_buffer_queries++] = buffer->query;
321 }
322}
323
324/* Context activation is done by the caller. */
325void device_update_stream_info(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
326{
327 struct wined3d_stream_info *stream_info = &device->stream_info;
328 const struct wined3d_state *state = &device->stateBlock->state;
329 DWORD prev_all_vbo = stream_info->all_vbo;
330
331 TRACE("============================= Vertex Declaration =============================\n");
332 device_stream_info_from_declaration(device, stream_info);
333
334 if (state->vertex_shader && !stream_info->position_transformed)
335 {
336 if (state->vertex_declaration->half_float_conv_needed && !stream_info->all_vbo)
337 {
338 TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion.\n");
339 device->useDrawStridedSlow = TRUE;
340 }
341 else
342 {
343 device->useDrawStridedSlow = FALSE;
344 }
345 }
346 else
347 {
348 WORD slow_mask = (1 << WINED3D_FFP_PSIZE);
349 slow_mask |= -!gl_info->supported[ARB_VERTEX_ARRAY_BGRA]
350 & ((1 << WINED3D_FFP_DIFFUSE) | (1 << WINED3D_FFP_SPECULAR));
351
352 if (((stream_info->position_transformed && !device->adapter->d3d_info.xyzrhw)
353 || (stream_info->use_map & slow_mask)) && !stream_info->all_vbo)
354 device->useDrawStridedSlow = TRUE;
355 else
356 device->useDrawStridedSlow = FALSE;
357 }
358
359 if (prev_all_vbo != stream_info->all_vbo)
360 device_invalidate_state(device, STATE_INDEXBUFFER);
361}
362
363static void device_preload_texture(const struct wined3d_state *state, unsigned int idx)
364{
365 struct wined3d_texture *texture;
366 enum WINED3DSRGB srgb;
367
368 if (!(texture = state->textures[idx])) return;
369 srgb = state->sampler_states[idx][WINED3D_SAMP_SRGB_TEXTURE] ? SRGB_SRGB : SRGB_RGB;
370 texture->texture_ops->texture_preload(texture, srgb);
371}
372
373void device_preload_textures(const struct wined3d_device *device)
374{
375 const struct wined3d_state *state = &device->stateBlock->state;
376 unsigned int i;
377
378 if (use_vs(state))
379 {
380 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
381 {
382 if (state->vertex_shader->reg_maps.sampler_type[i])
383 device_preload_texture(state, MAX_FRAGMENT_SAMPLERS + i);
384 }
385 }
386
387 if (use_ps(state))
388 {
389 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
390 {
391 if (state->pixel_shader->reg_maps.sampler_type[i])
392 device_preload_texture(state, i);
393 }
394 }
395 else
396 {
397 WORD ffu_map = device->fixed_function_usage_map;
398
399 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
400 {
401 if (ffu_map & 1)
402 device_preload_texture(state, i);
403 }
404 }
405}
406
407BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
408{
409 struct wined3d_context **new_array;
410
411 TRACE("Adding context %p.\n", context);
412
413 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
414 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
415 sizeof(*new_array) * (device->context_count + 1));
416
417 if (!new_array)
418 {
419 ERR("Failed to grow the context array.\n");
420 return FALSE;
421 }
422
423 new_array[device->context_count++] = context;
424 device->contexts = new_array;
425 return TRUE;
426}
427
428void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
429{
430 struct wined3d_context **new_array;
431 BOOL found = FALSE;
432 UINT i;
433
434 TRACE("Removing context %p.\n", context);
435
436 for (i = 0; i < device->context_count; ++i)
437 {
438 if (device->contexts[i] == context)
439 {
440 found = TRUE;
441 break;
442 }
443 }
444
445 if (!found)
446 {
447 ERR("Context %p doesn't exist in context array.\n", context);
448 return;
449 }
450
451 if (!--device->context_count)
452 {
453 HeapFree(GetProcessHeap(), 0, device->contexts);
454 device->contexts = NULL;
455 return;
456 }
457
458 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
459 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
460 if (!new_array)
461 {
462 ERR("Failed to shrink context array. Oh well.\n");
463 return;
464 }
465
466 device->contexts = new_array;
467}
468
469/* Do not call while under the GL lock. */
470void device_switch_onscreen_ds(struct wined3d_device *device,
471 struct wined3d_context *context, struct wined3d_surface *depth_stencil)
472{
473 if (device->onscreen_depth_stencil)
474 {
475 surface_load_ds_location(device->onscreen_depth_stencil, context, SFLAG_INTEXTURE);
476
477 surface_modify_ds_location(device->onscreen_depth_stencil, SFLAG_INTEXTURE,
478 device->onscreen_depth_stencil->ds_current_size.cx,
479 device->onscreen_depth_stencil->ds_current_size.cy);
480 wined3d_surface_decref(device->onscreen_depth_stencil);
481 }
482 device->onscreen_depth_stencil = depth_stencil;
483 wined3d_surface_incref(device->onscreen_depth_stencil);
484}
485
486static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
487{
488 /* partial draw rect */
489 if (draw_rect->left || draw_rect->top
490 || draw_rect->right < target->resource.width
491 || draw_rect->bottom < target->resource.height)
492 return FALSE;
493
494 /* partial clear rect */
495 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
496 || clear_rect->right < target->resource.width
497 || clear_rect->bottom < target->resource.height))
498 return FALSE;
499
500 return TRUE;
501}
502
503static void prepare_ds_clear(struct wined3d_surface *ds, struct wined3d_context *context,
504 DWORD location, const RECT *draw_rect, UINT rect_count, const RECT *clear_rect, RECT *out_rect)
505{
506 RECT current_rect, r;
507
508 if (ds->flags & SFLAG_DISCARDED)
509 {
510 /* Depth buffer was discarded, make it entirely current in its new location since
511 * there is no other place where we would get data anyway. */
512 SetRect(out_rect, 0, 0, ds->resource.width, ds->resource.height);
513 return;
514 }
515
516 if (ds->flags & location)
517 SetRect(&current_rect, 0, 0,
518 ds->ds_current_size.cx,
519 ds->ds_current_size.cy);
520 else
521 SetRectEmpty(&current_rect);
522
523 IntersectRect(&r, draw_rect, &current_rect);
524 if (EqualRect(&r, draw_rect))
525 {
526 /* current_rect ⊇ draw_rect, modify only. */
527 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
528 return;
529 }
530
531 if (EqualRect(&r, &current_rect))
532 {
533 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
534
535 if (!clear_rect)
536 {
537 /* Full clear, modify only. */
538 *out_rect = *draw_rect;
539 return;
540 }
541
542 IntersectRect(&r, draw_rect, clear_rect);
543 if (EqualRect(&r, draw_rect))
544 {
545 /* clear_rect ⊇ draw_rect, modify only. */
546 *out_rect = *draw_rect;
547 return;
548 }
549 }
550
551 /* Full load. */
552 surface_load_ds_location(ds, context, location);
553 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
554}
555
556/* Do not call while under the GL lock. */
557void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
558 UINT rect_count, const RECT *rects, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
559 float depth, DWORD stencil)
560{
561 const RECT *clear_rect = (rect_count > 0 && rects) ? (const RECT *)rects : NULL;
562 struct wined3d_surface *target = rt_count ? fb->render_targets[0] : NULL;
563 const struct wined3d_gl_info *gl_info;
564 UINT drawable_width, drawable_height;
565 struct wined3d_context *context;
566 GLbitfield clear_mask = 0;
567 BOOL render_offscreen;
568 unsigned int i;
569 RECT ds_rect;
570
571 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
572 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
573 * for the cleared parts, and the untouched parts.
574 *
575 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
576 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
577 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
578 * checking all this if the dest surface is in the drawable anyway. */
579 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, clear_rect))
580 {
581 for (i = 0; i < rt_count; ++i)
582 {
583 struct wined3d_surface *rt = fb->render_targets[i];
584 if (rt)
585 surface_load_location(rt, rt->draw_binding, NULL);
586 }
587 }
588
589 context = context_acquire(device, target);
590 if (!context->valid)
591 {
592 context_release(context);
593 WARN("Invalid context, skipping clear.\n");
594 return;
595 }
596 gl_info = context->gl_info;
597
598 if (target)
599 {
600 render_offscreen = context->render_offscreen;
601 target->get_drawable_size(context, &drawable_width, &drawable_height);
602 }
603 else
604 {
605 render_offscreen = TRUE;
606 drawable_width = fb->depth_stencil->pow2Width;
607 drawable_height = fb->depth_stencil->pow2Height;
608 }
609
610 if (flags & WINED3DCLEAR_ZBUFFER)
611 {
612 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : SFLAG_INDRAWABLE;
613
614 if (!render_offscreen && fb->depth_stencil != device->onscreen_depth_stencil)
615 device_switch_onscreen_ds(device, context, fb->depth_stencil);
616 prepare_ds_clear(fb->depth_stencil, context, location,
617 draw_rect, rect_count, clear_rect, &ds_rect);
618 }
619
620 if (!context_apply_clear_state(context, device, rt_count, fb))
621 {
622 context_release(context);
623 WARN("Failed to apply clear state, skipping clear.\n");
624 return;
625 }
626
627 /* Only set the values up once, as they are not changing. */
628 if (flags & WINED3DCLEAR_STENCIL)
629 {
630 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
631 {
632 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
633 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
634 }
635 gl_info->gl_ops.gl.p_glStencilMask(~0U);
636 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
637 gl_info->gl_ops.gl.p_glClearStencil(stencil);
638 checkGLcall("glClearStencil");
639 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
640 }
641
642 if (flags & WINED3DCLEAR_ZBUFFER)
643 {
644 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : SFLAG_INDRAWABLE;
645
646 surface_modify_ds_location(fb->depth_stencil, location, ds_rect.right, ds_rect.bottom);
647
648 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
649 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
650 gl_info->gl_ops.gl.p_glClearDepth(depth);
651 checkGLcall("glClearDepth");
652 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
653 }
654
655 if (flags & WINED3DCLEAR_TARGET)
656 {
657 for (i = 0; i < rt_count; ++i)
658 {
659 struct wined3d_surface *rt = fb->render_targets[i];
660
661 if (rt)
662 surface_modify_location(rt, rt->draw_binding, TRUE);
663 }
664
665 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
666 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
667 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
668 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
669 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
670 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
671 checkGLcall("glClearColor");
672 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
673 }
674
675 if (!clear_rect)
676 {
677 if (render_offscreen)
678 {
679 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
680 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
681 }
682 else
683 {
684 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
685 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
686 }
687 checkGLcall("glScissor");
688 gl_info->gl_ops.gl.p_glClear(clear_mask);
689 checkGLcall("glClear");
690 }
691 else
692 {
693 RECT current_rect;
694
695 /* Now process each rect in turn. */
696 for (i = 0; i < rect_count; ++i)
697 {
698 /* Note that GL uses lower left, width/height. */
699 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
700
701 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
702 wine_dbgstr_rect(&clear_rect[i]),
703 wine_dbgstr_rect(&current_rect));
704
705 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
706 * The rectangle is not cleared, no error is returned, but further rectangles are
707 * still cleared if they are valid. */
708 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
709 {
710 TRACE("Rectangle with negative dimensions, ignoring.\n");
711 continue;
712 }
713
714 if (render_offscreen)
715 {
716 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
717 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
718 }
719 else
720 {
721 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
722 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
723 }
724 checkGLcall("glScissor");
725
726 gl_info->gl_ops.gl.p_glClear(clear_mask);
727 checkGLcall("glClear");
728 }
729 }
730
731 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
732 && target->swapchain && target->swapchain->front_buffer == target))
733 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
734
735 context_release(context);
736}
737
738ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
739{
740 ULONG refcount = InterlockedIncrement(&device->ref);
741
742 TRACE("%p increasing refcount to %u.\n", device, refcount);
743
744 return refcount;
745}
746
747ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
748{
749 ULONG refcount = InterlockedDecrement(&device->ref);
750
751 TRACE("%p decreasing refcount to %u.\n", device, refcount);
752
753 if (!refcount)
754 {
755 struct wined3d_stateblock *stateblock;
756 UINT i;
757
758 if (wined3d_stateblock_decref(device->updateStateBlock)
759 && device->updateStateBlock != device->stateBlock)
760 FIXME("Something's still holding the update stateblock.\n");
761 device->updateStateBlock = NULL;
762
763 stateblock = device->stateBlock;
764 device->stateBlock = NULL;
765 if (wined3d_stateblock_decref(stateblock))
766 FIXME("Something's still holding the stateblock.\n");
767
768 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
769 {
770 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
771 device->multistate_funcs[i] = NULL;
772 }
773
774 if (!list_empty(&device->resources))
775 {
776 struct wined3d_resource *resource;
777
778 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
779
780 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
781 {
782 FIXME("Leftover resource %p with type %s (%#x).\n",
783 resource, debug_d3dresourcetype(resource->type), resource->type);
784 }
785 }
786
787 if (device->contexts)
788 ERR("Context array not freed!\n");
789 if (device->hardwareCursor)
790 DestroyCursor(device->hardwareCursor);
791 device->hardwareCursor = 0;
792
793 wined3d_decref(device->wined3d);
794 device->wined3d = NULL;
795 HeapFree(GetProcessHeap(), 0, device);
796 TRACE("Freed device %p.\n", device);
797 }
798
799 return refcount;
800}
801
802UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
803{
804 TRACE("device %p.\n", device);
805
806 return device->swapchain_count;
807}
808
809struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
810{
811 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
812
813 if (swapchain_idx >= device->swapchain_count)
814 {
815 WARN("swapchain_idx %u >= swapchain_count %u.\n",
816 swapchain_idx, device->swapchain_count);
817 return NULL;
818 }
819
820 return device->swapchains[swapchain_idx];
821}
822
823#ifndef VBOX
824static void device_load_logo(struct wined3d_device *device, const char *filename)
825{
826 struct wined3d_color_key color_key;
827 HBITMAP hbm;
828 BITMAP bm;
829 HRESULT hr;
830 HDC dcb = NULL, dcs = NULL;
831
832 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
833 if(hbm)
834 {
835 GetObjectA(hbm, sizeof(BITMAP), &bm);
836 dcb = CreateCompatibleDC(NULL);
837 if(!dcb) goto out;
838 SelectObject(dcb, hbm);
839 }
840 else
841 {
842 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
843 * couldn't be loaded
844 */
845 memset(&bm, 0, sizeof(bm));
846 bm.bmWidth = 32;
847 bm.bmHeight = 32;
848 }
849
850 hr = wined3d_surface_create(device, bm.bmWidth, bm.bmHeight, WINED3DFMT_B5G6R5_UNORM, 0,
851 WINED3D_POOL_SYSTEM_MEM, WINED3D_MULTISAMPLE_NONE, 0, WINED3D_SURFACE_MAPPABLE,
852 NULL, &wined3d_null_parent_ops, &device->logo_surface);
853 if (FAILED(hr))
854 {
855 ERR("Wine logo requested, but failed to create surface, hr %#x.\n", hr);
856 goto out;
857 }
858
859 if (dcb)
860 {
861 if (FAILED(hr = wined3d_surface_getdc(device->logo_surface, &dcs)))
862 goto out;
863 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
864 wined3d_surface_releasedc(device->logo_surface, dcs);
865
866 color_key.color_space_low_value = 0;
867 color_key.color_space_high_value = 0;
868 wined3d_surface_set_color_key(device->logo_surface, WINEDDCKEY_SRCBLT, &color_key);
869 }
870 else
871 {
872 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
873 /* Fill the surface with a white color to show that wined3d is there */
874 wined3d_device_color_fill(device, device->logo_surface, NULL, &c);
875 }
876
877out:
878 if (dcb) DeleteDC(dcb);
879 if (hbm) DeleteObject(hbm);
880}
881#endif
882
883/* Context activation is done by the caller. */
884static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
885{
886 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
887 unsigned int i, j, count;
888 /* Under DirectX you can sample even if no texture is bound, whereas
889 * OpenGL will only allow that when a valid texture is bound.
890 * We emulate this by creating dummy textures and binding them
891 * to each texture stage when the currently set D3D texture is NULL. */
892
893 if (gl_info->supported[APPLE_CLIENT_STORAGE])
894 {
895 /* The dummy texture does not have client storage backing */
896 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
897 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
898 }
899
900 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
901 for (i = 0; i < count; ++i)
902 {
903 DWORD color = 0x000000ff;
904
905 /* Make appropriate texture active */
906 context_active_texture(context, gl_info, i);
907
908 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
909 checkGLcall("glGenTextures");
910 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
911
912 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
913 checkGLcall("glBindTexture");
914
915 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
916 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
917 checkGLcall("glTexImage2D");
918
919 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
920 {
921 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
922 checkGLcall("glGenTextures");
923 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
924
925 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
926 checkGLcall("glBindTexture");
927
928 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
929 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
930 checkGLcall("glTexImage2D");
931 }
932
933 if (gl_info->supported[EXT_TEXTURE3D])
934 {
935 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
936 checkGLcall("glGenTextures");
937 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
938
939 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
940 checkGLcall("glBindTexture");
941
942 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
943 checkGLcall("glTexImage3D");
944 }
945
946 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
947 {
948 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
949 checkGLcall("glGenTextures");
950 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
951
952 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
953 checkGLcall("glBindTexture");
954
955 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
956 {
957 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
958 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
959 checkGLcall("glTexImage2D");
960 }
961 }
962 }
963
964 if (gl_info->supported[APPLE_CLIENT_STORAGE])
965 {
966 /* Re-enable because if supported it is enabled by default */
967 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
968 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
969 }
970}
971
972/* Context activation is done by the caller. */
973static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
974{
975 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
976
977 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
978 {
979 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
980 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
981 }
982
983 if (gl_info->supported[EXT_TEXTURE3D])
984 {
985 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
986 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
987 }
988
989 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
990 {
991 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
992 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
993 }
994
995 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
996 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
997
998 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
999 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
1000 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
1001 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
1002}
1003
1004static LONG fullscreen_style(LONG style)
1005{
1006 /* Make sure the window is managed, otherwise we won't get keyboard input. */
1007 style |= WS_POPUP | WS_SYSMENU;
1008 style &= ~(WS_CAPTION | WS_THICKFRAME);
1009
1010 return style;
1011}
1012
1013static LONG fullscreen_exstyle(LONG exstyle)
1014{
1015 /* Filter out window decorations. */
1016 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
1017
1018 return exstyle;
1019}
1020
1021void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
1022{
1023 BOOL filter_messages;
1024 LONG style, exstyle;
1025
1026 TRACE("Setting up window %p for fullscreen mode.\n", window);
1027
1028 if (device->style || device->exStyle)
1029 {
1030 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
1031 window, device->style, device->exStyle);
1032 }
1033
1034 device->style = GetWindowLongW(window, GWL_STYLE);
1035 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
1036
1037 style = fullscreen_style(device->style);
1038 exstyle = fullscreen_exstyle(device->exStyle);
1039
1040 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
1041 device->style, device->exStyle, style, exstyle);
1042
1043 filter_messages = device->filter_messages;
1044 device->filter_messages = TRUE;
1045
1046 SetWindowLongW(window, GWL_STYLE, style);
1047 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
1048 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
1049
1050 device->filter_messages = filter_messages;
1051}
1052
1053void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
1054{
1055 BOOL filter_messages;
1056 LONG style, exstyle;
1057
1058 if (!device->style && !device->exStyle) return;
1059
1060 style = GetWindowLongW(window, GWL_STYLE);
1061 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
1062
1063 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
1064 * application, and we want to ignore them in the test below, since it's
1065 * not the application's fault that they changed. Additionally, we want to
1066 * preserve the current status of these flags (i.e. don't restore them) to
1067 * more closely emulate the behavior of Direct3D, which leaves these flags
1068 * alone when returning to windowed mode. */
1069 device->style ^= (device->style ^ style) & WS_VISIBLE;
1070 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
1071
1072 TRACE("Restoring window style of window %p to %08x, %08x.\n",
1073 window, device->style, device->exStyle);
1074
1075 filter_messages = device->filter_messages;
1076 device->filter_messages = TRUE;
1077
1078 /* Only restore the style if the application didn't modify it during the
1079 * fullscreen phase. Some applications change it before calling Reset()
1080 * when switching between windowed and fullscreen modes (HL2), some
1081 * depend on the original style (Eve Online). */
1082 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
1083 {
1084 SetWindowLongW(window, GWL_STYLE, device->style);
1085 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
1086 }
1087 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1088
1089 device->filter_messages = filter_messages;
1090
1091 /* Delete the old values. */
1092 device->style = 0;
1093 device->exStyle = 0;
1094}
1095
1096HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
1097{
1098#ifndef VBOX_WITH_WDDM
1099 TRACE("device %p, window %p.\n", device, window);
1100
1101 if (!wined3d_register_window(window, device))
1102 {
1103 ERR("Failed to register window %p.\n", window);
1104 return E_FAIL;
1105 }
1106
1107 InterlockedExchangePointer((void **)&device->focus_window, window);
1108 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
1109
1110 return WINED3D_OK;
1111#else
1112 ERR("unsupported!");
1113 return E_FAIL;
1114#endif
1115}
1116
1117void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
1118{
1119#ifndef VBOX_WITH_WDDM
1120 TRACE("device %p.\n", device);
1121
1122 if (device->focus_window) wined3d_unregister_window(device->focus_window);
1123 InterlockedExchangePointer((void **)&device->focus_window, NULL);
1124#else
1125 ERR("unsupported!");
1126#endif
1127}
1128
1129HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1130 struct wined3d_swapchain_desc *swapchain_desc)
1131{
1132 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1133 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1134 struct wined3d_swapchain *swapchain = NULL;
1135 struct wined3d_context *context;
1136 HRESULT hr;
1137 DWORD state;
1138
1139 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1140
1141 if (device->d3d_initialized)
1142 return WINED3DERR_INVALIDCALL;
1143 if (device->wined3d->flags & WINED3D_NO3D)
1144 return WINED3DERR_INVALIDCALL;
1145
1146#ifdef VBOX_WITH_WDDM
1147 if (!swapchain_desc->pHgsmi)
1148 {
1149 ERR("hgsmi not specified!");
1150 return WINED3DERR_INVALIDCALL;
1151 }
1152 device->pHgsmi = swapchain_desc->pHgsmi;
1153#endif
1154
1155 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1156 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
1157
1158 /* Initialize the texture unit mapping to a 1:1 mapping */
1159 for (state = 0; state < MAX_COMBINED_SAMPLERS; ++state)
1160 {
1161 if (state < gl_info->limits.fragment_samplers)
1162 {
1163 device->texUnitMap[state] = state;
1164 device->rev_tex_unit_map[state] = state;
1165 }
1166 else
1167 {
1168 device->texUnitMap[state] = WINED3D_UNMAPPED_STAGE;
1169 device->rev_tex_unit_map[state] = WINED3D_UNMAPPED_STAGE;
1170 }
1171 }
1172
1173 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1174 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1175 {
1176 TRACE("Shader private data couldn't be allocated\n");
1177 goto err_out;
1178 }
1179 if (FAILED(hr = device->blitter->alloc_private(device)))
1180 {
1181 TRACE("Blitter private data couldn't be allocated\n");
1182 goto err_out;
1183 }
1184
1185 /* Setup the implicit swapchain. This also initializes a context. */
1186 TRACE("Creating implicit swapchain\n");
1187 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1188 swapchain_desc, &swapchain);
1189 if (FAILED(hr))
1190 {
1191 WARN("Failed to create implicit swapchain\n");
1192 goto err_out;
1193 }
1194
1195 device->swapchain_count = 1;
1196 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1197 if (!device->swapchains)
1198 {
1199 ERR("Out of memory!\n");
1200 goto err_out;
1201 }
1202 device->swapchains[0] = swapchain;
1203
1204 if (swapchain->back_buffers && swapchain->back_buffers[0])
1205 {
1206 TRACE("Setting rendertarget to %p.\n", swapchain->back_buffers);
1207 device->fb.render_targets[0] = swapchain->back_buffers[0];
1208 }
1209 else
1210 {
1211 TRACE("Setting rendertarget to %p.\n", swapchain->front_buffer);
1212 device->fb.render_targets[0] = swapchain->front_buffer;
1213 }
1214 wined3d_surface_incref(device->fb.render_targets[0]);
1215
1216 /* Depth Stencil support */
1217 device->fb.depth_stencil = device->auto_depth_stencil;
1218 if (device->fb.depth_stencil)
1219 wined3d_surface_incref(device->fb.depth_stencil);
1220
1221 /* Set up some starting GL setup */
1222
1223 /* Setup all the devices defaults */
1224 stateblock_init_default_state(device->stateBlock);
1225
1226 context = context_acquire(device, swapchain->front_buffer);
1227
1228 create_dummy_textures(device, context);
1229
1230 device->contexts[0]->last_was_rhw = 0;
1231
1232 switch (wined3d_settings.offscreen_rendering_mode)
1233 {
1234 case ORM_FBO:
1235 device->offscreenBuffer = GL_COLOR_ATTACHMENT0;
1236 break;
1237
1238 case ORM_BACKBUFFER:
1239 {
1240 if (context_get_current()->aux_buffers > 0)
1241 {
1242 TRACE("Using auxiliary buffer for offscreen rendering\n");
1243 device->offscreenBuffer = GL_AUX0;
1244 }
1245 else
1246 {
1247 TRACE("Using back buffer for offscreen rendering\n");
1248 device->offscreenBuffer = GL_BACK;
1249 }
1250 }
1251 }
1252
1253 TRACE("All defaults now set up, leaving 3D init.\n");
1254
1255 context_release(context);
1256
1257 /* Clear the screen */
1258 wined3d_device_clear(device, 0, NULL, WINED3DCLEAR_TARGET
1259 | (swapchain_desc->enable_auto_depth_stencil ? WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL : 0),
1260 &black, 1.0f, 0);
1261
1262 device->d3d_initialized = TRUE;
1263
1264#ifndef VBOX
1265 if (wined3d_settings.logo)
1266 device_load_logo(device, wined3d_settings.logo);
1267#endif
1268 return WINED3D_OK;
1269
1270err_out:
1271 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1272 HeapFree(GetProcessHeap(), 0, device->swapchains);
1273 device->swapchain_count = 0;
1274 if (swapchain)
1275 wined3d_swapchain_decref(swapchain);
1276 if (device->blit_priv)
1277 device->blitter->free_private(device);
1278 if (device->shader_priv)
1279 device->shader_backend->shader_free_private(device);
1280
1281 return hr;
1282}
1283
1284HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1285 struct wined3d_swapchain_desc *swapchain_desc)
1286{
1287 struct wined3d_swapchain *swapchain = NULL;
1288 HRESULT hr;
1289
1290 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1291
1292 /* Setup the implicit swapchain */
1293 TRACE("Creating implicit swapchain\n");
1294 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1295 swapchain_desc, &swapchain);
1296 if (FAILED(hr))
1297 {
1298 WARN("Failed to create implicit swapchain\n");
1299 goto err_out;
1300 }
1301
1302 device->swapchain_count = 1;
1303 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1304 if (!device->swapchains)
1305 {
1306 ERR("Out of memory!\n");
1307 goto err_out;
1308 }
1309 device->swapchains[0] = swapchain;
1310 return WINED3D_OK;
1311
1312err_out:
1313 wined3d_swapchain_decref(swapchain);
1314 return hr;
1315}
1316
1317HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1318{
1319 struct wined3d_resource *resource, *cursor;
1320 const struct wined3d_gl_info *gl_info;
1321 struct wined3d_context *context;
1322 struct wined3d_surface *surface;
1323 UINT i;
1324
1325 TRACE("device %p.\n", device);
1326
1327 if (!device->d3d_initialized)
1328 return WINED3DERR_INVALIDCALL;
1329
1330#ifdef VBOX_WINE_WITH_PROFILE
1331 VBOXWINEPROFILE_DRAWPRIM_TERM(&device->DrawPrimProfile);
1332#endif
1333
1334 /* Force making the context current again, to verify it is still valid
1335 * (workaround for broken drivers) */
1336 context_set_current(NULL);
1337 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1338 * it was created. Thus make sure a context is active for the glDelete* calls
1339 */
1340 context = context_acquire(device, NULL);
1341 gl_info = context->gl_info;
1342
1343 zv_destroy(device);
1344
1345 if (device->logo_surface)
1346 wined3d_surface_decref(device->logo_surface);
1347
1348 stateblock_unbind_resources(device->stateBlock);
1349
1350 /* Unload resources */
1351 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1352 {
1353 TRACE("Unloading resource %p.\n", resource);
1354
1355 resource->resource_ops->resource_unload(resource);
1356 }
1357
1358 /* Delete the mouse cursor texture */
1359 if (device->cursorTexture)
1360 {
1361 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
1362 device->cursorTexture = 0;
1363 }
1364
1365 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1366 * private data, it might contain opengl pointers
1367 */
1368 if (device->depth_blt_texture)
1369 {
1370 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1371 device->depth_blt_texture = 0;
1372 }
1373
1374#ifdef VBOX_WINE_WITH_SHADER_CACHE
1375 shader_chaches_term(device);
1376#endif
1377
1378 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1379 device->blitter->free_private(device);
1380 device->shader_backend->shader_free_private(device);
1381
1382 /* Release the buffers (with sanity checks)*/
1383 if (device->onscreen_depth_stencil)
1384 {
1385 surface = device->onscreen_depth_stencil;
1386 device->onscreen_depth_stencil = NULL;
1387 wined3d_surface_decref(surface);
1388 }
1389
1390 if (device->fb.depth_stencil)
1391 {
1392 surface = device->fb.depth_stencil;
1393
1394 TRACE("Releasing depth/stencil buffer %p.\n", surface);
1395
1396 device->fb.depth_stencil = NULL;
1397 wined3d_surface_decref(surface);
1398 }
1399
1400 if (device->auto_depth_stencil)
1401 {
1402 surface = device->auto_depth_stencil;
1403 device->auto_depth_stencil = NULL;
1404 if (wined3d_surface_decref(surface))
1405 FIXME("Something's still holding the auto depth stencil buffer (%p).\n", surface);
1406 }
1407
1408 for (i = 1; i < gl_info->limits.buffers; ++i)
1409 {
1410 wined3d_device_set_render_target(device, i, NULL, FALSE);
1411 }
1412
1413 surface = device->fb.render_targets[0];
1414 TRACE("Setting rendertarget 0 to NULL\n");
1415 device->fb.render_targets[0] = NULL;
1416 TRACE("Releasing the render target at %p\n", surface);
1417 wined3d_surface_decref(surface);
1418
1419 context_release(context);
1420
1421 for (i = 0; i < device->swapchain_count; ++i)
1422 {
1423 TRACE("Releasing the implicit swapchain %u.\n", i);
1424 if (wined3d_swapchain_decref(device->swapchains[i]))
1425 FIXME("Something's still holding the implicit swapchain.\n");
1426 }
1427
1428#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
1429 while (device->context_count)
1430 {
1431 context_destroy(device, device->contexts[0]);
1432 }
1433#endif
1434
1435 HeapFree(GetProcessHeap(), 0, device->swapchains);
1436 device->swapchains = NULL;
1437 device->swapchain_count = 0;
1438
1439 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1440 device->fb.render_targets = NULL;
1441
1442 device->d3d_initialized = FALSE;
1443
1444 return WINED3D_OK;
1445}
1446
1447HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1448{
1449 unsigned int i;
1450
1451 for (i = 0; i < device->swapchain_count; ++i)
1452 {
1453 TRACE("Releasing the implicit swapchain %u.\n", i);
1454 if (wined3d_swapchain_decref(device->swapchains[i]))
1455 FIXME("Something's still holding the implicit swapchain.\n");
1456 }
1457
1458 HeapFree(GetProcessHeap(), 0, device->swapchains);
1459 device->swapchains = NULL;
1460 device->swapchain_count = 0;
1461 return WINED3D_OK;
1462}
1463
1464/* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1465 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1466 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1467 *
1468 * There is no way to deactivate thread safety once it is enabled.
1469 */
1470void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1471{
1472 TRACE("device %p.\n", device);
1473
1474 /* For now just store the flag (needed in case of ddraw). */
1475 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1476}
1477
1478
1479UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1480{
1481#ifndef VBOX_WITH_WDDM
1482 TRACE("device %p.\n", device);
1483
1484 TRACE("Emulating %d MB, returning %d MB left.\n",
1485 device->adapter->TextureRam / (1024 * 1024),
1486 (device->adapter->TextureRam - device->adapter->UsedTextureRam) / (1024 * 1024));
1487
1488 return device->adapter->TextureRam - device->adapter->UsedTextureRam;
1489#else
1490 ERR("unsupported!");
1491 return ~0UL;
1492#endif
1493}
1494
1495void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1496 struct wined3d_buffer *buffer, UINT offset)
1497{
1498 struct wined3d_buffer *prev_buffer;
1499
1500 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1501
1502 if (idx >= MAX_STREAM_OUT)
1503 {
1504 WARN("Invalid stream output %u.\n", idx);
1505 return;
1506 }
1507
1508 prev_buffer = device->updateStateBlock->state.stream_output[idx].buffer;
1509 device->updateStateBlock->state.stream_output[idx].buffer = buffer;
1510 device->updateStateBlock->state.stream_output[idx].offset = offset;
1511
1512 if (device->isRecordingState)
1513 {
1514 if (buffer)
1515 wined3d_buffer_incref(buffer);
1516 if (prev_buffer)
1517 wined3d_buffer_decref(prev_buffer);
1518 return;
1519 }
1520
1521 if (prev_buffer != buffer)
1522 {
1523 if (buffer)
1524 {
1525 InterlockedIncrement(&buffer->resource.bind_count);
1526 wined3d_buffer_incref(buffer);
1527 }
1528 if (prev_buffer)
1529 {
1530 InterlockedDecrement(&prev_buffer->resource.bind_count);
1531 wined3d_buffer_decref(prev_buffer);
1532 }
1533 }
1534}
1535
1536struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1537 UINT idx, UINT *offset)
1538{
1539 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1540
1541 if (idx >= MAX_STREAM_OUT)
1542 {
1543 WARN("Invalid stream output %u.\n", idx);
1544 return NULL;
1545 }
1546
1547 *offset = device->stateBlock->state.stream_output[idx].offset;
1548 return device->stateBlock->state.stream_output[idx].buffer;
1549}
1550
1551HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1552 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1553{
1554 struct wined3d_stream_state *stream;
1555 struct wined3d_buffer *prev_buffer;
1556
1557 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1558 device, stream_idx, buffer, offset, stride);
1559
1560 if (stream_idx >= MAX_STREAMS)
1561 {
1562 WARN("Stream index %u out of range.\n", stream_idx);
1563 return WINED3DERR_INVALIDCALL;
1564 }
1565 else if (offset & 0x3)
1566 {
1567 WARN("Offset %u is not 4 byte aligned.\n", offset);
1568 return WINED3DERR_INVALIDCALL;
1569 }
1570
1571 stream = &device->updateStateBlock->state.streams[stream_idx];
1572 prev_buffer = stream->buffer;
1573
1574 device->updateStateBlock->changed.streamSource |= 1 << stream_idx;
1575
1576 if (prev_buffer == buffer
1577 && stream->stride == stride
1578 && stream->offset == offset)
1579 {
1580 TRACE("Application is setting the old values over, nothing to do.\n");
1581 return WINED3D_OK;
1582 }
1583
1584 stream->buffer = buffer;
1585 if (buffer)
1586 {
1587 stream->stride = stride;
1588 stream->offset = offset;
1589 }
1590
1591 /* Handle recording of state blocks. */
1592 if (device->isRecordingState)
1593 {
1594 TRACE("Recording... not performing anything.\n");
1595 if (buffer)
1596 wined3d_buffer_incref(buffer);
1597 if (prev_buffer)
1598 wined3d_buffer_decref(prev_buffer);
1599 return WINED3D_OK;
1600 }
1601
1602 if (buffer)
1603 {
1604 InterlockedIncrement(&buffer->resource.bind_count);
1605 wined3d_buffer_incref(buffer);
1606 }
1607 if (prev_buffer)
1608 {
1609 InterlockedDecrement(&prev_buffer->resource.bind_count);
1610 wined3d_buffer_decref(prev_buffer);
1611 }
1612
1613 device_invalidate_state(device, STATE_STREAMSRC);
1614
1615 return WINED3D_OK;
1616}
1617
1618HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1619 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1620{
1621 struct wined3d_stream_state *stream;
1622
1623 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1624 device, stream_idx, buffer, offset, stride);
1625
1626 if (stream_idx >= MAX_STREAMS)
1627 {
1628 WARN("Stream index %u out of range.\n", stream_idx);
1629 return WINED3DERR_INVALIDCALL;
1630 }
1631
1632 stream = &device->stateBlock->state.streams[stream_idx];
1633 *buffer = stream->buffer;
1634 if (*buffer)
1635 wined3d_buffer_incref(*buffer);
1636 if (offset)
1637 *offset = stream->offset;
1638 *stride = stream->stride;
1639
1640 return WINED3D_OK;
1641}
1642
1643HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1644{
1645 struct wined3d_stream_state *stream;
1646 UINT old_flags, old_freq;
1647
1648 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1649
1650 /* Verify input. At least in d3d9 this is invalid. */
1651 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1652 {
1653 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1654 return WINED3DERR_INVALIDCALL;
1655 }
1656 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1657 {
1658 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1659 return WINED3DERR_INVALIDCALL;
1660 }
1661 if (!divider)
1662 {
1663 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1664 return WINED3DERR_INVALIDCALL;
1665 }
1666
1667 stream = &device->updateStateBlock->state.streams[stream_idx];
1668 old_flags = stream->flags;
1669 old_freq = stream->frequency;
1670
1671 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1672 stream->frequency = divider & 0x7fffff;
1673
1674 device->updateStateBlock->changed.streamFreq |= 1 << stream_idx;
1675
1676 if (stream->frequency != old_freq || stream->flags != old_flags)
1677 device_invalidate_state(device, STATE_STREAMSRC);
1678
1679 return WINED3D_OK;
1680}
1681
1682HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1683 UINT stream_idx, UINT *divider)
1684{
1685 struct wined3d_stream_state *stream;
1686
1687 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1688
1689 stream = &device->updateStateBlock->state.streams[stream_idx];
1690 *divider = stream->flags | stream->frequency;
1691
1692 TRACE("Returning %#x.\n", *divider);
1693
1694 return WINED3D_OK;
1695}
1696
1697void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1698 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1699{
1700 TRACE("device %p, state %s, matrix %p.\n",
1701 device, debug_d3dtstype(d3dts), matrix);
1702 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._11, matrix->u.s._12, matrix->u.s._13, matrix->u.s._14);
1703 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._21, matrix->u.s._22, matrix->u.s._23, matrix->u.s._24);
1704 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._31, matrix->u.s._32, matrix->u.s._33, matrix->u.s._34);
1705 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._41, matrix->u.s._42, matrix->u.s._43, matrix->u.s._44);
1706
1707 /* Handle recording of state blocks. */
1708 if (device->isRecordingState)
1709 {
1710 TRACE("Recording... not performing anything.\n");
1711 device->updateStateBlock->changed.transform[d3dts >> 5] |= 1 << (d3dts & 0x1f);
1712 device->updateStateBlock->state.transforms[d3dts] = *matrix;
1713 return;
1714 }
1715
1716 /* If the new matrix is the same as the current one,
1717 * we cut off any further processing. this seems to be a reasonable
1718 * optimization because as was noticed, some apps (warcraft3 for example)
1719 * tend towards setting the same matrix repeatedly for some reason.
1720 *
1721 * From here on we assume that the new matrix is different, wherever it matters. */
1722 if (!memcmp(&device->stateBlock->state.transforms[d3dts].u.m[0][0], matrix, sizeof(*matrix)))
1723 {
1724 TRACE("The application is setting the same matrix over again.\n");
1725 return;
1726 }
1727
1728 device->stateBlock->state.transforms[d3dts] = *matrix;
1729
1730 if (d3dts < WINED3D_TS_WORLD_MATRIX(device->adapter->gl_info.limits.blends))
1731 device_invalidate_state(device, STATE_TRANSFORM(d3dts));
1732}
1733
1734void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1735 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1736{
1737 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1738
1739 *matrix = device->stateBlock->state.transforms[state];
1740}
1741
1742void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1743 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1744{
1745 const struct wined3d_matrix *mat;
1746 struct wined3d_matrix temp;
1747
1748 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1749
1750 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1751 * below means it will be recorded in a state block change, but it
1752 * works regardless where it is recorded.
1753 * If this is found to be wrong, change to StateBlock. */
1754 if (state > HIGHEST_TRANSFORMSTATE)
1755 {
1756 WARN("Unhandled transform state %#x.\n", state);
1757 return;
1758 }
1759
1760 mat = &device->updateStateBlock->state.transforms[state];
1761 multiply_matrix(&temp, mat, matrix);
1762
1763 /* Apply change via set transform - will reapply to eg. lights this way. */
1764 wined3d_device_set_transform(device, state, &temp);
1765}
1766
1767/* Note lights are real special cases. Although the device caps state only
1768 * e.g. 8 are supported, you can reference any indexes you want as long as
1769 * that number max are enabled at any one point in time. Therefore since the
1770 * indices can be anything, we need a hashmap of them. However, this causes
1771 * stateblock problems. When capturing the state block, I duplicate the
1772 * hashmap, but when recording, just build a chain pretty much of commands to
1773 * be replayed. */
1774HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1775 UINT light_idx, const struct wined3d_light *light)
1776{
1777 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1778 struct wined3d_light_info *object = NULL;
1779 struct list *e;
1780 float rho;
1781
1782 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1783
1784 /* Check the parameter range. Need for speed most wanted sets junk lights
1785 * which confuse the GL driver. */
1786 if (!light)
1787 return WINED3DERR_INVALIDCALL;
1788
1789 switch (light->type)
1790 {
1791 case WINED3D_LIGHT_POINT:
1792 case WINED3D_LIGHT_SPOT:
1793 case WINED3D_LIGHT_PARALLELPOINT:
1794 case WINED3D_LIGHT_GLSPOT:
1795 /* Incorrect attenuation values can cause the gl driver to crash.
1796 * Happens with Need for speed most wanted. */
1797 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1798 {
1799 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1800 return WINED3DERR_INVALIDCALL;
1801 }
1802 break;
1803
1804 case WINED3D_LIGHT_DIRECTIONAL:
1805 /* Ignores attenuation */
1806 break;
1807
1808 default:
1809 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1810 return WINED3DERR_INVALIDCALL;
1811 }
1812
1813 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
1814 {
1815 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1816 if (object->OriginalIndex == light_idx)
1817 break;
1818 object = NULL;
1819 }
1820
1821 if (!object)
1822 {
1823 TRACE("Adding new light\n");
1824 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1825 if (!object)
1826 return E_OUTOFMEMORY;
1827
1828 list_add_head(&device->updateStateBlock->state.light_map[hash_idx], &object->entry);
1829 object->glIndex = -1;
1830 object->OriginalIndex = light_idx;
1831 }
1832
1833 /* Initialize the object. */
1834 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1835 light_idx, light->type,
1836 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1837 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1838 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1839 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1840 light->direction.x, light->direction.y, light->direction.z);
1841 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1842 light->range, light->falloff, light->theta, light->phi);
1843
1844 /* Update the live definitions if the light is currently assigned a glIndex. */
1845 if (object->glIndex != -1 && !device->isRecordingState)
1846 {
1847 if (object->OriginalParms.type != light->type)
1848 device_invalidate_state(device, STATE_LIGHT_TYPE);
1849 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1850 }
1851
1852 /* Save away the information. */
1853 object->OriginalParms = *light;
1854
1855 switch (light->type)
1856 {
1857 case WINED3D_LIGHT_POINT:
1858 /* Position */
1859 object->lightPosn[0] = light->position.x;
1860 object->lightPosn[1] = light->position.y;
1861 object->lightPosn[2] = light->position.z;
1862 object->lightPosn[3] = 1.0f;
1863 object->cutoff = 180.0f;
1864 /* FIXME: Range */
1865 break;
1866
1867 case WINED3D_LIGHT_DIRECTIONAL:
1868 /* Direction */
1869 object->lightPosn[0] = -light->direction.x;
1870 object->lightPosn[1] = -light->direction.y;
1871 object->lightPosn[2] = -light->direction.z;
1872 object->lightPosn[3] = 0.0f;
1873 object->exponent = 0.0f;
1874 object->cutoff = 180.0f;
1875 break;
1876
1877 case WINED3D_LIGHT_SPOT:
1878 /* Position */
1879 object->lightPosn[0] = light->position.x;
1880 object->lightPosn[1] = light->position.y;
1881 object->lightPosn[2] = light->position.z;
1882 object->lightPosn[3] = 1.0f;
1883
1884 /* Direction */
1885 object->lightDirn[0] = light->direction.x;
1886 object->lightDirn[1] = light->direction.y;
1887 object->lightDirn[2] = light->direction.z;
1888 object->lightDirn[3] = 1.0f;
1889
1890 /* opengl-ish and d3d-ish spot lights use too different models
1891 * for the light "intensity" as a function of the angle towards
1892 * the main light direction, so we only can approximate very
1893 * roughly. However, spot lights are rather rarely used in games
1894 * (if ever used at all). Furthermore if still used, probably
1895 * nobody pays attention to such details. */
1896 if (!light->falloff)
1897 {
1898 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1899 * equations have the falloff resp. exponent parameter as an
1900 * exponent, so the spot light lighting will always be 1.0 for
1901 * both of them, and we don't have to care for the rest of the
1902 * rather complex calculation. */
1903 object->exponent = 0.0f;
1904 }
1905 else
1906 {
1907 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1908 if (rho < 0.0001f)
1909 rho = 0.0001f;
1910#ifdef VBOX_WITH_WINE_FIXES
1911 object->exponent = -0.3f / log(cos(rho / 2));
1912#else
1913 object->exponent = -0.3f / logf(cosf(rho / 2));
1914#endif
1915 }
1916
1917 if (object->exponent > 128.0f)
1918 object->exponent = 128.0f;
1919
1920 object->cutoff = (float)(light->phi * 90 / M_PI);
1921 /* FIXME: Range */
1922 break;
1923
1924 default:
1925 FIXME("Unrecognized light type %#x.\n", light->type);
1926 }
1927
1928 return WINED3D_OK;
1929}
1930
1931HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1932 UINT light_idx, struct wined3d_light *light)
1933{
1934 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1935 struct wined3d_light_info *light_info = NULL;
1936 struct list *e;
1937
1938 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1939
1940 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
1941 {
1942 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1943 if (light_info->OriginalIndex == light_idx)
1944 break;
1945 light_info = NULL;
1946 }
1947
1948 if (!light_info)
1949 {
1950 TRACE("Light information requested but light not defined\n");
1951 return WINED3DERR_INVALIDCALL;
1952 }
1953
1954 *light = light_info->OriginalParms;
1955 return WINED3D_OK;
1956}
1957
1958HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1959{
1960 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1961 struct wined3d_light_info *light_info = NULL;
1962 struct list *e;
1963
1964 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1965
1966 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
1967 {
1968 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1969 if (light_info->OriginalIndex == light_idx)
1970 break;
1971 light_info = NULL;
1972 }
1973 TRACE("Found light %p.\n", light_info);
1974
1975 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1976 if (!light_info)
1977 {
1978 TRACE("Light enabled requested but light not defined, so defining one!\n");
1979 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1980
1981 /* Search for it again! Should be fairly quick as near head of list. */
1982 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
1983 {
1984 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1985 if (light_info->OriginalIndex == light_idx)
1986 break;
1987 light_info = NULL;
1988 }
1989 if (!light_info)
1990 {
1991 FIXME("Adding default lights has failed dismally\n");
1992 return WINED3DERR_INVALIDCALL;
1993 }
1994 }
1995
1996 if (!enable)
1997 {
1998 if (light_info->glIndex != -1)
1999 {
2000 if (!device->isRecordingState)
2001 {
2002 device_invalidate_state(device, STATE_LIGHT_TYPE);
2003 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
2004 }
2005
2006 device->updateStateBlock->state.lights[light_info->glIndex] = NULL;
2007 light_info->glIndex = -1;
2008 }
2009 else
2010 {
2011 TRACE("Light already disabled, nothing to do\n");
2012 }
2013 light_info->enabled = FALSE;
2014 }
2015 else
2016 {
2017 light_info->enabled = TRUE;
2018 if (light_info->glIndex != -1)
2019 {
2020 TRACE("Nothing to do as light was enabled\n");
2021 }
2022 else
2023 {
2024 unsigned int i;
2025 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2026 /* Find a free GL light. */
2027 for (i = 0; i < gl_info->limits.lights; ++i)
2028 {
2029 if (!device->updateStateBlock->state.lights[i])
2030 {
2031 device->updateStateBlock->state.lights[i] = light_info;
2032 light_info->glIndex = i;
2033 break;
2034 }
2035 }
2036 if (light_info->glIndex == -1)
2037 {
2038 /* Our tests show that Windows returns D3D_OK in this situation, even with
2039 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
2040 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
2041 * as well for those lights.
2042 *
2043 * TODO: Test how this affects rendering. */
2044 WARN("Too many concurrently active lights\n");
2045 return WINED3D_OK;
2046 }
2047
2048 /* i == light_info->glIndex */
2049 if (!device->isRecordingState)
2050 {
2051 device_invalidate_state(device, STATE_LIGHT_TYPE);
2052 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
2053 }
2054 }
2055 }
2056
2057 return WINED3D_OK;
2058}
2059
2060HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
2061{
2062 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
2063 struct wined3d_light_info *light_info = NULL;
2064 struct list *e;
2065
2066 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
2067
2068 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
2069 {
2070 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2071 if (light_info->OriginalIndex == light_idx)
2072 break;
2073 light_info = NULL;
2074 }
2075
2076 if (!light_info)
2077 {
2078 TRACE("Light enabled state requested but light not defined.\n");
2079 return WINED3DERR_INVALIDCALL;
2080 }
2081 /* true is 128 according to SetLightEnable */
2082 *enable = light_info->enabled ? 128 : 0;
2083 return WINED3D_OK;
2084}
2085
2086HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
2087 UINT plane_idx, const struct wined3d_vec4 *plane)
2088{
2089 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2090
2091 /* Validate plane_idx. */
2092 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2093 {
2094 TRACE("Application has requested clipplane this device doesn't support.\n");
2095 return WINED3DERR_INVALIDCALL;
2096 }
2097
2098 device->updateStateBlock->changed.clipplane |= 1 << plane_idx;
2099
2100 if (!memcmp(&device->updateStateBlock->state.clip_planes[plane_idx], plane, sizeof(*plane)))
2101 {
2102 TRACE("Application is setting old values over, nothing to do.\n");
2103 return WINED3D_OK;
2104 }
2105
2106 device->updateStateBlock->state.clip_planes[plane_idx] = *plane;
2107
2108 /* Handle recording of state blocks. */
2109 if (device->isRecordingState)
2110 {
2111 TRACE("Recording... not performing anything.\n");
2112 return WINED3D_OK;
2113 }
2114
2115 device_invalidate_state(device, STATE_CLIPPLANE(plane_idx));
2116
2117 return WINED3D_OK;
2118}
2119
2120HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
2121 UINT plane_idx, struct wined3d_vec4 *plane)
2122{
2123 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2124
2125 /* Validate plane_idx. */
2126 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2127 {
2128 TRACE("Application has requested clipplane this device doesn't support.\n");
2129 return WINED3DERR_INVALIDCALL;
2130 }
2131
2132 *plane = device->stateBlock->state.clip_planes[plane_idx];
2133
2134 return WINED3D_OK;
2135}
2136
2137HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
2138 const struct wined3d_clip_status *clip_status)
2139{
2140 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2141
2142 if (!clip_status)
2143 return WINED3DERR_INVALIDCALL;
2144
2145 return WINED3D_OK;
2146}
2147
2148HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
2149 struct wined3d_clip_status *clip_status)
2150{
2151 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2152
2153 if (!clip_status)
2154 return WINED3DERR_INVALIDCALL;
2155
2156 return WINED3D_OK;
2157}
2158
2159void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
2160{
2161 TRACE("device %p, material %p.\n", device, material);
2162
2163 device->updateStateBlock->changed.material = TRUE;
2164 device->updateStateBlock->state.material = *material;
2165
2166 /* Handle recording of state blocks */
2167 if (device->isRecordingState)
2168 {
2169 TRACE("Recording... not performing anything.\n");
2170 return;
2171 }
2172
2173 device_invalidate_state(device, STATE_MATERIAL);
2174}
2175
2176void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
2177{
2178 TRACE("device %p, material %p.\n", device, material);
2179
2180 *material = device->updateStateBlock->state.material;
2181
2182 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
2183 material->diffuse.r, material->diffuse.g,
2184 material->diffuse.b, material->diffuse.a);
2185 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
2186 material->ambient.r, material->ambient.g,
2187 material->ambient.b, material->ambient.a);
2188 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
2189 material->specular.r, material->specular.g,
2190 material->specular.b, material->specular.a);
2191 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
2192 material->emissive.r, material->emissive.g,
2193 material->emissive.b, material->emissive.a);
2194 TRACE("power %.8e.\n", material->power);
2195}
2196
2197void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
2198 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
2199{
2200 struct wined3d_buffer *prev_buffer;
2201
2202 TRACE("device %p, buffer %p, format %s.\n",
2203 device, buffer, debug_d3dformat(format_id));
2204
2205 prev_buffer = device->updateStateBlock->state.index_buffer;
2206
2207 device->updateStateBlock->changed.indices = TRUE;
2208 device->updateStateBlock->state.index_buffer = buffer;
2209 device->updateStateBlock->state.index_format = format_id;
2210
2211 /* Handle recording of state blocks. */
2212 if (device->isRecordingState)
2213 {
2214 TRACE("Recording... not performing anything.\n");
2215 if (buffer)
2216 wined3d_buffer_incref(buffer);
2217 if (prev_buffer)
2218 wined3d_buffer_decref(prev_buffer);
2219 return;
2220 }
2221
2222 if (prev_buffer != buffer)
2223 {
2224 device_invalidate_state(device, STATE_INDEXBUFFER);
2225 if (buffer)
2226 {
2227 InterlockedIncrement(&buffer->resource.bind_count);
2228 wined3d_buffer_incref(buffer);
2229 }
2230 if (prev_buffer)
2231 {
2232 InterlockedDecrement(&prev_buffer->resource.bind_count);
2233 wined3d_buffer_decref(prev_buffer);
2234 }
2235 }
2236}
2237
2238struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
2239 enum wined3d_format_id *format)
2240{
2241 TRACE("device %p, format %p.\n", device, format);
2242
2243 *format = device->stateBlock->state.index_format;
2244 return device->stateBlock->state.index_buffer;
2245}
2246
2247void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
2248{
2249 TRACE("device %p, base_index %d.\n", device, base_index);
2250
2251 device->updateStateBlock->state.base_vertex_index = base_index;
2252}
2253
2254INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
2255{
2256 TRACE("device %p.\n", device);
2257
2258 return device->stateBlock->state.base_vertex_index;
2259}
2260
2261void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
2262{
2263 TRACE("device %p, viewport %p.\n", device, viewport);
2264 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
2265 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
2266
2267 device->updateStateBlock->changed.viewport = TRUE;
2268 device->updateStateBlock->state.viewport = *viewport;
2269
2270 /* Handle recording of state blocks */
2271 if (device->isRecordingState)
2272 {
2273 TRACE("Recording... not performing anything\n");
2274 return;
2275 }
2276
2277 device_invalidate_state(device, STATE_VIEWPORT);
2278}
2279
2280void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
2281{
2282 TRACE("device %p, viewport %p.\n", device, viewport);
2283
2284 *viewport = device->stateBlock->state.viewport;
2285}
2286
2287static void resolve_depth_buffer(struct wined3d_state *state)
2288{
2289 struct wined3d_texture *texture = state->textures[0];
2290 struct wined3d_surface *depth_stencil, *surface;
2291
2292 if (!texture || texture->resource.type != WINED3D_RTYPE_TEXTURE
2293 || !(texture->resource.format->flags & WINED3DFMT_FLAG_DEPTH))
2294 return;
2295 surface = surface_from_resource(texture->sub_resources[0]);
2296 depth_stencil = state->fb->depth_stencil;
2297 if (!depth_stencil)
2298 return;
2299
2300 wined3d_surface_blt(surface, NULL, depth_stencil, NULL, 0, NULL, WINED3D_TEXF_POINT);
2301}
2302
2303void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2304 enum wined3d_render_state state, DWORD value)
2305{
2306 DWORD old_value = device->stateBlock->state.render_states[state];
2307
2308 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2309
2310 device->updateStateBlock->changed.renderState[state >> 5] |= 1 << (state & 0x1f);
2311 device->updateStateBlock->state.render_states[state] = value;
2312
2313 /* Handle recording of state blocks. */
2314 if (device->isRecordingState)
2315 {
2316 TRACE("Recording... not performing anything.\n");
2317 return;
2318 }
2319
2320 /* Compared here and not before the assignment to allow proper stateblock recording. */
2321 if (value == old_value)
2322 TRACE("Application is setting the old value over, nothing to do.\n");
2323 else
2324 device_invalidate_state(device, STATE_RENDER(state));
2325
2326 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2327 {
2328 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2329 resolve_depth_buffer(&device->stateBlock->state);
2330 }
2331}
2332
2333DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2334{
2335 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2336
2337 return device->stateBlock->state.render_states[state];
2338}
2339
2340void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2341 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2342{
2343 DWORD old_value;
2344
2345 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2346 device, sampler_idx, debug_d3dsamplerstate(state), value);
2347
2348 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2349 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2350
2351 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2352 / sizeof(*device->stateBlock->state.sampler_states))
2353 {
2354 WARN("Invalid sampler %u.\n", sampler_idx);
2355 return; /* Windows accepts overflowing this array ... we do not. */
2356 }
2357
2358 old_value = device->stateBlock->state.sampler_states[sampler_idx][state];
2359 device->updateStateBlock->state.sampler_states[sampler_idx][state] = value;
2360 device->updateStateBlock->changed.samplerState[sampler_idx] |= 1 << state;
2361
2362 /* Handle recording of state blocks. */
2363 if (device->isRecordingState)
2364 {
2365 TRACE("Recording... not performing anything.\n");
2366 return;
2367 }
2368
2369 if (old_value == value)
2370 {
2371 TRACE("Application is setting the old value over, nothing to do.\n");
2372 return;
2373 }
2374
2375 device_invalidate_state(device, STATE_SAMPLER(sampler_idx));
2376}
2377
2378DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2379 UINT sampler_idx, enum wined3d_sampler_state state)
2380{
2381 TRACE("device %p, sampler_idx %u, state %s.\n",
2382 device, sampler_idx, debug_d3dsamplerstate(state));
2383
2384 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2385 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2386
2387 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2388 / sizeof(*device->stateBlock->state.sampler_states))
2389 {
2390 WARN("Invalid sampler %u.\n", sampler_idx);
2391 return 0; /* Windows accepts overflowing this array ... we do not. */
2392 }
2393
2394 return device->stateBlock->state.sampler_states[sampler_idx][state];
2395}
2396
2397void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2398{
2399 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2400
2401 device->updateStateBlock->changed.scissorRect = TRUE;
2402 if (EqualRect(&device->updateStateBlock->state.scissor_rect, rect))
2403 {
2404 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2405 return;
2406 }
2407 CopyRect(&device->updateStateBlock->state.scissor_rect, rect);
2408
2409 if (device->isRecordingState)
2410 {
2411 TRACE("Recording... not performing anything.\n");
2412 return;
2413 }
2414
2415 device_invalidate_state(device, STATE_SCISSORRECT);
2416}
2417
2418void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2419{
2420 TRACE("device %p, rect %p.\n", device, rect);
2421
2422 *rect = device->updateStateBlock->state.scissor_rect;
2423 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2424}
2425
2426void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2427 struct wined3d_vertex_declaration *declaration)
2428{
2429 struct wined3d_vertex_declaration *prev = device->updateStateBlock->state.vertex_declaration;
2430
2431 TRACE("device %p, declaration %p.\n", device, declaration);
2432
2433 if (declaration)
2434 wined3d_vertex_declaration_incref(declaration);
2435 if (prev)
2436 wined3d_vertex_declaration_decref(prev);
2437
2438 device->updateStateBlock->state.vertex_declaration = declaration;
2439 device->updateStateBlock->changed.vertexDecl = TRUE;
2440
2441 if (device->isRecordingState)
2442 {
2443 TRACE("Recording... not performing anything.\n");
2444 return;
2445 }
2446
2447 if (declaration == prev)
2448 {
2449 /* Checked after the assignment to allow proper stateblock recording. */
2450 TRACE("Application is setting the old declaration over, nothing to do.\n");
2451 return;
2452 }
2453
2454 device_invalidate_state(device, STATE_VDECL);
2455}
2456
2457struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2458{
2459 TRACE("device %p.\n", device);
2460
2461 return device->stateBlock->state.vertex_declaration;
2462}
2463
2464void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2465{
2466 struct wined3d_shader *prev = device->updateStateBlock->state.vertex_shader;
2467
2468 TRACE("device %p, shader %p.\n", device, shader);
2469
2470 if (shader)
2471 wined3d_shader_incref(shader);
2472 if (prev)
2473 wined3d_shader_decref(prev);
2474
2475 device->updateStateBlock->state.vertex_shader = shader;
2476 device->updateStateBlock->changed.vertexShader = TRUE;
2477
2478 if (device->isRecordingState)
2479 {
2480 TRACE("Recording... not performing anything.\n");
2481 return;
2482 }
2483
2484 if (shader == prev)
2485 {
2486 TRACE("Application is setting the old shader over, nothing to do.\n");
2487 return;
2488 }
2489
2490 device_invalidate_state(device, STATE_VSHADER);
2491}
2492
2493struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2494{
2495 TRACE("device %p.\n", device);
2496
2497 return device->stateBlock->state.vertex_shader;
2498}
2499
2500void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2501{
2502 struct wined3d_buffer *prev;
2503
2504 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2505
2506 if (idx >= MAX_CONSTANT_BUFFERS)
2507 {
2508 WARN("Invalid constant buffer index %u.\n", idx);
2509 return;
2510 }
2511
2512 prev = device->updateStateBlock->state.vs_cb[idx];
2513 device->updateStateBlock->state.vs_cb[idx] = buffer;
2514
2515 if (device->isRecordingState)
2516 {
2517 if (buffer)
2518 wined3d_buffer_incref(buffer);
2519 if (prev)
2520 wined3d_buffer_decref(prev);
2521 return;
2522 }
2523
2524 if (prev != buffer)
2525 {
2526 if (buffer)
2527 {
2528 InterlockedIncrement(&buffer->resource.bind_count);
2529 wined3d_buffer_incref(buffer);
2530 }
2531 if (prev)
2532 {
2533 InterlockedDecrement(&prev->resource.bind_count);
2534 wined3d_buffer_decref(prev);
2535 }
2536 }
2537}
2538
2539struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2540{
2541 TRACE("device %p, idx %u.\n", device, idx);
2542
2543 if (idx >= MAX_CONSTANT_BUFFERS)
2544 {
2545 WARN("Invalid constant buffer index %u.\n", idx);
2546 return NULL;
2547 }
2548
2549 return device->stateBlock->state.vs_cb[idx];
2550}
2551
2552void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2553{
2554 struct wined3d_sampler *prev;
2555
2556 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2557
2558 if (idx >= MAX_SAMPLER_OBJECTS)
2559 {
2560 WARN("Invalid sampler index %u.\n", idx);
2561 return;
2562 }
2563
2564 prev = device->updateStateBlock->state.vs_sampler[idx];
2565 device->updateStateBlock->state.vs_sampler[idx] = sampler;
2566
2567 if (sampler)
2568 wined3d_sampler_incref(sampler);
2569 if (prev)
2570 wined3d_sampler_decref(prev);
2571}
2572
2573struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2574{
2575 TRACE("device %p, idx %u.\n", device, idx);
2576
2577 if (idx >= MAX_SAMPLER_OBJECTS)
2578 {
2579 WARN("Invalid sampler index %u.\n", idx);
2580 return NULL;
2581 }
2582
2583 return device->stateBlock->state.vs_sampler[idx];
2584}
2585
2586HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2587 UINT start_register, const BOOL *constants, UINT bool_count)
2588{
2589 UINT count = min(bool_count, MAX_CONST_B - start_register);
2590 UINT i;
2591
2592 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2593 device, start_register, constants, bool_count);
2594
2595 if (!constants || start_register >= MAX_CONST_B)
2596 return WINED3DERR_INVALIDCALL;
2597
2598 memcpy(&device->updateStateBlock->state.vs_consts_b[start_register], constants, count * sizeof(BOOL));
2599 for (i = 0; i < count; ++i)
2600 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2601
2602 for (i = start_register; i < count + start_register; ++i)
2603 device->updateStateBlock->changed.vertexShaderConstantsB |= (1 << i);
2604
2605 if (!device->isRecordingState)
2606 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2607
2608 return WINED3D_OK;
2609}
2610
2611HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2612 UINT start_register, BOOL *constants, UINT bool_count)
2613{
2614 UINT count = min(bool_count, MAX_CONST_B - start_register);
2615
2616 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2617 device, start_register, constants, bool_count);
2618
2619 if (!constants || start_register >= MAX_CONST_B)
2620 return WINED3DERR_INVALIDCALL;
2621
2622 memcpy(constants, &device->stateBlock->state.vs_consts_b[start_register], count * sizeof(BOOL));
2623
2624 return WINED3D_OK;
2625}
2626
2627HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2628 UINT start_register, const int *constants, UINT vector4i_count)
2629{
2630 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2631 UINT i;
2632
2633 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2634 device, start_register, constants, vector4i_count);
2635
2636 if (!constants || start_register >= MAX_CONST_I)
2637 return WINED3DERR_INVALIDCALL;
2638
2639 memcpy(&device->updateStateBlock->state.vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2640 for (i = 0; i < count; ++i)
2641 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2642 constants[i * 4], constants[i * 4 + 1],
2643 constants[i * 4 + 2], constants[i * 4 + 3]);
2644
2645 for (i = start_register; i < count + start_register; ++i)
2646 device->updateStateBlock->changed.vertexShaderConstantsI |= (1 << i);
2647
2648 if (!device->isRecordingState)
2649 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2650
2651 return WINED3D_OK;
2652}
2653
2654HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2655 UINT start_register, int *constants, UINT vector4i_count)
2656{
2657 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2658
2659 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2660 device, start_register, constants, vector4i_count);
2661
2662 if (!constants || start_register >= MAX_CONST_I)
2663 return WINED3DERR_INVALIDCALL;
2664
2665 memcpy(constants, &device->stateBlock->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2666 return WINED3D_OK;
2667}
2668
2669HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2670 UINT start_register, const float *constants, UINT vector4f_count)
2671{
2672 UINT i;
2673 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2674
2675 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2676 device, start_register, constants, vector4f_count);
2677
2678 /* Specifically test start_register > limit to catch MAX_UINT overflows
2679 * when adding start_register + vector4f_count. */
2680 if (!constants
2681 || start_register + vector4f_count > d3d_info->limits.vs_uniform_count
2682 || start_register > d3d_info->limits.vs_uniform_count)
2683 return WINED3DERR_INVALIDCALL;
2684
2685 memcpy(&device->updateStateBlock->state.vs_consts_f[start_register * 4],
2686 constants, vector4f_count * sizeof(float) * 4);
2687 if (TRACE_ON(d3d))
2688 {
2689 for (i = 0; i < vector4f_count; ++i)
2690 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2691 constants[i * 4], constants[i * 4 + 1],
2692 constants[i * 4 + 2], constants[i * 4 + 3]);
2693 }
2694
2695 if (!device->isRecordingState)
2696 {
2697 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2698 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2699 }
2700
2701 memset(device->updateStateBlock->changed.vertexShaderConstantsF + start_register, 1,
2702 sizeof(*device->updateStateBlock->changed.vertexShaderConstantsF) * vector4f_count);
2703
2704 return WINED3D_OK;
2705}
2706
2707HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2708 UINT start_register, float *constants, UINT vector4f_count)
2709{
2710 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2711 int count = min(vector4f_count, d3d_info->limits.vs_uniform_count - start_register);
2712
2713 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2714 device, start_register, constants, vector4f_count);
2715
2716 if (!constants || count < 0)
2717 return WINED3DERR_INVALIDCALL;
2718
2719 memcpy(constants, &device->stateBlock->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2720
2721 return WINED3D_OK;
2722}
2723
2724static void device_invalidate_texture_stage(const struct wined3d_device *device, DWORD stage)
2725{
2726 DWORD i;
2727
2728 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
2729 {
2730 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, i));
2731 }
2732}
2733
2734static void device_map_stage(struct wined3d_device *device, DWORD stage, DWORD unit)
2735{
2736 DWORD i = device->rev_tex_unit_map[unit];
2737 DWORD j = device->texUnitMap[stage];
2738
2739 device->texUnitMap[stage] = unit;
2740 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
2741 device->texUnitMap[i] = WINED3D_UNMAPPED_STAGE;
2742
2743 device->rev_tex_unit_map[unit] = stage;
2744 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
2745 device->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
2746}
2747
2748static void device_update_fixed_function_usage_map(struct wined3d_device *device)
2749{
2750 UINT i;
2751
2752 device->fixed_function_usage_map = 0;
2753 for (i = 0; i < MAX_TEXTURES; ++i)
2754 {
2755 const struct wined3d_state *state = &device->stateBlock->state;
2756 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
2757 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
2758 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
2759 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
2760 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
2761 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
2762 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
2763 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
2764
2765 /* Not used, and disable higher stages. */
2766 if (color_op == WINED3D_TOP_DISABLE)
2767 break;
2768
2769 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
2770 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
2771 || ((color_arg3 == WINED3DTA_TEXTURE)
2772 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
2773 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
2774 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
2775 || ((alpha_arg3 == WINED3DTA_TEXTURE)
2776 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
2777 device->fixed_function_usage_map |= (1 << i);
2778
2779 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
2780 && i < MAX_TEXTURES - 1)
2781 device->fixed_function_usage_map |= (1 << (i + 1));
2782 }
2783}
2784
2785static void device_map_fixed_function_samplers(struct wined3d_device *device, const struct wined3d_d3d_info *d3d_info)
2786{
2787 unsigned int i, tex;
2788 WORD ffu_map;
2789
2790 device_update_fixed_function_usage_map(device);
2791 ffu_map = device->fixed_function_usage_map;
2792
2793 if (d3d_info->limits.ffp_textures == d3d_info->limits.ffp_blend_stages
2794 || device->stateBlock->state.lowest_disabled_stage <= d3d_info->limits.ffp_textures)
2795 {
2796 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2797 {
2798 if (!(ffu_map & 1)) continue;
2799
2800 if (device->texUnitMap[i] != i)
2801 {
2802 device_map_stage(device, i, i);
2803 device_invalidate_state(device, STATE_SAMPLER(i));
2804 device_invalidate_texture_stage(device, i);
2805 }
2806 }
2807 return;
2808 }
2809
2810 /* Now work out the mapping */
2811 tex = 0;
2812 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2813 {
2814 if (!(ffu_map & 1)) continue;
2815
2816 if (device->texUnitMap[i] != tex)
2817 {
2818 device_map_stage(device, i, tex);
2819 device_invalidate_state(device, STATE_SAMPLER(i));
2820 device_invalidate_texture_stage(device, i);
2821 }
2822
2823 ++tex;
2824 }
2825}
2826
2827static void device_map_psamplers(struct wined3d_device *device, const struct wined3d_d3d_info *d3d_info)
2828{
2829 const enum wined3d_sampler_texture_type *sampler_type =
2830 device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2831 unsigned int i;
2832
2833 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2834 {
2835 if (sampler_type[i] && device->texUnitMap[i] != i)
2836 {
2837 device_map_stage(device, i, i);
2838 device_invalidate_state(device, STATE_SAMPLER(i));
2839 if (i < d3d_info->limits.ffp_blend_stages)
2840 device_invalidate_texture_stage(device, i);
2841 }
2842 }
2843}
2844
2845static BOOL device_unit_free_for_vs(const struct wined3d_device *device,
2846 const enum wined3d_sampler_texture_type *pshader_sampler_tokens,
2847 const enum wined3d_sampler_texture_type *vshader_sampler_tokens, DWORD unit)
2848{
2849 DWORD current_mapping = device->rev_tex_unit_map[unit];
2850
2851 /* Not currently used */
2852 if (current_mapping == WINED3D_UNMAPPED_STAGE) return TRUE;
2853
2854 if (current_mapping < MAX_FRAGMENT_SAMPLERS) {
2855 /* Used by a fragment sampler */
2856
2857 if (!pshader_sampler_tokens) {
2858 /* No pixel shader, check fixed function */
2859 return current_mapping >= MAX_TEXTURES || !(device->fixed_function_usage_map & (1 << current_mapping));
2860 }
2861
2862 /* Pixel shader, check the shader's sampler map */
2863 return !pshader_sampler_tokens[current_mapping];
2864 }
2865
2866 /* Used by a vertex sampler */
2867 return !vshader_sampler_tokens[current_mapping - MAX_FRAGMENT_SAMPLERS];
2868}
2869
2870static void device_map_vsamplers(struct wined3d_device *device, BOOL ps, const struct wined3d_gl_info *gl_info)
2871{
2872 const enum wined3d_sampler_texture_type *vshader_sampler_type =
2873 device->stateBlock->state.vertex_shader->reg_maps.sampler_type;
2874 const enum wined3d_sampler_texture_type *pshader_sampler_type = NULL;
2875 int start = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers) - 1;
2876 int i;
2877
2878 if (ps)
2879 {
2880 /* Note that we only care if a sampler is sampled or not, not the sampler's specific type.
2881 * Otherwise we'd need to call shader_update_samplers() here for 1.x pixelshaders. */
2882 pshader_sampler_type = device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2883 }
2884
2885 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
2886 DWORD vsampler_idx = i + MAX_FRAGMENT_SAMPLERS;
2887 if (vshader_sampler_type[i])
2888 {
2889 if (device->texUnitMap[vsampler_idx] != WINED3D_UNMAPPED_STAGE)
2890 {
2891 /* Already mapped somewhere */
2892 continue;
2893 }
2894
2895 while (start >= 0)
2896 {
2897 if (device_unit_free_for_vs(device, pshader_sampler_type, vshader_sampler_type, start))
2898 {
2899 device_map_stage(device, vsampler_idx, start);
2900 device_invalidate_state(device, STATE_SAMPLER(vsampler_idx));
2901
2902 --start;
2903 break;
2904 }
2905
2906 --start;
2907 }
2908 }
2909 }
2910}
2911
2912void device_update_tex_unit_map(struct wined3d_device *device)
2913{
2914 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2915 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2916 const struct wined3d_state *state = &device->stateBlock->state;
2917 BOOL vs = use_vs(state);
2918 BOOL ps = use_ps(state);
2919 /*
2920 * Rules are:
2921 * -> Pixel shaders need a 1:1 map. In theory the shader input could be mapped too, but
2922 * that would be really messy and require shader recompilation
2923 * -> When the mapping of a stage is changed, sampler and ALL texture stage states have
2924 * to be reset. Because of that try to work with a 1:1 mapping as much as possible
2925 */
2926 if (ps)
2927 device_map_psamplers(device, d3d_info);
2928 else
2929 device_map_fixed_function_samplers(device, d3d_info);
2930
2931 if (vs)
2932 device_map_vsamplers(device, ps, gl_info);
2933}
2934
2935void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2936{
2937 struct wined3d_shader *prev = device->updateStateBlock->state.pixel_shader;
2938
2939 TRACE("device %p, shader %p.\n", device, shader);
2940
2941 if (shader)
2942 wined3d_shader_incref(shader);
2943 if (prev)
2944 wined3d_shader_decref(prev);
2945
2946 device->updateStateBlock->state.pixel_shader = shader;
2947 device->updateStateBlock->changed.pixelShader = TRUE;
2948
2949 if (device->isRecordingState)
2950 {
2951 TRACE("Recording... not performing anything.\n");
2952 return;
2953 }
2954
2955 if (shader == prev)
2956 {
2957 TRACE("Application is setting the old shader over, nothing to do.\n");
2958 return;
2959 }
2960
2961 device_invalidate_state(device, STATE_PIXELSHADER);
2962}
2963
2964struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2965{
2966 TRACE("device %p.\n", device);
2967
2968 return device->stateBlock->state.pixel_shader;
2969}
2970
2971void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2972{
2973 struct wined3d_buffer *prev;
2974
2975 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2976
2977 if (idx >= MAX_CONSTANT_BUFFERS)
2978 {
2979 WARN("Invalid constant buffer index %u.\n", idx);
2980 return;
2981 }
2982
2983 prev = device->updateStateBlock->state.ps_cb[idx];
2984 device->updateStateBlock->state.ps_cb[idx] = buffer;
2985
2986 if (device->isRecordingState)
2987 {
2988 if (buffer)
2989 wined3d_buffer_incref(buffer);
2990 if (prev)
2991 wined3d_buffer_decref(prev);
2992 return;
2993 }
2994
2995 if (prev != buffer)
2996 {
2997 if (buffer)
2998 {
2999 InterlockedIncrement(&buffer->resource.bind_count);
3000 wined3d_buffer_incref(buffer);
3001 }
3002 if (prev)
3003 {
3004 InterlockedDecrement(&prev->resource.bind_count);
3005 wined3d_buffer_decref(prev);
3006 }
3007 }
3008}
3009
3010struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
3011{
3012 TRACE("device %p, idx %u.\n", device, idx);
3013
3014 if (idx >= MAX_CONSTANT_BUFFERS)
3015 {
3016 WARN("Invalid constant buffer index %u.\n", idx);
3017 return NULL;
3018 }
3019
3020 return device->stateBlock->state.ps_cb[idx];
3021}
3022
3023void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
3024{
3025 struct wined3d_sampler *prev;
3026
3027 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
3028
3029 if (idx >= MAX_SAMPLER_OBJECTS)
3030 {
3031 WARN("Invalid sampler index %u.\n", idx);
3032 return;
3033 }
3034
3035 prev = device->updateStateBlock->state.ps_sampler[idx];
3036 device->updateStateBlock->state.ps_sampler[idx] = sampler;
3037
3038 if (sampler)
3039 wined3d_sampler_incref(sampler);
3040 if (prev)
3041 wined3d_sampler_decref(prev);
3042}
3043
3044struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
3045{
3046 TRACE("device %p, idx %u.\n", device, idx);
3047
3048 if (idx >= MAX_SAMPLER_OBJECTS)
3049 {
3050 WARN("Invalid sampler index %u.\n", idx);
3051 return NULL;
3052 }
3053
3054 return device->stateBlock->state.ps_sampler[idx];
3055}
3056
3057HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
3058 UINT start_register, const BOOL *constants, UINT bool_count)
3059{
3060 UINT count = min(bool_count, MAX_CONST_B - start_register);
3061 UINT i;
3062
3063 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
3064 device, start_register, constants, bool_count);
3065
3066 if (!constants || start_register >= MAX_CONST_B)
3067 return WINED3DERR_INVALIDCALL;
3068
3069 memcpy(&device->updateStateBlock->state.ps_consts_b[start_register], constants, count * sizeof(BOOL));
3070 for (i = 0; i < count; ++i)
3071 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
3072
3073 for (i = start_register; i < count + start_register; ++i)
3074 device->updateStateBlock->changed.pixelShaderConstantsB |= (1 << i);
3075
3076 if (!device->isRecordingState)
3077 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3078
3079 return WINED3D_OK;
3080}
3081
3082HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
3083 UINT start_register, BOOL *constants, UINT bool_count)
3084{
3085 UINT count = min(bool_count, MAX_CONST_B - start_register);
3086
3087 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
3088 device, start_register, constants, bool_count);
3089
3090 if (!constants || start_register >= MAX_CONST_B)
3091 return WINED3DERR_INVALIDCALL;
3092
3093 memcpy(constants, &device->stateBlock->state.ps_consts_b[start_register], count * sizeof(BOOL));
3094
3095 return WINED3D_OK;
3096}
3097
3098HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
3099 UINT start_register, const int *constants, UINT vector4i_count)
3100{
3101 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
3102 UINT i;
3103
3104 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3105 device, start_register, constants, vector4i_count);
3106
3107 if (!constants || start_register >= MAX_CONST_I)
3108 return WINED3DERR_INVALIDCALL;
3109
3110 memcpy(&device->updateStateBlock->state.ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
3111 for (i = 0; i < count; ++i)
3112 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
3113 constants[i * 4], constants[i * 4 + 1],
3114 constants[i * 4 + 2], constants[i * 4 + 3]);
3115
3116 for (i = start_register; i < count + start_register; ++i)
3117 device->updateStateBlock->changed.pixelShaderConstantsI |= (1 << i);
3118
3119 if (!device->isRecordingState)
3120 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3121
3122 return WINED3D_OK;
3123}
3124
3125HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
3126 UINT start_register, int *constants, UINT vector4i_count)
3127{
3128 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
3129
3130 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3131 device, start_register, constants, vector4i_count);
3132
3133 if (!constants || start_register >= MAX_CONST_I)
3134 return WINED3DERR_INVALIDCALL;
3135
3136 memcpy(constants, &device->stateBlock->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
3137
3138 return WINED3D_OK;
3139}
3140
3141HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
3142 UINT start_register, const float *constants, UINT vector4f_count)
3143{
3144 UINT i;
3145 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3146
3147 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3148 device, start_register, constants, vector4f_count);
3149
3150 /* Specifically test start_register > limit to catch MAX_UINT overflows
3151 * when adding start_register + vector4f_count. */
3152 if (!constants
3153 || start_register + vector4f_count > d3d_info->limits.ps_uniform_count
3154 || start_register > d3d_info->limits.ps_uniform_count)
3155 return WINED3DERR_INVALIDCALL;
3156
3157 memcpy(&device->updateStateBlock->state.ps_consts_f[start_register * 4],
3158 constants, vector4f_count * sizeof(float) * 4);
3159 if (TRACE_ON(d3d))
3160 {
3161 for (i = 0; i < vector4f_count; ++i)
3162 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
3163 constants[i * 4], constants[i * 4 + 1],
3164 constants[i * 4 + 2], constants[i * 4 + 3]);
3165 }
3166
3167 if (!device->isRecordingState)
3168 {
3169 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
3170 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3171 }
3172
3173 memset(device->updateStateBlock->changed.pixelShaderConstantsF + start_register, 1,
3174 sizeof(*device->updateStateBlock->changed.pixelShaderConstantsF) * vector4f_count);
3175
3176 return WINED3D_OK;
3177}
3178
3179HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
3180 UINT start_register, float *constants, UINT vector4f_count)
3181{
3182 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3183 int count = min(vector4f_count, d3d_info->limits.ps_uniform_count - start_register);
3184
3185 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3186 device, start_register, constants, vector4f_count);
3187
3188 if (!constants || count < 0)
3189 return WINED3DERR_INVALIDCALL;
3190
3191 memcpy(constants, &device->stateBlock->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
3192
3193 return WINED3D_OK;
3194}
3195
3196void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
3197{
3198 struct wined3d_shader *prev = device->updateStateBlock->state.geometry_shader;
3199
3200 TRACE("device %p, shader %p.\n", device, shader);
3201
3202 if (shader)
3203 wined3d_shader_incref(shader);
3204 if (prev)
3205 wined3d_shader_decref(prev);
3206
3207 device->updateStateBlock->state.geometry_shader = shader;
3208
3209 if (device->isRecordingState || shader == prev)
3210 return;
3211
3212 device_invalidate_state(device, STATE_GEOMETRY_SHADER);
3213}
3214
3215struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
3216{
3217 TRACE("device %p.\n", device);
3218
3219 return device->stateBlock->state.geometry_shader;
3220}
3221
3222void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
3223{
3224 struct wined3d_buffer *prev;
3225
3226 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
3227
3228 if (idx >= MAX_CONSTANT_BUFFERS)
3229 {
3230 WARN("Invalid constant buffer index %u.\n", idx);
3231 return;
3232 }
3233
3234 prev = device->updateStateBlock->state.gs_cb[idx];
3235 device->updateStateBlock->state.gs_cb[idx] = buffer;
3236
3237 if (device->isRecordingState)
3238 {
3239 if (buffer)
3240 wined3d_buffer_incref(buffer);
3241 if (prev)
3242 wined3d_buffer_decref(prev);
3243 return;
3244 }
3245
3246 if (prev != buffer)
3247 {
3248 if (buffer)
3249 {
3250 InterlockedIncrement(&buffer->resource.bind_count);
3251 wined3d_buffer_incref(buffer);
3252 }
3253 if (prev)
3254 {
3255 InterlockedDecrement(&prev->resource.bind_count);
3256 wined3d_buffer_decref(prev);
3257 }
3258 }
3259}
3260
3261struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
3262{
3263 TRACE("device %p, idx %u.\n", device, idx);
3264
3265 if (idx >= MAX_CONSTANT_BUFFERS)
3266 {
3267 WARN("Invalid constant buffer index %u.\n", idx);
3268 return NULL;
3269 }
3270
3271 return device->stateBlock->state.gs_cb[idx];
3272}
3273
3274void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
3275{
3276 struct wined3d_sampler *prev;
3277
3278 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
3279
3280 if (idx >= MAX_SAMPLER_OBJECTS)
3281 {
3282 WARN("Invalid sampler index %u.\n", idx);
3283 return;
3284 }
3285
3286 prev = device->updateStateBlock->state.gs_sampler[idx];
3287 device->updateStateBlock->state.gs_sampler[idx] = sampler;
3288
3289 if (sampler)
3290 wined3d_sampler_incref(sampler);
3291 if (prev)
3292 wined3d_sampler_decref(prev);
3293}
3294
3295struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
3296{
3297 TRACE("device %p, idx %u.\n", device, idx);
3298
3299 if (idx >= MAX_SAMPLER_OBJECTS)
3300 {
3301 WARN("Invalid sampler index %u.\n", idx);
3302 return NULL;
3303 }
3304
3305 return device->stateBlock->state.gs_sampler[idx];
3306}
3307
3308/* Context activation is done by the caller. */
3309/* Do not call while under the GL lock. */
3310#define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3311static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3312 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
3313 DWORD DestFVF)
3314{
3315 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3316 struct wined3d_viewport vp;
3317 UINT vertex_size;
3318 unsigned int i;
3319 BYTE *dest_ptr;
3320 BOOL doClip;
3321 DWORD numTextures;
3322 HRESULT hr;
3323
3324 if (stream_info->use_map & (1 << WINED3D_FFP_NORMAL))
3325 {
3326 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3327 }
3328
3329 if (!(stream_info->use_map & (1 << WINED3D_FFP_POSITION)))
3330 {
3331 ERR("Source has no position mask\n");
3332 return WINED3DERR_INVALIDCALL;
3333 }
3334
3335 if (device->stateBlock->state.render_states[WINED3D_RS_CLIPPING])
3336 {
3337 static BOOL warned = FALSE;
3338 /*
3339 * The clipping code is not quite correct. Some things need
3340 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3341 * so disable clipping for now.
3342 * (The graphics in Half-Life are broken, and my processvertices
3343 * test crashes with IDirect3DDevice3)
3344 doClip = TRUE;
3345 */
3346 doClip = FALSE;
3347 if(!warned) {
3348 warned = TRUE;
3349 FIXME("Clipping is broken and disabled for now\n");
3350 }
3351 }
3352 else
3353 doClip = FALSE;
3354
3355 vertex_size = get_flexible_vertex_size(DestFVF);
3356 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
3357 {
3358 WARN("Failed to map buffer, hr %#x.\n", hr);
3359 return hr;
3360 }
3361
3362 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3363 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3364 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3365
3366 TRACE("View mat:\n");
3367 TRACE("%f %f %f %f\n", view_mat.u.s._11, view_mat.u.s._12, view_mat.u.s._13, view_mat.u.s._14);
3368 TRACE("%f %f %f %f\n", view_mat.u.s._21, view_mat.u.s._22, view_mat.u.s._23, view_mat.u.s._24);
3369 TRACE("%f %f %f %f\n", view_mat.u.s._31, view_mat.u.s._32, view_mat.u.s._33, view_mat.u.s._34);
3370 TRACE("%f %f %f %f\n", view_mat.u.s._41, view_mat.u.s._42, view_mat.u.s._43, view_mat.u.s._44);
3371
3372 TRACE("Proj mat:\n");
3373 TRACE("%f %f %f %f\n", proj_mat.u.s._11, proj_mat.u.s._12, proj_mat.u.s._13, proj_mat.u.s._14);
3374 TRACE("%f %f %f %f\n", proj_mat.u.s._21, proj_mat.u.s._22, proj_mat.u.s._23, proj_mat.u.s._24);
3375 TRACE("%f %f %f %f\n", proj_mat.u.s._31, proj_mat.u.s._32, proj_mat.u.s._33, proj_mat.u.s._34);
3376 TRACE("%f %f %f %f\n", proj_mat.u.s._41, proj_mat.u.s._42, proj_mat.u.s._43, proj_mat.u.s._44);
3377
3378 TRACE("World mat:\n");
3379 TRACE("%f %f %f %f\n", world_mat.u.s._11, world_mat.u.s._12, world_mat.u.s._13, world_mat.u.s._14);
3380 TRACE("%f %f %f %f\n", world_mat.u.s._21, world_mat.u.s._22, world_mat.u.s._23, world_mat.u.s._24);
3381 TRACE("%f %f %f %f\n", world_mat.u.s._31, world_mat.u.s._32, world_mat.u.s._33, world_mat.u.s._34);
3382 TRACE("%f %f %f %f\n", world_mat.u.s._41, world_mat.u.s._42, world_mat.u.s._43, world_mat.u.s._44);
3383
3384 /* Get the viewport */
3385 wined3d_device_get_viewport(device, &vp);
3386 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
3387 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3388
3389 multiply_matrix(&mat,&view_mat,&world_mat);
3390 multiply_matrix(&mat,&proj_mat,&mat);
3391
3392 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3393
3394 for (i = 0; i < dwCount; i+= 1) {
3395 unsigned int tex_index;
3396
3397 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3398 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3399 /* The position first */
3400 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
3401 const float *p = (const float *)(element->data.addr + i * element->stride);
3402 float x, y, z, rhw;
3403 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3404
3405 /* Multiplication with world, view and projection matrix */
3406 x = (p[0] * mat.u.s._11) + (p[1] * mat.u.s._21) + (p[2] * mat.u.s._31) + (1.0f * mat.u.s._41);
3407 y = (p[0] * mat.u.s._12) + (p[1] * mat.u.s._22) + (p[2] * mat.u.s._32) + (1.0f * mat.u.s._42);
3408 z = (p[0] * mat.u.s._13) + (p[1] * mat.u.s._23) + (p[2] * mat.u.s._33) + (1.0f * mat.u.s._43);
3409 rhw = (p[0] * mat.u.s._14) + (p[1] * mat.u.s._24) + (p[2] * mat.u.s._34) + (1.0f * mat.u.s._44);
3410
3411 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3412
3413 /* WARNING: The following things are taken from d3d7 and were not yet checked
3414 * against d3d8 or d3d9!
3415 */
3416
3417 /* Clipping conditions: From msdn
3418 *
3419 * A vertex is clipped if it does not match the following requirements
3420 * -rhw < x <= rhw
3421 * -rhw < y <= rhw
3422 * 0 < z <= rhw
3423 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3424 *
3425 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3426 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3427 *
3428 */
3429
3430 if( !doClip ||
3431 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3432 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3433 ( rhw > eps ) ) ) {
3434
3435 /* "Normal" viewport transformation (not clipped)
3436 * 1) The values are divided by rhw
3437 * 2) The y axis is negative, so multiply it with -1
3438 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3439 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3440 * 4) Multiply x with Width/2 and add Width/2
3441 * 5) The same for the height
3442 * 6) Add the viewpoint X and Y to the 2D coordinates and
3443 * The minimum Z value to z
3444 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3445 *
3446 * Well, basically it's simply a linear transformation into viewport
3447 * coordinates
3448 */
3449
3450 x /= rhw;
3451 y /= rhw;
3452 z /= rhw;
3453
3454 y *= -1;
3455
3456 x *= vp.width / 2;
3457 y *= vp.height / 2;
3458 z *= vp.max_z - vp.min_z;
3459
3460 x += vp.width / 2 + vp.x;
3461 y += vp.height / 2 + vp.y;
3462 z += vp.min_z;
3463
3464 rhw = 1 / rhw;
3465 } else {
3466 /* That vertex got clipped
3467 * Contrary to OpenGL it is not dropped completely, it just
3468 * undergoes a different calculation.
3469 */
3470 TRACE("Vertex got clipped\n");
3471 x += rhw;
3472 y += rhw;
3473
3474 x /= 2;
3475 y /= 2;
3476
3477 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3478 * outside of the main vertex buffer memory. That needs some more
3479 * investigation...
3480 */
3481 }
3482
3483 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3484
3485
3486 ( (float *) dest_ptr)[0] = x;
3487 ( (float *) dest_ptr)[1] = y;
3488 ( (float *) dest_ptr)[2] = z;
3489 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3490
3491 dest_ptr += 3 * sizeof(float);
3492
3493 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3494 dest_ptr += sizeof(float);
3495 }
3496
3497 if (DestFVF & WINED3DFVF_PSIZE)
3498 dest_ptr += sizeof(DWORD);
3499
3500 if (DestFVF & WINED3DFVF_NORMAL)
3501 {
3502 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3503 const float *normal = (const float *)(element->data.addr + i * element->stride);
3504 /* AFAIK this should go into the lighting information */
3505 FIXME("Didn't expect the destination to have a normal\n");
3506 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3507 }
3508
3509 if (DestFVF & WINED3DFVF_DIFFUSE)
3510 {
3511 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3512 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3513 if (!(stream_info->use_map & (1 << WINED3D_FFP_DIFFUSE)))
3514 {
3515 static BOOL warned = FALSE;
3516
3517 if(!warned) {
3518 ERR("No diffuse color in source, but destination has one\n");
3519 warned = TRUE;
3520 }
3521
3522 *( (DWORD *) dest_ptr) = 0xffffffff;
3523 dest_ptr += sizeof(DWORD);
3524 }
3525 else
3526 {
3527 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3528 }
3529 }
3530
3531 if (DestFVF & WINED3DFVF_SPECULAR)
3532 {
3533 /* What's the color value in the feedback buffer? */
3534 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3535 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3536 if (!(stream_info->use_map & (1 << WINED3D_FFP_SPECULAR)))
3537 {
3538 static BOOL warned = FALSE;
3539
3540 if(!warned) {
3541 ERR("No specular color in source, but destination has one\n");
3542 warned = TRUE;
3543 }
3544
3545 *(DWORD *)dest_ptr = 0xff000000;
3546 dest_ptr += sizeof(DWORD);
3547 }
3548 else
3549 {
3550 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3551 }
3552 }
3553
3554 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3555 {
3556 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3557 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3558 if (!(stream_info->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3559 {
3560 ERR("No source texture, but destination requests one\n");
3561 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3562 }
3563 else
3564 {
3565 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3566 }
3567 }
3568 }
3569
3570 wined3d_buffer_unmap(dest);
3571
3572 return WINED3D_OK;
3573}
3574#undef copy_and_next
3575
3576/* Do not call while under the GL lock. */
3577HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3578 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3579 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3580{
3581 struct wined3d_state *state = &device->stateBlock->state;
3582 struct wined3d_stream_info stream_info;
3583 const struct wined3d_gl_info *gl_info;
3584 struct wined3d_context *context;
3585 struct wined3d_shader *vs;
3586 unsigned int i;
3587 HRESULT hr;
3588
3589 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3590 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3591 device, src_start_idx, dst_idx, vertex_count,
3592 dst_buffer, declaration, flags, dst_fvf);
3593
3594 if (declaration)
3595 FIXME("Output vertex declaration not implemented yet.\n");
3596
3597 /* Need any context to write to the vbo. */
3598 context = context_acquire(device, NULL);
3599 gl_info = context->gl_info;
3600
3601 vs = state->vertex_shader;
3602 state->vertex_shader = NULL;
3603 device_stream_info_from_declaration(device, &stream_info);
3604 state->vertex_shader = vs;
3605
3606 /* We can't convert FROM a VBO, and vertex buffers used to source into
3607 * process_vertices() are unlikely to ever be used for drawing. Release
3608 * VBOs in those buffers and fix up the stream_info structure.
3609 *
3610 * Also apply the start index. */
3611 for (i = 0; i < (sizeof(stream_info.elements) / sizeof(*stream_info.elements)); ++i)
3612 {
3613 struct wined3d_stream_info_element *e;
3614
3615 if (!(stream_info.use_map & (1 << i)))
3616 continue;
3617
3618 e = &stream_info.elements[i];
3619 if (e->data.buffer_object)
3620 {
3621 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
3622 e->data.buffer_object = 0;
3623 e->data.addr = (BYTE *)((ULONG_PTR)e->data.addr + (ULONG_PTR)buffer_get_sysmem(vb, gl_info));
3624 GL_EXTCALL(glDeleteBuffersARB(1, &vb->buffer_object));
3625 vb->buffer_object = 0;
3626 }
3627 if (e->data.addr)
3628 e->data.addr += e->stride * src_start_idx;
3629 }
3630
3631 hr = process_vertices_strided(device, dst_idx, vertex_count,
3632 &stream_info, dst_buffer, flags, dst_fvf);
3633
3634 context_release(context);
3635
3636 return hr;
3637}
3638
3639void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3640 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3641{
3642 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3643 DWORD old_value;
3644
3645 TRACE("device %p, stage %u, state %s, value %#x.\n",
3646 device, stage, debug_d3dtexturestate(state), value);
3647
3648 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3649 {
3650 WARN("Invalid state %#x passed.\n", state);
3651 return;
3652 }
3653
3654 if (stage >= d3d_info->limits.ffp_blend_stages)
3655 {
3656 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3657 stage, d3d_info->limits.ffp_blend_stages - 1);
3658 return;
3659 }
3660
3661 old_value = device->updateStateBlock->state.texture_states[stage][state];
3662 device->updateStateBlock->changed.textureState[stage] |= 1 << state;
3663 device->updateStateBlock->state.texture_states[stage][state] = value;
3664
3665 if (device->isRecordingState)
3666 {
3667 TRACE("Recording... not performing anything.\n");
3668 return;
3669 }
3670
3671 /* Checked after the assignments to allow proper stateblock recording. */
3672 if (old_value == value)
3673 {
3674 TRACE("Application is setting the old value over, nothing to do.\n");
3675 return;
3676 }
3677
3678 if (stage > device->stateBlock->state.lowest_disabled_stage
3679 && device->StateTable[STATE_TEXTURESTAGE(0, state)].representative
3680 == STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP))
3681 {
3682 /* Colorop change above lowest disabled stage? That won't change
3683 * anything in the GL setup. Changes in other states are important on
3684 * disabled stages too. */
3685 return;
3686 }
3687
3688 if (state == WINED3D_TSS_COLOR_OP)
3689 {
3690 unsigned int i;
3691
3692 if (value == WINED3D_TOP_DISABLE && old_value != WINED3D_TOP_DISABLE)
3693 {
3694 /* Previously enabled stage disabled now. Make sure to dirtify
3695 * all enabled stages above stage, they have to be disabled.
3696 *
3697 * The current stage is dirtified below. */
3698 for (i = stage + 1; i < device->stateBlock->state.lowest_disabled_stage; ++i)
3699 {
3700 TRACE("Additionally dirtifying stage %u.\n", i);
3701 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3702 }
3703 device->stateBlock->state.lowest_disabled_stage = stage;
3704 TRACE("New lowest disabled: %u.\n", stage);
3705 }
3706 else if (value != WINED3D_TOP_DISABLE && old_value == WINED3D_TOP_DISABLE)
3707 {
3708 /* Previously disabled stage enabled. Stages above it may need
3709 * enabling. Stage must be lowest_disabled_stage here, if it's
3710 * bigger success is returned above, and stages below the lowest
3711 * disabled stage can't be enabled (because they are enabled
3712 * already).
3713 *
3714 * Again stage stage doesn't need to be dirtified here, it is
3715 * handled below. */
3716 for (i = stage + 1; i < d3d_info->limits.ffp_blend_stages; ++i)
3717 {
3718 if (device->updateStateBlock->state.texture_states[i][WINED3D_TSS_COLOR_OP] == WINED3D_TOP_DISABLE)
3719 break;
3720 TRACE("Additionally dirtifying stage %u due to enable.\n", i);
3721 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3722 }
3723 device->stateBlock->state.lowest_disabled_stage = i;
3724 TRACE("New lowest disabled: %u.\n", i);
3725 }
3726 }
3727
3728 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, state));
3729}
3730
3731DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3732 UINT stage, enum wined3d_texture_stage_state state)
3733{
3734 TRACE("device %p, stage %u, state %s.\n",
3735 device, stage, debug_d3dtexturestate(state));
3736
3737 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3738 {
3739 WARN("Invalid state %#x passed.\n", state);
3740 return 0;
3741 }
3742
3743 return device->updateStateBlock->state.texture_states[stage][state];
3744}
3745
3746HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3747 UINT stage, struct wined3d_texture *texture)
3748{
3749 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3750 struct wined3d_texture *prev;
3751
3752 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3753
3754 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3755 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3756
3757 /* Windows accepts overflowing this array... we do not. */
3758 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3759 {
3760 WARN("Ignoring invalid stage %u.\n", stage);
3761 return WINED3D_OK;
3762 }
3763
3764 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3765 {
3766 WARN("Rejecting attempt to set scratch texture.\n");
3767 return WINED3DERR_INVALIDCALL;
3768 }
3769
3770 device->updateStateBlock->changed.textures |= 1 << stage;
3771
3772 prev = device->updateStateBlock->state.textures[stage];
3773 TRACE("Previous texture %p.\n", prev);
3774
3775 if (texture == prev)
3776 {
3777 TRACE("App is setting the same texture again, nothing to do.\n");
3778 return WINED3D_OK;
3779 }
3780
3781 TRACE("Setting new texture to %p.\n", texture);
3782 device->updateStateBlock->state.textures[stage] = texture;
3783
3784 if (device->isRecordingState)
3785 {
3786 TRACE("Recording... not performing anything\n");
3787
3788 if (texture) wined3d_texture_incref(texture);
3789 if (prev) wined3d_texture_decref(prev);
3790
3791 return WINED3D_OK;
3792 }
3793
3794 if (texture)
3795 {
3796 LONG bind_count = InterlockedIncrement(&texture->resource.bind_count);
3797
3798 wined3d_texture_incref(texture);
3799
3800 if (!prev || texture->target != prev->target)
3801 device_invalidate_state(device, STATE_PIXELSHADER);
3802
3803 if (!prev && stage < d3d_info->limits.ffp_blend_stages)
3804 {
3805 /* The source arguments for color and alpha ops have different
3806 * meanings when a NULL texture is bound, so the COLOR_OP and
3807 * ALPHA_OP have to be dirtified. */
3808 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3809 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3810 }
3811
3812 if (bind_count == 1)
3813 texture->sampler = stage;
3814 }
3815
3816 if (prev)
3817 {
3818 LONG bind_count = InterlockedDecrement(&prev->resource.bind_count);
3819
3820 if (!texture && stage < d3d_info->limits.ffp_blend_stages)
3821 {
3822 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3823 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3824 }
3825
3826 if (bind_count && prev->sampler == stage)
3827 {
3828 unsigned int i;
3829
3830 /* Search for other stages the texture is bound to. Shouldn't
3831 * happen if applications bind textures to a single stage only. */
3832 TRACE("Searching for other stages the texture is bound to.\n");
3833 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3834 {
3835 if (device->updateStateBlock->state.textures[i] == prev)
3836 {
3837 TRACE("Texture is also bound to stage %u.\n", i);
3838 prev->sampler = i;
3839 break;
3840 }
3841 }
3842 }
3843
3844 wined3d_texture_decref(prev);
3845 }
3846
3847 device_invalidate_state(device, STATE_SAMPLER(stage));
3848
3849 return WINED3D_OK;
3850}
3851
3852struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3853{
3854 TRACE("device %p, stage %u.\n", device, stage);
3855
3856 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3857 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3858
3859 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3860 {
3861 WARN("Ignoring invalid stage %u.\n", stage);
3862 return NULL; /* Windows accepts overflowing this array ... we do not. */
3863 }
3864
3865 return device->stateBlock->state.textures[stage];
3866}
3867
3868HRESULT CDECL wined3d_device_get_back_buffer(const struct wined3d_device *device, UINT swapchain_idx,
3869 UINT backbuffer_idx, enum wined3d_backbuffer_type backbuffer_type, struct wined3d_surface **backbuffer)
3870{
3871 struct wined3d_swapchain *swapchain;
3872
3873 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3874 device, swapchain_idx, backbuffer_idx, backbuffer_type, backbuffer);
3875
3876 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3877 return WINED3DERR_INVALIDCALL;
3878
3879 if (!(*backbuffer = wined3d_swapchain_get_back_buffer(swapchain, backbuffer_idx, backbuffer_type)))
3880 return WINED3DERR_INVALIDCALL;
3881 return WINED3D_OK;
3882}
3883
3884HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3885{
3886 TRACE("device %p, caps %p.\n", device, caps);
3887
3888 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3889 device->create_parms.device_type, caps);
3890}
3891
3892HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3893 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3894{
3895 struct wined3d_swapchain *swapchain;
3896
3897 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3898 device, swapchain_idx, mode, rotation);
3899
3900 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3901 return WINED3DERR_INVALIDCALL;
3902
3903 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3904}
3905
3906HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3907{
3908 struct wined3d_stateblock *stateblock;
3909 HRESULT hr;
3910
3911 TRACE("device %p.\n", device);
3912
3913 if (device->isRecordingState)
3914 return WINED3DERR_INVALIDCALL;
3915
3916 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3917 if (FAILED(hr))
3918 return hr;
3919
3920 wined3d_stateblock_decref(device->updateStateBlock);
3921 device->updateStateBlock = stateblock;
3922 device->isRecordingState = TRUE;
3923
3924 TRACE("Recording stateblock %p.\n", stateblock);
3925
3926 return WINED3D_OK;
3927}
3928
3929HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3930 struct wined3d_stateblock **stateblock)
3931{
3932 struct wined3d_stateblock *object = device->updateStateBlock;
3933
3934 TRACE("device %p, stateblock %p.\n", device, stateblock);
3935
3936 if (!device->isRecordingState)
3937 {
3938 WARN("Not recording.\n");
3939 *stateblock = NULL;
3940 return WINED3DERR_INVALIDCALL;
3941 }
3942
3943 stateblock_init_contained_states(object);
3944
3945 *stateblock = object;
3946 device->isRecordingState = FALSE;
3947 device->updateStateBlock = device->stateBlock;
3948 wined3d_stateblock_incref(device->updateStateBlock);
3949
3950 TRACE("Returning stateblock %p.\n", *stateblock);
3951
3952 return WINED3D_OK;
3953}
3954
3955HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3956{
3957 /* At the moment we have no need for any functionality at the beginning
3958 * of a scene. */
3959 TRACE("device %p.\n", device);
3960
3961 if (device->inScene)
3962 {
3963 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3964 return WINED3DERR_INVALIDCALL;
3965 }
3966 device->inScene = TRUE;
3967 return WINED3D_OK;
3968}
3969
3970HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3971{
3972 struct wined3d_context *context;
3973
3974 TRACE("device %p.\n", device);
3975
3976 if (!device->inScene)
3977 {
3978 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3979 return WINED3DERR_INVALIDCALL;
3980 }
3981
3982 context = context_acquire(device, NULL);
3983 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3984 context->gl_info->gl_ops.gl.p_glFlush();
3985 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3986 * fails. */
3987 context_release(context);
3988
3989 device->inScene = FALSE;
3990 return WINED3D_OK;
3991}
3992
3993HRESULT CDECL wined3d_device_present(const struct wined3d_device *device, const RECT *src_rect,
3994 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region, DWORD flags)
3995{
3996 UINT i;
3997
3998 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3999 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
4000 dst_window_override, dirty_region, flags);
4001
4002 for (i = 0; i < device->swapchain_count; ++i)
4003 {
4004 wined3d_swapchain_present(device->swapchains[i], src_rect,
4005 dst_rect, dst_window_override, dirty_region, flags);
4006 }
4007
4008 return WINED3D_OK;
4009}
4010
4011/* Do not call while under the GL lock. */
4012HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
4013 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
4014{
4015 RECT draw_rect;
4016
4017 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
4018 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
4019
4020 if (!rect_count && rects)
4021 {
4022 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
4023 return WINED3D_OK;
4024 }
4025
4026 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
4027 {
4028 struct wined3d_surface *ds = device->fb.depth_stencil;
4029 if (!ds)
4030 {
4031 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
4032 /* TODO: What about depth stencil buffers without stencil bits? */
4033 return WINED3DERR_INVALIDCALL;
4034 }
4035 else if (flags & WINED3DCLEAR_TARGET)
4036 {
4037 if (ds->resource.width < device->fb.render_targets[0]->resource.width
4038 || ds->resource.height < device->fb.render_targets[0]->resource.height)
4039 {
4040 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
4041 return WINED3D_OK;
4042 }
4043 }
4044 }
4045
4046 wined3d_get_draw_rect(&device->stateBlock->state, &draw_rect);
4047 device_clear_render_targets(device, device->adapter->gl_info.limits.buffers,
4048 &device->fb, rect_count, rects, &draw_rect, flags, color, depth, stencil);
4049
4050 return WINED3D_OK;
4051}
4052
4053void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
4054 enum wined3d_primitive_type primitive_type)
4055{
4056 GLenum gl_primitive_type, prev;
4057
4058 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
4059
4060 device->updateStateBlock->changed.primitive_type = TRUE;
4061 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
4062 prev = device->updateStateBlock->state.gl_primitive_type;
4063 device->updateStateBlock->state.gl_primitive_type = gl_primitive_type;
4064 if (!device->isRecordingState && gl_primitive_type != prev
4065 && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
4066 device_invalidate_state(device, STATE_POINT_SIZE_ENABLE);
4067}
4068
4069void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
4070 enum wined3d_primitive_type *primitive_type)
4071{
4072 TRACE("device %p, primitive_type %p\n", device, primitive_type);
4073
4074 *primitive_type = d3d_primitive_type_from_gl(device->stateBlock->state.gl_primitive_type);
4075
4076 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
4077}
4078
4079HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
4080{
4081 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
4082
4083 if (!device->stateBlock->state.vertex_declaration)
4084 {
4085 WARN("Called without a valid vertex declaration set.\n");
4086 return WINED3DERR_INVALIDCALL;
4087 }
4088
4089 if (device->stateBlock->state.load_base_vertex_index)
4090 {
4091 device->stateBlock->state.load_base_vertex_index = 0;
4092 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4093 }
4094
4095 /* Account for the loading offset due to index buffers. Instead of
4096 * reloading all sources correct it with the startvertex parameter. */
4097 draw_primitive(device, start_vertex, vertex_count, 0, 0, FALSE);
4098 return WINED3D_OK;
4099}
4100
4101HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
4102{
4103 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4104
4105 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
4106
4107 if (!device->stateBlock->state.index_buffer)
4108 {
4109 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
4110 * without an index buffer set. (The first time at least...)
4111 * D3D8 simply dies, but I doubt it can do much harm to return
4112 * D3DERR_INVALIDCALL there as well. */
4113 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
4114 return WINED3DERR_INVALIDCALL;
4115 }
4116
4117 if (!device->stateBlock->state.vertex_declaration)
4118 {
4119 WARN("Called without a valid vertex declaration set.\n");
4120 return WINED3DERR_INVALIDCALL;
4121 }
4122
4123 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
4124 device->stateBlock->state.load_base_vertex_index != device->stateBlock->state.base_vertex_index)
4125 {
4126 device->stateBlock->state.load_base_vertex_index = device->stateBlock->state.base_vertex_index;
4127 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4128 }
4129
4130 draw_primitive(device, start_idx, index_count, 0, 0, TRUE);
4131
4132 return WINED3D_OK;
4133}
4134
4135void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
4136 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
4137{
4138 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
4139
4140 draw_primitive(device, start_idx, index_count, start_instance, instance_count, TRUE);
4141}
4142
4143/* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
4144static HRESULT device_update_volume(struct wined3d_device *device,
4145 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
4146{
4147 struct wined3d_map_desc src;
4148 struct wined3d_map_desc dst;
4149 HRESULT hr;
4150
4151 TRACE("device %p, src_volume %p, dst_volume %p.\n",
4152 device, src_volume, dst_volume);
4153
4154 /* TODO: Implement direct loading into the gl volume instead of using
4155 * memcpy and dirtification to improve loading performance. */
4156 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
4157 return hr;
4158 if (FAILED(hr = wined3d_volume_map(dst_volume, &dst, NULL, WINED3D_MAP_DISCARD)))
4159 {
4160 wined3d_volume_unmap(src_volume);
4161 return hr;
4162 }
4163
4164 memcpy(dst.data, src.data, dst_volume->resource.size);
4165
4166 hr = wined3d_volume_unmap(dst_volume);
4167 if (FAILED(hr))
4168 wined3d_volume_unmap(src_volume);
4169 else
4170 hr = wined3d_volume_unmap(src_volume);
4171
4172 return hr;
4173}
4174
4175HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4176 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4177{
4178 enum wined3d_resource_type type;
4179 unsigned int level_count, i;
4180 HRESULT hr;
4181
4182 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4183
4184 /* Verify that the source and destination textures are non-NULL. */
4185 if (!src_texture || !dst_texture)
4186 {
4187 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4188 return WINED3DERR_INVALIDCALL;
4189 }
4190
4191 if (src_texture == dst_texture)
4192 {
4193 WARN("Source and destination are the same object, returning WINED3DERR_INVALIDCALL.\n");
4194 return WINED3DERR_INVALIDCALL;
4195 }
4196
4197 /* Verify that the source and destination textures are the same type. */
4198 type = src_texture->resource.type;
4199 if (dst_texture->resource.type != type)
4200 {
4201 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4202 return WINED3DERR_INVALIDCALL;
4203 }
4204
4205 /* Check that both textures have the identical numbers of levels. */
4206 level_count = wined3d_texture_get_level_count(src_texture);
4207 if (wined3d_texture_get_level_count(dst_texture) != level_count)
4208 {
4209 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
4210 return WINED3DERR_INVALIDCALL;
4211 }
4212
4213 /* Make sure that the destination texture is loaded. */
4214 dst_texture->texture_ops->texture_preload(dst_texture, SRGB_RGB);
4215
4216 /* Update every surface level of the texture. */
4217 switch (type)
4218 {
4219 case WINED3D_RTYPE_TEXTURE:
4220 {
4221 struct wined3d_surface *src_surface;
4222 struct wined3d_surface *dst_surface;
4223
4224 for (i = 0; i < level_count; ++i)
4225 {
4226 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4227 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4228 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4229 if (FAILED(hr))
4230 {
4231 WARN("Failed to update surface, hr %#x.\n", hr);
4232 return hr;
4233 }
4234 }
4235 break;
4236 }
4237
4238 case WINED3D_RTYPE_CUBE_TEXTURE:
4239 {
4240 struct wined3d_surface *src_surface;
4241 struct wined3d_surface *dst_surface;
4242
4243 for (i = 0; i < level_count * 6; ++i)
4244 {
4245 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4246 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4247 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4248 if (FAILED(hr))
4249 {
4250 WARN("Failed to update surface, hr %#x.\n", hr);
4251 return hr;
4252 }
4253 }
4254 break;
4255 }
4256
4257 case WINED3D_RTYPE_VOLUME_TEXTURE:
4258 {
4259 for (i = 0; i < level_count; ++i)
4260 {
4261 hr = device_update_volume(device,
4262 volume_from_resource(wined3d_texture_get_sub_resource(src_texture, i)),
4263 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
4264 if (FAILED(hr))
4265 {
4266 WARN("Failed to update volume, hr %#x.\n", hr);
4267 return hr;
4268 }
4269 }
4270 break;
4271 }
4272
4273 default:
4274 FIXME("Unsupported texture type %#x.\n", type);
4275 return WINED3DERR_INVALIDCALL;
4276 }
4277
4278 return WINED3D_OK;
4279}
4280
4281HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
4282 UINT swapchain_idx, struct wined3d_surface *dst_surface)
4283{
4284 struct wined3d_swapchain *swapchain;
4285
4286 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
4287
4288 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4289 return WINED3DERR_INVALIDCALL;
4290
4291 return wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
4292}
4293
4294HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4295{
4296 const struct wined3d_state *state = &device->stateBlock->state;
4297 struct wined3d_texture *texture;
4298 DWORD i;
4299
4300 TRACE("device %p, num_passes %p.\n", device, num_passes);
4301
4302 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4303 {
4304 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4305 {
4306 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4307 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4308 }
4309 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4310 {
4311 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4312 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4313 }
4314
4315 texture = state->textures[i];
4316 if (!texture || texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING) continue;
4317
4318 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4319 {
4320 WARN("Non-filterable texture and mag filter enabled on samper %u, returning E_FAIL\n", i);
4321 return E_FAIL;
4322 }
4323 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4324 {
4325 WARN("Non-filterable texture and min filter enabled on samper %u, returning E_FAIL\n", i);
4326 return E_FAIL;
4327 }
4328 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4329 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4330 {
4331 WARN("Non-filterable texture and mip filter enabled on samper %u, returning E_FAIL\n", i);
4332 return E_FAIL;
4333 }
4334 }
4335
4336 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
4337 || state->render_states[WINED3D_RS_STENCILENABLE])
4338 {
4339 struct wined3d_surface *ds = device->fb.depth_stencil;
4340 struct wined3d_surface *target = device->fb.render_targets[0];
4341
4342 if(ds && target
4343 && (ds->resource.width < target->resource.width || ds->resource.height < target->resource.height))
4344 {
4345 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4346 return WINED3DERR_CONFLICTINGRENDERSTATE;
4347 }
4348 }
4349
4350 /* return a sensible default */
4351 *num_passes = 1;
4352
4353 TRACE("returning D3D_OK\n");
4354 return WINED3D_OK;
4355}
4356
4357void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4358{
4359 static BOOL warned;
4360
4361 TRACE("device %p, software %#x.\n", device, software);
4362
4363 if (!warned)
4364 {
4365 FIXME("device %p, software %#x stub!\n", device, software);
4366 warned = TRUE;
4367 }
4368
4369 device->softwareVertexProcessing = software;
4370}
4371
4372BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4373{
4374 static BOOL warned;
4375
4376 TRACE("device %p.\n", device);
4377
4378 if (!warned)
4379 {
4380 TRACE("device %p stub!\n", device);
4381 warned = TRUE;
4382 }
4383
4384 return device->softwareVertexProcessing;
4385}
4386
4387HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4388 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4389{
4390 struct wined3d_swapchain *swapchain;
4391
4392 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4393 device, swapchain_idx, raster_status);
4394
4395 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4396 return WINED3DERR_INVALIDCALL;
4397
4398 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4399}
4400
4401HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4402{
4403 static BOOL warned;
4404
4405 TRACE("device %p, segments %.8e.\n", device, segments);
4406
4407 if (segments != 0.0f)
4408 {
4409 if (!warned)
4410 {
4411 FIXME("device %p, segments %.8e stub!\n", device, segments);
4412 warned = TRUE;
4413 }
4414 }
4415
4416 return WINED3D_OK;
4417}
4418
4419float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4420{
4421 static BOOL warned;
4422
4423 TRACE("device %p.\n", device);
4424
4425 if (!warned)
4426 {
4427 FIXME("device %p stub!\n", device);
4428 warned = TRUE;
4429 }
4430
4431 return 0.0f;
4432}
4433
4434HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
4435 struct wined3d_surface *src_surface, const RECT *src_rect,
4436 struct wined3d_surface *dst_surface, const POINT *dst_point)
4437{
4438 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
4439 device, src_surface, wine_dbgstr_rect(src_rect),
4440 dst_surface, wine_dbgstr_point(dst_point));
4441
4442 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
4443 {
4444 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
4445 src_surface, dst_surface);
4446 return WINED3DERR_INVALIDCALL;
4447 }
4448
4449 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
4450}
4451
4452/* Do not call while under the GL lock. */
4453HRESULT CDECL wined3d_device_color_fill(struct wined3d_device *device,
4454 struct wined3d_surface *surface, const RECT *rect, const struct wined3d_color *color)
4455{
4456 RECT r;
4457
4458 TRACE("device %p, surface %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
4459 device, surface, wine_dbgstr_rect(rect),
4460 color->r, color->g, color->b, color->a);
4461
4462 if (surface->resource.pool != WINED3D_POOL_DEFAULT && surface->resource.pool != WINED3D_POOL_SYSTEM_MEM)
4463 {
4464 WARN("Color-fill not allowed on %s surfaces.\n", debug_d3dpool(surface->resource.pool));
4465 return WINED3DERR_INVALIDCALL;
4466 }
4467
4468 if (!rect)
4469 {
4470 SetRect(&r, 0, 0, surface->resource.width, surface->resource.height);
4471 rect = &r;
4472 }
4473
4474 return surface_color_fill(surface, rect, color);
4475}
4476
4477/* Do not call while under the GL lock. */
4478void CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4479 struct wined3d_rendertarget_view *rendertarget_view, const struct wined3d_color *color)
4480{
4481 struct wined3d_resource *resource;
4482 HRESULT hr;
4483 RECT rect;
4484
4485 resource = rendertarget_view->resource;
4486 if (resource->type != WINED3D_RTYPE_SURFACE)
4487 {
4488 FIXME("Only supported on surface resources\n");
4489 return;
4490 }
4491
4492 SetRect(&rect, 0, 0, resource->width, resource->height);
4493 hr = surface_color_fill(surface_from_resource(resource), &rect, color);
4494 if (FAILED(hr)) ERR("Color fill failed, hr %#x.\n", hr);
4495}
4496
4497struct wined3d_surface * CDECL wined3d_device_get_render_target(const struct wined3d_device *device,
4498 UINT render_target_idx)
4499{
4500 TRACE("device %p, render_target_idx %u.\n", device, render_target_idx);
4501
4502 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4503 {
4504 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4505 return NULL;
4506 }
4507
4508 return device->fb.render_targets[render_target_idx];
4509}
4510
4511struct wined3d_surface * CDECL wined3d_device_get_depth_stencil(const struct wined3d_device *device)
4512{
4513 TRACE("device %p.\n", device);
4514
4515 return device->fb.depth_stencil;
4516}
4517
4518HRESULT CDECL wined3d_device_set_render_target(struct wined3d_device *device,
4519 UINT render_target_idx, struct wined3d_surface *render_target, BOOL set_viewport)
4520{
4521 struct wined3d_surface *prev;
4522
4523 TRACE("device %p, render_target_idx %u, render_target %p, set_viewport %#x.\n",
4524 device, render_target_idx, render_target, set_viewport);
4525
4526 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4527 {
4528 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4529 return WINED3DERR_INVALIDCALL;
4530 }
4531
4532 /* Render target 0 can't be set to NULL. */
4533 if (!render_target && !render_target_idx)
4534 {
4535 WARN("Trying to set render target 0 to NULL.\n");
4536 return WINED3DERR_INVALIDCALL;
4537 }
4538
4539 if (render_target && !(render_target->resource.usage & WINED3DUSAGE_RENDERTARGET))
4540 {
4541 FIXME("Surface %p doesn't have render target usage.\n", render_target);
4542 return WINED3DERR_INVALIDCALL;
4543 }
4544
4545 /* Set the viewport and scissor rectangles, if requested. Tests show that
4546 * stateblock recording is ignored, the change goes directly into the
4547 * primary stateblock. */
4548 if (!render_target_idx && set_viewport)
4549 {
4550 struct wined3d_state *state = &device->stateBlock->state;
4551
4552 state->viewport.x = 0;
4553 state->viewport.y = 0;
4554 state->viewport.width = render_target->resource.width;
4555 state->viewport.height = render_target->resource.height;
4556 state->viewport.min_z = 0.0f;
4557 state->viewport.max_z = 1.0f;
4558 device_invalidate_state(device, STATE_VIEWPORT);
4559
4560 state->scissor_rect.top = 0;
4561 state->scissor_rect.left = 0;
4562 state->scissor_rect.right = render_target->resource.width;
4563 state->scissor_rect.bottom = render_target->resource.height;
4564 device_invalidate_state(device, STATE_SCISSORRECT);
4565 }
4566
4567
4568 prev = device->fb.render_targets[render_target_idx];
4569 if (render_target == prev)
4570 return WINED3D_OK;
4571
4572 if (render_target)
4573 wined3d_surface_incref(render_target);
4574 device->fb.render_targets[render_target_idx] = render_target;
4575 /* Release after the assignment, to prevent device_resource_released()
4576 * from seeing the surface as still in use. */
4577 if (prev)
4578 wined3d_surface_decref(prev);
4579
4580 device_invalidate_state(device, STATE_FRAMEBUFFER);
4581
4582 return WINED3D_OK;
4583}
4584
4585void CDECL wined3d_device_set_depth_stencil(struct wined3d_device *device, struct wined3d_surface *depth_stencil)
4586{
4587 struct wined3d_surface *prev = device->fb.depth_stencil;
4588
4589 TRACE("device %p, depth_stencil %p, old depth_stencil %p.\n",
4590 device, depth_stencil, prev);
4591
4592 if (prev == depth_stencil)
4593 {
4594 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4595 return;
4596 }
4597
4598 if (prev)
4599 {
4600 if (device->swapchains[0]->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
4601 || prev->flags & SFLAG_DISCARD)
4602 {
4603 surface_modify_ds_location(prev, SFLAG_DISCARDED,
4604 prev->resource.width, prev->resource.height);
4605 if (prev == device->onscreen_depth_stencil)
4606 {
4607 wined3d_surface_decref(device->onscreen_depth_stencil);
4608 device->onscreen_depth_stencil = NULL;
4609 }
4610 }
4611 }
4612
4613 device->fb.depth_stencil = depth_stencil;
4614 if (depth_stencil)
4615 wined3d_surface_incref(depth_stencil);
4616
4617 if (!prev != !depth_stencil)
4618 {
4619 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
4620 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
4621 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
4622 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
4623 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4624 }
4625 else if (prev && prev->resource.format->depth_size != depth_stencil->resource.format->depth_size)
4626 {
4627 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4628 }
4629 if (prev)
4630 wined3d_surface_decref(prev);
4631
4632 device_invalidate_state(device, STATE_FRAMEBUFFER);
4633
4634 return;
4635}
4636
4637HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4638 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
4639{
4640 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
4641 device, x_hotspot, y_hotspot, cursor_image);
4642
4643 /* some basic validation checks */
4644 if (device->cursorTexture)
4645 {
4646 struct wined3d_context *context = context_acquire(device, NULL);
4647 context->gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
4648 context_release(context);
4649 device->cursorTexture = 0;
4650 }
4651
4652 if (cursor_image)
4653 {
4654 struct wined3d_display_mode mode;
4655 struct wined3d_map_desc map_desc;
4656 HRESULT hr;
4657
4658 /* MSDN: Cursor must be A8R8G8B8 */
4659 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4660 {
4661 WARN("surface %p has an invalid format.\n", cursor_image);
4662 return WINED3DERR_INVALIDCALL;
4663 }
4664
4665 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4666 {
4667 ERR("Failed to get display mode, hr %#x.\n", hr);
4668 return WINED3DERR_INVALIDCALL;
4669 }
4670
4671 /* MSDN: Cursor must be smaller than the display mode */
4672 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
4673 {
4674 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4675 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
4676 mode.width, mode.height);
4677 return WINED3DERR_INVALIDCALL;
4678 }
4679
4680 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4681
4682 /* Do not store the surface's pointer because the application may
4683 * release it after setting the cursor image. Windows doesn't
4684 * addref the set surface, so we can't do this either without
4685 * creating circular refcount dependencies. Copy out the gl texture
4686 * instead. */
4687 device->cursorWidth = cursor_image->resource.width;
4688 device->cursorHeight = cursor_image->resource.height;
4689 if (SUCCEEDED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
4690 {
4691 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4692 const struct wined3d_format *format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
4693 struct wined3d_context *context;
4694 char *mem, *bits = map_desc.data;
4695 GLint intfmt = format->glInternal;
4696 GLint gl_format = format->glFormat;
4697 GLint type = format->glType;
4698 INT height = device->cursorHeight;
4699 INT width = device->cursorWidth;
4700 INT bpp = format->byte_count;
4701 INT i;
4702
4703 /* Reformat the texture memory (pitch and width can be
4704 * different) */
4705 mem = HeapAlloc(GetProcessHeap(), 0, width * height * bpp);
4706 for (i = 0; i < height; ++i)
4707 memcpy(&mem[width * bpp * i], &bits[map_desc.row_pitch * i], width * bpp);
4708 wined3d_surface_unmap(cursor_image);
4709
4710 context = context_acquire(device, NULL);
4711
4712 if (gl_info->supported[APPLE_CLIENT_STORAGE])
4713 {
4714 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
4715 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
4716 }
4717
4718 invalidate_active_texture(device, context);
4719 /* Create a new cursor texture */
4720 gl_info->gl_ops.gl.p_glGenTextures(1, &device->cursorTexture);
4721 checkGLcall("glGenTextures");
4722 context_bind_texture(context, GL_TEXTURE_2D, device->cursorTexture);
4723 /* Copy the bitmap memory into the cursor texture */
4724 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, intfmt, width, height, 0, gl_format, type, mem);
4725 checkGLcall("glTexImage2D");
4726 HeapFree(GetProcessHeap(), 0, mem);
4727
4728 if (gl_info->supported[APPLE_CLIENT_STORAGE])
4729 {
4730 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
4731 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
4732 }
4733
4734 context_release(context);
4735 }
4736 else
4737 {
4738 FIXME("A cursor texture was not returned.\n");
4739 device->cursorTexture = 0;
4740 }
4741
4742 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
4743 {
4744 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
4745 ICONINFO cursorInfo;
4746 DWORD *maskBits;
4747 HCURSOR cursor;
4748
4749 /* 32-bit user32 cursors ignore the alpha channel if it's all
4750 * zeroes, and use the mask instead. Fill the mask with all ones
4751 * to ensure we still get a fully transparent cursor. */
4752 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
4753 memset(maskBits, 0xff, mask_size);
4754 wined3d_surface_map(cursor_image, &map_desc, NULL,
4755 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4756 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
4757
4758 cursorInfo.fIcon = FALSE;
4759 cursorInfo.xHotspot = x_hotspot;
4760 cursorInfo.yHotspot = y_hotspot;
4761 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4762 1, 1, maskBits);
4763 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4764 1, 32, map_desc.data);
4765 wined3d_surface_unmap(cursor_image);
4766 /* Create our cursor and clean up. */
4767 cursor = CreateIconIndirect(&cursorInfo);
4768 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
4769 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
4770 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
4771 device->hardwareCursor = cursor;
4772 if (device->bCursorVisible) SetCursor( cursor );
4773 HeapFree(GetProcessHeap(), 0, maskBits);
4774 }
4775 }
4776
4777 device->xHotSpot = x_hotspot;
4778 device->yHotSpot = y_hotspot;
4779 return WINED3D_OK;
4780}
4781
4782void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4783 int x_screen_space, int y_screen_space, DWORD flags)
4784{
4785 TRACE("device %p, x %d, y %d, flags %#x.\n",
4786 device, x_screen_space, y_screen_space, flags);
4787
4788 device->xScreenSpace = x_screen_space;
4789 device->yScreenSpace = y_screen_space;
4790
4791 if (device->hardwareCursor)
4792 {
4793 POINT pt;
4794
4795 GetCursorPos( &pt );
4796 if (x_screen_space == pt.x && y_screen_space == pt.y)
4797 return;
4798 SetCursorPos( x_screen_space, y_screen_space );
4799
4800 /* Switch to the software cursor if position diverges from the hardware one. */
4801 GetCursorPos( &pt );
4802 if (x_screen_space != pt.x || y_screen_space != pt.y)
4803 {
4804 if (device->bCursorVisible) SetCursor( NULL );
4805 DestroyCursor( device->hardwareCursor );
4806 device->hardwareCursor = 0;
4807 }
4808 }
4809}
4810
4811BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4812{
4813 BOOL oldVisible = device->bCursorVisible;
4814
4815 TRACE("device %p, show %#x.\n", device, show);
4816
4817 /*
4818 * When ShowCursor is first called it should make the cursor appear at the OS's last
4819 * known cursor position.
4820 */
4821 if (show && !oldVisible)
4822 {
4823 POINT pt;
4824 GetCursorPos(&pt);
4825 device->xScreenSpace = pt.x;
4826 device->yScreenSpace = pt.y;
4827 }
4828
4829 if (device->hardwareCursor)
4830 {
4831 device->bCursorVisible = show;
4832 if (show)
4833 SetCursor(device->hardwareCursor);
4834 else
4835 SetCursor(NULL);
4836 }
4837 else
4838 {
4839 if (device->cursorTexture)
4840 device->bCursorVisible = show;
4841 }
4842
4843 return oldVisible;
4844}
4845
4846void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4847{
4848 struct wined3d_resource *resource, *cursor;
4849
4850 TRACE("device %p.\n", device);
4851
4852 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4853 {
4854 TRACE("Checking resource %p for eviction.\n", resource);
4855
4856 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4857 {
4858 TRACE("Evicting %p.\n", resource);
4859 resource->resource_ops->resource_unload(resource);
4860 }
4861 }
4862
4863 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4864 device_invalidate_state(device, STATE_STREAMSRC);
4865}
4866
4867#ifndef VBOX_WITH_WDDM
4868/* Do not call while under the GL lock. */
4869static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4870{
4871 struct wined3d_resource *resource, *cursor;
4872 const struct wined3d_gl_info *gl_info;
4873 struct wined3d_context *context;
4874 struct wined3d_shader *shader;
4875
4876 context = context_acquire(device, NULL);
4877 gl_info = context->gl_info;
4878
4879 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4880 {
4881 TRACE("Unloading resource %p.\n", resource);
4882
4883 resource->resource_ops->resource_unload(resource);
4884 }
4885
4886 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4887 {
4888 device->shader_backend->shader_destroy(shader);
4889 }
4890
4891 if (device->depth_blt_texture)
4892 {
4893 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4894 device->depth_blt_texture = 0;
4895 }
4896 if (device->cursorTexture)
4897 {
4898 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
4899 device->cursorTexture = 0;
4900 }
4901
4902 device->blitter->free_private(device);
4903 device->shader_backend->shader_free_private(device);
4904 destroy_dummy_textures(device, gl_info);
4905
4906 context_release(context);
4907
4908 while (device->context_count)
4909 {
4910#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
4911 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4912#else
4913 context_destroy(device, device->contexts[0]);
4914#endif
4915 }
4916
4917#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
4918 HeapFree(GetProcessHeap(), 0, swapchain->context);
4919 swapchain->context = NULL;
4920#else
4921#endif
4922}
4923
4924/* Do not call while under the GL lock. */
4925static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4926{
4927 struct wined3d_context *context;
4928 struct wined3d_surface *target;
4929 HRESULT hr;
4930
4931 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4932 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4933 {
4934 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4935 return hr;
4936 }
4937
4938 if (FAILED(hr = device->blitter->alloc_private(device)))
4939 {
4940 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4941 device->shader_backend->shader_free_private(device);
4942 return hr;
4943 }
4944
4945 /* Recreate the primary swapchain's context */
4946#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
4947 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4948 if (!swapchain->context)
4949#else
4950 device->contexts = HeapAlloc(GetProcessHeap(), 0, sizeof(*device->contexts));
4951 if (!device->contexts)
4952#endif
4953 {
4954 ERR("Failed to allocate memory for swapchain context array.\n");
4955 device->blitter->free_private(device);
4956 device->shader_backend->shader_free_private(device);
4957 return E_OUTOFMEMORY;
4958 }
4959
4960 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4961 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4962 {
4963 WARN("Failed to create context.\n");
4964 device->blitter->free_private(device);
4965 device->shader_backend->shader_free_private(device);
4966#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
4967 HeapFree(GetProcessHeap(), 0, swapchain->context);
4968#else
4969 HeapFree(GetProcessHeap(), 0, device->contexts);
4970#endif
4971 return E_FAIL;
4972 }
4973
4974#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
4975 swapchain->context[0] = context;
4976 swapchain->num_contexts = 1;
4977#endif
4978 create_dummy_textures(device, context);
4979 context_release(context);
4980
4981 return WINED3D_OK;
4982}
4983#endif
4984/* Do not call while under the GL lock. */
4985HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4986 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4987 wined3d_device_reset_cb callback, BOOL reset_state)
4988{
4989#ifndef VBOX_WITH_WDDM
4990 struct wined3d_resource *resource, *cursor;
4991 struct wined3d_swapchain *swapchain;
4992 struct wined3d_display_mode m;
4993 BOOL DisplayModeChanged = FALSE;
4994 BOOL update_desc = FALSE;
4995 UINT backbuffer_width = swapchain_desc->backbuffer_width;
4996 UINT backbuffer_height = swapchain_desc->backbuffer_height;
4997 HRESULT hr = WINED3D_OK;
4998 unsigned int i;
4999
5000 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device, swapchain_desc, mode, callback);
5001
5002 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
5003 {
5004 ERR("Failed to get the first implicit swapchain.\n");
5005 return WINED3DERR_INVALIDCALL;
5006 }
5007
5008 if (reset_state)
5009 stateblock_unbind_resources(device->stateBlock);
5010
5011 if (device->fb.render_targets)
5012 {
5013 if (swapchain->back_buffers && swapchain->back_buffers[0])
5014 wined3d_device_set_render_target(device, 0, swapchain->back_buffers[0], FALSE);
5015 else
5016 wined3d_device_set_render_target(device, 0, swapchain->front_buffer, FALSE);
5017 for (i = 1; i < device->adapter->gl_info.limits.buffers; ++i)
5018 {
5019 wined3d_device_set_render_target(device, i, NULL, FALSE);
5020 }
5021 }
5022 wined3d_device_set_depth_stencil(device, NULL);
5023
5024 if (device->onscreen_depth_stencil)
5025 {
5026 wined3d_surface_decref(device->onscreen_depth_stencil);
5027 device->onscreen_depth_stencil = NULL;
5028 }
5029
5030 if (reset_state)
5031 {
5032 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5033 {
5034 TRACE("Enumerating resource %p.\n", resource);
5035 if (FAILED(hr = callback(resource)))
5036 return hr;
5037 }
5038 }
5039
5040 /* Is it necessary to recreate the gl context? Actually every setting can be changed
5041 * on an existing gl context, so there's no real need for recreation.
5042 *
5043 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
5044 *
5045 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
5046 */
5047 TRACE("New params:\n");
5048 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5049 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5050 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5051 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5052 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5053 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5054 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5055 TRACE("device_window %p\n", swapchain_desc->device_window);
5056 TRACE("windowed %#x\n", swapchain_desc->windowed);
5057 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5058 if (swapchain_desc->enable_auto_depth_stencil)
5059 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5060 TRACE("flags %#x\n", swapchain_desc->flags);
5061 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5062 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
5063 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5064
5065 /* No special treatment of these parameters. Just store them */
5066 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
5067 swapchain->desc.flags = swapchain_desc->flags;
5068 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
5069 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
5070
5071 /* What to do about these? */
5072 if (swapchain_desc->backbuffer_count
5073 && swapchain_desc->backbuffer_count != swapchain->desc.backbuffer_count)
5074 FIXME("Cannot change the back buffer count yet.\n");
5075
5076 if (swapchain_desc->device_window
5077 && swapchain_desc->device_window != swapchain->desc.device_window)
5078 {
5079 TRACE("Changing the device window from %p to %p.\n",
5080 swapchain->desc.device_window, swapchain_desc->device_window);
5081 swapchain->desc.device_window = swapchain_desc->device_window;
5082 swapchain->device_window = swapchain_desc->device_window;
5083 wined3d_swapchain_set_window(swapchain, NULL);
5084 }
5085
5086 if (swapchain_desc->enable_auto_depth_stencil && !device->auto_depth_stencil)
5087 {
5088 struct wined3d_resource_desc surface_desc;
5089
5090 TRACE("Creating the depth stencil buffer\n");
5091
5092 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
5093 surface_desc.format = swapchain_desc->auto_depth_stencil_format;
5094 surface_desc.multisample_type = swapchain_desc->multisample_type;
5095 surface_desc.multisample_quality = swapchain_desc->multisample_quality;
5096 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
5097 surface_desc.pool = WINED3D_POOL_DEFAULT;
5098 surface_desc.width = swapchain_desc->backbuffer_width;
5099 surface_desc.height = swapchain_desc->backbuffer_height;
5100 surface_desc.depth = 1;
5101 surface_desc.size = 0;
5102
5103 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
5104 device->device_parent, &surface_desc, &device->auto_depth_stencil)))
5105 {
5106 ERR("Failed to create the depth stencil buffer, hr %#x.\n", hr);
5107 return WINED3DERR_INVALIDCALL;
5108 }
5109 }
5110
5111 /* Reset the depth stencil */
5112 if (swapchain_desc->enable_auto_depth_stencil)
5113 wined3d_device_set_depth_stencil(device, device->auto_depth_stencil);
5114
5115 if (mode)
5116 {
5117 DisplayModeChanged = TRUE;
5118 m = *mode;
5119 }
5120 else if (swapchain_desc->windowed)
5121 {
5122 m.width = swapchain->orig_width;
5123 m.height = swapchain->orig_height;
5124 m.refresh_rate = 0;
5125 m.format_id = swapchain->desc.backbuffer_format;
5126 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
5127 }
5128 else
5129 {
5130 m.width = swapchain_desc->backbuffer_width;
5131 m.height = swapchain_desc->backbuffer_height;
5132 m.refresh_rate = swapchain_desc->refresh_rate;
5133 m.format_id = swapchain_desc->backbuffer_format;
5134 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
5135 }
5136
5137 if (!backbuffer_width || !backbuffer_height)
5138 {
5139 /* The application is requesting that either the swapchain width or
5140 * height be set to the corresponding dimension in the window's
5141 * client rect. */
5142
5143 RECT client_rect;
5144
5145 if (!swapchain_desc->windowed)
5146 return WINED3DERR_INVALIDCALL;
5147
5148 if (!GetClientRect(swapchain->device_window, &client_rect))
5149 {
5150 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
5151 return WINED3DERR_INVALIDCALL;
5152 }
5153
5154 if (!backbuffer_width)
5155 backbuffer_width = client_rect.right;
5156
5157 if (!backbuffer_height)
5158 backbuffer_height = client_rect.bottom;
5159 }
5160
5161 if (backbuffer_width != swapchain->desc.backbuffer_width
5162 || backbuffer_height != swapchain->desc.backbuffer_height)
5163 {
5164 if (!swapchain_desc->windowed)
5165 DisplayModeChanged = TRUE;
5166
5167 swapchain->desc.backbuffer_width = backbuffer_width;
5168 swapchain->desc.backbuffer_height = backbuffer_height;
5169 update_desc = TRUE;
5170 }
5171
5172 if (swapchain_desc->backbuffer_format != WINED3DFMT_UNKNOWN
5173 && swapchain_desc->backbuffer_format != swapchain->desc.backbuffer_format)
5174 {
5175 swapchain->desc.backbuffer_format = swapchain_desc->backbuffer_format;
5176 update_desc = TRUE;
5177 }
5178
5179 if (swapchain_desc->multisample_type != swapchain->desc.multisample_type
5180 || swapchain_desc->multisample_quality != swapchain->desc.multisample_quality)
5181 {
5182 swapchain->desc.multisample_type = swapchain_desc->multisample_type;
5183 swapchain->desc.multisample_quality = swapchain_desc->multisample_quality;
5184 update_desc = TRUE;
5185 }
5186
5187 if (update_desc)
5188 {
5189 UINT i;
5190
5191 if (FAILED(hr = wined3d_surface_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
5192 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5193 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5194 return hr;
5195
5196 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
5197 {
5198 if (FAILED(hr = wined3d_surface_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
5199 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5200 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5201 return hr;
5202 }
5203 if (device->auto_depth_stencil)
5204 {
5205 if (FAILED(hr = wined3d_surface_update_desc(device->auto_depth_stencil, swapchain->desc.backbuffer_width,
5206 swapchain->desc.backbuffer_height, device->auto_depth_stencil->resource.format->id,
5207 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5208 return hr;
5209 }
5210 }
5211
5212 if (!swapchain_desc->windowed != !swapchain->desc.windowed
5213 || DisplayModeChanged)
5214 {
5215 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
5216 {
5217 WARN("Failed to set display mode, hr %#x.\n", hr);
5218 return WINED3DERR_INVALIDCALL;
5219 }
5220
5221 if (!swapchain_desc->windowed)
5222 {
5223 if (swapchain->desc.windowed)
5224 {
5225 HWND focus_window = device->create_parms.focus_window;
5226 if (!focus_window)
5227 focus_window = swapchain_desc->device_window;
5228 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5229 {
5230 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5231 return hr;
5232 }
5233
5234 /* switch from windowed to fs */
5235 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5236 swapchain_desc->backbuffer_width,
5237 swapchain_desc->backbuffer_height);
5238 }
5239 else
5240 {
5241 /* Fullscreen -> fullscreen mode change */
5242 MoveWindow(swapchain->device_window, 0, 0,
5243 swapchain_desc->backbuffer_width,
5244 swapchain_desc->backbuffer_height,
5245 TRUE);
5246 }
5247 }
5248 else if (!swapchain->desc.windowed)
5249 {
5250 /* Fullscreen -> windowed switch */
5251 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
5252 wined3d_device_release_focus_window(device);
5253 }
5254 swapchain->desc.windowed = swapchain_desc->windowed;
5255 }
5256 else if (!swapchain_desc->windowed)
5257 {
5258 DWORD style = device->style;
5259 DWORD exStyle = device->exStyle;
5260 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
5261 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
5262 * Reset to clear up their mess. Guild Wars also loses the device during that.
5263 */
5264 device->style = 0;
5265 device->exStyle = 0;
5266 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5267 swapchain_desc->backbuffer_width,
5268 swapchain_desc->backbuffer_height);
5269 device->style = style;
5270 device->exStyle = exStyle;
5271 }
5272
5273 if (reset_state)
5274 {
5275 TRACE("Resetting stateblock.\n");
5276 wined3d_stateblock_decref(device->updateStateBlock);
5277 wined3d_stateblock_decref(device->stateBlock);
5278
5279 if (device->d3d_initialized)
5280 delete_opengl_contexts(device, swapchain);
5281
5282 /* Note: No parent needed for initial internal stateblock */
5283 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5284 if (FAILED(hr))
5285 ERR("Resetting the stateblock failed with error %#x.\n", hr);
5286 else
5287 TRACE("Created stateblock %p.\n", device->stateBlock);
5288 device->updateStateBlock = device->stateBlock;
5289 wined3d_stateblock_incref(device->updateStateBlock);
5290
5291 stateblock_init_default_state(device->stateBlock);
5292 }
5293 else
5294 {
5295 struct wined3d_surface *rt = device->fb.render_targets[0];
5296 struct wined3d_state *state = &device->stateBlock->state;
5297
5298 /* Note the min_z / max_z is not reset. */
5299 state->viewport.x = 0;
5300 state->viewport.y = 0;
5301 state->viewport.width = rt->resource.width;
5302 state->viewport.height = rt->resource.height;
5303 device_invalidate_state(device, STATE_VIEWPORT);
5304
5305 state->scissor_rect.top = 0;
5306 state->scissor_rect.left = 0;
5307 state->scissor_rect.right = rt->resource.width;
5308 state->scissor_rect.bottom = rt->resource.height;
5309 device_invalidate_state(device, STATE_SCISSORRECT);
5310 }
5311
5312 swapchain_update_render_to_fbo(swapchain);
5313 swapchain_update_draw_bindings(swapchain);
5314
5315 if (reset_state && device->d3d_initialized)
5316 hr = create_primary_opengl_context(device, swapchain);
5317
5318 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5319 * first use
5320 */
5321 return hr;
5322#else
5323 ERR("not supported!");
5324 return E_FAIL;
5325#endif
5326}
5327
5328HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5329{
5330 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5331
5332 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5333
5334 return WINED3D_OK;
5335}
5336
5337
5338void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5339 struct wined3d_device_creation_parameters *parameters)
5340{
5341 TRACE("device %p, parameters %p.\n", device, parameters);
5342
5343 *parameters = device->create_parms;
5344}
5345
5346void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5347 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5348{
5349 struct wined3d_swapchain *swapchain;
5350
5351 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5352 device, swapchain_idx, flags, ramp);
5353
5354 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5355 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5356}
5357
5358void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5359 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5360{
5361 struct wined3d_swapchain *swapchain;
5362
5363 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5364 device, swapchain_idx, ramp);
5365
5366 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5367 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5368}
5369
5370void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5371{
5372 TRACE("device %p, resource %p.\n", device, resource);
5373
5374 list_add_head(&device->resources, &resource->resource_list_entry);
5375}
5376
5377static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5378{
5379 TRACE("device %p, resource %p.\n", device, resource);
5380
5381 list_remove(&resource->resource_list_entry);
5382}
5383
5384void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5385{
5386 enum wined3d_resource_type type = resource->type;
5387 unsigned int i;
5388
5389 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5390
5391 context_resource_released(device, resource, type);
5392
5393 switch (type)
5394 {
5395 case WINED3D_RTYPE_SURFACE:
5396 {
5397 struct wined3d_surface *surface = surface_from_resource(resource);
5398
5399 if (!device->d3d_initialized) break;
5400
5401 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
5402 {
5403 if (device->fb.render_targets[i] == surface)
5404 {
5405 ERR("Surface %p is still in use as render target %u.\n", surface, i);
5406 device->fb.render_targets[i] = NULL;
5407 }
5408 }
5409
5410 if (device->fb.depth_stencil == surface)
5411 {
5412 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
5413 device->fb.depth_stencil = NULL;
5414 }
5415 }
5416 break;
5417
5418 case WINED3D_RTYPE_TEXTURE:
5419 case WINED3D_RTYPE_CUBE_TEXTURE:
5420 case WINED3D_RTYPE_VOLUME_TEXTURE:
5421 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5422 {
5423 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
5424
5425 if (device->stateBlock && device->stateBlock->state.textures[i] == texture)
5426 {
5427 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5428 texture, device->stateBlock, i);
5429 device->stateBlock->state.textures[i] = NULL;
5430 }
5431
5432 if (device->updateStateBlock != device->stateBlock
5433 && device->updateStateBlock->state.textures[i] == texture)
5434 {
5435 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5436 texture, device->updateStateBlock, i);
5437 device->updateStateBlock->state.textures[i] = NULL;
5438 }
5439 }
5440 break;
5441
5442 case WINED3D_RTYPE_BUFFER:
5443 {
5444 struct wined3d_buffer *buffer = buffer_from_resource(resource);
5445
5446 for (i = 0; i < MAX_STREAMS; ++i)
5447 {
5448 if (device->stateBlock && device->stateBlock->state.streams[i].buffer == buffer)
5449 {
5450 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5451 buffer, device->stateBlock, i);
5452 device->stateBlock->state.streams[i].buffer = NULL;
5453 }
5454
5455 if (device->updateStateBlock != device->stateBlock
5456 && device->updateStateBlock->state.streams[i].buffer == buffer)
5457 {
5458 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5459 buffer, device->updateStateBlock, i);
5460 device->updateStateBlock->state.streams[i].buffer = NULL;
5461 }
5462
5463 }
5464
5465 if (device->stateBlock && device->stateBlock->state.index_buffer == buffer)
5466 {
5467 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5468 buffer, device->stateBlock);
5469 device->stateBlock->state.index_buffer = NULL;
5470 }
5471
5472 if (device->updateStateBlock != device->stateBlock
5473 && device->updateStateBlock->state.index_buffer == buffer)
5474 {
5475 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5476 buffer, device->updateStateBlock);
5477 device->updateStateBlock->state.index_buffer = NULL;
5478 }
5479 }
5480 break;
5481
5482 default:
5483 break;
5484 }
5485
5486 /* Remove the resource from the resourceStore */
5487 device_resource_remove(device, resource);
5488
5489 TRACE("Resource released.\n");
5490}
5491
5492struct wined3d_surface * CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device, HDC dc)
5493{
5494 struct wined3d_resource *resource;
5495
5496 TRACE("device %p, dc %p.\n", device, dc);
5497
5498 if (!dc)
5499 return NULL;
5500
5501 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
5502 {
5503 if (resource->type == WINED3D_RTYPE_SURFACE)
5504 {
5505 struct wined3d_surface *s = surface_from_resource(resource);
5506
5507 if (s->hDC == dc)
5508 {
5509 TRACE("Found surface %p for dc %p.\n", s, dc);
5510 return s;
5511 }
5512 }
5513 }
5514
5515 return NULL;
5516}
5517
5518HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5519 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5520 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5521{
5522 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5523 const struct fragment_pipeline *fragment_pipeline;
5524 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5525 unsigned int i;
5526 HRESULT hr;
5527
5528 device->ref = 1;
5529 device->wined3d = wined3d;
5530 wined3d_incref(device->wined3d);
5531 device->adapter = wined3d->adapter_count ? adapter : NULL;
5532 device->device_parent = device_parent;
5533 list_init(&device->resources);
5534 list_init(&device->shaders);
5535 device->surface_alignment = surface_alignment;
5536
5537 /* Save the creation parameters. */
5538 device->create_parms.adapter_idx = adapter_idx;
5539 device->create_parms.device_type = device_type;
5540 device->create_parms.focus_window = focus_window;
5541 device->create_parms.flags = flags;
5542
5543#ifdef VBOX_WINE_WITH_PROFILE
5544 VBOXWINEPROFILE_DRAWPRIM_INIT(&device->DrawPrimProfile);
5545#endif
5546
5547 device->shader_backend = adapter->shader_backend;
5548
5549 vertex_pipeline = adapter->vertex_pipe;
5550
5551 fragment_pipeline = adapter->fragment_pipe;
5552
5553 if (vertex_pipeline->vp_states && fragment_pipeline->states
5554 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5555 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5556 fragment_pipeline, misc_state_template)))
5557 {
5558 ERR("Failed to compile state table, hr %#x.\n", hr);
5559 wined3d_decref(device->wined3d);
5560 return hr;
5561 }
5562
5563 device->blitter = adapter->blitter;
5564
5565 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5566 if (FAILED(hr))
5567 {
5568 WARN("Failed to create stateblock.\n");
5569 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5570 {
5571 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5572 }
5573 wined3d_decref(device->wined3d);
5574 return hr;
5575 }
5576
5577 TRACE("Created stateblock %p.\n", device->stateBlock);
5578 device->updateStateBlock = device->stateBlock;
5579 wined3d_stateblock_incref(device->updateStateBlock);
5580
5581#ifdef VBOX_WINE_WITH_SHADER_CACHE
5582 shader_chaches_init(device);
5583#endif
5584
5585 return WINED3D_OK;
5586}
5587
5588
5589void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5590{
5591 DWORD rep = device->StateTable[state].representative;
5592 struct wined3d_context *context;
5593 DWORD idx;
5594 BYTE shift;
5595 UINT i;
5596
5597 for (i = 0; i < device->context_count; ++i)
5598 {
5599 context = device->contexts[i];
5600 if(isStateDirty(context, rep)) continue;
5601
5602 context->dirtyArray[context->numDirtyEntries++] = rep;
5603 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5604 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5605 context->isStateDirty[idx] |= (1 << shift);
5606 }
5607}
5608
5609void get_drawable_size_fbo(const struct wined3d_context *context, UINT *width, UINT *height)
5610{
5611 /* The drawable size of a fbo target is the opengl texture size, which is the power of two size. */
5612 *width = context->current_rt->pow2Width;
5613 *height = context->current_rt->pow2Height;
5614}
5615
5616void get_drawable_size_backbuffer(const struct wined3d_context *context, UINT *width, UINT *height)
5617{
5618 const struct wined3d_swapchain *swapchain = context->swapchain;
5619 /* The drawable size of a backbuffer / aux buffer offscreen target is the size of the
5620 * current context's drawable, which is the size of the back buffer of the swapchain
5621 * the active context belongs to. */
5622 *width = swapchain->desc.backbuffer_width;
5623 *height = swapchain->desc.backbuffer_height;
5624}
5625
5626#ifndef VBOX_WITH_WDDM
5627LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5628 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5629{
5630 if (device->filter_messages)
5631 {
5632 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5633 window, message, wparam, lparam);
5634 if (unicode)
5635 return DefWindowProcW(window, message, wparam, lparam);
5636 else
5637 return DefWindowProcA(window, message, wparam, lparam);
5638 }
5639
5640 if (message == WM_DESTROY)
5641 {
5642 TRACE("unregister window %p.\n", window);
5643 wined3d_unregister_window(window);
5644
5645 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5646 ERR("Window %p is not the focus window for device %p.\n", window, device);
5647 }
5648 else if (message == WM_DISPLAYCHANGE)
5649 {
5650 device->device_parent->ops->mode_changed(device->device_parent);
5651 }
5652
5653 if (unicode)
5654 return CallWindowProcW(proc, window, message, wparam, lparam);
5655 else
5656 return CallWindowProcA(proc, window, message, wparam, lparam);
5657}
5658
5659#else
5660HRESULT CDECL wined3d_device_flush(struct wined3d_device *device)
5661{
5662 /* we use only one context, so just acquiring it here should do the job,
5663 * @todo: perhaps we should introduce the mechanism similar to the one used in flush_to_host
5664 * to avoid context acquisition */
5665 struct wined3d_context *context = context_acquire(device, NULL);
5666 Assert(context->valid);
5667 context->gl_info->gl_ops.gl.p_glFlush();
5668 context_release(context);
5669 return WINED3D_OK;
5670}
5671
5672HRESULT CDECL wined3d_device_flush_to_host(struct wined3d_device *device)
5673{
5674 struct wined3d_context *context;
5675 int i;
5676
5677 /* no context acquisition is needed */
5678 for (i = 0; i < device->context_count; ++i)
5679 {
5680 context = device->contexts[i];
5681 pVBoxFlushToHost(context->glCtx);
5682 }
5683 return WINED3D_OK;
5684}
5685
5686HRESULT CDECL wined3d_device_finish(struct wined3d_device *device)
5687{
5688 /* we use only one context, so just acquiring it here should do the job,
5689 * @todo: perhaps we should introduce the mechanism similar to the one used in flush_to_host
5690 * to avoid context acquisition */
5691 struct wined3d_context *context = context_acquire(device, NULL);
5692 Assert(context->valid);
5693 context->gl_info->gl_ops.gl.p_glFinish();
5694 context_release(context);
5695 return WINED3D_OK;
5696
5697}
5698
5699HRESULT CDECL wined3d_device_get_host_id(struct wined3d_device *device, int32_t *pid)
5700{
5701 int32_t id = pVBoxGetContextId(device->contexts[0]->glCtx);
5702 if (!id)
5703 {
5704 *pid = 0;
5705 ERR("pVBoxGetContextId to get id for context 0x%x", device->contexts[0]->glCtx);
5706 return E_FAIL;
5707 }
5708
5709 *pid = id;
5710 return WINED3D_OK;
5711}
5712#endif
5713
5714
5715#ifdef VBOX_WITH_WINE_FIX_ZEROVERTATTR
5716static GLuint zv_value_el_size(GLenum enmzvValue)
5717{
5718 switch (enmzvValue)
5719 {
5720 case GL_FLOAT:
5721 return 4;
5722 case GL_UNSIGNED_SHORT:
5723 case GL_SHORT:
5724 return 2;
5725 case GL_BYTE:
5726 case GL_UNSIGNED_BYTE:
5727 return 1;
5728 default:
5729 ERR("unexpected value type %#x\n", enmzvValue);
5730 return 0;
5731 }
5732}
5733
5734static void zv_create(struct wined3d_device *device, GLenum enmzvValue, GLuint czvValue, GLuint czvValueElements, const GLvoid *pzvValue)
5735{
5736 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
5737 GLuint cbValel = zv_value_el_size(enmzvValue);
5738 GLuint cbVal = cbValel * czvValueElements;
5739 GLuint cbBuf = cbVal * czvValue;
5740 GLvoid *pvBuf;
5741 GLubyte *pubBuf;
5742 GLuint i;
5743
5744 /* quickly sort out if we can use the current value */
5745 if (device->zvBuffer
5746 && device->enmzvValue == enmzvValue
5747 && device->czvValue >= czvValue
5748 && device->czvValueElements == czvValueElements
5749 && !memcmp(pzvValue, &device->zvValue, cbVal))
5750 return;
5751
5752 if (czvValueElements > 4)
5753 {
5754 ERR("invalid czvValueElements %d\n", czvValueElements);
5755 return;
5756 }
5757
5758 pvBuf = HeapAlloc(GetProcessHeap(), 0, cbBuf);
5759 pubBuf = (GLubyte*)pvBuf;
5760
5761 for (i = 0; i < czvValue; ++i)
5762 {
5763 memcpy(pubBuf, pzvValue, cbVal);
5764 pubBuf += cbVal;
5765 }
5766
5767 /* */
5768 if (!device->zvBuffer)
5769 {
5770 GL_EXTCALL(glGenBuffersARB(1, &device->zvBuffer));
5771 Assert(device->zvBuffer);
5772 }
5773
5774 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, device->zvBuffer));
5775
5776 if (device->cbzvBuffer < cbBuf)
5777 {
5778 GL_EXTCALL(glBufferDataARB(GL_ARRAY_BUFFER_ARB, cbBuf, pvBuf, GL_DYNAMIC_DRAW_ARB));
5779 device->cbzvBuffer = cbBuf;
5780 }
5781 else
5782 {
5783 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, cbBuf, pvBuf));
5784 }
5785
5786 device->enmzvValue = enmzvValue;
5787 device->czvValue = czvValue;
5788 device->czvValueElements = czvValueElements;
5789 memcpy(&device->zvValue, pzvValue, cbVal);
5790
5791 HeapFree(GetProcessHeap(), 0, pvBuf);
5792}
5793
5794void zv_destroy(struct wined3d_device *device)
5795{
5796 if (device->zvBuffer)
5797 {
5798 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
5799 GL_EXTCALL(glDeleteBuffersARB(1, &device->zvBuffer));
5800 }
5801
5802 device->zvBuffer = 0;
5803 device->cbzvBuffer = 0;
5804}
5805
5806void zv_bind(struct wined3d_context *context, GLenum enmzvValue, GLuint czvValue, GLuint czvValueElements, GLboolean bzvNormalized, const GLvoid *pzvValue)
5807{
5808 struct wined3d_device *device = context->swapchain->device;
5809 const struct wined3d_gl_info *gl_info = context->gl_info;
5810
5811 zv_create(device, enmzvValue, czvValue, czvValueElements, pzvValue);
5812
5813 Assert(device->zvBuffer);
5814
5815 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, device->zvBuffer));
5816
5817 GL_EXTCALL(glVertexAttribPointerARB(0, czvValueElements,
5818 enmzvValue,
5819 bzvNormalized,
5820 0, /*stride*/
5821 NULL /*addr*/));
5822
5823 if (!(context->numbered_array_mask & (1 << 0)))
5824 {
5825 GL_EXTCALL(glEnableVertexAttribArrayARB(0));
5826 context->numbered_array_mask |= (1 << 0);
5827 }
5828}
5829
5830void zv_bind_by_element(struct wined3d_context *context, const struct wined3d_stream_info_element *element, GLuint czvValue, const GLvoid *pzvValue)
5831{
5832 zv_bind(context, element->format->gl_vtx_type, czvValue, element->format->gl_vtx_format, element->format->gl_normalized, pzvValue);
5833}
5834
5835#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette