VirtualBox

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

最後變更 在這個檔案從46545是 46521,由 vboxsync 提交於 12 年 前

wine/new: export

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

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