d2d1: Partially implement d2d_device_context_DrawImage().
[wine.git] / dlls / wined3d / cs.c
blob0b9c5f7004369179eaa7855925f94d0076693f62
1 /*
2 * Copyright 2013 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
21 #include "wined3d_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
24 WINE_DECLARE_DEBUG_CHANNEL(d3d_sync);
25 WINE_DECLARE_DEBUG_CHANNEL(fps);
27 #define WINED3D_INITIAL_CS_SIZE 4096
29 enum wined3d_cs_op
31 WINED3D_CS_OP_NOP,
32 WINED3D_CS_OP_PRESENT,
33 WINED3D_CS_OP_CLEAR,
34 WINED3D_CS_OP_DISPATCH,
35 WINED3D_CS_OP_DRAW,
36 WINED3D_CS_OP_FLUSH,
37 WINED3D_CS_OP_SET_PREDICATION,
38 WINED3D_CS_OP_SET_VIEWPORTS,
39 WINED3D_CS_OP_SET_SCISSOR_RECTS,
40 WINED3D_CS_OP_SET_RENDERTARGET_VIEW,
41 WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW,
42 WINED3D_CS_OP_SET_VERTEX_DECLARATION,
43 WINED3D_CS_OP_SET_STREAM_SOURCE,
44 WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ,
45 WINED3D_CS_OP_SET_STREAM_OUTPUT,
46 WINED3D_CS_OP_SET_INDEX_BUFFER,
47 WINED3D_CS_OP_SET_CONSTANT_BUFFER,
48 WINED3D_CS_OP_SET_TEXTURE,
49 WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW,
50 WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW,
51 WINED3D_CS_OP_SET_SAMPLER,
52 WINED3D_CS_OP_SET_SHADER,
53 WINED3D_CS_OP_SET_BLEND_STATE,
54 WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE,
55 WINED3D_CS_OP_SET_RASTERIZER_STATE,
56 WINED3D_CS_OP_SET_RENDER_STATE,
57 WINED3D_CS_OP_SET_TEXTURE_STATE,
58 WINED3D_CS_OP_SET_SAMPLER_STATE,
59 WINED3D_CS_OP_SET_TRANSFORM,
60 WINED3D_CS_OP_SET_CLIP_PLANE,
61 WINED3D_CS_OP_SET_COLOR_KEY,
62 WINED3D_CS_OP_SET_MATERIAL,
63 WINED3D_CS_OP_SET_LIGHT,
64 WINED3D_CS_OP_SET_LIGHT_ENABLE,
65 WINED3D_CS_OP_PUSH_CONSTANTS,
66 WINED3D_CS_OP_RESET_STATE,
67 WINED3D_CS_OP_CALLBACK,
68 WINED3D_CS_OP_QUERY_ISSUE,
69 WINED3D_CS_OP_PRELOAD_RESOURCE,
70 WINED3D_CS_OP_UNLOAD_RESOURCE,
71 WINED3D_CS_OP_MAP,
72 WINED3D_CS_OP_UNMAP,
73 WINED3D_CS_OP_BLT_SUB_RESOURCE,
74 WINED3D_CS_OP_UPDATE_SUB_RESOURCE,
75 WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION,
76 WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW,
77 WINED3D_CS_OP_COPY_UAV_COUNTER,
78 WINED3D_CS_OP_GENERATE_MIPMAPS,
79 WINED3D_CS_OP_STOP,
82 struct wined3d_cs_packet
84 size_t size;
85 BYTE data[1];
88 struct wined3d_cs_nop
90 enum wined3d_cs_op opcode;
93 struct wined3d_cs_present
95 enum wined3d_cs_op opcode;
96 HWND dst_window_override;
97 struct wined3d_swapchain *swapchain;
98 RECT src_rect;
99 RECT dst_rect;
100 unsigned int swap_interval;
101 DWORD flags;
104 struct wined3d_cs_clear
106 enum wined3d_cs_op opcode;
107 DWORD flags;
108 unsigned int rt_count;
109 struct wined3d_fb_state *fb;
110 RECT draw_rect;
111 struct wined3d_color color;
112 float depth;
113 DWORD stencil;
114 unsigned int rect_count;
115 RECT rects[1];
118 struct wined3d_cs_dispatch
120 enum wined3d_cs_op opcode;
121 struct wined3d_dispatch_parameters parameters;
124 struct wined3d_cs_draw
126 enum wined3d_cs_op opcode;
127 enum wined3d_primitive_type primitive_type;
128 GLint patch_vertex_count;
129 struct wined3d_draw_parameters parameters;
132 struct wined3d_cs_flush
134 enum wined3d_cs_op opcode;
137 struct wined3d_cs_set_predication
139 enum wined3d_cs_op opcode;
140 struct wined3d_query *predicate;
141 BOOL value;
144 struct wined3d_cs_set_viewports
146 enum wined3d_cs_op opcode;
147 unsigned int viewport_count;
148 struct wined3d_viewport viewports[1];
151 struct wined3d_cs_set_scissor_rects
153 enum wined3d_cs_op opcode;
154 unsigned int rect_count;
155 RECT rects[1];
158 struct wined3d_cs_set_rendertarget_view
160 enum wined3d_cs_op opcode;
161 unsigned int view_idx;
162 struct wined3d_rendertarget_view *view;
165 struct wined3d_cs_set_depth_stencil_view
167 enum wined3d_cs_op opcode;
168 struct wined3d_rendertarget_view *view;
171 struct wined3d_cs_set_vertex_declaration
173 enum wined3d_cs_op opcode;
174 struct wined3d_vertex_declaration *declaration;
177 struct wined3d_cs_set_stream_source
179 enum wined3d_cs_op opcode;
180 UINT stream_idx;
181 struct wined3d_buffer *buffer;
182 UINT offset;
183 UINT stride;
186 struct wined3d_cs_set_stream_source_freq
188 enum wined3d_cs_op opcode;
189 UINT stream_idx;
190 UINT frequency;
191 UINT flags;
194 struct wined3d_cs_set_stream_output
196 enum wined3d_cs_op opcode;
197 UINT stream_idx;
198 struct wined3d_buffer *buffer;
199 UINT offset;
202 struct wined3d_cs_set_index_buffer
204 enum wined3d_cs_op opcode;
205 struct wined3d_buffer *buffer;
206 enum wined3d_format_id format_id;
207 unsigned int offset;
210 struct wined3d_cs_set_constant_buffer
212 enum wined3d_cs_op opcode;
213 enum wined3d_shader_type type;
214 UINT cb_idx;
215 struct wined3d_buffer *buffer;
218 struct wined3d_cs_set_texture
220 enum wined3d_cs_op opcode;
221 UINT stage;
222 struct wined3d_texture *texture;
225 struct wined3d_cs_set_color_key
227 enum wined3d_cs_op opcode;
228 struct wined3d_texture *texture;
229 WORD flags;
230 WORD set;
231 struct wined3d_color_key color_key;
234 struct wined3d_cs_set_shader_resource_view
236 enum wined3d_cs_op opcode;
237 enum wined3d_shader_type type;
238 UINT view_idx;
239 struct wined3d_shader_resource_view *view;
242 struct wined3d_cs_set_unordered_access_view
244 enum wined3d_cs_op opcode;
245 enum wined3d_pipeline pipeline;
246 unsigned int view_idx;
247 struct wined3d_unordered_access_view *view;
248 unsigned int initial_count;
251 struct wined3d_cs_set_sampler
253 enum wined3d_cs_op opcode;
254 enum wined3d_shader_type type;
255 UINT sampler_idx;
256 struct wined3d_sampler *sampler;
259 struct wined3d_cs_set_shader
261 enum wined3d_cs_op opcode;
262 enum wined3d_shader_type type;
263 struct wined3d_shader *shader;
266 struct wined3d_cs_set_blend_state
268 enum wined3d_cs_op opcode;
269 struct wined3d_blend_state *state;
270 struct wined3d_color factor;
271 unsigned int sample_mask;
274 struct wined3d_cs_set_depth_stencil_state
276 enum wined3d_cs_op opcode;
277 struct wined3d_depth_stencil_state *state;
280 struct wined3d_cs_set_rasterizer_state
282 enum wined3d_cs_op opcode;
283 struct wined3d_rasterizer_state *state;
286 struct wined3d_cs_set_render_state
288 enum wined3d_cs_op opcode;
289 enum wined3d_render_state state;
290 DWORD value;
293 struct wined3d_cs_set_texture_state
295 enum wined3d_cs_op opcode;
296 UINT stage;
297 enum wined3d_texture_stage_state state;
298 DWORD value;
301 struct wined3d_cs_set_sampler_state
303 enum wined3d_cs_op opcode;
304 UINT sampler_idx;
305 enum wined3d_sampler_state state;
306 DWORD value;
309 struct wined3d_cs_set_transform
311 enum wined3d_cs_op opcode;
312 enum wined3d_transform_state state;
313 struct wined3d_matrix matrix;
316 struct wined3d_cs_set_clip_plane
318 enum wined3d_cs_op opcode;
319 UINT plane_idx;
320 struct wined3d_vec4 plane;
323 struct wined3d_cs_set_material
325 enum wined3d_cs_op opcode;
326 struct wined3d_material material;
329 struct wined3d_cs_set_light
331 enum wined3d_cs_op opcode;
332 struct wined3d_light_info light;
335 struct wined3d_cs_set_light_enable
337 enum wined3d_cs_op opcode;
338 unsigned int idx;
339 BOOL enable;
342 struct wined3d_cs_push_constants
344 enum wined3d_cs_op opcode;
345 enum wined3d_push_constants type;
346 unsigned int start_idx;
347 unsigned int count;
348 BYTE constants[1];
351 struct wined3d_cs_reset_state
353 enum wined3d_cs_op opcode;
356 struct wined3d_cs_callback
358 enum wined3d_cs_op opcode;
359 void (*callback)(void *object);
360 void *object;
363 struct wined3d_cs_query_issue
365 enum wined3d_cs_op opcode;
366 struct wined3d_query *query;
367 DWORD flags;
370 struct wined3d_cs_preload_resource
372 enum wined3d_cs_op opcode;
373 struct wined3d_resource *resource;
376 struct wined3d_cs_unload_resource
378 enum wined3d_cs_op opcode;
379 struct wined3d_resource *resource;
382 struct wined3d_cs_map
384 enum wined3d_cs_op opcode;
385 struct wined3d_resource *resource;
386 unsigned int sub_resource_idx;
387 struct wined3d_map_desc *map_desc;
388 const struct wined3d_box *box;
389 DWORD flags;
390 HRESULT *hr;
393 struct wined3d_cs_unmap
395 enum wined3d_cs_op opcode;
396 struct wined3d_resource *resource;
397 unsigned int sub_resource_idx;
398 HRESULT *hr;
401 struct wined3d_cs_blt_sub_resource
403 enum wined3d_cs_op opcode;
404 struct wined3d_resource *dst_resource;
405 unsigned int dst_sub_resource_idx;
406 struct wined3d_box dst_box;
407 struct wined3d_resource *src_resource;
408 unsigned int src_sub_resource_idx;
409 struct wined3d_box src_box;
410 DWORD flags;
411 struct wined3d_blt_fx fx;
412 enum wined3d_texture_filter_type filter;
415 struct wined3d_cs_update_sub_resource
417 enum wined3d_cs_op opcode;
418 struct wined3d_resource *resource;
419 unsigned int sub_resource_idx;
420 struct wined3d_box box;
421 struct wined3d_sub_resource_data data;
424 struct wined3d_cs_add_dirty_texture_region
426 enum wined3d_cs_op opcode;
427 struct wined3d_texture *texture;
428 unsigned int layer;
431 struct wined3d_cs_clear_unordered_access_view
433 enum wined3d_cs_op opcode;
434 struct wined3d_unordered_access_view *view;
435 struct wined3d_uvec4 clear_value;
438 struct wined3d_cs_copy_uav_counter
440 enum wined3d_cs_op opcode;
441 struct wined3d_buffer *buffer;
442 unsigned int offset;
443 struct wined3d_unordered_access_view *view;
446 struct wined3d_cs_generate_mipmaps
448 enum wined3d_cs_op opcode;
449 struct wined3d_shader_resource_view *view;
452 struct wined3d_cs_stop
454 enum wined3d_cs_op opcode;
457 static inline void *wined3d_cs_require_space(struct wined3d_cs *cs,
458 size_t size, enum wined3d_cs_queue_id queue_id)
460 return cs->ops->require_space(cs, size, queue_id);
463 static inline void wined3d_cs_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
465 cs->ops->submit(cs, queue_id);
468 static const char *debug_cs_op(enum wined3d_cs_op op)
470 switch (op)
472 #define WINED3D_TO_STR(type) case type: return #type
473 WINED3D_TO_STR(WINED3D_CS_OP_NOP);
474 WINED3D_TO_STR(WINED3D_CS_OP_PRESENT);
475 WINED3D_TO_STR(WINED3D_CS_OP_CLEAR);
476 WINED3D_TO_STR(WINED3D_CS_OP_DISPATCH);
477 WINED3D_TO_STR(WINED3D_CS_OP_DRAW);
478 WINED3D_TO_STR(WINED3D_CS_OP_FLUSH);
479 WINED3D_TO_STR(WINED3D_CS_OP_SET_PREDICATION);
480 WINED3D_TO_STR(WINED3D_CS_OP_SET_VIEWPORTS);
481 WINED3D_TO_STR(WINED3D_CS_OP_SET_SCISSOR_RECTS);
482 WINED3D_TO_STR(WINED3D_CS_OP_SET_RENDERTARGET_VIEW);
483 WINED3D_TO_STR(WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW);
484 WINED3D_TO_STR(WINED3D_CS_OP_SET_VERTEX_DECLARATION);
485 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_SOURCE);
486 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ);
487 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_OUTPUT);
488 WINED3D_TO_STR(WINED3D_CS_OP_SET_INDEX_BUFFER);
489 WINED3D_TO_STR(WINED3D_CS_OP_SET_CONSTANT_BUFFER);
490 WINED3D_TO_STR(WINED3D_CS_OP_SET_TEXTURE);
491 WINED3D_TO_STR(WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW);
492 WINED3D_TO_STR(WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW);
493 WINED3D_TO_STR(WINED3D_CS_OP_SET_SAMPLER);
494 WINED3D_TO_STR(WINED3D_CS_OP_SET_SHADER);
495 WINED3D_TO_STR(WINED3D_CS_OP_SET_BLEND_STATE);
496 WINED3D_TO_STR(WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE);
497 WINED3D_TO_STR(WINED3D_CS_OP_SET_RASTERIZER_STATE);
498 WINED3D_TO_STR(WINED3D_CS_OP_SET_RENDER_STATE);
499 WINED3D_TO_STR(WINED3D_CS_OP_SET_TEXTURE_STATE);
500 WINED3D_TO_STR(WINED3D_CS_OP_SET_SAMPLER_STATE);
501 WINED3D_TO_STR(WINED3D_CS_OP_SET_TRANSFORM);
502 WINED3D_TO_STR(WINED3D_CS_OP_SET_CLIP_PLANE);
503 WINED3D_TO_STR(WINED3D_CS_OP_SET_COLOR_KEY);
504 WINED3D_TO_STR(WINED3D_CS_OP_SET_MATERIAL);
505 WINED3D_TO_STR(WINED3D_CS_OP_SET_LIGHT);
506 WINED3D_TO_STR(WINED3D_CS_OP_SET_LIGHT_ENABLE);
507 WINED3D_TO_STR(WINED3D_CS_OP_PUSH_CONSTANTS);
508 WINED3D_TO_STR(WINED3D_CS_OP_RESET_STATE);
509 WINED3D_TO_STR(WINED3D_CS_OP_CALLBACK);
510 WINED3D_TO_STR(WINED3D_CS_OP_QUERY_ISSUE);
511 WINED3D_TO_STR(WINED3D_CS_OP_PRELOAD_RESOURCE);
512 WINED3D_TO_STR(WINED3D_CS_OP_UNLOAD_RESOURCE);
513 WINED3D_TO_STR(WINED3D_CS_OP_MAP);
514 WINED3D_TO_STR(WINED3D_CS_OP_UNMAP);
515 WINED3D_TO_STR(WINED3D_CS_OP_BLT_SUB_RESOURCE);
516 WINED3D_TO_STR(WINED3D_CS_OP_UPDATE_SUB_RESOURCE);
517 WINED3D_TO_STR(WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION);
518 WINED3D_TO_STR(WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW);
519 WINED3D_TO_STR(WINED3D_CS_OP_COPY_UAV_COUNTER);
520 WINED3D_TO_STR(WINED3D_CS_OP_GENERATE_MIPMAPS);
521 WINED3D_TO_STR(WINED3D_CS_OP_STOP);
522 #undef WINED3D_TO_STR
524 return wine_dbg_sprintf("UNKNOWN_OP(%#x)", op);
527 static void wined3d_cs_exec_nop(struct wined3d_cs *cs, const void *data)
531 static void wined3d_cs_exec_present(struct wined3d_cs *cs, const void *data)
533 struct wined3d_texture *logo_texture, *cursor_texture, *back_buffer;
534 struct wined3d_rendertarget_view *dsv = cs->state.fb.depth_stencil;
535 const struct wined3d_cs_present *op = data;
536 const struct wined3d_swapchain_desc *desc;
537 struct wined3d_swapchain *swapchain;
538 unsigned int i;
540 swapchain = op->swapchain;
541 desc = &swapchain->state.desc;
542 back_buffer = swapchain->back_buffers[0];
543 wined3d_swapchain_set_window(swapchain, op->dst_window_override);
545 if ((logo_texture = swapchain->device->logo_texture))
547 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
549 /* Blit the logo into the upper left corner of the back-buffer. */
550 wined3d_texture_blt(back_buffer, 0, &rect, logo_texture, 0,
551 &rect, WINED3D_BLT_SRC_CKEY, NULL, WINED3D_TEXF_POINT);
554 if ((cursor_texture = swapchain->device->cursor_texture)
555 && swapchain->device->bCursorVisible && !swapchain->device->hardwareCursor)
557 RECT dst_rect =
559 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
560 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
561 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
562 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
564 RECT src_rect =
566 0, 0, cursor_texture->resource.width, cursor_texture->resource.height
568 const RECT clip_rect = {0, 0, back_buffer->resource.width, back_buffer->resource.height};
570 TRACE("Rendering the software cursor.\n");
572 if (desc->windowed)
573 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
574 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
575 wined3d_texture_blt(back_buffer, 0, &dst_rect, cursor_texture, 0,
576 &src_rect, WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
579 swapchain->swapchain_ops->swapchain_present(swapchain, &op->src_rect, &op->dst_rect, op->swap_interval, op->flags);
581 /* Discard buffers if the swap effect allows it. */
582 back_buffer = swapchain->back_buffers[desc->backbuffer_count - 1];
583 if (desc->swap_effect == WINED3D_SWAP_EFFECT_DISCARD || desc->swap_effect == WINED3D_SWAP_EFFECT_FLIP_DISCARD)
584 wined3d_texture_validate_location(back_buffer, 0, WINED3D_LOCATION_DISCARDED);
586 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
588 struct wined3d_texture *ds = texture_from_resource(dsv->resource);
590 if ((desc->flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL || ds->flags & WINED3D_TEXTURE_DISCARD))
591 wined3d_rendertarget_view_validate_location(dsv, WINED3D_LOCATION_DISCARDED);
594 if (TRACE_ON(fps))
596 DWORD time = GetTickCount();
597 ++swapchain->frames;
599 /* every 1.5 seconds */
600 if (time - swapchain->prev_time > 1500)
602 TRACE_(fps)("%p @ approx %.2ffps\n",
603 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
604 swapchain->prev_time = time;
605 swapchain->frames = 0;
609 wined3d_resource_release(&swapchain->front_buffer->resource);
610 for (i = 0; i < desc->backbuffer_count; ++i)
612 wined3d_resource_release(&swapchain->back_buffers[i]->resource);
615 InterlockedDecrement(&cs->pending_presents);
618 void wined3d_cs_emit_present(struct wined3d_cs *cs, struct wined3d_swapchain *swapchain,
619 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
620 unsigned int swap_interval, DWORD flags)
622 struct wined3d_cs_present *op;
623 unsigned int i;
624 LONG pending;
626 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
627 op->opcode = WINED3D_CS_OP_PRESENT;
628 op->dst_window_override = dst_window_override;
629 op->swapchain = swapchain;
630 op->src_rect = *src_rect;
631 op->dst_rect = *dst_rect;
632 op->swap_interval = swap_interval;
633 op->flags = flags;
635 pending = InterlockedIncrement(&cs->pending_presents);
637 wined3d_resource_acquire(&swapchain->front_buffer->resource);
638 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
640 wined3d_resource_acquire(&swapchain->back_buffers[i]->resource);
643 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
645 /* Limit input latency by limiting the number of presents that we can get
646 * ahead of the worker thread. */
647 while (pending >= swapchain->max_frame_latency)
649 wined3d_pause();
650 pending = InterlockedCompareExchange(&cs->pending_presents, 0, 0);
654 static void wined3d_cs_exec_clear(struct wined3d_cs *cs, const void *data)
656 const struct wined3d_cs_clear *op = data;
657 struct wined3d_device *device;
658 unsigned int i;
660 device = cs->device;
661 device->blitter->ops->blitter_clear(device->blitter, device, op->rt_count, op->fb,
662 op->rect_count, op->rects, &op->draw_rect, op->flags, &op->color, op->depth, op->stencil);
664 if (op->flags & WINED3DCLEAR_TARGET)
666 for (i = 0; i < op->rt_count; ++i)
668 if (op->fb->render_targets[i])
669 wined3d_resource_release(op->fb->render_targets[i]->resource);
672 if (op->flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
673 wined3d_resource_release(op->fb->depth_stencil->resource);
676 void wined3d_cs_emit_clear(struct wined3d_cs *cs, DWORD rect_count, const RECT *rects,
677 DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
679 const struct wined3d_state *state = &cs->device->state;
680 const struct wined3d_viewport *vp = &state->viewports[0];
681 struct wined3d_rendertarget_view *view;
682 struct wined3d_cs_clear *op;
683 unsigned int rt_count, i;
685 rt_count = flags & WINED3DCLEAR_TARGET ? cs->device->adapter->d3d_info.limits.max_rt_count : 0;
687 op = wined3d_cs_require_space(cs, FIELD_OFFSET(struct wined3d_cs_clear, rects[rect_count]),
688 WINED3D_CS_QUEUE_DEFAULT);
689 op->opcode = WINED3D_CS_OP_CLEAR;
690 op->flags = flags & (WINED3DCLEAR_TARGET | WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
691 op->rt_count = rt_count;
692 op->fb = &cs->state.fb;
693 SetRect(&op->draw_rect, vp->x, vp->y, vp->x + vp->width, vp->y + vp->height);
694 if (state->rasterizer_state && state->rasterizer_state->desc.scissor)
695 IntersectRect(&op->draw_rect, &op->draw_rect, &state->scissor_rects[0]);
696 op->color = *color;
697 op->depth = depth;
698 op->stencil = stencil;
699 op->rect_count = rect_count;
700 memcpy(op->rects, rects, sizeof(*rects) * rect_count);
702 for (i = 0; i < rt_count; ++i)
704 if ((view = state->fb.render_targets[i]))
705 wined3d_resource_acquire(view->resource);
707 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
709 view = state->fb.depth_stencil;
710 wined3d_resource_acquire(view->resource);
713 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
716 void wined3d_cs_emit_clear_rendertarget_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view,
717 const RECT *rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
719 struct wined3d_cs_clear *op;
720 size_t size;
722 size = FIELD_OFFSET(struct wined3d_cs_clear, rects[1]) + sizeof(struct wined3d_fb_state);
723 op = wined3d_cs_require_space(cs, size, WINED3D_CS_QUEUE_DEFAULT);
724 op->fb = (void *)&op->rects[1];
726 op->opcode = WINED3D_CS_OP_CLEAR;
727 op->flags = flags & (WINED3DCLEAR_TARGET | WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
728 if (flags & WINED3DCLEAR_TARGET)
730 op->rt_count = 1;
731 op->fb->render_targets[0] = view;
732 op->fb->depth_stencil = NULL;
733 op->color = *color;
735 else
737 op->rt_count = 0;
738 op->fb->render_targets[0] = NULL;
739 op->fb->depth_stencil = view;
740 op->depth = depth;
741 op->stencil = stencil;
743 SetRect(&op->draw_rect, 0, 0, view->width, view->height);
744 op->rect_count = 1;
745 op->rects[0] = *rect;
747 wined3d_resource_acquire(view->resource);
749 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
750 if (flags & WINED3DCLEAR_SYNCHRONOUS)
751 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_DEFAULT);
754 static void acquire_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
756 struct wined3d_shader_sampler_map_entry *entry;
757 struct wined3d_shader_resource_view *view;
758 struct wined3d_shader *shader;
759 unsigned int i, j;
761 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
763 if (!(shader_mask & (1u << i)))
764 continue;
766 if (!(shader = state->shader[i]))
767 continue;
769 for (j = 0; j < WINED3D_MAX_CBS; ++j)
771 if (state->cb[i][j])
772 wined3d_resource_acquire(&state->cb[i][j]->resource);
775 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
777 entry = &shader->reg_maps.sampler_map.entries[j];
779 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
780 continue;
782 wined3d_resource_acquire(view->resource);
787 static void release_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
789 struct wined3d_shader_sampler_map_entry *entry;
790 struct wined3d_shader_resource_view *view;
791 struct wined3d_shader *shader;
792 unsigned int i, j;
794 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
796 if (!(shader_mask & (1u << i)))
797 continue;
799 if (!(shader = state->shader[i]))
800 continue;
802 for (j = 0; j < WINED3D_MAX_CBS; ++j)
804 if (state->cb[i][j])
805 wined3d_resource_release(&state->cb[i][j]->resource);
808 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
810 entry = &shader->reg_maps.sampler_map.entries[j];
812 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
813 continue;
815 wined3d_resource_release(view->resource);
820 static void acquire_unordered_access_resources(const struct wined3d_shader *shader,
821 struct wined3d_unordered_access_view * const *views)
823 unsigned int i;
825 if (!shader)
826 return;
828 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
830 if (!shader->reg_maps.uav_resource_info[i].type)
831 continue;
833 if (!views[i])
834 continue;
836 wined3d_resource_acquire(views[i]->resource);
840 static void release_unordered_access_resources(const struct wined3d_shader *shader,
841 struct wined3d_unordered_access_view * const *views)
843 unsigned int i;
845 if (!shader)
846 return;
848 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
850 if (!shader->reg_maps.uav_resource_info[i].type)
851 continue;
853 if (!views[i])
854 continue;
856 wined3d_resource_release(views[i]->resource);
860 static void wined3d_cs_exec_dispatch(struct wined3d_cs *cs, const void *data)
862 const struct wined3d_cs_dispatch *op = data;
863 struct wined3d_state *state = &cs->state;
865 if (!state->shader[WINED3D_SHADER_TYPE_COMPUTE])
866 WARN("No compute shader bound, skipping dispatch.\n");
867 else
868 cs->device->adapter->adapter_ops->adapter_dispatch_compute(cs->device, state, &op->parameters);
870 if (op->parameters.indirect)
871 wined3d_resource_release(&op->parameters.u.indirect.buffer->resource);
873 release_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
874 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
875 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
878 static void acquire_compute_pipeline_resources(const struct wined3d_state *state)
880 acquire_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
881 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
882 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
885 void wined3d_cs_emit_dispatch(struct wined3d_cs *cs,
886 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
888 const struct wined3d_state *state = &cs->device->state;
889 struct wined3d_cs_dispatch *op;
891 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
892 op->opcode = WINED3D_CS_OP_DISPATCH;
893 op->parameters.indirect = FALSE;
894 op->parameters.u.direct.group_count_x = group_count_x;
895 op->parameters.u.direct.group_count_y = group_count_y;
896 op->parameters.u.direct.group_count_z = group_count_z;
898 acquire_compute_pipeline_resources(state);
900 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
903 void wined3d_cs_emit_dispatch_indirect(struct wined3d_cs *cs,
904 struct wined3d_buffer *buffer, unsigned int offset)
906 const struct wined3d_state *state = &cs->device->state;
907 struct wined3d_cs_dispatch *op;
909 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
910 op->opcode = WINED3D_CS_OP_DISPATCH;
911 op->parameters.indirect = TRUE;
912 op->parameters.u.indirect.buffer = buffer;
913 op->parameters.u.indirect.offset = offset;
915 acquire_compute_pipeline_resources(state);
916 wined3d_resource_acquire(&buffer->resource);
918 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
921 static void wined3d_cs_exec_draw(struct wined3d_cs *cs, const void *data)
923 const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
924 const struct wined3d_shader *geometry_shader;
925 struct wined3d_device *device = cs->device;
926 int base_vertex_idx, load_base_vertex_idx;
927 struct wined3d_state *state = &cs->state;
928 const struct wined3d_cs_draw *op = data;
929 unsigned int i;
931 base_vertex_idx = 0;
932 if (!op->parameters.indirect)
934 const struct wined3d_direct_draw_parameters *direct = &op->parameters.u.direct;
936 if (op->parameters.indexed && d3d_info->draw_base_vertex_offset)
937 base_vertex_idx = direct->base_vertex_idx;
938 else if (!op->parameters.indexed)
939 base_vertex_idx = direct->start_idx;
942 /* ARB_draw_indirect always supports a base vertex offset. */
943 if (!op->parameters.indirect && !d3d_info->draw_base_vertex_offset)
944 load_base_vertex_idx = op->parameters.u.direct.base_vertex_idx;
945 else
946 load_base_vertex_idx = 0;
948 if (state->base_vertex_index != base_vertex_idx)
950 state->base_vertex_index = base_vertex_idx;
951 for (i = 0; i < device->context_count; ++i)
952 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_BASE_VERTEX_ID;
955 if (state->load_base_vertex_index != load_base_vertex_idx)
957 state->load_base_vertex_index = load_base_vertex_idx;
958 device_invalidate_state(cs->device, STATE_BASEVERTEXINDEX);
961 if (state->primitive_type != op->primitive_type)
963 if ((geometry_shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]) && !geometry_shader->function)
964 device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY));
965 if (state->primitive_type == WINED3D_PT_POINTLIST || op->primitive_type == WINED3D_PT_POINTLIST)
966 device_invalidate_state(cs->device, STATE_POINT_ENABLE);
967 state->primitive_type = op->primitive_type;
969 state->patch_vertex_count = op->patch_vertex_count;
971 cs->device->adapter->adapter_ops->adapter_draw_primitive(cs->device, state, &op->parameters);
973 if (op->parameters.indirect)
975 struct wined3d_buffer *buffer = op->parameters.u.indirect.buffer;
976 wined3d_resource_release(&buffer->resource);
979 if (op->parameters.indexed)
980 wined3d_resource_release(&state->index_buffer->resource);
981 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
983 if (state->streams[i].buffer)
984 wined3d_resource_release(&state->streams[i].buffer->resource);
986 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
988 if (state->stream_output[i].buffer)
989 wined3d_resource_release(&state->stream_output[i].buffer->resource);
991 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
993 if (state->textures[i])
994 wined3d_resource_release(&state->textures[i]->resource);
996 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
998 if (state->fb.render_targets[i])
999 wined3d_resource_release(state->fb.render_targets[i]->resource);
1001 if (state->fb.depth_stencil)
1002 wined3d_resource_release(state->fb.depth_stencil->resource);
1003 release_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
1004 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
1005 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
1008 static void acquire_graphics_pipeline_resources(const struct wined3d_state *state,
1009 BOOL indexed, const struct wined3d_d3d_info *d3d_info)
1011 unsigned int i;
1013 if (indexed)
1014 wined3d_resource_acquire(&state->index_buffer->resource);
1015 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
1017 if (state->streams[i].buffer)
1018 wined3d_resource_acquire(&state->streams[i].buffer->resource);
1020 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
1022 if (state->stream_output[i].buffer)
1023 wined3d_resource_acquire(&state->stream_output[i].buffer->resource);
1025 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
1027 if (state->textures[i])
1028 wined3d_resource_acquire(&state->textures[i]->resource);
1030 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
1032 if (state->fb.render_targets[i])
1033 wined3d_resource_acquire(state->fb.render_targets[i]->resource);
1035 if (state->fb.depth_stencil)
1036 wined3d_resource_acquire(state->fb.depth_stencil->resource);
1037 acquire_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
1038 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
1039 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
1042 void wined3d_cs_emit_draw(struct wined3d_cs *cs, enum wined3d_primitive_type primitive_type,
1043 unsigned int patch_vertex_count, int base_vertex_idx, unsigned int start_idx,
1044 unsigned int index_count, unsigned int start_instance, unsigned int instance_count, bool indexed)
1046 const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
1047 const struct wined3d_state *state = &cs->device->state;
1048 struct wined3d_cs_draw *op;
1050 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1051 op->opcode = WINED3D_CS_OP_DRAW;
1052 op->primitive_type = primitive_type;
1053 op->patch_vertex_count = patch_vertex_count;
1054 op->parameters.indirect = FALSE;
1055 op->parameters.u.direct.base_vertex_idx = base_vertex_idx;
1056 op->parameters.u.direct.start_idx = start_idx;
1057 op->parameters.u.direct.index_count = index_count;
1058 op->parameters.u.direct.start_instance = start_instance;
1059 op->parameters.u.direct.instance_count = instance_count;
1060 op->parameters.indexed = indexed;
1062 acquire_graphics_pipeline_resources(state, indexed, d3d_info);
1064 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1067 void wined3d_cs_emit_draw_indirect(struct wined3d_cs *cs, enum wined3d_primitive_type primitive_type,
1068 unsigned int patch_vertex_count, struct wined3d_buffer *buffer, unsigned int offset, bool indexed)
1070 const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
1071 const struct wined3d_state *state = &cs->device->state;
1072 struct wined3d_cs_draw *op;
1074 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1075 op->opcode = WINED3D_CS_OP_DRAW;
1076 op->primitive_type = primitive_type;
1077 op->patch_vertex_count = patch_vertex_count;
1078 op->parameters.indirect = TRUE;
1079 op->parameters.u.indirect.buffer = buffer;
1080 op->parameters.u.indirect.offset = offset;
1081 op->parameters.indexed = indexed;
1083 acquire_graphics_pipeline_resources(state, indexed, d3d_info);
1084 wined3d_resource_acquire(&buffer->resource);
1086 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1089 static void wined3d_cs_exec_flush(struct wined3d_cs *cs, const void *data)
1091 struct wined3d_context *context;
1093 context = context_acquire(cs->device, NULL, 0);
1094 cs->device->adapter->adapter_ops->adapter_flush_context(context);
1095 context_release(context);
1098 void wined3d_cs_emit_flush(struct wined3d_cs *cs)
1100 struct wined3d_cs_flush *op;
1102 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1103 op->opcode = WINED3D_CS_OP_FLUSH;
1105 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1106 cs->queries_flushed = TRUE;
1109 static void wined3d_cs_exec_set_predication(struct wined3d_cs *cs, const void *data)
1111 const struct wined3d_cs_set_predication *op = data;
1113 cs->state.predicate = op->predicate;
1114 cs->state.predicate_value = op->value;
1117 void wined3d_cs_emit_set_predication(struct wined3d_cs *cs, struct wined3d_query *predicate, BOOL value)
1119 struct wined3d_cs_set_predication *op;
1121 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1122 op->opcode = WINED3D_CS_OP_SET_PREDICATION;
1123 op->predicate = predicate;
1124 op->value = value;
1126 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1129 static void wined3d_cs_exec_set_viewports(struct wined3d_cs *cs, const void *data)
1131 const struct wined3d_cs_set_viewports *op = data;
1133 if (op->viewport_count)
1134 memcpy(cs->state.viewports, op->viewports, op->viewport_count * sizeof(*op->viewports));
1135 else
1136 memset(cs->state.viewports, 0, sizeof(*cs->state.viewports));
1137 cs->state.viewport_count = op->viewport_count;
1138 device_invalidate_state(cs->device, STATE_VIEWPORT);
1141 void wined3d_cs_emit_set_viewports(struct wined3d_cs *cs, unsigned int viewport_count,
1142 const struct wined3d_viewport *viewports)
1144 struct wined3d_cs_set_viewports *op;
1146 op = wined3d_cs_require_space(cs, FIELD_OFFSET(struct wined3d_cs_set_viewports, viewports[viewport_count]),
1147 WINED3D_CS_QUEUE_DEFAULT);
1148 op->opcode = WINED3D_CS_OP_SET_VIEWPORTS;
1149 memcpy(op->viewports, viewports, viewport_count * sizeof(*viewports));
1150 op->viewport_count = viewport_count;
1152 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1155 static void wined3d_cs_exec_set_scissor_rects(struct wined3d_cs *cs, const void *data)
1157 const struct wined3d_cs_set_scissor_rects *op = data;
1159 if (op->rect_count)
1160 memcpy(cs->state.scissor_rects, op->rects, op->rect_count * sizeof(*op->rects));
1161 else
1162 SetRectEmpty(cs->state.scissor_rects);
1163 cs->state.scissor_rect_count = op->rect_count;
1164 device_invalidate_state(cs->device, STATE_SCISSORRECT);
1167 void wined3d_cs_emit_set_scissor_rects(struct wined3d_cs *cs, unsigned int rect_count, const RECT *rects)
1169 struct wined3d_cs_set_scissor_rects *op;
1171 op = wined3d_cs_require_space(cs, FIELD_OFFSET(struct wined3d_cs_set_scissor_rects, rects[rect_count]),
1172 WINED3D_CS_QUEUE_DEFAULT);
1173 op->opcode = WINED3D_CS_OP_SET_SCISSOR_RECTS;
1174 memcpy(op->rects, rects, rect_count * sizeof(*rects));
1175 op->rect_count = rect_count;
1177 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1180 static void wined3d_cs_exec_set_rendertarget_view(struct wined3d_cs *cs, const void *data)
1182 const struct wined3d_cs_set_rendertarget_view *op = data;
1183 BOOL prev_alpha_swizzle, curr_alpha_swizzle;
1184 struct wined3d_rendertarget_view *prev;
1186 prev = cs->state.fb.render_targets[op->view_idx];
1187 cs->state.fb.render_targets[op->view_idx] = op->view;
1188 device_invalidate_state(cs->device, STATE_FRAMEBUFFER);
1190 prev_alpha_swizzle = prev && prev->format->id == WINED3DFMT_A8_UNORM;
1191 curr_alpha_swizzle = op->view && op->view->format->id == WINED3DFMT_A8_UNORM;
1192 if (prev_alpha_swizzle != curr_alpha_swizzle)
1193 device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1196 void wined3d_cs_emit_set_rendertarget_view(struct wined3d_cs *cs, unsigned int view_idx,
1197 struct wined3d_rendertarget_view *view)
1199 struct wined3d_cs_set_rendertarget_view *op;
1201 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1202 op->opcode = WINED3D_CS_OP_SET_RENDERTARGET_VIEW;
1203 op->view_idx = view_idx;
1204 op->view = view;
1206 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1209 static void wined3d_cs_exec_set_depth_stencil_view(struct wined3d_cs *cs, const void *data)
1211 const struct wined3d_cs_set_depth_stencil_view *op = data;
1212 struct wined3d_device *device = cs->device;
1213 struct wined3d_rendertarget_view *prev;
1215 if ((prev = cs->state.fb.depth_stencil) && prev->resource->type != WINED3D_RTYPE_BUFFER)
1217 struct wined3d_texture *prev_texture = texture_from_resource(prev->resource);
1219 if (device->swapchains[0]->state.desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
1220 || prev_texture->flags & WINED3D_TEXTURE_DISCARD)
1221 wined3d_texture_validate_location(prev_texture,
1222 prev->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
1225 cs->state.fb.depth_stencil = op->view;
1227 if (!prev != !op->view)
1229 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
1230 device_invalidate_state(device, STATE_DEPTH_STENCIL);
1231 device_invalidate_state(device, STATE_RASTERIZER);
1233 else if (prev)
1235 if (prev->format->depth_bias_scale != op->view->format->depth_bias_scale)
1236 device_invalidate_state(device, STATE_RASTERIZER);
1237 if (prev->format->stencil_size != op->view->format->stencil_size)
1238 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILREF));
1241 device_invalidate_state(device, STATE_FRAMEBUFFER);
1244 void wined3d_cs_emit_set_depth_stencil_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view)
1246 struct wined3d_cs_set_depth_stencil_view *op;
1248 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1249 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW;
1250 op->view = view;
1252 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1255 static void wined3d_cs_exec_set_vertex_declaration(struct wined3d_cs *cs, const void *data)
1257 const struct wined3d_cs_set_vertex_declaration *op = data;
1259 cs->state.vertex_declaration = op->declaration;
1260 device_invalidate_state(cs->device, STATE_VDECL);
1263 void wined3d_cs_emit_set_vertex_declaration(struct wined3d_cs *cs, struct wined3d_vertex_declaration *declaration)
1265 struct wined3d_cs_set_vertex_declaration *op;
1267 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1268 op->opcode = WINED3D_CS_OP_SET_VERTEX_DECLARATION;
1269 op->declaration = declaration;
1271 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1274 static void wined3d_cs_exec_set_stream_source(struct wined3d_cs *cs, const void *data)
1276 const struct wined3d_cs_set_stream_source *op = data;
1277 struct wined3d_stream_state *stream;
1278 struct wined3d_buffer *prev;
1280 stream = &cs->state.streams[op->stream_idx];
1281 prev = stream->buffer;
1282 stream->buffer = op->buffer;
1283 stream->offset = op->offset;
1284 stream->stride = op->stride;
1286 if (op->buffer)
1287 InterlockedIncrement(&op->buffer->resource.bind_count);
1288 if (prev)
1289 InterlockedDecrement(&prev->resource.bind_count);
1291 device_invalidate_state(cs->device, STATE_STREAMSRC);
1294 void wined3d_cs_emit_set_stream_source(struct wined3d_cs *cs, UINT stream_idx,
1295 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1297 struct wined3d_cs_set_stream_source *op;
1299 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1300 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE;
1301 op->stream_idx = stream_idx;
1302 op->buffer = buffer;
1303 op->offset = offset;
1304 op->stride = stride;
1306 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1309 static void wined3d_cs_exec_set_stream_source_freq(struct wined3d_cs *cs, const void *data)
1311 const struct wined3d_cs_set_stream_source_freq *op = data;
1312 struct wined3d_stream_state *stream;
1314 stream = &cs->state.streams[op->stream_idx];
1315 stream->frequency = op->frequency;
1316 stream->flags = op->flags;
1318 device_invalidate_state(cs->device, STATE_STREAMSRC);
1321 void wined3d_cs_emit_set_stream_source_freq(struct wined3d_cs *cs, UINT stream_idx, UINT frequency, UINT flags)
1323 struct wined3d_cs_set_stream_source_freq *op;
1325 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1326 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ;
1327 op->stream_idx = stream_idx;
1328 op->frequency = frequency;
1329 op->flags = flags;
1331 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1334 static void wined3d_cs_exec_set_stream_output(struct wined3d_cs *cs, const void *data)
1336 const struct wined3d_cs_set_stream_output *op = data;
1337 struct wined3d_stream_output *stream;
1338 struct wined3d_buffer *prev;
1340 stream = &cs->state.stream_output[op->stream_idx];
1341 prev = stream->buffer;
1342 stream->buffer = op->buffer;
1343 stream->offset = op->offset;
1345 if (op->buffer)
1346 InterlockedIncrement(&op->buffer->resource.bind_count);
1347 if (prev)
1348 InterlockedDecrement(&prev->resource.bind_count);
1350 device_invalidate_state(cs->device, STATE_STREAM_OUTPUT);
1353 void wined3d_cs_emit_set_stream_output(struct wined3d_cs *cs, UINT stream_idx,
1354 struct wined3d_buffer *buffer, UINT offset)
1356 struct wined3d_cs_set_stream_output *op;
1358 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1359 op->opcode = WINED3D_CS_OP_SET_STREAM_OUTPUT;
1360 op->stream_idx = stream_idx;
1361 op->buffer = buffer;
1362 op->offset = offset;
1364 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1367 static void wined3d_cs_exec_set_index_buffer(struct wined3d_cs *cs, const void *data)
1369 const struct wined3d_cs_set_index_buffer *op = data;
1370 struct wined3d_buffer *prev;
1372 prev = cs->state.index_buffer;
1373 cs->state.index_buffer = op->buffer;
1374 cs->state.index_format = op->format_id;
1375 cs->state.index_offset = op->offset;
1377 if (op->buffer)
1378 InterlockedIncrement(&op->buffer->resource.bind_count);
1379 if (prev)
1380 InterlockedDecrement(&prev->resource.bind_count);
1382 device_invalidate_state(cs->device, STATE_INDEXBUFFER);
1385 void wined3d_cs_emit_set_index_buffer(struct wined3d_cs *cs, struct wined3d_buffer *buffer,
1386 enum wined3d_format_id format_id, unsigned int offset)
1388 struct wined3d_cs_set_index_buffer *op;
1390 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1391 op->opcode = WINED3D_CS_OP_SET_INDEX_BUFFER;
1392 op->buffer = buffer;
1393 op->format_id = format_id;
1394 op->offset = offset;
1396 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1399 static void wined3d_cs_exec_set_constant_buffer(struct wined3d_cs *cs, const void *data)
1401 const struct wined3d_cs_set_constant_buffer *op = data;
1402 struct wined3d_buffer *prev;
1404 prev = cs->state.cb[op->type][op->cb_idx];
1405 cs->state.cb[op->type][op->cb_idx] = op->buffer;
1407 if (op->buffer)
1408 InterlockedIncrement(&op->buffer->resource.bind_count);
1409 if (prev)
1410 InterlockedDecrement(&prev->resource.bind_count);
1412 device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(op->type));
1415 void wined3d_cs_emit_set_constant_buffer(struct wined3d_cs *cs, enum wined3d_shader_type type,
1416 UINT cb_idx, struct wined3d_buffer *buffer)
1418 struct wined3d_cs_set_constant_buffer *op;
1420 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1421 op->opcode = WINED3D_CS_OP_SET_CONSTANT_BUFFER;
1422 op->type = type;
1423 op->cb_idx = cb_idx;
1424 op->buffer = buffer;
1426 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1429 static void wined3d_cs_exec_set_texture(struct wined3d_cs *cs, const void *data)
1431 const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
1432 const struct wined3d_cs_set_texture *op = data;
1433 struct wined3d_texture *prev;
1434 BOOL old_use_color_key = FALSE, new_use_color_key = FALSE;
1436 prev = cs->state.textures[op->stage];
1437 cs->state.textures[op->stage] = op->texture;
1439 if (op->texture)
1441 const struct wined3d_format *new_format = op->texture->resource.format;
1442 const struct wined3d_format *old_format = prev ? prev->resource.format : NULL;
1443 unsigned int old_fmt_flags = prev ? prev->resource.format_flags : 0;
1444 unsigned int new_fmt_flags = op->texture->resource.format_flags;
1446 if (InterlockedIncrement(&op->texture->resource.bind_count) == 1)
1447 op->texture->sampler = op->stage;
1449 if (!prev || wined3d_texture_gl(op->texture)->target != wined3d_texture_gl(prev)->target
1450 || (!is_same_fixup(new_format->color_fixup, old_format->color_fixup)
1451 && !(can_use_texture_swizzle(d3d_info, new_format) && can_use_texture_swizzle(d3d_info, old_format)))
1452 || (new_fmt_flags & WINED3DFMT_FLAG_SHADOW) != (old_fmt_flags & WINED3DFMT_FLAG_SHADOW))
1453 device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1455 if (!prev && op->stage < d3d_info->limits.ffp_blend_stages)
1457 /* The source arguments for color and alpha ops have different
1458 * meanings when a NULL texture is bound, so the COLOR_OP and
1459 * ALPHA_OP have to be dirtified. */
1460 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1461 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1464 if (!op->stage && op->texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1465 new_use_color_key = TRUE;
1468 if (prev)
1470 if (InterlockedDecrement(&prev->resource.bind_count) && prev->sampler == op->stage)
1472 unsigned int i;
1474 /* Search for other stages the texture is bound to. Shouldn't
1475 * happen if applications bind textures to a single stage only. */
1476 TRACE("Searching for other stages the texture is bound to.\n");
1477 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1479 if (cs->state.textures[i] == prev)
1481 TRACE("Texture is also bound to stage %u.\n", i);
1482 prev->sampler = i;
1483 break;
1488 if (!op->texture && op->stage < d3d_info->limits.ffp_blend_stages)
1490 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1491 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1494 if (!op->stage && prev->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1495 old_use_color_key = TRUE;
1498 device_invalidate_state(cs->device, STATE_SAMPLER(op->stage));
1500 if (new_use_color_key != old_use_color_key)
1501 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1503 if (new_use_color_key)
1504 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1507 void wined3d_cs_emit_set_texture(struct wined3d_cs *cs, UINT stage, struct wined3d_texture *texture)
1509 struct wined3d_cs_set_texture *op;
1511 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1512 op->opcode = WINED3D_CS_OP_SET_TEXTURE;
1513 op->stage = stage;
1514 op->texture = texture;
1516 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1519 static void wined3d_cs_exec_set_shader_resource_view(struct wined3d_cs *cs, const void *data)
1521 const struct wined3d_cs_set_shader_resource_view *op = data;
1522 struct wined3d_shader_resource_view *prev;
1524 prev = cs->state.shader_resource_view[op->type][op->view_idx];
1525 cs->state.shader_resource_view[op->type][op->view_idx] = op->view;
1527 if (op->view)
1528 InterlockedIncrement(&op->view->resource->bind_count);
1529 if (prev)
1530 InterlockedDecrement(&prev->resource->bind_count);
1532 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1533 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1534 else
1535 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1538 void wined3d_cs_emit_set_shader_resource_view(struct wined3d_cs *cs, enum wined3d_shader_type type,
1539 UINT view_idx, struct wined3d_shader_resource_view *view)
1541 struct wined3d_cs_set_shader_resource_view *op;
1543 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1544 op->opcode = WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW;
1545 op->type = type;
1546 op->view_idx = view_idx;
1547 op->view = view;
1549 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1552 static void wined3d_cs_exec_set_unordered_access_view(struct wined3d_cs *cs, const void *data)
1554 const struct wined3d_cs_set_unordered_access_view *op = data;
1555 struct wined3d_unordered_access_view *prev;
1557 prev = cs->state.unordered_access_view[op->pipeline][op->view_idx];
1558 cs->state.unordered_access_view[op->pipeline][op->view_idx] = op->view;
1560 if (op->view)
1561 InterlockedIncrement(&op->view->resource->bind_count);
1562 if (prev)
1563 InterlockedDecrement(&prev->resource->bind_count);
1565 if (op->view && op->initial_count != ~0u)
1566 wined3d_unordered_access_view_set_counter(op->view, op->initial_count);
1568 device_invalidate_state(cs->device, STATE_UNORDERED_ACCESS_VIEW_BINDING(op->pipeline));
1571 void wined3d_cs_emit_set_unordered_access_view(struct wined3d_cs *cs, enum wined3d_pipeline pipeline,
1572 unsigned int view_idx, struct wined3d_unordered_access_view *view, unsigned int initial_count)
1574 struct wined3d_cs_set_unordered_access_view *op;
1576 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1577 op->opcode = WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW;
1578 op->pipeline = pipeline;
1579 op->view_idx = view_idx;
1580 op->view = view;
1581 op->initial_count = initial_count;
1583 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1586 static void wined3d_cs_exec_set_sampler(struct wined3d_cs *cs, const void *data)
1588 const struct wined3d_cs_set_sampler *op = data;
1590 cs->state.sampler[op->type][op->sampler_idx] = op->sampler;
1591 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1592 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1593 else
1594 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1597 void wined3d_cs_emit_set_sampler(struct wined3d_cs *cs, enum wined3d_shader_type type,
1598 UINT sampler_idx, struct wined3d_sampler *sampler)
1600 struct wined3d_cs_set_sampler *op;
1602 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1603 op->opcode = WINED3D_CS_OP_SET_SAMPLER;
1604 op->type = type;
1605 op->sampler_idx = sampler_idx;
1606 op->sampler = sampler;
1608 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1611 static void wined3d_cs_exec_set_shader(struct wined3d_cs *cs, const void *data)
1613 const struct wined3d_cs_set_shader *op = data;
1615 cs->state.shader[op->type] = op->shader;
1616 device_invalidate_state(cs->device, STATE_SHADER(op->type));
1617 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1618 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1619 else
1620 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1623 void wined3d_cs_emit_set_shader(struct wined3d_cs *cs, enum wined3d_shader_type type, struct wined3d_shader *shader)
1625 struct wined3d_cs_set_shader *op;
1627 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1628 op->opcode = WINED3D_CS_OP_SET_SHADER;
1629 op->type = type;
1630 op->shader = shader;
1632 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1635 static void wined3d_cs_exec_set_blend_state(struct wined3d_cs *cs, const void *data)
1637 const struct wined3d_cs_set_blend_state *op = data;
1638 struct wined3d_state *state = &cs->state;
1640 if (state->blend_state != op->state)
1642 state->blend_state = op->state;
1643 device_invalidate_state(cs->device, STATE_BLEND);
1645 state->blend_factor = op->factor;
1646 device_invalidate_state(cs->device, STATE_BLEND_FACTOR);
1647 state->sample_mask = op->sample_mask;
1648 device_invalidate_state(cs->device, STATE_SAMPLE_MASK);
1651 void wined3d_cs_emit_set_blend_state(struct wined3d_cs *cs, struct wined3d_blend_state *state,
1652 const struct wined3d_color *blend_factor, unsigned int sample_mask)
1654 struct wined3d_cs_set_blend_state *op;
1656 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1657 op->opcode = WINED3D_CS_OP_SET_BLEND_STATE;
1658 op->state = state;
1659 op->factor = *blend_factor;
1660 op->sample_mask = sample_mask;
1662 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1665 static void wined3d_cs_exec_set_depth_stencil_state(struct wined3d_cs *cs, const void *data)
1667 const struct wined3d_cs_set_depth_stencil_state *op = data;
1669 cs->state.depth_stencil_state = op->state;
1670 device_invalidate_state(cs->device, STATE_DEPTH_STENCIL);
1673 void wined3d_cs_emit_set_depth_stencil_state(struct wined3d_cs *cs,
1674 struct wined3d_depth_stencil_state *state)
1676 struct wined3d_cs_set_depth_stencil_state *op;
1678 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1679 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE;
1680 op->state = state;
1682 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1685 static void wined3d_cs_exec_set_rasterizer_state(struct wined3d_cs *cs, const void *data)
1687 const struct wined3d_cs_set_rasterizer_state *op = data;
1689 cs->state.rasterizer_state = op->state;
1690 device_invalidate_state(cs->device, STATE_RASTERIZER);
1693 void wined3d_cs_emit_set_rasterizer_state(struct wined3d_cs *cs,
1694 struct wined3d_rasterizer_state *rasterizer_state)
1696 struct wined3d_cs_set_rasterizer_state *op;
1698 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1699 op->opcode = WINED3D_CS_OP_SET_RASTERIZER_STATE;
1700 op->state = rasterizer_state;
1702 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1705 static void wined3d_cs_exec_set_render_state(struct wined3d_cs *cs, const void *data)
1707 const struct wined3d_cs_set_render_state *op = data;
1709 cs->state.render_states[op->state] = op->value;
1710 device_invalidate_state(cs->device, STATE_RENDER(op->state));
1713 void wined3d_cs_emit_set_render_state(struct wined3d_cs *cs, enum wined3d_render_state state, DWORD value)
1715 struct wined3d_cs_set_render_state *op;
1717 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1718 op->opcode = WINED3D_CS_OP_SET_RENDER_STATE;
1719 op->state = state;
1720 op->value = value;
1722 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1725 static void wined3d_cs_exec_set_texture_state(struct wined3d_cs *cs, const void *data)
1727 const struct wined3d_cs_set_texture_state *op = data;
1729 cs->state.texture_states[op->stage][op->state] = op->value;
1730 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, op->state));
1733 void wined3d_cs_emit_set_texture_state(struct wined3d_cs *cs, UINT stage,
1734 enum wined3d_texture_stage_state state, DWORD value)
1736 struct wined3d_cs_set_texture_state *op;
1738 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1739 op->opcode = WINED3D_CS_OP_SET_TEXTURE_STATE;
1740 op->stage = stage;
1741 op->state = state;
1742 op->value = value;
1744 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1747 static void wined3d_cs_exec_set_sampler_state(struct wined3d_cs *cs, const void *data)
1749 const struct wined3d_cs_set_sampler_state *op = data;
1751 cs->state.sampler_states[op->sampler_idx][op->state] = op->value;
1752 device_invalidate_state(cs->device, STATE_SAMPLER(op->sampler_idx));
1755 void wined3d_cs_emit_set_sampler_state(struct wined3d_cs *cs, UINT sampler_idx,
1756 enum wined3d_sampler_state state, DWORD value)
1758 struct wined3d_cs_set_sampler_state *op;
1760 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1761 op->opcode = WINED3D_CS_OP_SET_SAMPLER_STATE;
1762 op->sampler_idx = sampler_idx;
1763 op->state = state;
1764 op->value = value;
1766 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1769 static void wined3d_cs_exec_set_transform(struct wined3d_cs *cs, const void *data)
1771 const struct wined3d_cs_set_transform *op = data;
1773 cs->state.transforms[op->state] = op->matrix;
1774 if (op->state < WINED3D_TS_WORLD_MATRIX(cs->device->adapter->d3d_info.limits.ffp_vertex_blend_matrices))
1775 device_invalidate_state(cs->device, STATE_TRANSFORM(op->state));
1778 void wined3d_cs_emit_set_transform(struct wined3d_cs *cs, enum wined3d_transform_state state,
1779 const struct wined3d_matrix *matrix)
1781 struct wined3d_cs_set_transform *op;
1783 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1784 op->opcode = WINED3D_CS_OP_SET_TRANSFORM;
1785 op->state = state;
1786 op->matrix = *matrix;
1788 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1791 static void wined3d_cs_exec_set_clip_plane(struct wined3d_cs *cs, const void *data)
1793 const struct wined3d_cs_set_clip_plane *op = data;
1795 cs->state.clip_planes[op->plane_idx] = op->plane;
1796 device_invalidate_state(cs->device, STATE_CLIPPLANE(op->plane_idx));
1799 void wined3d_cs_emit_set_clip_plane(struct wined3d_cs *cs, UINT plane_idx, const struct wined3d_vec4 *plane)
1801 struct wined3d_cs_set_clip_plane *op;
1803 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1804 op->opcode = WINED3D_CS_OP_SET_CLIP_PLANE;
1805 op->plane_idx = plane_idx;
1806 op->plane = *plane;
1808 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1811 static void wined3d_cs_exec_set_color_key(struct wined3d_cs *cs, const void *data)
1813 const struct wined3d_cs_set_color_key *op = data;
1814 struct wined3d_texture *texture = op->texture;
1816 if (op->set)
1818 switch (op->flags)
1820 case WINED3D_CKEY_DST_BLT:
1821 texture->async.dst_blt_color_key = op->color_key;
1822 texture->async.color_key_flags |= WINED3D_CKEY_DST_BLT;
1823 break;
1825 case WINED3D_CKEY_DST_OVERLAY:
1826 texture->async.dst_overlay_color_key = op->color_key;
1827 texture->async.color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
1828 break;
1830 case WINED3D_CKEY_SRC_BLT:
1831 if (texture == cs->state.textures[0])
1833 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1834 if (!(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1835 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1838 texture->async.src_blt_color_key = op->color_key;
1839 texture->async.color_key_flags |= WINED3D_CKEY_SRC_BLT;
1840 break;
1842 case WINED3D_CKEY_SRC_OVERLAY:
1843 texture->async.src_overlay_color_key = op->color_key;
1844 texture->async.color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
1845 break;
1848 else
1850 switch (op->flags)
1852 case WINED3D_CKEY_DST_BLT:
1853 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_BLT;
1854 break;
1856 case WINED3D_CKEY_DST_OVERLAY:
1857 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
1858 break;
1860 case WINED3D_CKEY_SRC_BLT:
1861 if (texture == cs->state.textures[0] && texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1862 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1864 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
1865 break;
1867 case WINED3D_CKEY_SRC_OVERLAY:
1868 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
1869 break;
1874 void wined3d_cs_emit_set_color_key(struct wined3d_cs *cs, struct wined3d_texture *texture,
1875 WORD flags, const struct wined3d_color_key *color_key)
1877 struct wined3d_cs_set_color_key *op;
1879 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1880 op->opcode = WINED3D_CS_OP_SET_COLOR_KEY;
1881 op->texture = texture;
1882 op->flags = flags;
1883 if (color_key)
1885 op->color_key = *color_key;
1886 op->set = 1;
1888 else
1889 op->set = 0;
1891 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1894 static void wined3d_cs_exec_set_material(struct wined3d_cs *cs, const void *data)
1896 const struct wined3d_cs_set_material *op = data;
1898 cs->state.material = op->material;
1899 device_invalidate_state(cs->device, STATE_MATERIAL);
1902 void wined3d_cs_emit_set_material(struct wined3d_cs *cs, const struct wined3d_material *material)
1904 struct wined3d_cs_set_material *op;
1906 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1907 op->opcode = WINED3D_CS_OP_SET_MATERIAL;
1908 op->material = *material;
1910 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1913 static void wined3d_cs_exec_set_light(struct wined3d_cs *cs, const void *data)
1915 const struct wined3d_cs_set_light *op = data;
1916 struct wined3d_light_info *light_info;
1917 unsigned int light_idx, hash_idx;
1919 light_idx = op->light.OriginalIndex;
1921 if (!(light_info = wined3d_light_state_get_light(&cs->state.light_state, light_idx)))
1923 TRACE("Adding new light.\n");
1924 if (!(light_info = heap_alloc_zero(sizeof(*light_info))))
1926 ERR("Failed to allocate light info.\n");
1927 return;
1930 hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1931 list_add_head(&cs->state.light_state.light_map[hash_idx], &light_info->entry);
1932 light_info->glIndex = -1;
1933 light_info->OriginalIndex = light_idx;
1936 if (light_info->glIndex != -1)
1938 if (light_info->OriginalParms.type != op->light.OriginalParms.type)
1939 device_invalidate_state(cs->device, STATE_LIGHT_TYPE);
1940 device_invalidate_state(cs->device, STATE_ACTIVELIGHT(light_info->glIndex));
1943 light_info->OriginalParms = op->light.OriginalParms;
1944 light_info->position = op->light.position;
1945 light_info->direction = op->light.direction;
1946 light_info->exponent = op->light.exponent;
1947 light_info->cutoff = op->light.cutoff;
1950 void wined3d_cs_emit_set_light(struct wined3d_cs *cs, const struct wined3d_light_info *light)
1952 struct wined3d_cs_set_light *op;
1954 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1955 op->opcode = WINED3D_CS_OP_SET_LIGHT;
1956 op->light = *light;
1958 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1961 static void wined3d_cs_exec_set_light_enable(struct wined3d_cs *cs, const void *data)
1963 const struct wined3d_cs_set_light_enable *op = data;
1964 struct wined3d_device *device = cs->device;
1965 struct wined3d_light_info *light_info;
1966 int prev_idx;
1968 if (!(light_info = wined3d_light_state_get_light(&cs->state.light_state, op->idx)))
1970 ERR("Light doesn't exist.\n");
1971 return;
1974 prev_idx = light_info->glIndex;
1975 wined3d_light_state_enable_light(&cs->state.light_state, &device->adapter->d3d_info, light_info, op->enable);
1976 if (light_info->glIndex != prev_idx)
1978 device_invalidate_state(device, STATE_LIGHT_TYPE);
1979 device_invalidate_state(device, STATE_ACTIVELIGHT(op->enable ? light_info->glIndex : prev_idx));
1983 void wined3d_cs_emit_set_light_enable(struct wined3d_cs *cs, unsigned int idx, BOOL enable)
1985 struct wined3d_cs_set_light_enable *op;
1987 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1988 op->opcode = WINED3D_CS_OP_SET_LIGHT_ENABLE;
1989 op->idx = idx;
1990 op->enable = enable;
1992 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1995 static const struct
1997 size_t offset;
1998 size_t size;
1999 DWORD mask;
2001 wined3d_cs_push_constant_info[] =
2003 /* WINED3D_PUSH_CONSTANTS_VS_F */
2004 {FIELD_OFFSET(struct wined3d_state, vs_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_VS_F},
2005 /* WINED3D_PUSH_CONSTANTS_PS_F */
2006 {FIELD_OFFSET(struct wined3d_state, ps_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_PS_F},
2007 /* WINED3D_PUSH_CONSTANTS_VS_I */
2008 {FIELD_OFFSET(struct wined3d_state, vs_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_VS_I},
2009 /* WINED3D_PUSH_CONSTANTS_PS_I */
2010 {FIELD_OFFSET(struct wined3d_state, ps_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_PS_I},
2011 /* WINED3D_PUSH_CONSTANTS_VS_B */
2012 {FIELD_OFFSET(struct wined3d_state, vs_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_VS_B},
2013 /* WINED3D_PUSH_CONSTANTS_PS_B */
2014 {FIELD_OFFSET(struct wined3d_state, ps_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_PS_B},
2017 static void wined3d_cs_st_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
2018 unsigned int start_idx, unsigned int count, const void *constants)
2020 struct wined3d_device *device = cs->device;
2021 unsigned int context_count;
2022 unsigned int i;
2023 size_t offset;
2025 if (p == WINED3D_PUSH_CONSTANTS_VS_F)
2026 device->shader_backend->shader_update_float_vertex_constants(device, start_idx, count);
2027 else if (p == WINED3D_PUSH_CONSTANTS_PS_F)
2028 device->shader_backend->shader_update_float_pixel_constants(device, start_idx, count);
2030 offset = wined3d_cs_push_constant_info[p].offset + start_idx * wined3d_cs_push_constant_info[p].size;
2031 memcpy((BYTE *)&cs->state + offset, constants, count * wined3d_cs_push_constant_info[p].size);
2032 for (i = 0, context_count = device->context_count; i < context_count; ++i)
2034 device->contexts[i]->constant_update_mask |= wined3d_cs_push_constant_info[p].mask;
2038 static void wined3d_cs_exec_push_constants(struct wined3d_cs *cs, const void *data)
2040 const struct wined3d_cs_push_constants *op = data;
2042 wined3d_cs_st_push_constants(cs, op->type, op->start_idx, op->count, op->constants);
2045 static void wined3d_cs_mt_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
2046 unsigned int start_idx, unsigned int count, const void *constants)
2048 struct wined3d_cs_push_constants *op;
2049 size_t size;
2051 size = count * wined3d_cs_push_constant_info[p].size;
2052 op = wined3d_cs_require_space(cs, FIELD_OFFSET(struct wined3d_cs_push_constants, constants[size]),
2053 WINED3D_CS_QUEUE_DEFAULT);
2054 op->opcode = WINED3D_CS_OP_PUSH_CONSTANTS;
2055 op->type = p;
2056 op->start_idx = start_idx;
2057 op->count = count;
2058 memcpy(op->constants, constants, size);
2060 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2063 static void wined3d_cs_exec_reset_state(struct wined3d_cs *cs, const void *data)
2065 struct wined3d_adapter *adapter = cs->device->adapter;
2067 state_cleanup(&cs->state);
2068 memset(&cs->state, 0, sizeof(cs->state));
2069 state_init(&cs->state, &adapter->d3d_info, WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
2072 void wined3d_cs_emit_reset_state(struct wined3d_cs *cs)
2074 struct wined3d_cs_reset_state *op;
2076 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2077 op->opcode = WINED3D_CS_OP_RESET_STATE;
2079 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2082 static void wined3d_cs_exec_callback(struct wined3d_cs *cs, const void *data)
2084 const struct wined3d_cs_callback *op = data;
2086 op->callback(op->object);
2089 static void wined3d_cs_emit_callback(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2091 struct wined3d_cs_callback *op;
2093 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2094 op->opcode = WINED3D_CS_OP_CALLBACK;
2095 op->callback = callback;
2096 op->object = object;
2098 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2101 void wined3d_cs_destroy_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2103 wined3d_cs_emit_callback(cs, callback, object);
2106 void wined3d_cs_init_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2108 wined3d_cs_emit_callback(cs, callback, object);
2111 static void wined3d_cs_exec_query_issue(struct wined3d_cs *cs, const void *data)
2113 const struct wined3d_cs_query_issue *op = data;
2114 struct wined3d_query *query = op->query;
2115 BOOL poll;
2117 poll = query->query_ops->query_issue(query, op->flags);
2119 if (!cs->thread)
2120 return;
2122 if (poll && list_empty(&query->poll_list_entry))
2124 if (query->buffer_object)
2125 InterlockedIncrement(&query->counter_retrieved);
2126 else
2127 list_add_tail(&cs->query_poll_list, &query->poll_list_entry);
2128 return;
2131 /* This can happen if occlusion queries are restarted. This discards the
2132 * old result, since polling it could result in a GL error. */
2133 if ((op->flags & WINED3DISSUE_BEGIN) && !poll && !list_empty(&query->poll_list_entry))
2135 list_remove(&query->poll_list_entry);
2136 list_init(&query->poll_list_entry);
2137 InterlockedIncrement(&query->counter_retrieved);
2138 return;
2141 /* This can happen when an occlusion query is ended without being started,
2142 * in which case we don't want to poll, but still have to counter-balance
2143 * the increment of the main counter.
2145 * This can also happen if an event query is re-issued before the first
2146 * fence was reached. In this case the query is already in the list and
2147 * the poll function will check the new fence. We have to counter-balance
2148 * the discarded increment. */
2149 if (op->flags & WINED3DISSUE_END)
2150 InterlockedIncrement(&query->counter_retrieved);
2153 void wined3d_cs_emit_query_issue(struct wined3d_cs *cs, struct wined3d_query *query, DWORD flags)
2155 struct wined3d_cs_query_issue *op;
2157 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2158 op->opcode = WINED3D_CS_OP_QUERY_ISSUE;
2159 op->query = query;
2160 op->flags = flags;
2162 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2163 cs->queries_flushed = FALSE;
2166 static void wined3d_cs_exec_preload_resource(struct wined3d_cs *cs, const void *data)
2168 const struct wined3d_cs_preload_resource *op = data;
2169 struct wined3d_resource *resource = op->resource;
2171 resource->resource_ops->resource_preload(resource);
2172 wined3d_resource_release(resource);
2175 void wined3d_cs_emit_preload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
2177 struct wined3d_cs_preload_resource *op;
2179 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2180 op->opcode = WINED3D_CS_OP_PRELOAD_RESOURCE;
2181 op->resource = resource;
2183 wined3d_resource_acquire(resource);
2185 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2188 static void wined3d_cs_exec_unload_resource(struct wined3d_cs *cs, const void *data)
2190 const struct wined3d_cs_unload_resource *op = data;
2191 struct wined3d_resource *resource = op->resource;
2193 resource->resource_ops->resource_unload(resource);
2194 wined3d_resource_release(resource);
2197 void wined3d_cs_emit_unload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
2199 struct wined3d_cs_unload_resource *op;
2201 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2202 op->opcode = WINED3D_CS_OP_UNLOAD_RESOURCE;
2203 op->resource = resource;
2205 wined3d_resource_acquire(resource);
2207 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2210 static void wined3d_cs_exec_map(struct wined3d_cs *cs, const void *data)
2212 const struct wined3d_cs_map *op = data;
2213 struct wined3d_resource *resource = op->resource;
2215 *op->hr = resource->resource_ops->resource_sub_resource_map(resource,
2216 op->sub_resource_idx, op->map_desc, op->box, op->flags);
2219 HRESULT wined3d_cs_map(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx,
2220 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
2222 struct wined3d_cs_map *op;
2223 HRESULT hr;
2225 /* Mapping resources from the worker thread isn't an issue by itself, but
2226 * increasing the map count would be visible to applications. */
2227 wined3d_not_from_cs(cs);
2229 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2230 op->opcode = WINED3D_CS_OP_MAP;
2231 op->resource = resource;
2232 op->sub_resource_idx = sub_resource_idx;
2233 op->map_desc = map_desc;
2234 op->box = box;
2235 op->flags = flags;
2236 op->hr = &hr;
2238 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_MAP);
2239 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_MAP);
2241 return hr;
2244 static void wined3d_cs_exec_unmap(struct wined3d_cs *cs, const void *data)
2246 const struct wined3d_cs_unmap *op = data;
2247 struct wined3d_resource *resource = op->resource;
2249 *op->hr = resource->resource_ops->resource_sub_resource_unmap(resource, op->sub_resource_idx);
2252 HRESULT wined3d_cs_unmap(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx)
2254 struct wined3d_cs_unmap *op;
2255 HRESULT hr;
2257 wined3d_not_from_cs(cs);
2259 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2260 op->opcode = WINED3D_CS_OP_UNMAP;
2261 op->resource = resource;
2262 op->sub_resource_idx = sub_resource_idx;
2263 op->hr = &hr;
2265 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_MAP);
2266 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_MAP);
2268 return hr;
2271 static void wined3d_cs_exec_blt_sub_resource(struct wined3d_cs *cs, const void *data)
2273 const struct wined3d_cs_blt_sub_resource *op = data;
2275 if (op->dst_resource->type == WINED3D_RTYPE_BUFFER)
2277 wined3d_buffer_copy(buffer_from_resource(op->dst_resource), op->dst_box.left,
2278 buffer_from_resource(op->src_resource), op->src_box.left,
2279 op->src_box.right - op->src_box.left);
2281 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_3D)
2283 struct wined3d_texture *src_texture, *dst_texture;
2284 unsigned int level, update_w, update_h, update_d;
2285 unsigned int row_pitch, slice_pitch;
2286 struct wined3d_context *context;
2287 struct wined3d_bo_address addr;
2289 if (op->flags & ~WINED3D_BLT_RAW)
2291 FIXME("Flags %#x not implemented for %s resources.\n",
2292 op->flags, debug_d3dresourcetype(op->dst_resource->type));
2293 goto error;
2296 if (!(op->flags & WINED3D_BLT_RAW) && op->src_resource->format != op->dst_resource->format)
2298 FIXME("Format conversion not implemented for %s resources.\n",
2299 debug_d3dresourcetype(op->dst_resource->type));
2300 goto error;
2303 update_w = op->dst_box.right - op->dst_box.left;
2304 update_h = op->dst_box.bottom - op->dst_box.top;
2305 update_d = op->dst_box.back - op->dst_box.front;
2306 if (op->src_box.right - op->src_box.left != update_w
2307 || op->src_box.bottom - op->src_box.top != update_h
2308 || op->src_box.back - op->src_box.front != update_d)
2310 FIXME("Stretching not implemented for %s resources.\n",
2311 debug_d3dresourcetype(op->dst_resource->type));
2312 goto error;
2315 dst_texture = texture_from_resource(op->dst_resource);
2316 src_texture = texture_from_resource(op->src_resource);
2318 context = context_acquire(cs->device, NULL, 0);
2320 if (!wined3d_texture_load_location(src_texture, op->src_sub_resource_idx,
2321 context, src_texture->resource.map_binding))
2323 ERR("Failed to load source sub-resource into %s.\n",
2324 wined3d_debug_location(src_texture->resource.map_binding));
2325 context_release(context);
2326 goto error;
2329 level = op->dst_sub_resource_idx % dst_texture->level_count;
2330 if (update_w == wined3d_texture_get_level_width(dst_texture, level)
2331 && update_h == wined3d_texture_get_level_height(dst_texture, level)
2332 && update_d == wined3d_texture_get_level_depth(dst_texture, level))
2334 wined3d_texture_prepare_location(dst_texture, op->dst_sub_resource_idx,
2335 context, WINED3D_LOCATION_TEXTURE_RGB);
2337 else if (!wined3d_texture_load_location(dst_texture, op->dst_sub_resource_idx,
2338 context, WINED3D_LOCATION_TEXTURE_RGB))
2340 ERR("Failed to load destination sub-resource.\n");
2341 context_release(context);
2342 goto error;
2345 wined3d_texture_get_memory(src_texture, op->src_sub_resource_idx, &addr, src_texture->resource.map_binding);
2346 wined3d_texture_get_pitch(src_texture, op->src_sub_resource_idx % src_texture->level_count,
2347 &row_pitch, &slice_pitch);
2349 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&addr),
2350 dst_texture->resource.format, &op->src_box, row_pitch, slice_pitch, dst_texture,
2351 op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
2352 op->dst_box.left, op->dst_box.top, op->dst_box.front);
2353 wined3d_texture_validate_location(dst_texture, op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2354 wined3d_texture_invalidate_location(dst_texture, op->dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2356 context_release(context);
2358 else
2360 if (FAILED(texture2d_blt(texture_from_resource(op->dst_resource), op->dst_sub_resource_idx,
2361 &op->dst_box, texture_from_resource(op->src_resource), op->src_sub_resource_idx,
2362 &op->src_box, op->flags, &op->fx, op->filter)))
2363 FIXME("Blit failed.\n");
2366 error:
2367 if (op->src_resource)
2368 wined3d_resource_release(op->src_resource);
2369 wined3d_resource_release(op->dst_resource);
2372 void wined3d_cs_emit_blt_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *dst_resource,
2373 unsigned int dst_sub_resource_idx, const struct wined3d_box *dst_box, struct wined3d_resource *src_resource,
2374 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, DWORD flags,
2375 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2377 struct wined3d_cs_blt_sub_resource *op;
2379 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2380 op->opcode = WINED3D_CS_OP_BLT_SUB_RESOURCE;
2381 op->dst_resource = dst_resource;
2382 op->dst_sub_resource_idx = dst_sub_resource_idx;
2383 op->dst_box = *dst_box;
2384 op->src_resource = src_resource;
2385 op->src_sub_resource_idx = src_sub_resource_idx;
2386 op->src_box = *src_box;
2387 op->flags = flags;
2388 if (fx)
2389 op->fx = *fx;
2390 else
2391 memset(&op->fx, 0, sizeof(op->fx));
2392 op->filter = filter;
2394 wined3d_resource_acquire(dst_resource);
2395 if (src_resource)
2396 wined3d_resource_acquire(src_resource);
2398 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2399 if (flags & WINED3D_BLT_SYNCHRONOUS)
2400 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2403 static void wined3d_cs_exec_update_sub_resource(struct wined3d_cs *cs, const void *data)
2405 const struct wined3d_cs_update_sub_resource *op = data;
2406 struct wined3d_resource *resource = op->resource;
2407 const struct wined3d_box *box = &op->box;
2408 unsigned int width, height, depth, level;
2409 struct wined3d_const_bo_address addr;
2410 struct wined3d_context *context;
2411 struct wined3d_texture *texture;
2412 struct wined3d_box src_box;
2414 context = context_acquire(cs->device, NULL, 0);
2416 if (resource->type == WINED3D_RTYPE_BUFFER)
2418 struct wined3d_buffer *buffer = buffer_from_resource(resource);
2420 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
2422 ERR("Failed to load buffer location.\n");
2423 goto done;
2426 wined3d_buffer_upload_data(buffer, context, box, op->data.data);
2427 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
2428 goto done;
2431 texture = wined3d_texture_from_resource(resource);
2433 level = op->sub_resource_idx % texture->level_count;
2434 width = wined3d_texture_get_level_width(texture, level);
2435 height = wined3d_texture_get_level_height(texture, level);
2436 depth = wined3d_texture_get_level_depth(texture, level);
2438 addr.buffer_object = 0;
2439 addr.addr = op->data.data;
2441 /* Only load the sub-resource for partial updates. */
2442 if (!box->left && !box->top && !box->front
2443 && box->right == width && box->bottom == height && box->back == depth)
2444 wined3d_texture_prepare_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2445 else
2446 wined3d_texture_load_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2448 wined3d_box_set(&src_box, 0, 0, box->right - box->left, box->bottom - box->top, 0, box->back - box->front);
2449 texture->texture_ops->texture_upload_data(context, &addr, texture->resource.format, &src_box,
2450 op->data.row_pitch, op->data.slice_pitch, texture, op->sub_resource_idx,
2451 WINED3D_LOCATION_TEXTURE_RGB, box->left, box->top, box->front);
2453 wined3d_texture_validate_location(texture, op->sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2454 wined3d_texture_invalidate_location(texture, op->sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2456 done:
2457 context_release(context);
2459 wined3d_resource_release(resource);
2462 void wined3d_cs_emit_update_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *resource,
2463 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
2464 unsigned int slice_pitch)
2466 struct wined3d_cs_update_sub_resource *op;
2468 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2469 op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2470 op->resource = resource;
2471 op->sub_resource_idx = sub_resource_idx;
2472 op->box = *box;
2473 op->data.row_pitch = row_pitch;
2474 op->data.slice_pitch = slice_pitch;
2475 op->data.data = data;
2477 wined3d_resource_acquire(resource);
2479 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_MAP);
2480 /* The data pointer may go away, so we need to wait until it is read.
2481 * Copying the data may be faster if it's small. */
2482 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_MAP);
2485 static void wined3d_cs_exec_add_dirty_texture_region(struct wined3d_cs *cs, const void *data)
2487 const struct wined3d_cs_add_dirty_texture_region *op = data;
2488 struct wined3d_texture *texture = op->texture;
2489 unsigned int sub_resource_idx, i;
2490 struct wined3d_context *context;
2492 context = context_acquire(cs->device, NULL, 0);
2493 sub_resource_idx = op->layer * texture->level_count;
2494 for (i = 0; i < texture->level_count; ++i, ++sub_resource_idx)
2496 if (wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
2497 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2498 else
2499 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
2501 context_release(context);
2503 wined3d_resource_release(&texture->resource);
2506 void wined3d_cs_emit_add_dirty_texture_region(struct wined3d_cs *cs,
2507 struct wined3d_texture *texture, unsigned int layer)
2509 struct wined3d_cs_add_dirty_texture_region *op;
2511 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2512 op->opcode = WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION;
2513 op->texture = texture;
2514 op->layer = layer;
2516 wined3d_resource_acquire(&texture->resource);
2518 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2521 static void wined3d_cs_exec_clear_unordered_access_view(struct wined3d_cs *cs, const void *data)
2523 const struct wined3d_cs_clear_unordered_access_view *op = data;
2524 struct wined3d_unordered_access_view *view = op->view;
2525 struct wined3d_context *context;
2527 context = context_acquire(cs->device, NULL, 0);
2528 cs->device->adapter->adapter_ops->adapter_clear_uav(context, view, &op->clear_value);
2529 context_release(context);
2531 wined3d_resource_release(view->resource);
2534 void wined3d_cs_emit_clear_unordered_access_view_uint(struct wined3d_cs *cs,
2535 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
2537 struct wined3d_cs_clear_unordered_access_view *op;
2539 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2540 op->opcode = WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW;
2541 op->view = view;
2542 op->clear_value = *clear_value;
2544 wined3d_resource_acquire(view->resource);
2546 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2549 static void wined3d_cs_exec_copy_uav_counter(struct wined3d_cs *cs, const void *data)
2551 const struct wined3d_cs_copy_uav_counter *op = data;
2552 struct wined3d_unordered_access_view *view = op->view;
2553 struct wined3d_context *context;
2555 context = context_acquire(cs->device, NULL, 0);
2556 wined3d_unordered_access_view_copy_counter(view, op->buffer, op->offset, context);
2557 context_release(context);
2559 wined3d_resource_release(&op->buffer->resource);
2562 void wined3d_cs_emit_copy_uav_counter(struct wined3d_cs *cs, struct wined3d_buffer *dst_buffer,
2563 unsigned int offset, struct wined3d_unordered_access_view *uav)
2565 struct wined3d_cs_copy_uav_counter *op;
2567 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2568 op->opcode = WINED3D_CS_OP_COPY_UAV_COUNTER;
2569 op->buffer = dst_buffer;
2570 op->offset = offset;
2571 op->view = uav;
2573 wined3d_resource_acquire(&dst_buffer->resource);
2575 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2578 static void wined3d_cs_exec_generate_mipmaps(struct wined3d_cs *cs, const void *data)
2580 const struct wined3d_cs_generate_mipmaps *op = data;
2581 struct wined3d_shader_resource_view *view = op->view;
2583 shader_resource_view_generate_mipmaps(view);
2584 wined3d_resource_release(view->resource);
2587 void wined3d_cs_emit_generate_mipmaps(struct wined3d_cs *cs, struct wined3d_shader_resource_view *view)
2589 struct wined3d_cs_generate_mipmaps *op;
2591 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2592 op->opcode = WINED3D_CS_OP_GENERATE_MIPMAPS;
2593 op->view = view;
2595 wined3d_resource_acquire(view->resource);
2597 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2600 static void wined3d_cs_emit_stop(struct wined3d_cs *cs)
2602 struct wined3d_cs_stop *op;
2604 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2605 op->opcode = WINED3D_CS_OP_STOP;
2607 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2608 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2611 static void (* const wined3d_cs_op_handlers[])(struct wined3d_cs *cs, const void *data) =
2613 /* WINED3D_CS_OP_NOP */ wined3d_cs_exec_nop,
2614 /* WINED3D_CS_OP_PRESENT */ wined3d_cs_exec_present,
2615 /* WINED3D_CS_OP_CLEAR */ wined3d_cs_exec_clear,
2616 /* WINED3D_CS_OP_DISPATCH */ wined3d_cs_exec_dispatch,
2617 /* WINED3D_CS_OP_DRAW */ wined3d_cs_exec_draw,
2618 /* WINED3D_CS_OP_FLUSH */ wined3d_cs_exec_flush,
2619 /* WINED3D_CS_OP_SET_PREDICATION */ wined3d_cs_exec_set_predication,
2620 /* WINED3D_CS_OP_SET_VIEWPORTS */ wined3d_cs_exec_set_viewports,
2621 /* WINED3D_CS_OP_SET_SCISSOR_RECTS */ wined3d_cs_exec_set_scissor_rects,
2622 /* WINED3D_CS_OP_SET_RENDERTARGET_VIEW */ wined3d_cs_exec_set_rendertarget_view,
2623 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW */ wined3d_cs_exec_set_depth_stencil_view,
2624 /* WINED3D_CS_OP_SET_VERTEX_DECLARATION */ wined3d_cs_exec_set_vertex_declaration,
2625 /* WINED3D_CS_OP_SET_STREAM_SOURCE */ wined3d_cs_exec_set_stream_source,
2626 /* WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ */ wined3d_cs_exec_set_stream_source_freq,
2627 /* WINED3D_CS_OP_SET_STREAM_OUTPUT */ wined3d_cs_exec_set_stream_output,
2628 /* WINED3D_CS_OP_SET_INDEX_BUFFER */ wined3d_cs_exec_set_index_buffer,
2629 /* WINED3D_CS_OP_SET_CONSTANT_BUFFER */ wined3d_cs_exec_set_constant_buffer,
2630 /* WINED3D_CS_OP_SET_TEXTURE */ wined3d_cs_exec_set_texture,
2631 /* WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW */ wined3d_cs_exec_set_shader_resource_view,
2632 /* WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_set_unordered_access_view,
2633 /* WINED3D_CS_OP_SET_SAMPLER */ wined3d_cs_exec_set_sampler,
2634 /* WINED3D_CS_OP_SET_SHADER */ wined3d_cs_exec_set_shader,
2635 /* WINED3D_CS_OP_SET_BLEND_STATE */ wined3d_cs_exec_set_blend_state,
2636 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE */ wined3d_cs_exec_set_depth_stencil_state,
2637 /* WINED3D_CS_OP_SET_RASTERIZER_STATE */ wined3d_cs_exec_set_rasterizer_state,
2638 /* WINED3D_CS_OP_SET_RENDER_STATE */ wined3d_cs_exec_set_render_state,
2639 /* WINED3D_CS_OP_SET_TEXTURE_STATE */ wined3d_cs_exec_set_texture_state,
2640 /* WINED3D_CS_OP_SET_SAMPLER_STATE */ wined3d_cs_exec_set_sampler_state,
2641 /* WINED3D_CS_OP_SET_TRANSFORM */ wined3d_cs_exec_set_transform,
2642 /* WINED3D_CS_OP_SET_CLIP_PLANE */ wined3d_cs_exec_set_clip_plane,
2643 /* WINED3D_CS_OP_SET_COLOR_KEY */ wined3d_cs_exec_set_color_key,
2644 /* WINED3D_CS_OP_SET_MATERIAL */ wined3d_cs_exec_set_material,
2645 /* WINED3D_CS_OP_SET_LIGHT */ wined3d_cs_exec_set_light,
2646 /* WINED3D_CS_OP_SET_LIGHT_ENABLE */ wined3d_cs_exec_set_light_enable,
2647 /* WINED3D_CS_OP_PUSH_CONSTANTS */ wined3d_cs_exec_push_constants,
2648 /* WINED3D_CS_OP_RESET_STATE */ wined3d_cs_exec_reset_state,
2649 /* WINED3D_CS_OP_CALLBACK */ wined3d_cs_exec_callback,
2650 /* WINED3D_CS_OP_QUERY_ISSUE */ wined3d_cs_exec_query_issue,
2651 /* WINED3D_CS_OP_PRELOAD_RESOURCE */ wined3d_cs_exec_preload_resource,
2652 /* WINED3D_CS_OP_UNLOAD_RESOURCE */ wined3d_cs_exec_unload_resource,
2653 /* WINED3D_CS_OP_MAP */ wined3d_cs_exec_map,
2654 /* WINED3D_CS_OP_UNMAP */ wined3d_cs_exec_unmap,
2655 /* WINED3D_CS_OP_BLT_SUB_RESOURCE */ wined3d_cs_exec_blt_sub_resource,
2656 /* WINED3D_CS_OP_UPDATE_SUB_RESOURCE */ wined3d_cs_exec_update_sub_resource,
2657 /* WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION */ wined3d_cs_exec_add_dirty_texture_region,
2658 /* WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_clear_unordered_access_view,
2659 /* WINED3D_CS_OP_COPY_UAV_COUNTER */ wined3d_cs_exec_copy_uav_counter,
2660 /* WINED3D_CS_OP_GENERATE_MIPMAPS */ wined3d_cs_exec_generate_mipmaps,
2663 static void *wined3d_cs_st_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2665 if (size > (cs->data_size - cs->end))
2667 size_t new_size;
2668 void *new_data;
2670 new_size = max(size, cs->data_size * 2);
2671 if (!cs->end)
2672 new_data = heap_realloc(cs->data, new_size);
2673 else
2674 new_data = heap_alloc(new_size);
2675 if (!new_data)
2676 return NULL;
2678 cs->data_size = new_size;
2679 cs->start = cs->end = 0;
2680 cs->data = new_data;
2683 cs->end += size;
2685 return (BYTE *)cs->data + cs->start;
2688 static void wined3d_cs_st_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2690 enum wined3d_cs_op opcode;
2691 size_t start;
2692 BYTE *data;
2694 data = cs->data;
2695 start = cs->start;
2696 cs->start = cs->end;
2698 opcode = *(const enum wined3d_cs_op *)&data[start];
2699 if (opcode >= WINED3D_CS_OP_STOP)
2700 ERR("Invalid opcode %#x.\n", opcode);
2701 else
2702 wined3d_cs_op_handlers[opcode](cs, &data[start]);
2704 if (cs->data == data)
2705 cs->start = cs->end = start;
2706 else if (!start)
2707 heap_free(data);
2710 static void wined3d_cs_st_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2714 static const struct wined3d_cs_ops wined3d_cs_st_ops =
2716 wined3d_cs_st_require_space,
2717 wined3d_cs_st_submit,
2718 wined3d_cs_st_finish,
2719 wined3d_cs_st_push_constants,
2722 static BOOL wined3d_cs_queue_is_empty(const struct wined3d_cs *cs, const struct wined3d_cs_queue *queue)
2724 wined3d_from_cs(cs);
2725 return *(volatile LONG *)&queue->head == queue->tail;
2728 static void wined3d_cs_queue_submit(struct wined3d_cs_queue *queue, struct wined3d_cs *cs)
2730 struct wined3d_cs_packet *packet;
2731 size_t packet_size;
2733 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2734 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2735 InterlockedExchange(&queue->head, (queue->head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1));
2737 if (InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2738 SetEvent(cs->event);
2741 static void wined3d_cs_mt_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2743 if (cs->thread_id == GetCurrentThreadId())
2744 return wined3d_cs_st_submit(cs, queue_id);
2746 wined3d_cs_queue_submit(&cs->queue[queue_id], cs);
2749 static void *wined3d_cs_queue_require_space(struct wined3d_cs_queue *queue, size_t size, struct wined3d_cs *cs)
2751 size_t queue_size = ARRAY_SIZE(queue->data);
2752 size_t header_size, packet_size, remaining;
2753 struct wined3d_cs_packet *packet;
2755 header_size = FIELD_OFFSET(struct wined3d_cs_packet, data[0]);
2756 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[size]);
2757 packet_size = (packet_size + header_size - 1) & ~(header_size - 1);
2758 size = packet_size - header_size;
2759 if (packet_size >= WINED3D_CS_QUEUE_SIZE)
2761 ERR("Packet size %lu >= queue size %u.\n",
2762 (unsigned long)packet_size, WINED3D_CS_QUEUE_SIZE);
2763 return NULL;
2766 remaining = queue_size - queue->head;
2767 if (remaining < packet_size)
2769 size_t nop_size = remaining - header_size;
2770 struct wined3d_cs_nop *nop;
2772 TRACE("Inserting a nop for %lu + %lu bytes.\n",
2773 (unsigned long)header_size, (unsigned long)nop_size);
2775 nop = wined3d_cs_queue_require_space(queue, nop_size, cs);
2776 if (nop_size)
2777 nop->opcode = WINED3D_CS_OP_NOP;
2779 wined3d_cs_queue_submit(queue, cs);
2780 assert(!queue->head);
2783 for (;;)
2785 LONG tail = *(volatile LONG *)&queue->tail;
2786 LONG head = queue->head;
2787 LONG new_pos;
2789 /* Empty. */
2790 if (head == tail)
2791 break;
2792 new_pos = (head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1);
2793 /* Head ahead of tail. We checked the remaining size above, so we only
2794 * need to make sure we don't make head equal to tail. */
2795 if (head > tail && (new_pos != tail))
2796 break;
2797 /* Tail ahead of head. Make sure the new head is before the tail as
2798 * well. Note that new_pos is 0 when it's at the end of the queue. */
2799 if (new_pos < tail && new_pos)
2800 break;
2802 TRACE("Waiting for free space. Head %u, tail %u, packet size %lu.\n",
2803 head, tail, (unsigned long)packet_size);
2806 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2807 packet->size = size;
2808 return packet->data;
2811 static void *wined3d_cs_mt_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2813 if (cs->thread_id == GetCurrentThreadId())
2814 return wined3d_cs_st_require_space(cs, size, queue_id);
2816 return wined3d_cs_queue_require_space(&cs->queue[queue_id], size, cs);
2819 static void wined3d_cs_mt_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2821 if (cs->thread_id == GetCurrentThreadId())
2822 return wined3d_cs_st_finish(cs, queue_id);
2824 while (cs->queue[queue_id].head != *(volatile LONG *)&cs->queue[queue_id].tail)
2825 wined3d_pause();
2828 static const struct wined3d_cs_ops wined3d_cs_mt_ops =
2830 wined3d_cs_mt_require_space,
2831 wined3d_cs_mt_submit,
2832 wined3d_cs_mt_finish,
2833 wined3d_cs_mt_push_constants,
2836 static void poll_queries(struct wined3d_cs *cs)
2838 struct wined3d_query *query, *cursor;
2840 LIST_FOR_EACH_ENTRY_SAFE(query, cursor, &cs->query_poll_list, struct wined3d_query, poll_list_entry)
2842 if (!query->query_ops->query_poll(query, 0))
2843 continue;
2845 list_remove(&query->poll_list_entry);
2846 list_init(&query->poll_list_entry);
2847 InterlockedIncrement(&query->counter_retrieved);
2851 static void wined3d_cs_wait_event(struct wined3d_cs *cs)
2853 InterlockedExchange(&cs->waiting_for_event, TRUE);
2855 /* The main thread might have enqueued a command and blocked on it after
2856 * the CS thread decided to enter wined3d_cs_wait_event(), but before
2857 * "waiting_for_event" was set.
2859 * Likewise, we can race with the main thread when resetting
2860 * "waiting_for_event", in which case we would need to call
2861 * WaitForSingleObject() because the main thread called SetEvent(). */
2862 if (!(wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_DEFAULT])
2863 && wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_MAP]))
2864 && InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2865 return;
2867 WaitForSingleObject(cs->event, INFINITE);
2870 static void wined3d_cs_command_lock(const struct wined3d_cs *cs)
2872 if (cs->serialize_commands)
2873 EnterCriticalSection(&wined3d_command_cs);
2876 static void wined3d_cs_command_unlock(const struct wined3d_cs *cs)
2878 if (cs->serialize_commands)
2879 LeaveCriticalSection(&wined3d_command_cs);
2882 static DWORD WINAPI wined3d_cs_run(void *ctx)
2884 struct wined3d_cs_packet *packet;
2885 struct wined3d_cs_queue *queue;
2886 unsigned int spin_count = 0;
2887 struct wined3d_cs *cs = ctx;
2888 enum wined3d_cs_op opcode;
2889 HMODULE wined3d_module;
2890 unsigned int poll = 0;
2891 LONG tail;
2893 TRACE("Started.\n");
2895 /* Copy the module handle to a local variable to avoid racing with the
2896 * thread freeing "cs" before the FreeLibraryAndExitThread() call. */
2897 wined3d_module = cs->wined3d_module;
2899 list_init(&cs->query_poll_list);
2900 cs->thread_id = GetCurrentThreadId();
2901 for (;;)
2903 if (++poll == WINED3D_CS_QUERY_POLL_INTERVAL)
2905 wined3d_cs_command_lock(cs);
2906 poll_queries(cs);
2907 wined3d_cs_command_unlock(cs);
2908 poll = 0;
2911 queue = &cs->queue[WINED3D_CS_QUEUE_MAP];
2912 if (wined3d_cs_queue_is_empty(cs, queue))
2914 queue = &cs->queue[WINED3D_CS_QUEUE_DEFAULT];
2915 if (wined3d_cs_queue_is_empty(cs, queue))
2917 if (++spin_count >= WINED3D_CS_SPIN_COUNT && list_empty(&cs->query_poll_list))
2918 wined3d_cs_wait_event(cs);
2919 continue;
2922 spin_count = 0;
2924 tail = queue->tail;
2925 packet = (struct wined3d_cs_packet *)&queue->data[tail];
2926 if (packet->size)
2928 opcode = *(const enum wined3d_cs_op *)packet->data;
2930 TRACE("Executing %s.\n", debug_cs_op(opcode));
2931 if (opcode >= WINED3D_CS_OP_STOP)
2933 if (opcode > WINED3D_CS_OP_STOP)
2934 ERR("Invalid opcode %#x.\n", opcode);
2935 break;
2938 wined3d_cs_command_lock(cs);
2939 wined3d_cs_op_handlers[opcode](cs, packet->data);
2940 wined3d_cs_command_unlock(cs);
2941 TRACE("%s executed.\n", debug_cs_op(opcode));
2944 tail += FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2945 tail &= (WINED3D_CS_QUEUE_SIZE - 1);
2946 InterlockedExchange(&queue->tail, tail);
2949 cs->queue[WINED3D_CS_QUEUE_MAP].tail = cs->queue[WINED3D_CS_QUEUE_MAP].head;
2950 cs->queue[WINED3D_CS_QUEUE_DEFAULT].tail = cs->queue[WINED3D_CS_QUEUE_DEFAULT].head;
2951 TRACE("Stopped.\n");
2952 FreeLibraryAndExitThread(wined3d_module, 0);
2955 struct wined3d_cs *wined3d_cs_create(struct wined3d_device *device)
2957 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2958 struct wined3d_cs *cs;
2960 if (!(cs = heap_alloc_zero(sizeof(*cs))))
2961 return NULL;
2963 cs->ops = &wined3d_cs_st_ops;
2964 cs->device = device;
2965 cs->serialize_commands = TRACE_ON(d3d_sync) || wined3d_settings.cs_multithreaded & WINED3D_CSMT_SERIALIZE;
2967 state_init(&cs->state, d3d_info, WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
2969 cs->data_size = WINED3D_INITIAL_CS_SIZE;
2970 if (!(cs->data = heap_alloc(cs->data_size)))
2971 goto fail;
2973 if (wined3d_settings.cs_multithreaded & WINED3D_CSMT_ENABLE
2974 && !RtlIsCriticalSectionLockedByThread(NtCurrentTeb()->Peb->LoaderLock))
2976 cs->ops = &wined3d_cs_mt_ops;
2978 if (!(cs->event = CreateEventW(NULL, FALSE, FALSE, NULL)))
2980 ERR("Failed to create command stream event.\n");
2981 heap_free(cs->data);
2982 goto fail;
2985 if (!(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2986 (const WCHAR *)wined3d_cs_run, &cs->wined3d_module)))
2988 ERR("Failed to get wined3d module handle.\n");
2989 CloseHandle(cs->event);
2990 heap_free(cs->data);
2991 goto fail;
2994 if (!(cs->thread = CreateThread(NULL, 0, wined3d_cs_run, cs, 0, NULL)))
2996 ERR("Failed to create wined3d command stream thread.\n");
2997 FreeLibrary(cs->wined3d_module);
2998 CloseHandle(cs->event);
2999 heap_free(cs->data);
3000 goto fail;
3004 return cs;
3006 fail:
3007 state_cleanup(&cs->state);
3008 heap_free(cs);
3009 return NULL;
3012 void wined3d_cs_destroy(struct wined3d_cs *cs)
3014 if (cs->thread)
3016 wined3d_cs_emit_stop(cs);
3017 CloseHandle(cs->thread);
3018 if (!CloseHandle(cs->event))
3019 ERR("Closing event failed.\n");
3022 state_cleanup(&cs->state);
3023 heap_free(cs->data);
3024 heap_free(cs);