wined3d: Limit clears to the size of the framebuffer.
[wine.git] / dlls / wined3d / cs.c
blob2f0c4ed52e4ffe66aca0b54f956ce6dda6753edb
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);
25 #define WINED3D_INITIAL_CS_SIZE 4096
27 enum wined3d_cs_op
29 WINED3D_CS_OP_NOP,
30 WINED3D_CS_OP_PRESENT,
31 WINED3D_CS_OP_CLEAR,
32 WINED3D_CS_OP_DISPATCH,
33 WINED3D_CS_OP_DRAW,
34 WINED3D_CS_OP_FLUSH,
35 WINED3D_CS_OP_SET_PREDICATION,
36 WINED3D_CS_OP_SET_VIEWPORT,
37 WINED3D_CS_OP_SET_SCISSOR_RECT,
38 WINED3D_CS_OP_SET_RENDERTARGET_VIEW,
39 WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW,
40 WINED3D_CS_OP_SET_VERTEX_DECLARATION,
41 WINED3D_CS_OP_SET_STREAM_SOURCE,
42 WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ,
43 WINED3D_CS_OP_SET_STREAM_OUTPUT,
44 WINED3D_CS_OP_SET_INDEX_BUFFER,
45 WINED3D_CS_OP_SET_CONSTANT_BUFFER,
46 WINED3D_CS_OP_SET_TEXTURE,
47 WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW,
48 WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW,
49 WINED3D_CS_OP_SET_SAMPLER,
50 WINED3D_CS_OP_SET_SHADER,
51 WINED3D_CS_OP_SET_BLEND_STATE,
52 WINED3D_CS_OP_SET_RASTERIZER_STATE,
53 WINED3D_CS_OP_SET_RENDER_STATE,
54 WINED3D_CS_OP_SET_TEXTURE_STATE,
55 WINED3D_CS_OP_SET_SAMPLER_STATE,
56 WINED3D_CS_OP_SET_TRANSFORM,
57 WINED3D_CS_OP_SET_CLIP_PLANE,
58 WINED3D_CS_OP_SET_COLOR_KEY,
59 WINED3D_CS_OP_SET_MATERIAL,
60 WINED3D_CS_OP_SET_LIGHT,
61 WINED3D_CS_OP_SET_LIGHT_ENABLE,
62 WINED3D_CS_OP_PUSH_CONSTANTS,
63 WINED3D_CS_OP_RESET_STATE,
64 WINED3D_CS_OP_CALLBACK,
65 WINED3D_CS_OP_QUERY_ISSUE,
66 WINED3D_CS_OP_PRELOAD_RESOURCE,
67 WINED3D_CS_OP_UNLOAD_RESOURCE,
68 WINED3D_CS_OP_MAP,
69 WINED3D_CS_OP_UNMAP,
70 WINED3D_CS_OP_BLT_SUB_RESOURCE,
71 WINED3D_CS_OP_UPDATE_SUB_RESOURCE,
72 WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION,
73 WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW,
74 WINED3D_CS_OP_COPY_UAV_COUNTER,
75 WINED3D_CS_OP_GENERATE_MIPMAPS,
76 WINED3D_CS_OP_STOP,
79 struct wined3d_cs_packet
81 size_t size;
82 BYTE data[1];
85 struct wined3d_cs_nop
87 enum wined3d_cs_op opcode;
90 struct wined3d_cs_present
92 enum wined3d_cs_op opcode;
93 HWND dst_window_override;
94 struct wined3d_swapchain *swapchain;
95 RECT src_rect;
96 RECT dst_rect;
97 unsigned int swap_interval;
98 DWORD flags;
101 struct wined3d_cs_clear
103 enum wined3d_cs_op opcode;
104 DWORD flags;
105 unsigned int rt_count;
106 struct wined3d_fb_state *fb;
107 RECT draw_rect;
108 struct wined3d_color color;
109 float depth;
110 DWORD stencil;
111 unsigned int rect_count;
112 RECT rects[1];
115 struct wined3d_cs_dispatch
117 enum wined3d_cs_op opcode;
118 struct wined3d_dispatch_parameters parameters;
121 struct wined3d_cs_draw
123 enum wined3d_cs_op opcode;
124 GLenum primitive_type;
125 GLint patch_vertex_count;
126 struct wined3d_draw_parameters parameters;
129 struct wined3d_cs_flush
131 enum wined3d_cs_op opcode;
134 struct wined3d_cs_set_predication
136 enum wined3d_cs_op opcode;
137 struct wined3d_query *predicate;
138 BOOL value;
141 struct wined3d_cs_set_viewport
143 enum wined3d_cs_op opcode;
144 struct wined3d_viewport viewport;
147 struct wined3d_cs_set_scissor_rect
149 enum wined3d_cs_op opcode;
150 RECT rect;
153 struct wined3d_cs_set_rendertarget_view
155 enum wined3d_cs_op opcode;
156 unsigned int view_idx;
157 struct wined3d_rendertarget_view *view;
160 struct wined3d_cs_set_depth_stencil_view
162 enum wined3d_cs_op opcode;
163 struct wined3d_rendertarget_view *view;
166 struct wined3d_cs_set_vertex_declaration
168 enum wined3d_cs_op opcode;
169 struct wined3d_vertex_declaration *declaration;
172 struct wined3d_cs_set_stream_source
174 enum wined3d_cs_op opcode;
175 UINT stream_idx;
176 struct wined3d_buffer *buffer;
177 UINT offset;
178 UINT stride;
181 struct wined3d_cs_set_stream_source_freq
183 enum wined3d_cs_op opcode;
184 UINT stream_idx;
185 UINT frequency;
186 UINT flags;
189 struct wined3d_cs_set_stream_output
191 enum wined3d_cs_op opcode;
192 UINT stream_idx;
193 struct wined3d_buffer *buffer;
194 UINT offset;
197 struct wined3d_cs_set_index_buffer
199 enum wined3d_cs_op opcode;
200 struct wined3d_buffer *buffer;
201 enum wined3d_format_id format_id;
202 unsigned int offset;
205 struct wined3d_cs_set_constant_buffer
207 enum wined3d_cs_op opcode;
208 enum wined3d_shader_type type;
209 UINT cb_idx;
210 struct wined3d_buffer *buffer;
213 struct wined3d_cs_set_texture
215 enum wined3d_cs_op opcode;
216 UINT stage;
217 struct wined3d_texture *texture;
220 struct wined3d_cs_set_color_key
222 enum wined3d_cs_op opcode;
223 struct wined3d_texture *texture;
224 WORD flags;
225 WORD set;
226 struct wined3d_color_key color_key;
229 struct wined3d_cs_set_shader_resource_view
231 enum wined3d_cs_op opcode;
232 enum wined3d_shader_type type;
233 UINT view_idx;
234 struct wined3d_shader_resource_view *view;
237 struct wined3d_cs_set_unordered_access_view
239 enum wined3d_cs_op opcode;
240 enum wined3d_pipeline pipeline;
241 unsigned int view_idx;
242 struct wined3d_unordered_access_view *view;
243 unsigned int initial_count;
246 struct wined3d_cs_set_sampler
248 enum wined3d_cs_op opcode;
249 enum wined3d_shader_type type;
250 UINT sampler_idx;
251 struct wined3d_sampler *sampler;
254 struct wined3d_cs_set_shader
256 enum wined3d_cs_op opcode;
257 enum wined3d_shader_type type;
258 struct wined3d_shader *shader;
261 struct wined3d_cs_set_blend_state
263 enum wined3d_cs_op opcode;
264 struct wined3d_blend_state *state;
267 struct wined3d_cs_set_rasterizer_state
269 enum wined3d_cs_op opcode;
270 struct wined3d_rasterizer_state *state;
273 struct wined3d_cs_set_render_state
275 enum wined3d_cs_op opcode;
276 enum wined3d_render_state state;
277 DWORD value;
280 struct wined3d_cs_set_texture_state
282 enum wined3d_cs_op opcode;
283 UINT stage;
284 enum wined3d_texture_stage_state state;
285 DWORD value;
288 struct wined3d_cs_set_sampler_state
290 enum wined3d_cs_op opcode;
291 UINT sampler_idx;
292 enum wined3d_sampler_state state;
293 DWORD value;
296 struct wined3d_cs_set_transform
298 enum wined3d_cs_op opcode;
299 enum wined3d_transform_state state;
300 struct wined3d_matrix matrix;
303 struct wined3d_cs_set_clip_plane
305 enum wined3d_cs_op opcode;
306 UINT plane_idx;
307 struct wined3d_vec4 plane;
310 struct wined3d_cs_set_material
312 enum wined3d_cs_op opcode;
313 struct wined3d_material material;
316 struct wined3d_cs_set_light
318 enum wined3d_cs_op opcode;
319 struct wined3d_light_info light;
322 struct wined3d_cs_set_light_enable
324 enum wined3d_cs_op opcode;
325 unsigned int idx;
326 BOOL enable;
329 struct wined3d_cs_push_constants
331 enum wined3d_cs_op opcode;
332 enum wined3d_push_constants type;
333 unsigned int start_idx;
334 unsigned int count;
335 BYTE constants[1];
338 struct wined3d_cs_reset_state
340 enum wined3d_cs_op opcode;
343 struct wined3d_cs_callback
345 enum wined3d_cs_op opcode;
346 void (*callback)(void *object);
347 void *object;
350 struct wined3d_cs_query_issue
352 enum wined3d_cs_op opcode;
353 struct wined3d_query *query;
354 DWORD flags;
357 struct wined3d_cs_preload_resource
359 enum wined3d_cs_op opcode;
360 struct wined3d_resource *resource;
363 struct wined3d_cs_unload_resource
365 enum wined3d_cs_op opcode;
366 struct wined3d_resource *resource;
369 struct wined3d_cs_map
371 enum wined3d_cs_op opcode;
372 struct wined3d_resource *resource;
373 unsigned int sub_resource_idx;
374 struct wined3d_map_desc *map_desc;
375 const struct wined3d_box *box;
376 DWORD flags;
377 HRESULT *hr;
380 struct wined3d_cs_unmap
382 enum wined3d_cs_op opcode;
383 struct wined3d_resource *resource;
384 unsigned int sub_resource_idx;
385 HRESULT *hr;
388 struct wined3d_cs_blt_sub_resource
390 enum wined3d_cs_op opcode;
391 struct wined3d_resource *dst_resource;
392 unsigned int dst_sub_resource_idx;
393 struct wined3d_box dst_box;
394 struct wined3d_resource *src_resource;
395 unsigned int src_sub_resource_idx;
396 struct wined3d_box src_box;
397 DWORD flags;
398 struct wined3d_blt_fx fx;
399 enum wined3d_texture_filter_type filter;
402 struct wined3d_cs_update_sub_resource
404 enum wined3d_cs_op opcode;
405 struct wined3d_resource *resource;
406 unsigned int sub_resource_idx;
407 struct wined3d_box box;
408 struct wined3d_sub_resource_data data;
411 struct wined3d_cs_add_dirty_texture_region
413 enum wined3d_cs_op opcode;
414 struct wined3d_texture *texture;
415 unsigned int layer;
418 struct wined3d_cs_clear_unordered_access_view
420 enum wined3d_cs_op opcode;
421 struct wined3d_unordered_access_view *view;
422 struct wined3d_uvec4 clear_value;
425 struct wined3d_cs_copy_uav_counter
427 enum wined3d_cs_op opcode;
428 struct wined3d_buffer *buffer;
429 unsigned int offset;
430 struct wined3d_unordered_access_view *view;
433 struct wined3d_cs_generate_mipmaps
435 enum wined3d_cs_op opcode;
436 struct wined3d_shader_resource_view *view;
439 struct wined3d_cs_stop
441 enum wined3d_cs_op opcode;
444 static void wined3d_cs_exec_nop(struct wined3d_cs *cs, const void *data)
448 static void wined3d_cs_exec_present(struct wined3d_cs *cs, const void *data)
450 const struct wined3d_cs_present *op = data;
451 struct wined3d_swapchain *swapchain;
452 unsigned int i;
454 swapchain = op->swapchain;
455 wined3d_swapchain_set_window(swapchain, op->dst_window_override);
456 wined3d_swapchain_set_swap_interval(swapchain, op->swap_interval);
458 swapchain->swapchain_ops->swapchain_present(swapchain, &op->src_rect, &op->dst_rect, op->flags);
460 wined3d_resource_release(&swapchain->front_buffer->resource);
461 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
463 wined3d_resource_release(&swapchain->back_buffers[i]->resource);
466 InterlockedDecrement(&cs->pending_presents);
469 void wined3d_cs_emit_present(struct wined3d_cs *cs, struct wined3d_swapchain *swapchain,
470 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
471 unsigned int swap_interval, DWORD flags)
473 struct wined3d_cs_present *op;
474 unsigned int i;
475 LONG pending;
477 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
478 op->opcode = WINED3D_CS_OP_PRESENT;
479 op->dst_window_override = dst_window_override;
480 op->swapchain = swapchain;
481 op->src_rect = *src_rect;
482 op->dst_rect = *dst_rect;
483 op->swap_interval = swap_interval;
484 op->flags = flags;
486 pending = InterlockedIncrement(&cs->pending_presents);
488 wined3d_resource_acquire(&swapchain->front_buffer->resource);
489 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
491 wined3d_resource_acquire(&swapchain->back_buffers[i]->resource);
494 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
496 /* Limit input latency by limiting the number of presents that we can get
497 * ahead of the worker thread. We have a constant limit here, but
498 * IDXGIDevice1 allows tuning this. */
499 while (pending > 1)
501 wined3d_pause();
502 pending = InterlockedCompareExchange(&cs->pending_presents, 0, 0);
506 static void wined3d_cs_exec_clear(struct wined3d_cs *cs, const void *data)
508 const struct wined3d_cs_clear *op = data;
509 struct wined3d_device *device;
510 unsigned int i;
512 device = cs->device;
513 device->blitter->ops->blitter_clear(device->blitter, device, op->rt_count, op->fb,
514 op->rect_count, op->rects, &op->draw_rect, op->flags, &op->color, op->depth, op->stencil);
516 if (op->flags & WINED3DCLEAR_TARGET)
518 for (i = 0; i < op->rt_count; ++i)
520 if (op->fb->render_targets[i])
521 wined3d_resource_release(op->fb->render_targets[i]->resource);
524 if (op->flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
525 wined3d_resource_release(op->fb->depth_stencil->resource);
528 void wined3d_cs_emit_clear(struct wined3d_cs *cs, DWORD rect_count, const RECT *rects,
529 DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
531 unsigned int rt_count = cs->device->adapter->gl_info.limits.buffers;
532 const struct wined3d_state *state = &cs->device->state;
533 const struct wined3d_viewport *vp = &state->viewport;
534 struct wined3d_rendertarget_view *view;
535 struct wined3d_cs_clear *op;
536 RECT view_rect;
537 unsigned int i;
539 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_clear, rects[rect_count]),
540 WINED3D_CS_QUEUE_DEFAULT);
541 op->opcode = WINED3D_CS_OP_CLEAR;
542 op->flags = flags;
543 op->rt_count = rt_count;
544 op->fb = &cs->fb;
545 SetRect(&op->draw_rect, vp->x, vp->y, vp->x + vp->width, vp->y + vp->height);
546 if (state->render_states[WINED3D_RS_SCISSORTESTENABLE])
547 IntersectRect(&op->draw_rect, &op->draw_rect, &state->scissor_rect);
548 op->color = *color;
549 op->depth = depth;
550 op->stencil = stencil;
551 op->rect_count = rect_count;
552 memcpy(op->rects, rects, sizeof(*rects) * rect_count);
554 if (flags & WINED3DCLEAR_TARGET)
556 for (i = 0; i < rt_count; ++i)
558 if ((view = state->fb->render_targets[i]))
560 SetRect(&view_rect, 0, 0, view->width, view->height);
561 IntersectRect(&op->draw_rect, &op->draw_rect, &view_rect);
562 wined3d_resource_acquire(view->resource);
566 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
568 view = state->fb->depth_stencil;
569 SetRect(&view_rect, 0, 0, view->width, view->height);
570 IntersectRect(&op->draw_rect, &op->draw_rect, &view_rect);
571 wined3d_resource_acquire(view->resource);
574 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
577 void wined3d_cs_emit_clear_rendertarget_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view,
578 const RECT *rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
580 struct wined3d_cs_clear *op;
581 size_t size;
583 size = FIELD_OFFSET(struct wined3d_cs_clear, rects[1]) + sizeof(struct wined3d_fb_state);
584 op = cs->ops->require_space(cs, size, WINED3D_CS_QUEUE_DEFAULT);
585 op->fb = (void *)&op->rects[1];
587 op->opcode = WINED3D_CS_OP_CLEAR;
588 op->flags = flags;
589 if (flags & WINED3DCLEAR_TARGET)
591 op->rt_count = 1;
592 op->fb->render_targets[0] = view;
593 op->fb->depth_stencil = NULL;
594 op->color = *color;
596 else
598 op->rt_count = 0;
599 op->fb->render_targets[0] = NULL;
600 op->fb->depth_stencil = view;
601 op->depth = depth;
602 op->stencil = stencil;
604 SetRect(&op->draw_rect, 0, 0, view->width, view->height);
605 op->rect_count = 1;
606 op->rects[0] = *rect;
608 wined3d_resource_acquire(view->resource);
610 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
613 static void acquire_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
615 struct wined3d_shader_sampler_map_entry *entry;
616 struct wined3d_shader_resource_view *view;
617 struct wined3d_shader *shader;
618 unsigned int i, j;
620 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
622 if (!(shader_mask & (1u << i)))
623 continue;
625 if (!(shader = state->shader[i]))
626 continue;
628 for (j = 0; j < WINED3D_MAX_CBS; ++j)
630 if (state->cb[i][j])
631 wined3d_resource_acquire(&state->cb[i][j]->resource);
634 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
636 entry = &shader->reg_maps.sampler_map.entries[j];
638 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
639 continue;
641 wined3d_resource_acquire(view->resource);
646 static void release_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
648 struct wined3d_shader_sampler_map_entry *entry;
649 struct wined3d_shader_resource_view *view;
650 struct wined3d_shader *shader;
651 unsigned int i, j;
653 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
655 if (!(shader_mask & (1u << i)))
656 continue;
658 if (!(shader = state->shader[i]))
659 continue;
661 for (j = 0; j < WINED3D_MAX_CBS; ++j)
663 if (state->cb[i][j])
664 wined3d_resource_release(&state->cb[i][j]->resource);
667 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
669 entry = &shader->reg_maps.sampler_map.entries[j];
671 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
672 continue;
674 wined3d_resource_release(view->resource);
679 static void acquire_unordered_access_resources(const struct wined3d_shader *shader,
680 struct wined3d_unordered_access_view * const *views)
682 unsigned int i;
684 if (!shader)
685 return;
687 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
689 if (!shader->reg_maps.uav_resource_info[i].type)
690 continue;
692 if (!views[i])
693 continue;
695 wined3d_resource_acquire(views[i]->resource);
699 static void release_unordered_access_resources(const struct wined3d_shader *shader,
700 struct wined3d_unordered_access_view * const *views)
702 unsigned int i;
704 if (!shader)
705 return;
707 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
709 if (!shader->reg_maps.uav_resource_info[i].type)
710 continue;
712 if (!views[i])
713 continue;
715 wined3d_resource_release(views[i]->resource);
719 static void wined3d_cs_exec_dispatch(struct wined3d_cs *cs, const void *data)
721 const struct wined3d_cs_dispatch *op = data;
722 struct wined3d_state *state = &cs->state;
724 dispatch_compute(cs->device, state, &op->parameters);
726 if (op->parameters.indirect)
727 wined3d_resource_release(&op->parameters.u.indirect.buffer->resource);
729 release_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
730 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
731 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
734 static void acquire_compute_pipeline_resources(const struct wined3d_state *state)
736 acquire_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
737 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
738 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
741 void wined3d_cs_emit_dispatch(struct wined3d_cs *cs,
742 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
744 const struct wined3d_state *state = &cs->device->state;
745 struct wined3d_cs_dispatch *op;
747 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
748 op->opcode = WINED3D_CS_OP_DISPATCH;
749 op->parameters.indirect = FALSE;
750 op->parameters.u.direct.group_count_x = group_count_x;
751 op->parameters.u.direct.group_count_y = group_count_y;
752 op->parameters.u.direct.group_count_z = group_count_z;
754 acquire_compute_pipeline_resources(state);
756 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
759 void wined3d_cs_emit_dispatch_indirect(struct wined3d_cs *cs,
760 struct wined3d_buffer *buffer, unsigned int offset)
762 const struct wined3d_state *state = &cs->device->state;
763 struct wined3d_cs_dispatch *op;
765 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
766 op->opcode = WINED3D_CS_OP_DISPATCH;
767 op->parameters.indirect = TRUE;
768 op->parameters.u.indirect.buffer = buffer;
769 op->parameters.u.indirect.offset = offset;
771 acquire_compute_pipeline_resources(state);
772 wined3d_resource_acquire(&buffer->resource);
774 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
777 static void wined3d_cs_exec_draw(struct wined3d_cs *cs, const void *data)
779 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
780 struct wined3d_state *state = &cs->state;
781 const struct wined3d_cs_draw *op = data;
782 int load_base_vertex_idx;
783 unsigned int i;
785 /* ARB_draw_indirect always supports a base vertex offset. */
786 if (!op->parameters.indirect && !gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
787 load_base_vertex_idx = op->parameters.u.direct.base_vertex_idx;
788 else
789 load_base_vertex_idx = 0;
791 if (state->load_base_vertex_index != load_base_vertex_idx)
793 state->load_base_vertex_index = load_base_vertex_idx;
794 device_invalidate_state(cs->device, STATE_BASEVERTEXINDEX);
797 if (state->gl_primitive_type != op->primitive_type)
799 if (state->gl_primitive_type == GL_POINTS || op->primitive_type == GL_POINTS)
800 device_invalidate_state(cs->device, STATE_POINT_ENABLE);
801 state->gl_primitive_type = op->primitive_type;
803 state->gl_patch_vertices = op->patch_vertex_count;
805 draw_primitive(cs->device, state, &op->parameters);
807 if (op->parameters.indirect)
809 struct wined3d_buffer *buffer = op->parameters.u.indirect.buffer;
810 wined3d_resource_release(&buffer->resource);
813 if (op->parameters.indexed)
814 wined3d_resource_release(&state->index_buffer->resource);
815 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
817 if (state->streams[i].buffer)
818 wined3d_resource_release(&state->streams[i].buffer->resource);
820 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
822 if (state->stream_output[i].buffer)
823 wined3d_resource_release(&state->stream_output[i].buffer->resource);
825 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
827 if (state->textures[i])
828 wined3d_resource_release(&state->textures[i]->resource);
830 for (i = 0; i < gl_info->limits.buffers; ++i)
832 if (state->fb->render_targets[i])
833 wined3d_resource_release(state->fb->render_targets[i]->resource);
835 if (state->fb->depth_stencil)
836 wined3d_resource_release(state->fb->depth_stencil->resource);
837 release_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
838 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
839 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
842 static void acquire_graphics_pipeline_resources(const struct wined3d_state *state,
843 BOOL indexed, const struct wined3d_gl_info *gl_info)
845 unsigned int i;
847 if (indexed)
848 wined3d_resource_acquire(&state->index_buffer->resource);
849 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
851 if (state->streams[i].buffer)
852 wined3d_resource_acquire(&state->streams[i].buffer->resource);
854 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
856 if (state->stream_output[i].buffer)
857 wined3d_resource_acquire(&state->stream_output[i].buffer->resource);
859 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
861 if (state->textures[i])
862 wined3d_resource_acquire(&state->textures[i]->resource);
864 for (i = 0; i < gl_info->limits.buffers; ++i)
866 if (state->fb->render_targets[i])
867 wined3d_resource_acquire(state->fb->render_targets[i]->resource);
869 if (state->fb->depth_stencil)
870 wined3d_resource_acquire(state->fb->depth_stencil->resource);
871 acquire_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
872 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
873 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
876 void wined3d_cs_emit_draw(struct wined3d_cs *cs, GLenum primitive_type, unsigned int patch_vertex_count,
877 int base_vertex_idx, unsigned int start_idx, unsigned int index_count,
878 unsigned int start_instance, unsigned int instance_count, BOOL indexed)
880 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
881 const struct wined3d_state *state = &cs->device->state;
882 struct wined3d_cs_draw *op;
884 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
885 op->opcode = WINED3D_CS_OP_DRAW;
886 op->primitive_type = primitive_type;
887 op->patch_vertex_count = patch_vertex_count;
888 op->parameters.indirect = FALSE;
889 op->parameters.u.direct.base_vertex_idx = base_vertex_idx;
890 op->parameters.u.direct.start_idx = start_idx;
891 op->parameters.u.direct.index_count = index_count;
892 op->parameters.u.direct.start_instance = start_instance;
893 op->parameters.u.direct.instance_count = instance_count;
894 op->parameters.indexed = indexed;
896 acquire_graphics_pipeline_resources(state, indexed, gl_info);
898 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
901 void wined3d_cs_emit_draw_indirect(struct wined3d_cs *cs, GLenum primitive_type, unsigned int patch_vertex_count,
902 struct wined3d_buffer *buffer, unsigned int offset, BOOL indexed)
904 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
905 const struct wined3d_state *state = &cs->device->state;
906 struct wined3d_cs_draw *op;
908 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
909 op->opcode = WINED3D_CS_OP_DRAW;
910 op->primitive_type = primitive_type;
911 op->patch_vertex_count = patch_vertex_count;
912 op->parameters.indirect = TRUE;
913 op->parameters.u.indirect.buffer = buffer;
914 op->parameters.u.indirect.offset = offset;
915 op->parameters.indexed = indexed;
917 acquire_graphics_pipeline_resources(state, indexed, gl_info);
918 wined3d_resource_acquire(&buffer->resource);
920 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
923 static void wined3d_cs_exec_flush(struct wined3d_cs *cs, const void *data)
925 struct wined3d_context *context;
927 context = context_acquire(cs->device, NULL, 0);
928 if (context->valid)
929 context->gl_info->gl_ops.gl.p_glFlush();
930 context_release(context);
933 void wined3d_cs_emit_flush(struct wined3d_cs *cs)
935 struct wined3d_cs_flush *op;
937 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
938 op->opcode = WINED3D_CS_OP_FLUSH;
940 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
941 cs->queries_flushed = TRUE;
944 static void wined3d_cs_exec_set_predication(struct wined3d_cs *cs, const void *data)
946 const struct wined3d_cs_set_predication *op = data;
948 cs->state.predicate = op->predicate;
949 cs->state.predicate_value = op->value;
952 void wined3d_cs_emit_set_predication(struct wined3d_cs *cs, struct wined3d_query *predicate, BOOL value)
954 struct wined3d_cs_set_predication *op;
956 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
957 op->opcode = WINED3D_CS_OP_SET_PREDICATION;
958 op->predicate = predicate;
959 op->value = value;
961 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
964 static void wined3d_cs_exec_set_viewport(struct wined3d_cs *cs, const void *data)
966 const struct wined3d_cs_set_viewport *op = data;
968 cs->state.viewport = op->viewport;
969 device_invalidate_state(cs->device, STATE_VIEWPORT);
972 void wined3d_cs_emit_set_viewport(struct wined3d_cs *cs, const struct wined3d_viewport *viewport)
974 struct wined3d_cs_set_viewport *op;
976 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
977 op->opcode = WINED3D_CS_OP_SET_VIEWPORT;
978 op->viewport = *viewport;
980 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
983 static void wined3d_cs_exec_set_scissor_rect(struct wined3d_cs *cs, const void *data)
985 const struct wined3d_cs_set_scissor_rect *op = data;
987 cs->state.scissor_rect = op->rect;
988 device_invalidate_state(cs->device, STATE_SCISSORRECT);
991 void wined3d_cs_emit_set_scissor_rect(struct wined3d_cs *cs, const RECT *rect)
993 struct wined3d_cs_set_scissor_rect *op;
995 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
996 op->opcode = WINED3D_CS_OP_SET_SCISSOR_RECT;
997 op->rect = *rect;
999 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1002 static void wined3d_cs_exec_set_rendertarget_view(struct wined3d_cs *cs, const void *data)
1004 const struct wined3d_cs_set_rendertarget_view *op = data;
1006 cs->fb.render_targets[op->view_idx] = op->view;
1007 device_invalidate_state(cs->device, STATE_FRAMEBUFFER);
1010 void wined3d_cs_emit_set_rendertarget_view(struct wined3d_cs *cs, unsigned int view_idx,
1011 struct wined3d_rendertarget_view *view)
1013 struct wined3d_cs_set_rendertarget_view *op;
1015 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1016 op->opcode = WINED3D_CS_OP_SET_RENDERTARGET_VIEW;
1017 op->view_idx = view_idx;
1018 op->view = view;
1020 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1023 static void wined3d_cs_exec_set_depth_stencil_view(struct wined3d_cs *cs, const void *data)
1025 const struct wined3d_cs_set_depth_stencil_view *op = data;
1026 struct wined3d_device *device = cs->device;
1027 struct wined3d_rendertarget_view *prev;
1029 if ((prev = cs->state.fb->depth_stencil) && prev->resource->type != WINED3D_RTYPE_BUFFER)
1031 struct wined3d_texture *prev_texture = texture_from_resource(prev->resource);
1033 if (device->swapchains[0]->desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
1034 || prev_texture->flags & WINED3D_TEXTURE_DISCARD)
1035 wined3d_texture_validate_location(prev_texture,
1036 prev->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
1039 cs->fb.depth_stencil = op->view;
1041 if (!prev != !op->view)
1043 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
1044 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
1045 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
1046 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
1047 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
1049 else if (prev && prev->format->depth_bias_scale != op->view->format->depth_bias_scale)
1051 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
1054 device_invalidate_state(device, STATE_FRAMEBUFFER);
1057 void wined3d_cs_emit_set_depth_stencil_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view)
1059 struct wined3d_cs_set_depth_stencil_view *op;
1061 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1062 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW;
1063 op->view = view;
1065 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1068 static void wined3d_cs_exec_set_vertex_declaration(struct wined3d_cs *cs, const void *data)
1070 const struct wined3d_cs_set_vertex_declaration *op = data;
1072 cs->state.vertex_declaration = op->declaration;
1073 device_invalidate_state(cs->device, STATE_VDECL);
1076 void wined3d_cs_emit_set_vertex_declaration(struct wined3d_cs *cs, struct wined3d_vertex_declaration *declaration)
1078 struct wined3d_cs_set_vertex_declaration *op;
1080 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1081 op->opcode = WINED3D_CS_OP_SET_VERTEX_DECLARATION;
1082 op->declaration = declaration;
1084 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1087 static void wined3d_cs_exec_set_stream_source(struct wined3d_cs *cs, const void *data)
1089 const struct wined3d_cs_set_stream_source *op = data;
1090 struct wined3d_stream_state *stream;
1091 struct wined3d_buffer *prev;
1093 stream = &cs->state.streams[op->stream_idx];
1094 prev = stream->buffer;
1095 stream->buffer = op->buffer;
1096 stream->offset = op->offset;
1097 stream->stride = op->stride;
1099 if (op->buffer)
1100 InterlockedIncrement(&op->buffer->resource.bind_count);
1101 if (prev)
1102 InterlockedDecrement(&prev->resource.bind_count);
1104 device_invalidate_state(cs->device, STATE_STREAMSRC);
1107 void wined3d_cs_emit_set_stream_source(struct wined3d_cs *cs, UINT stream_idx,
1108 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1110 struct wined3d_cs_set_stream_source *op;
1112 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1113 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE;
1114 op->stream_idx = stream_idx;
1115 op->buffer = buffer;
1116 op->offset = offset;
1117 op->stride = stride;
1119 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1122 static void wined3d_cs_exec_set_stream_source_freq(struct wined3d_cs *cs, const void *data)
1124 const struct wined3d_cs_set_stream_source_freq *op = data;
1125 struct wined3d_stream_state *stream;
1127 stream = &cs->state.streams[op->stream_idx];
1128 stream->frequency = op->frequency;
1129 stream->flags = op->flags;
1131 device_invalidate_state(cs->device, STATE_STREAMSRC);
1134 void wined3d_cs_emit_set_stream_source_freq(struct wined3d_cs *cs, UINT stream_idx, UINT frequency, UINT flags)
1136 struct wined3d_cs_set_stream_source_freq *op;
1138 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1139 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ;
1140 op->stream_idx = stream_idx;
1141 op->frequency = frequency;
1142 op->flags = flags;
1144 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1147 static void wined3d_cs_exec_set_stream_output(struct wined3d_cs *cs, const void *data)
1149 const struct wined3d_cs_set_stream_output *op = data;
1150 struct wined3d_stream_output *stream;
1151 struct wined3d_buffer *prev;
1153 stream = &cs->state.stream_output[op->stream_idx];
1154 prev = stream->buffer;
1155 stream->buffer = op->buffer;
1156 stream->offset = op->offset;
1158 if (op->buffer)
1159 InterlockedIncrement(&op->buffer->resource.bind_count);
1160 if (prev)
1161 InterlockedDecrement(&prev->resource.bind_count);
1163 device_invalidate_state(cs->device, STATE_STREAM_OUTPUT);
1166 void wined3d_cs_emit_set_stream_output(struct wined3d_cs *cs, UINT stream_idx,
1167 struct wined3d_buffer *buffer, UINT offset)
1169 struct wined3d_cs_set_stream_output *op;
1171 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1172 op->opcode = WINED3D_CS_OP_SET_STREAM_OUTPUT;
1173 op->stream_idx = stream_idx;
1174 op->buffer = buffer;
1175 op->offset = offset;
1177 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1180 static void wined3d_cs_exec_set_index_buffer(struct wined3d_cs *cs, const void *data)
1182 const struct wined3d_cs_set_index_buffer *op = data;
1183 struct wined3d_buffer *prev;
1185 prev = cs->state.index_buffer;
1186 cs->state.index_buffer = op->buffer;
1187 cs->state.index_format = op->format_id;
1188 cs->state.index_offset = op->offset;
1190 if (op->buffer)
1191 InterlockedIncrement(&op->buffer->resource.bind_count);
1192 if (prev)
1193 InterlockedDecrement(&prev->resource.bind_count);
1195 device_invalidate_state(cs->device, STATE_INDEXBUFFER);
1198 void wined3d_cs_emit_set_index_buffer(struct wined3d_cs *cs, struct wined3d_buffer *buffer,
1199 enum wined3d_format_id format_id, unsigned int offset)
1201 struct wined3d_cs_set_index_buffer *op;
1203 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1204 op->opcode = WINED3D_CS_OP_SET_INDEX_BUFFER;
1205 op->buffer = buffer;
1206 op->format_id = format_id;
1207 op->offset = offset;
1209 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1212 static void wined3d_cs_exec_set_constant_buffer(struct wined3d_cs *cs, const void *data)
1214 const struct wined3d_cs_set_constant_buffer *op = data;
1215 struct wined3d_buffer *prev;
1217 prev = cs->state.cb[op->type][op->cb_idx];
1218 cs->state.cb[op->type][op->cb_idx] = op->buffer;
1220 if (op->buffer)
1221 InterlockedIncrement(&op->buffer->resource.bind_count);
1222 if (prev)
1223 InterlockedDecrement(&prev->resource.bind_count);
1225 device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(op->type));
1228 void wined3d_cs_emit_set_constant_buffer(struct wined3d_cs *cs, enum wined3d_shader_type type,
1229 UINT cb_idx, struct wined3d_buffer *buffer)
1231 struct wined3d_cs_set_constant_buffer *op;
1233 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1234 op->opcode = WINED3D_CS_OP_SET_CONSTANT_BUFFER;
1235 op->type = type;
1236 op->cb_idx = cb_idx;
1237 op->buffer = buffer;
1239 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1242 static void wined3d_cs_exec_set_texture(struct wined3d_cs *cs, const void *data)
1244 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
1245 const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
1246 const struct wined3d_cs_set_texture *op = data;
1247 struct wined3d_texture *prev;
1248 BOOL old_use_color_key = FALSE, new_use_color_key = FALSE;
1250 prev = cs->state.textures[op->stage];
1251 cs->state.textures[op->stage] = op->texture;
1253 if (op->texture)
1255 const struct wined3d_format *new_format = op->texture->resource.format;
1256 const struct wined3d_format *old_format = prev ? prev->resource.format : NULL;
1257 unsigned int old_fmt_flags = prev ? prev->resource.format_flags : 0;
1258 unsigned int new_fmt_flags = op->texture->resource.format_flags;
1260 if (InterlockedIncrement(&op->texture->resource.bind_count) == 1)
1261 op->texture->sampler = op->stage;
1263 if (!prev || op->texture->target != prev->target
1264 || (!is_same_fixup(new_format->color_fixup, old_format->color_fixup)
1265 && !(can_use_texture_swizzle(gl_info, new_format) && can_use_texture_swizzle(gl_info, old_format)))
1266 || (new_fmt_flags & WINED3DFMT_FLAG_SHADOW) != (old_fmt_flags & WINED3DFMT_FLAG_SHADOW))
1267 device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1269 if (!prev && op->stage < d3d_info->limits.ffp_blend_stages)
1271 /* The source arguments for color and alpha ops have different
1272 * meanings when a NULL texture is bound, so the COLOR_OP and
1273 * ALPHA_OP have to be dirtified. */
1274 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1275 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1278 if (!op->stage && op->texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1279 new_use_color_key = TRUE;
1282 if (prev)
1284 if (InterlockedDecrement(&prev->resource.bind_count) && prev->sampler == op->stage)
1286 unsigned int i;
1288 /* Search for other stages the texture is bound to. Shouldn't
1289 * happen if applications bind textures to a single stage only. */
1290 TRACE("Searching for other stages the texture is bound to.\n");
1291 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
1293 if (cs->state.textures[i] == prev)
1295 TRACE("Texture is also bound to stage %u.\n", i);
1296 prev->sampler = i;
1297 break;
1302 if (!op->texture && op->stage < d3d_info->limits.ffp_blend_stages)
1304 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1305 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1308 if (!op->stage && prev->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1309 old_use_color_key = TRUE;
1312 device_invalidate_state(cs->device, STATE_SAMPLER(op->stage));
1314 if (new_use_color_key != old_use_color_key)
1315 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1317 if (new_use_color_key)
1318 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1321 void wined3d_cs_emit_set_texture(struct wined3d_cs *cs, UINT stage, struct wined3d_texture *texture)
1323 struct wined3d_cs_set_texture *op;
1325 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1326 op->opcode = WINED3D_CS_OP_SET_TEXTURE;
1327 op->stage = stage;
1328 op->texture = texture;
1330 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1333 static void wined3d_cs_exec_set_shader_resource_view(struct wined3d_cs *cs, const void *data)
1335 const struct wined3d_cs_set_shader_resource_view *op = data;
1336 struct wined3d_shader_resource_view *prev;
1338 prev = cs->state.shader_resource_view[op->type][op->view_idx];
1339 cs->state.shader_resource_view[op->type][op->view_idx] = op->view;
1341 if (op->view)
1342 InterlockedIncrement(&op->view->resource->bind_count);
1343 if (prev)
1344 InterlockedDecrement(&prev->resource->bind_count);
1346 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1347 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1348 else
1349 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1352 void wined3d_cs_emit_set_shader_resource_view(struct wined3d_cs *cs, enum wined3d_shader_type type,
1353 UINT view_idx, struct wined3d_shader_resource_view *view)
1355 struct wined3d_cs_set_shader_resource_view *op;
1357 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1358 op->opcode = WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW;
1359 op->type = type;
1360 op->view_idx = view_idx;
1361 op->view = view;
1363 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1366 static void wined3d_cs_exec_set_unordered_access_view(struct wined3d_cs *cs, const void *data)
1368 const struct wined3d_cs_set_unordered_access_view *op = data;
1369 struct wined3d_unordered_access_view *prev;
1371 prev = cs->state.unordered_access_view[op->pipeline][op->view_idx];
1372 cs->state.unordered_access_view[op->pipeline][op->view_idx] = op->view;
1374 if (op->view)
1375 InterlockedIncrement(&op->view->resource->bind_count);
1376 if (prev)
1377 InterlockedDecrement(&prev->resource->bind_count);
1379 if (op->view && op->initial_count != ~0u)
1380 wined3d_unordered_access_view_set_counter(op->view, op->initial_count);
1382 device_invalidate_state(cs->device, STATE_UNORDERED_ACCESS_VIEW_BINDING(op->pipeline));
1385 void wined3d_cs_emit_set_unordered_access_view(struct wined3d_cs *cs, enum wined3d_pipeline pipeline,
1386 unsigned int view_idx, struct wined3d_unordered_access_view *view, unsigned int initial_count)
1388 struct wined3d_cs_set_unordered_access_view *op;
1390 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1391 op->opcode = WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW;
1392 op->pipeline = pipeline;
1393 op->view_idx = view_idx;
1394 op->view = view;
1395 op->initial_count = initial_count;
1397 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1400 static void wined3d_cs_exec_set_sampler(struct wined3d_cs *cs, const void *data)
1402 const struct wined3d_cs_set_sampler *op = data;
1404 cs->state.sampler[op->type][op->sampler_idx] = op->sampler;
1405 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1406 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1407 else
1408 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1411 void wined3d_cs_emit_set_sampler(struct wined3d_cs *cs, enum wined3d_shader_type type,
1412 UINT sampler_idx, struct wined3d_sampler *sampler)
1414 struct wined3d_cs_set_sampler *op;
1416 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1417 op->opcode = WINED3D_CS_OP_SET_SAMPLER;
1418 op->type = type;
1419 op->sampler_idx = sampler_idx;
1420 op->sampler = sampler;
1422 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1425 static void wined3d_cs_exec_set_shader(struct wined3d_cs *cs, const void *data)
1427 const struct wined3d_cs_set_shader *op = data;
1429 cs->state.shader[op->type] = op->shader;
1430 device_invalidate_state(cs->device, STATE_SHADER(op->type));
1431 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1432 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1433 else
1434 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1437 void wined3d_cs_emit_set_shader(struct wined3d_cs *cs, enum wined3d_shader_type type, struct wined3d_shader *shader)
1439 struct wined3d_cs_set_shader *op;
1441 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1442 op->opcode = WINED3D_CS_OP_SET_SHADER;
1443 op->type = type;
1444 op->shader = shader;
1446 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1449 static void wined3d_cs_exec_set_blend_state(struct wined3d_cs *cs, const void *data)
1451 const struct wined3d_cs_set_blend_state *op = data;
1453 cs->state.blend_state = op->state;
1454 device_invalidate_state(cs->device, STATE_BLEND);
1457 void wined3d_cs_emit_set_blend_state(struct wined3d_cs *cs, struct wined3d_blend_state *state)
1459 struct wined3d_cs_set_blend_state *op;
1461 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1462 op->opcode = WINED3D_CS_OP_SET_BLEND_STATE;
1463 op->state = state;
1465 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1468 static void wined3d_cs_exec_set_rasterizer_state(struct wined3d_cs *cs, const void *data)
1470 const struct wined3d_cs_set_rasterizer_state *op = data;
1472 cs->state.rasterizer_state = op->state;
1473 device_invalidate_state(cs->device, STATE_FRONTFACE);
1476 void wined3d_cs_emit_set_rasterizer_state(struct wined3d_cs *cs,
1477 struct wined3d_rasterizer_state *rasterizer_state)
1479 struct wined3d_cs_set_rasterizer_state *op;
1481 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1482 op->opcode = WINED3D_CS_OP_SET_RASTERIZER_STATE;
1483 op->state = rasterizer_state;
1485 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1488 static void wined3d_cs_exec_set_render_state(struct wined3d_cs *cs, const void *data)
1490 const struct wined3d_cs_set_render_state *op = data;
1492 cs->state.render_states[op->state] = op->value;
1493 device_invalidate_state(cs->device, STATE_RENDER(op->state));
1496 void wined3d_cs_emit_set_render_state(struct wined3d_cs *cs, enum wined3d_render_state state, DWORD value)
1498 struct wined3d_cs_set_render_state *op;
1500 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1501 op->opcode = WINED3D_CS_OP_SET_RENDER_STATE;
1502 op->state = state;
1503 op->value = value;
1505 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1508 static void wined3d_cs_exec_set_texture_state(struct wined3d_cs *cs, const void *data)
1510 const struct wined3d_cs_set_texture_state *op = data;
1512 cs->state.texture_states[op->stage][op->state] = op->value;
1513 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, op->state));
1516 void wined3d_cs_emit_set_texture_state(struct wined3d_cs *cs, UINT stage,
1517 enum wined3d_texture_stage_state state, DWORD value)
1519 struct wined3d_cs_set_texture_state *op;
1521 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1522 op->opcode = WINED3D_CS_OP_SET_TEXTURE_STATE;
1523 op->stage = stage;
1524 op->state = state;
1525 op->value = value;
1527 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1530 static void wined3d_cs_exec_set_sampler_state(struct wined3d_cs *cs, const void *data)
1532 const struct wined3d_cs_set_sampler_state *op = data;
1534 cs->state.sampler_states[op->sampler_idx][op->state] = op->value;
1535 device_invalidate_state(cs->device, STATE_SAMPLER(op->sampler_idx));
1538 void wined3d_cs_emit_set_sampler_state(struct wined3d_cs *cs, UINT sampler_idx,
1539 enum wined3d_sampler_state state, DWORD value)
1541 struct wined3d_cs_set_sampler_state *op;
1543 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1544 op->opcode = WINED3D_CS_OP_SET_SAMPLER_STATE;
1545 op->sampler_idx = sampler_idx;
1546 op->state = state;
1547 op->value = value;
1549 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1552 static void wined3d_cs_exec_set_transform(struct wined3d_cs *cs, const void *data)
1554 const struct wined3d_cs_set_transform *op = data;
1556 cs->state.transforms[op->state] = op->matrix;
1557 if (op->state < WINED3D_TS_WORLD_MATRIX(cs->device->adapter->d3d_info.limits.ffp_vertex_blend_matrices))
1558 device_invalidate_state(cs->device, STATE_TRANSFORM(op->state));
1561 void wined3d_cs_emit_set_transform(struct wined3d_cs *cs, enum wined3d_transform_state state,
1562 const struct wined3d_matrix *matrix)
1564 struct wined3d_cs_set_transform *op;
1566 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1567 op->opcode = WINED3D_CS_OP_SET_TRANSFORM;
1568 op->state = state;
1569 op->matrix = *matrix;
1571 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1574 static void wined3d_cs_exec_set_clip_plane(struct wined3d_cs *cs, const void *data)
1576 const struct wined3d_cs_set_clip_plane *op = data;
1578 cs->state.clip_planes[op->plane_idx] = op->plane;
1579 device_invalidate_state(cs->device, STATE_CLIPPLANE(op->plane_idx));
1582 void wined3d_cs_emit_set_clip_plane(struct wined3d_cs *cs, UINT plane_idx, const struct wined3d_vec4 *plane)
1584 struct wined3d_cs_set_clip_plane *op;
1586 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1587 op->opcode = WINED3D_CS_OP_SET_CLIP_PLANE;
1588 op->plane_idx = plane_idx;
1589 op->plane = *plane;
1591 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1594 static void wined3d_cs_exec_set_color_key(struct wined3d_cs *cs, const void *data)
1596 const struct wined3d_cs_set_color_key *op = data;
1597 struct wined3d_texture *texture = op->texture;
1599 if (op->set)
1601 switch (op->flags)
1603 case WINED3D_CKEY_DST_BLT:
1604 texture->async.dst_blt_color_key = op->color_key;
1605 texture->async.color_key_flags |= WINED3D_CKEY_DST_BLT;
1606 break;
1608 case WINED3D_CKEY_DST_OVERLAY:
1609 texture->async.dst_overlay_color_key = op->color_key;
1610 texture->async.color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
1611 break;
1613 case WINED3D_CKEY_SRC_BLT:
1614 if (texture == cs->state.textures[0])
1616 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1617 if (!(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1618 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1621 texture->async.src_blt_color_key = op->color_key;
1622 texture->async.color_key_flags |= WINED3D_CKEY_SRC_BLT;
1623 break;
1625 case WINED3D_CKEY_SRC_OVERLAY:
1626 texture->async.src_overlay_color_key = op->color_key;
1627 texture->async.color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
1628 break;
1631 else
1633 switch (op->flags)
1635 case WINED3D_CKEY_DST_BLT:
1636 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_BLT;
1637 break;
1639 case WINED3D_CKEY_DST_OVERLAY:
1640 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
1641 break;
1643 case WINED3D_CKEY_SRC_BLT:
1644 if (texture == cs->state.textures[0] && texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1645 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1647 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
1648 break;
1650 case WINED3D_CKEY_SRC_OVERLAY:
1651 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
1652 break;
1657 void wined3d_cs_emit_set_color_key(struct wined3d_cs *cs, struct wined3d_texture *texture,
1658 WORD flags, const struct wined3d_color_key *color_key)
1660 struct wined3d_cs_set_color_key *op;
1662 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1663 op->opcode = WINED3D_CS_OP_SET_COLOR_KEY;
1664 op->texture = texture;
1665 op->flags = flags;
1666 if (color_key)
1668 op->color_key = *color_key;
1669 op->set = 1;
1671 else
1672 op->set = 0;
1674 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1677 static void wined3d_cs_exec_set_material(struct wined3d_cs *cs, const void *data)
1679 const struct wined3d_cs_set_material *op = data;
1681 cs->state.material = op->material;
1682 device_invalidate_state(cs->device, STATE_MATERIAL);
1685 void wined3d_cs_emit_set_material(struct wined3d_cs *cs, const struct wined3d_material *material)
1687 struct wined3d_cs_set_material *op;
1689 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1690 op->opcode = WINED3D_CS_OP_SET_MATERIAL;
1691 op->material = *material;
1693 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1696 static void wined3d_cs_exec_set_light(struct wined3d_cs *cs, const void *data)
1698 const struct wined3d_cs_set_light *op = data;
1699 struct wined3d_light_info *light_info;
1700 unsigned int light_idx, hash_idx;
1702 light_idx = op->light.OriginalIndex;
1704 if (!(light_info = wined3d_state_get_light(&cs->state, light_idx)))
1706 TRACE("Adding new light.\n");
1707 if (!(light_info = heap_alloc_zero(sizeof(*light_info))))
1709 ERR("Failed to allocate light info.\n");
1710 return;
1713 hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1714 list_add_head(&cs->state.light_map[hash_idx], &light_info->entry);
1715 light_info->glIndex = -1;
1716 light_info->OriginalIndex = light_idx;
1719 if (light_info->glIndex != -1)
1721 if (light_info->OriginalParms.type != op->light.OriginalParms.type)
1722 device_invalidate_state(cs->device, STATE_LIGHT_TYPE);
1723 device_invalidate_state(cs->device, STATE_ACTIVELIGHT(light_info->glIndex));
1726 light_info->OriginalParms = op->light.OriginalParms;
1727 light_info->position = op->light.position;
1728 light_info->direction = op->light.direction;
1729 light_info->exponent = op->light.exponent;
1730 light_info->cutoff = op->light.cutoff;
1733 void wined3d_cs_emit_set_light(struct wined3d_cs *cs, const struct wined3d_light_info *light)
1735 struct wined3d_cs_set_light *op;
1737 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1738 op->opcode = WINED3D_CS_OP_SET_LIGHT;
1739 op->light = *light;
1741 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1744 static void wined3d_cs_exec_set_light_enable(struct wined3d_cs *cs, const void *data)
1746 const struct wined3d_cs_set_light_enable *op = data;
1747 struct wined3d_device *device = cs->device;
1748 struct wined3d_light_info *light_info;
1749 int prev_idx;
1751 if (!(light_info = wined3d_state_get_light(&cs->state, op->idx)))
1753 ERR("Light doesn't exist.\n");
1754 return;
1757 prev_idx = light_info->glIndex;
1758 wined3d_state_enable_light(&cs->state, &device->adapter->d3d_info, light_info, op->enable);
1759 if (light_info->glIndex != prev_idx)
1761 device_invalidate_state(device, STATE_LIGHT_TYPE);
1762 device_invalidate_state(device, STATE_ACTIVELIGHT(op->enable ? light_info->glIndex : prev_idx));
1766 void wined3d_cs_emit_set_light_enable(struct wined3d_cs *cs, unsigned int idx, BOOL enable)
1768 struct wined3d_cs_set_light_enable *op;
1770 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1771 op->opcode = WINED3D_CS_OP_SET_LIGHT_ENABLE;
1772 op->idx = idx;
1773 op->enable = enable;
1775 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1778 static const struct
1780 size_t offset;
1781 size_t size;
1782 DWORD mask;
1784 wined3d_cs_push_constant_info[] =
1786 /* WINED3D_PUSH_CONSTANTS_VS_F */
1787 {FIELD_OFFSET(struct wined3d_state, vs_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_VS_F},
1788 /* WINED3D_PUSH_CONSTANTS_PS_F */
1789 {FIELD_OFFSET(struct wined3d_state, ps_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_PS_F},
1790 /* WINED3D_PUSH_CONSTANTS_VS_I */
1791 {FIELD_OFFSET(struct wined3d_state, vs_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_VS_I},
1792 /* WINED3D_PUSH_CONSTANTS_PS_I */
1793 {FIELD_OFFSET(struct wined3d_state, ps_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_PS_I},
1794 /* WINED3D_PUSH_CONSTANTS_VS_B */
1795 {FIELD_OFFSET(struct wined3d_state, vs_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_VS_B},
1796 /* WINED3D_PUSH_CONSTANTS_PS_B */
1797 {FIELD_OFFSET(struct wined3d_state, ps_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_PS_B},
1800 static void wined3d_cs_st_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1801 unsigned int start_idx, unsigned int count, const void *constants)
1803 struct wined3d_device *device = cs->device;
1804 unsigned int context_count;
1805 unsigned int i;
1806 size_t offset;
1808 if (p == WINED3D_PUSH_CONSTANTS_VS_F)
1809 device->shader_backend->shader_update_float_vertex_constants(device, start_idx, count);
1810 else if (p == WINED3D_PUSH_CONSTANTS_PS_F)
1811 device->shader_backend->shader_update_float_pixel_constants(device, start_idx, count);
1813 offset = wined3d_cs_push_constant_info[p].offset + start_idx * wined3d_cs_push_constant_info[p].size;
1814 memcpy((BYTE *)&cs->state + offset, constants, count * wined3d_cs_push_constant_info[p].size);
1815 for (i = 0, context_count = device->context_count; i < context_count; ++i)
1817 device->contexts[i]->constant_update_mask |= wined3d_cs_push_constant_info[p].mask;
1821 static void wined3d_cs_exec_push_constants(struct wined3d_cs *cs, const void *data)
1823 const struct wined3d_cs_push_constants *op = data;
1825 wined3d_cs_st_push_constants(cs, op->type, op->start_idx, op->count, op->constants);
1828 static void wined3d_cs_mt_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1829 unsigned int start_idx, unsigned int count, const void *constants)
1831 struct wined3d_cs_push_constants *op;
1832 size_t size;
1834 size = count * wined3d_cs_push_constant_info[p].size;
1835 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_push_constants, constants[size]),
1836 WINED3D_CS_QUEUE_DEFAULT);
1837 op->opcode = WINED3D_CS_OP_PUSH_CONSTANTS;
1838 op->type = p;
1839 op->start_idx = start_idx;
1840 op->count = count;
1841 memcpy(op->constants, constants, size);
1843 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1846 static void wined3d_cs_exec_reset_state(struct wined3d_cs *cs, const void *data)
1848 struct wined3d_adapter *adapter = cs->device->adapter;
1850 state_cleanup(&cs->state);
1851 memset(&cs->state, 0, sizeof(cs->state));
1852 state_init(&cs->state, &cs->fb, &adapter->gl_info, &adapter->d3d_info,
1853 WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
1856 void wined3d_cs_emit_reset_state(struct wined3d_cs *cs)
1858 struct wined3d_cs_reset_state *op;
1860 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1861 op->opcode = WINED3D_CS_OP_RESET_STATE;
1863 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1866 static void wined3d_cs_exec_callback(struct wined3d_cs *cs, const void *data)
1868 const struct wined3d_cs_callback *op = data;
1870 op->callback(op->object);
1873 static void wined3d_cs_emit_callback(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1875 struct wined3d_cs_callback *op;
1877 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1878 op->opcode = WINED3D_CS_OP_CALLBACK;
1879 op->callback = callback;
1880 op->object = object;
1882 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1885 void wined3d_cs_destroy_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1887 wined3d_cs_emit_callback(cs, callback, object);
1890 void wined3d_cs_init_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1892 wined3d_cs_emit_callback(cs, callback, object);
1895 static void wined3d_cs_exec_query_issue(struct wined3d_cs *cs, const void *data)
1897 const struct wined3d_cs_query_issue *op = data;
1898 struct wined3d_query *query = op->query;
1899 BOOL poll;
1901 poll = query->query_ops->query_issue(query, op->flags);
1903 if (!cs->thread)
1904 return;
1906 if (poll && list_empty(&query->poll_list_entry))
1908 list_add_tail(&cs->query_poll_list, &query->poll_list_entry);
1909 return;
1912 /* This can happen if occlusion queries are restarted. This discards the
1913 * old result, since polling it could result in a GL error. */
1914 if ((op->flags & WINED3DISSUE_BEGIN) && !poll && !list_empty(&query->poll_list_entry))
1916 list_remove(&query->poll_list_entry);
1917 list_init(&query->poll_list_entry);
1918 InterlockedIncrement(&query->counter_retrieved);
1919 return;
1922 /* This can happen when an occlusion query is ended without being started,
1923 * in which case we don't want to poll, but still have to counter-balance
1924 * the increment of the main counter.
1926 * This can also happen if an event query is re-issued before the first
1927 * fence was reached. In this case the query is already in the list and
1928 * the poll function will check the new fence. We have to counter-balance
1929 * the discarded increment. */
1930 if (op->flags & WINED3DISSUE_END)
1931 InterlockedIncrement(&query->counter_retrieved);
1934 void wined3d_cs_emit_query_issue(struct wined3d_cs *cs, struct wined3d_query *query, DWORD flags)
1936 struct wined3d_cs_query_issue *op;
1938 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1939 op->opcode = WINED3D_CS_OP_QUERY_ISSUE;
1940 op->query = query;
1941 op->flags = flags;
1943 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1944 cs->queries_flushed = FALSE;
1947 static void wined3d_cs_exec_preload_resource(struct wined3d_cs *cs, const void *data)
1949 const struct wined3d_cs_preload_resource *op = data;
1950 struct wined3d_resource *resource = op->resource;
1952 resource->resource_ops->resource_preload(resource);
1953 wined3d_resource_release(resource);
1956 void wined3d_cs_emit_preload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
1958 struct wined3d_cs_preload_resource *op;
1960 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1961 op->opcode = WINED3D_CS_OP_PRELOAD_RESOURCE;
1962 op->resource = resource;
1964 wined3d_resource_acquire(resource);
1966 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1969 static void wined3d_cs_exec_unload_resource(struct wined3d_cs *cs, const void *data)
1971 const struct wined3d_cs_unload_resource *op = data;
1972 struct wined3d_resource *resource = op->resource;
1974 resource->resource_ops->resource_unload(resource);
1975 wined3d_resource_release(resource);
1978 void wined3d_cs_emit_unload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
1980 struct wined3d_cs_unload_resource *op;
1982 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1983 op->opcode = WINED3D_CS_OP_UNLOAD_RESOURCE;
1984 op->resource = resource;
1986 wined3d_resource_acquire(resource);
1988 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1991 static void wined3d_cs_exec_map(struct wined3d_cs *cs, const void *data)
1993 const struct wined3d_cs_map *op = data;
1994 struct wined3d_resource *resource = op->resource;
1996 *op->hr = resource->resource_ops->resource_sub_resource_map(resource,
1997 op->sub_resource_idx, op->map_desc, op->box, op->flags);
2000 HRESULT wined3d_cs_map(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx,
2001 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
2003 struct wined3d_cs_map *op;
2004 HRESULT hr;
2006 /* Mapping resources from the worker thread isn't an issue by itself, but
2007 * increasing the map count would be visible to applications. */
2008 wined3d_not_from_cs(cs);
2010 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2011 op->opcode = WINED3D_CS_OP_MAP;
2012 op->resource = resource;
2013 op->sub_resource_idx = sub_resource_idx;
2014 op->map_desc = map_desc;
2015 op->box = box;
2016 op->flags = flags;
2017 op->hr = &hr;
2019 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2020 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2022 return hr;
2025 static void wined3d_cs_exec_unmap(struct wined3d_cs *cs, const void *data)
2027 const struct wined3d_cs_unmap *op = data;
2028 struct wined3d_resource *resource = op->resource;
2030 *op->hr = resource->resource_ops->resource_sub_resource_unmap(resource, op->sub_resource_idx);
2033 HRESULT wined3d_cs_unmap(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx)
2035 struct wined3d_cs_unmap *op;
2036 HRESULT hr;
2038 wined3d_not_from_cs(cs);
2040 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2041 op->opcode = WINED3D_CS_OP_UNMAP;
2042 op->resource = resource;
2043 op->sub_resource_idx = sub_resource_idx;
2044 op->hr = &hr;
2046 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2047 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2049 return hr;
2052 static void wined3d_cs_exec_blt_sub_resource(struct wined3d_cs *cs, const void *data)
2054 const struct wined3d_cs_blt_sub_resource *op = data;
2056 if (op->dst_resource->type == WINED3D_RTYPE_BUFFER)
2058 wined3d_buffer_copy(buffer_from_resource(op->dst_resource), op->dst_box.left,
2059 buffer_from_resource(op->src_resource), op->src_box.left,
2060 op->src_box.right - op->src_box.left);
2062 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_2D)
2064 if (FAILED(texture2d_blt(texture_from_resource(op->dst_resource), op->dst_sub_resource_idx,
2065 &op->dst_box, texture_from_resource(op->src_resource), op->src_sub_resource_idx,
2066 &op->src_box, op->flags, &op->fx, op->filter)))
2067 FIXME("Blit failed.\n");
2069 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_3D)
2071 struct wined3d_texture *src_texture, *dst_texture;
2072 unsigned int level, update_w, update_h, update_d;
2073 unsigned int row_pitch, slice_pitch;
2074 struct wined3d_context *context;
2075 struct wined3d_bo_address addr;
2077 if (op->flags & ~WINED3D_BLT_RAW)
2079 FIXME("Flags %#x not implemented for %s resources.\n",
2080 op->flags, debug_d3dresourcetype(op->dst_resource->type));
2081 goto error;
2084 if (!(op->flags & WINED3D_BLT_RAW) && op->src_resource->format != op->dst_resource->format)
2086 FIXME("Format conversion not implemented for %s resources.\n",
2087 debug_d3dresourcetype(op->dst_resource->type));
2088 goto error;
2091 update_w = op->dst_box.right - op->dst_box.left;
2092 update_h = op->dst_box.bottom - op->dst_box.top;
2093 update_d = op->dst_box.back - op->dst_box.front;
2094 if (op->src_box.right - op->src_box.left != update_w
2095 || op->src_box.bottom - op->src_box.top != update_h
2096 || op->src_box.back - op->src_box.front != update_d)
2098 FIXME("Stretching not implemented for %s resources.\n",
2099 debug_d3dresourcetype(op->dst_resource->type));
2100 goto error;
2103 dst_texture = texture_from_resource(op->dst_resource);
2104 src_texture = texture_from_resource(op->src_resource);
2106 context = context_acquire(cs->device, NULL, 0);
2108 if (!wined3d_texture_load_location(src_texture, op->src_sub_resource_idx,
2109 context, src_texture->resource.map_binding))
2111 ERR("Failed to load source sub-resource into %s.\n",
2112 wined3d_debug_location(src_texture->resource.map_binding));
2113 context_release(context);
2114 goto error;
2117 level = op->dst_sub_resource_idx % dst_texture->level_count;
2118 if (update_w == wined3d_texture_get_level_width(dst_texture, level)
2119 && update_h == wined3d_texture_get_level_height(dst_texture, level)
2120 && update_d == wined3d_texture_get_level_depth(dst_texture, level))
2122 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
2124 else if (!wined3d_texture_load_location(dst_texture, op->dst_sub_resource_idx,
2125 context, WINED3D_LOCATION_TEXTURE_RGB))
2127 ERR("Failed to load destination sub-resource.\n");
2128 context_release(context);
2129 goto error;
2132 wined3d_texture_get_memory(src_texture, op->src_sub_resource_idx, &addr, src_texture->resource.map_binding);
2133 wined3d_texture_get_pitch(src_texture, op->src_sub_resource_idx % src_texture->level_count,
2134 &row_pitch, &slice_pitch);
2136 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
2137 wined3d_texture_upload_data(dst_texture, op->dst_sub_resource_idx, context,
2138 dst_texture->resource.format, &op->src_box, wined3d_const_bo_address(&addr),
2139 row_pitch, slice_pitch, op->dst_box.left, op->dst_box.top, op->dst_box.front, FALSE);
2140 wined3d_texture_validate_location(dst_texture, op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2141 wined3d_texture_invalidate_location(dst_texture, op->dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2143 context_release(context);
2145 else
2147 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(op->dst_resource->type));
2150 error:
2151 if (op->src_resource)
2152 wined3d_resource_release(op->src_resource);
2153 wined3d_resource_release(op->dst_resource);
2156 void wined3d_cs_emit_blt_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *dst_resource,
2157 unsigned int dst_sub_resource_idx, const struct wined3d_box *dst_box, struct wined3d_resource *src_resource,
2158 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, DWORD flags,
2159 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2161 struct wined3d_cs_blt_sub_resource *op;
2163 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2164 op->opcode = WINED3D_CS_OP_BLT_SUB_RESOURCE;
2165 op->dst_resource = dst_resource;
2166 op->dst_sub_resource_idx = dst_sub_resource_idx;
2167 op->dst_box = *dst_box;
2168 op->src_resource = src_resource;
2169 op->src_sub_resource_idx = src_sub_resource_idx;
2170 op->src_box = *src_box;
2171 op->flags = flags;
2172 if (fx)
2173 op->fx = *fx;
2174 else
2175 memset(&op->fx, 0, sizeof(op->fx));
2176 op->filter = filter;
2178 wined3d_resource_acquire(dst_resource);
2179 if (src_resource)
2180 wined3d_resource_acquire(src_resource);
2182 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2183 if (flags & WINED3D_BLT_SYNCHRONOUS)
2184 cs->ops->finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2187 static void wined3d_cs_exec_update_sub_resource(struct wined3d_cs *cs, const void *data)
2189 const struct wined3d_cs_update_sub_resource *op = data;
2190 struct wined3d_resource *resource = op->resource;
2191 const struct wined3d_box *box = &op->box;
2192 unsigned int width, height, depth, level;
2193 struct wined3d_const_bo_address addr;
2194 struct wined3d_context *context;
2195 struct wined3d_texture *texture;
2196 struct wined3d_box src_box;
2198 context = context_acquire(cs->device, NULL, 0);
2200 if (resource->type == WINED3D_RTYPE_BUFFER)
2202 struct wined3d_buffer *buffer = buffer_from_resource(resource);
2204 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
2206 ERR("Failed to load buffer location.\n");
2207 goto done;
2210 wined3d_buffer_upload_data(buffer, context, box, op->data.data);
2211 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
2212 goto done;
2215 texture = wined3d_texture_from_resource(resource);
2217 level = op->sub_resource_idx % texture->level_count;
2218 width = wined3d_texture_get_level_width(texture, level);
2219 height = wined3d_texture_get_level_height(texture, level);
2220 depth = wined3d_texture_get_level_depth(texture, level);
2222 addr.buffer_object = 0;
2223 addr.addr = op->data.data;
2225 /* Only load the sub-resource for partial updates. */
2226 if (!box->left && !box->top && !box->front
2227 && box->right == width && box->bottom == height && box->back == depth)
2228 wined3d_texture_prepare_texture(texture, context, FALSE);
2229 else
2230 wined3d_texture_load_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2231 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2233 wined3d_box_set(&src_box, 0, 0, box->right - box->left, box->bottom - box->top, 0, box->back - box->front);
2234 wined3d_texture_upload_data(texture, op->sub_resource_idx, context, texture->resource.format, &src_box,
2235 &addr, op->data.row_pitch, op->data.slice_pitch, box->left, box->top, box->front, FALSE);
2237 wined3d_texture_validate_location(texture, op->sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2238 wined3d_texture_invalidate_location(texture, op->sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2240 done:
2241 context_release(context);
2243 wined3d_resource_release(resource);
2246 void wined3d_cs_emit_update_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *resource,
2247 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
2248 unsigned int slice_pitch)
2250 struct wined3d_cs_update_sub_resource *op;
2252 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2253 op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2254 op->resource = resource;
2255 op->sub_resource_idx = sub_resource_idx;
2256 op->box = *box;
2257 op->data.row_pitch = row_pitch;
2258 op->data.slice_pitch = slice_pitch;
2259 op->data.data = data;
2261 wined3d_resource_acquire(resource);
2263 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2264 /* The data pointer may go away, so we need to wait until it is read.
2265 * Copying the data may be faster if it's small. */
2266 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2269 static void wined3d_cs_exec_add_dirty_texture_region(struct wined3d_cs *cs, const void *data)
2271 const struct wined3d_cs_add_dirty_texture_region *op = data;
2272 struct wined3d_texture *texture = op->texture;
2273 unsigned int sub_resource_idx, i;
2274 struct wined3d_context *context;
2276 context = context_acquire(cs->device, NULL, 0);
2277 sub_resource_idx = op->layer * texture->level_count;
2278 for (i = 0; i < texture->level_count; ++i, ++sub_resource_idx)
2280 if (wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
2281 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2282 else
2283 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
2285 context_release(context);
2287 wined3d_resource_release(&texture->resource);
2290 void wined3d_cs_emit_add_dirty_texture_region(struct wined3d_cs *cs,
2291 struct wined3d_texture *texture, unsigned int layer)
2293 struct wined3d_cs_add_dirty_texture_region *op;
2295 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2296 op->opcode = WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION;
2297 op->texture = texture;
2298 op->layer = layer;
2300 wined3d_resource_acquire(&texture->resource);
2302 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2305 static void wined3d_cs_exec_clear_unordered_access_view(struct wined3d_cs *cs, const void *data)
2307 const struct wined3d_cs_clear_unordered_access_view *op = data;
2308 struct wined3d_unordered_access_view *view = op->view;
2309 struct wined3d_context *context;
2311 context = context_acquire(cs->device, NULL, 0);
2312 wined3d_unordered_access_view_clear_uint(view, &op->clear_value, context);
2313 context_release(context);
2315 wined3d_resource_release(view->resource);
2318 void wined3d_cs_emit_clear_unordered_access_view_uint(struct wined3d_cs *cs,
2319 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
2321 struct wined3d_cs_clear_unordered_access_view *op;
2323 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2324 op->opcode = WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW;
2325 op->view = view;
2326 op->clear_value = *clear_value;
2328 wined3d_resource_acquire(view->resource);
2330 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2333 static void wined3d_cs_exec_copy_uav_counter(struct wined3d_cs *cs, const void *data)
2335 const struct wined3d_cs_copy_uav_counter *op = data;
2336 struct wined3d_unordered_access_view *view = op->view;
2337 struct wined3d_context *context;
2339 context = context_acquire(cs->device, NULL, 0);
2340 wined3d_unordered_access_view_copy_counter(view, op->buffer, op->offset, context);
2341 context_release(context);
2343 wined3d_resource_release(&op->buffer->resource);
2346 void wined3d_cs_emit_copy_uav_counter(struct wined3d_cs *cs, struct wined3d_buffer *dst_buffer,
2347 unsigned int offset, struct wined3d_unordered_access_view *uav)
2349 struct wined3d_cs_copy_uav_counter *op;
2351 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2352 op->opcode = WINED3D_CS_OP_COPY_UAV_COUNTER;
2353 op->buffer = dst_buffer;
2354 op->offset = offset;
2355 op->view = uav;
2357 wined3d_resource_acquire(&dst_buffer->resource);
2359 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2362 static void wined3d_cs_exec_generate_mipmaps(struct wined3d_cs *cs, const void *data)
2364 const struct wined3d_cs_generate_mipmaps *op = data;
2365 struct wined3d_shader_resource_view *view = op->view;
2367 shader_resource_view_generate_mipmaps(view);
2368 wined3d_resource_release(view->resource);
2371 void wined3d_cs_emit_generate_mipmaps(struct wined3d_cs *cs, struct wined3d_shader_resource_view *view)
2373 struct wined3d_cs_generate_mipmaps *op;
2375 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2376 op->opcode = WINED3D_CS_OP_GENERATE_MIPMAPS;
2377 op->view = view;
2379 wined3d_resource_acquire(view->resource);
2381 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2384 static void wined3d_cs_emit_stop(struct wined3d_cs *cs)
2386 struct wined3d_cs_stop *op;
2388 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2389 op->opcode = WINED3D_CS_OP_STOP;
2391 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2392 cs->ops->finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2395 static void (* const wined3d_cs_op_handlers[])(struct wined3d_cs *cs, const void *data) =
2397 /* WINED3D_CS_OP_NOP */ wined3d_cs_exec_nop,
2398 /* WINED3D_CS_OP_PRESENT */ wined3d_cs_exec_present,
2399 /* WINED3D_CS_OP_CLEAR */ wined3d_cs_exec_clear,
2400 /* WINED3D_CS_OP_DISPATCH */ wined3d_cs_exec_dispatch,
2401 /* WINED3D_CS_OP_DRAW */ wined3d_cs_exec_draw,
2402 /* WINED3D_CS_OP_FLUSH */ wined3d_cs_exec_flush,
2403 /* WINED3D_CS_OP_SET_PREDICATION */ wined3d_cs_exec_set_predication,
2404 /* WINED3D_CS_OP_SET_VIEWPORT */ wined3d_cs_exec_set_viewport,
2405 /* WINED3D_CS_OP_SET_SCISSOR_RECT */ wined3d_cs_exec_set_scissor_rect,
2406 /* WINED3D_CS_OP_SET_RENDERTARGET_VIEW */ wined3d_cs_exec_set_rendertarget_view,
2407 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW */ wined3d_cs_exec_set_depth_stencil_view,
2408 /* WINED3D_CS_OP_SET_VERTEX_DECLARATION */ wined3d_cs_exec_set_vertex_declaration,
2409 /* WINED3D_CS_OP_SET_STREAM_SOURCE */ wined3d_cs_exec_set_stream_source,
2410 /* WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ */ wined3d_cs_exec_set_stream_source_freq,
2411 /* WINED3D_CS_OP_SET_STREAM_OUTPUT */ wined3d_cs_exec_set_stream_output,
2412 /* WINED3D_CS_OP_SET_INDEX_BUFFER */ wined3d_cs_exec_set_index_buffer,
2413 /* WINED3D_CS_OP_SET_CONSTANT_BUFFER */ wined3d_cs_exec_set_constant_buffer,
2414 /* WINED3D_CS_OP_SET_TEXTURE */ wined3d_cs_exec_set_texture,
2415 /* WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW */ wined3d_cs_exec_set_shader_resource_view,
2416 /* WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_set_unordered_access_view,
2417 /* WINED3D_CS_OP_SET_SAMPLER */ wined3d_cs_exec_set_sampler,
2418 /* WINED3D_CS_OP_SET_SHADER */ wined3d_cs_exec_set_shader,
2419 /* WINED3D_CS_OP_SET_BLEND_STATE */ wined3d_cs_exec_set_blend_state,
2420 /* WINED3D_CS_OP_SET_RASTERIZER_STATE */ wined3d_cs_exec_set_rasterizer_state,
2421 /* WINED3D_CS_OP_SET_RENDER_STATE */ wined3d_cs_exec_set_render_state,
2422 /* WINED3D_CS_OP_SET_TEXTURE_STATE */ wined3d_cs_exec_set_texture_state,
2423 /* WINED3D_CS_OP_SET_SAMPLER_STATE */ wined3d_cs_exec_set_sampler_state,
2424 /* WINED3D_CS_OP_SET_TRANSFORM */ wined3d_cs_exec_set_transform,
2425 /* WINED3D_CS_OP_SET_CLIP_PLANE */ wined3d_cs_exec_set_clip_plane,
2426 /* WINED3D_CS_OP_SET_COLOR_KEY */ wined3d_cs_exec_set_color_key,
2427 /* WINED3D_CS_OP_SET_MATERIAL */ wined3d_cs_exec_set_material,
2428 /* WINED3D_CS_OP_SET_LIGHT */ wined3d_cs_exec_set_light,
2429 /* WINED3D_CS_OP_SET_LIGHT_ENABLE */ wined3d_cs_exec_set_light_enable,
2430 /* WINED3D_CS_OP_PUSH_CONSTANTS */ wined3d_cs_exec_push_constants,
2431 /* WINED3D_CS_OP_RESET_STATE */ wined3d_cs_exec_reset_state,
2432 /* WINED3D_CS_OP_CALLBACK */ wined3d_cs_exec_callback,
2433 /* WINED3D_CS_OP_QUERY_ISSUE */ wined3d_cs_exec_query_issue,
2434 /* WINED3D_CS_OP_PRELOAD_RESOURCE */ wined3d_cs_exec_preload_resource,
2435 /* WINED3D_CS_OP_UNLOAD_RESOURCE */ wined3d_cs_exec_unload_resource,
2436 /* WINED3D_CS_OP_MAP */ wined3d_cs_exec_map,
2437 /* WINED3D_CS_OP_UNMAP */ wined3d_cs_exec_unmap,
2438 /* WINED3D_CS_OP_BLT_SUB_RESOURCE */ wined3d_cs_exec_blt_sub_resource,
2439 /* WINED3D_CS_OP_UPDATE_SUB_RESOURCE */ wined3d_cs_exec_update_sub_resource,
2440 /* WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION */ wined3d_cs_exec_add_dirty_texture_region,
2441 /* WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_clear_unordered_access_view,
2442 /* WINED3D_CS_OP_COPY_UAV_COUNTER */ wined3d_cs_exec_copy_uav_counter,
2443 /* WINED3D_CS_OP_GENERATE_MIPMAPS */ wined3d_cs_exec_generate_mipmaps,
2446 static void *wined3d_cs_st_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2448 if (size > (cs->data_size - cs->end))
2450 size_t new_size;
2451 void *new_data;
2453 new_size = max(size, cs->data_size * 2);
2454 if (!cs->end)
2455 new_data = heap_realloc(cs->data, new_size);
2456 else
2457 new_data = heap_alloc(new_size);
2458 if (!new_data)
2459 return NULL;
2461 cs->data_size = new_size;
2462 cs->start = cs->end = 0;
2463 cs->data = new_data;
2466 cs->end += size;
2468 return (BYTE *)cs->data + cs->start;
2471 static void wined3d_cs_st_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2473 enum wined3d_cs_op opcode;
2474 size_t start;
2475 BYTE *data;
2477 data = cs->data;
2478 start = cs->start;
2479 cs->start = cs->end;
2481 opcode = *(const enum wined3d_cs_op *)&data[start];
2482 if (opcode >= WINED3D_CS_OP_STOP)
2483 ERR("Invalid opcode %#x.\n", opcode);
2484 else
2485 wined3d_cs_op_handlers[opcode](cs, &data[start]);
2487 if (cs->data == data)
2488 cs->start = cs->end = start;
2489 else if (!start)
2490 heap_free(data);
2493 static void wined3d_cs_st_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2497 static const struct wined3d_cs_ops wined3d_cs_st_ops =
2499 wined3d_cs_st_require_space,
2500 wined3d_cs_st_submit,
2501 wined3d_cs_st_finish,
2502 wined3d_cs_st_push_constants,
2505 static BOOL wined3d_cs_queue_is_empty(const struct wined3d_cs *cs, const struct wined3d_cs_queue *queue)
2507 wined3d_from_cs(cs);
2508 return *(volatile LONG *)&queue->head == queue->tail;
2511 static void wined3d_cs_queue_submit(struct wined3d_cs_queue *queue, struct wined3d_cs *cs)
2513 struct wined3d_cs_packet *packet;
2514 size_t packet_size;
2516 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2517 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2518 InterlockedExchange(&queue->head, (queue->head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1));
2520 if (InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2521 SetEvent(cs->event);
2524 static void wined3d_cs_mt_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2526 if (cs->thread_id == GetCurrentThreadId())
2527 return wined3d_cs_st_submit(cs, queue_id);
2529 wined3d_cs_queue_submit(&cs->queue[queue_id], cs);
2532 static void *wined3d_cs_queue_require_space(struct wined3d_cs_queue *queue, size_t size, struct wined3d_cs *cs)
2534 size_t queue_size = ARRAY_SIZE(queue->data);
2535 size_t header_size, packet_size, remaining;
2536 struct wined3d_cs_packet *packet;
2538 header_size = FIELD_OFFSET(struct wined3d_cs_packet, data[0]);
2539 size = (size + header_size - 1) & ~(header_size - 1);
2540 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[size]);
2541 if (packet_size >= WINED3D_CS_QUEUE_SIZE)
2543 ERR("Packet size %lu >= queue size %u.\n",
2544 (unsigned long)packet_size, WINED3D_CS_QUEUE_SIZE);
2545 return NULL;
2548 remaining = queue_size - queue->head;
2549 if (remaining < packet_size)
2551 size_t nop_size = remaining - header_size;
2552 struct wined3d_cs_nop *nop;
2554 TRACE("Inserting a nop for %lu + %lu bytes.\n",
2555 (unsigned long)header_size, (unsigned long)nop_size);
2557 nop = wined3d_cs_queue_require_space(queue, nop_size, cs);
2558 if (nop_size)
2559 nop->opcode = WINED3D_CS_OP_NOP;
2561 wined3d_cs_queue_submit(queue, cs);
2562 assert(!queue->head);
2565 for (;;)
2567 LONG tail = *(volatile LONG *)&queue->tail;
2568 LONG head = queue->head;
2569 LONG new_pos;
2571 /* Empty. */
2572 if (head == tail)
2573 break;
2574 new_pos = (head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1);
2575 /* Head ahead of tail. We checked the remaining size above, so we only
2576 * need to make sure we don't make head equal to tail. */
2577 if (head > tail && (new_pos != tail))
2578 break;
2579 /* Tail ahead of head. Make sure the new head is before the tail as
2580 * well. Note that new_pos is 0 when it's at the end of the queue. */
2581 if (new_pos < tail && new_pos)
2582 break;
2584 TRACE("Waiting for free space. Head %u, tail %u, packet size %lu.\n",
2585 head, tail, (unsigned long)packet_size);
2588 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2589 packet->size = size;
2590 return packet->data;
2593 static void *wined3d_cs_mt_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2595 if (cs->thread_id == GetCurrentThreadId())
2596 return wined3d_cs_st_require_space(cs, size, queue_id);
2598 return wined3d_cs_queue_require_space(&cs->queue[queue_id], size, cs);
2601 static void wined3d_cs_mt_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2603 if (cs->thread_id == GetCurrentThreadId())
2604 return wined3d_cs_st_finish(cs, queue_id);
2606 while (cs->queue[queue_id].head != *(volatile LONG *)&cs->queue[queue_id].tail)
2607 wined3d_pause();
2610 static const struct wined3d_cs_ops wined3d_cs_mt_ops =
2612 wined3d_cs_mt_require_space,
2613 wined3d_cs_mt_submit,
2614 wined3d_cs_mt_finish,
2615 wined3d_cs_mt_push_constants,
2618 static void poll_queries(struct wined3d_cs *cs)
2620 struct wined3d_query *query, *cursor;
2622 LIST_FOR_EACH_ENTRY_SAFE(query, cursor, &cs->query_poll_list, struct wined3d_query, poll_list_entry)
2624 if (!query->query_ops->query_poll(query, 0))
2625 continue;
2627 list_remove(&query->poll_list_entry);
2628 list_init(&query->poll_list_entry);
2629 InterlockedIncrement(&query->counter_retrieved);
2633 static void wined3d_cs_wait_event(struct wined3d_cs *cs)
2635 InterlockedExchange(&cs->waiting_for_event, TRUE);
2637 /* The main thread might have enqueued a command and blocked on it after
2638 * the CS thread decided to enter wined3d_cs_wait_event(), but before
2639 * "waiting_for_event" was set.
2641 * Likewise, we can race with the main thread when resetting
2642 * "waiting_for_event", in which case we would need to call
2643 * WaitForSingleObject() because the main thread called SetEvent(). */
2644 if (!(wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_DEFAULT])
2645 && wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_MAP]))
2646 && InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2647 return;
2649 WaitForSingleObject(cs->event, INFINITE);
2652 static DWORD WINAPI wined3d_cs_run(void *ctx)
2654 struct wined3d_cs_packet *packet;
2655 struct wined3d_cs_queue *queue;
2656 unsigned int spin_count = 0;
2657 struct wined3d_cs *cs = ctx;
2658 enum wined3d_cs_op opcode;
2659 HMODULE wined3d_module;
2660 unsigned int poll = 0;
2661 LONG tail;
2663 TRACE("Started.\n");
2665 /* Copy the module handle to a local variable to avoid racing with the
2666 * thread freeing "cs" before the FreeLibraryAndExitThread() call. */
2667 wined3d_module = cs->wined3d_module;
2669 list_init(&cs->query_poll_list);
2670 cs->thread_id = GetCurrentThreadId();
2671 for (;;)
2673 if (++poll == WINED3D_CS_QUERY_POLL_INTERVAL)
2675 poll_queries(cs);
2676 poll = 0;
2679 queue = &cs->queue[WINED3D_CS_QUEUE_MAP];
2680 if (wined3d_cs_queue_is_empty(cs, queue))
2682 queue = &cs->queue[WINED3D_CS_QUEUE_DEFAULT];
2683 if (wined3d_cs_queue_is_empty(cs, queue))
2685 if (++spin_count >= WINED3D_CS_SPIN_COUNT && list_empty(&cs->query_poll_list))
2686 wined3d_cs_wait_event(cs);
2687 continue;
2690 spin_count = 0;
2692 tail = queue->tail;
2693 packet = (struct wined3d_cs_packet *)&queue->data[tail];
2694 if (packet->size)
2696 opcode = *(const enum wined3d_cs_op *)packet->data;
2698 if (opcode >= WINED3D_CS_OP_STOP)
2700 if (opcode > WINED3D_CS_OP_STOP)
2701 ERR("Invalid opcode %#x.\n", opcode);
2702 break;
2705 wined3d_cs_op_handlers[opcode](cs, packet->data);
2708 tail += FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2709 tail &= (WINED3D_CS_QUEUE_SIZE - 1);
2710 InterlockedExchange(&queue->tail, tail);
2713 cs->queue[WINED3D_CS_QUEUE_MAP].tail = cs->queue[WINED3D_CS_QUEUE_MAP].head;
2714 cs->queue[WINED3D_CS_QUEUE_DEFAULT].tail = cs->queue[WINED3D_CS_QUEUE_DEFAULT].head;
2715 TRACE("Stopped.\n");
2716 FreeLibraryAndExitThread(wined3d_module, 0);
2719 struct wined3d_cs *wined3d_cs_create(struct wined3d_device *device)
2721 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2722 struct wined3d_cs *cs;
2724 if (!(cs = heap_alloc_zero(sizeof(*cs))))
2725 return NULL;
2727 cs->ops = &wined3d_cs_st_ops;
2728 cs->device = device;
2730 state_init(&cs->state, &cs->fb, gl_info, &device->adapter->d3d_info,
2731 WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
2733 cs->data_size = WINED3D_INITIAL_CS_SIZE;
2734 if (!(cs->data = heap_alloc(cs->data_size)))
2735 goto fail;
2737 if (wined3d_settings.cs_multithreaded
2738 && !RtlIsCriticalSectionLockedByThread(NtCurrentTeb()->Peb->LoaderLock))
2740 cs->ops = &wined3d_cs_mt_ops;
2742 if (!(cs->event = CreateEventW(NULL, FALSE, FALSE, NULL)))
2744 ERR("Failed to create command stream event.\n");
2745 heap_free(cs->data);
2746 goto fail;
2749 if (!(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2750 (const WCHAR *)wined3d_cs_run, &cs->wined3d_module)))
2752 ERR("Failed to get wined3d module handle.\n");
2753 CloseHandle(cs->event);
2754 heap_free(cs->data);
2755 goto fail;
2758 if (!(cs->thread = CreateThread(NULL, 0, wined3d_cs_run, cs, 0, NULL)))
2760 ERR("Failed to create wined3d command stream thread.\n");
2761 FreeLibrary(cs->wined3d_module);
2762 CloseHandle(cs->event);
2763 heap_free(cs->data);
2764 goto fail;
2768 return cs;
2770 fail:
2771 state_cleanup(&cs->state);
2772 heap_free(cs);
2773 return NULL;
2776 void wined3d_cs_destroy(struct wined3d_cs *cs)
2778 if (cs->thread)
2780 wined3d_cs_emit_stop(cs);
2781 CloseHandle(cs->thread);
2782 if (!CloseHandle(cs->event))
2783 ERR("Closing event failed.\n");
2786 state_cleanup(&cs->state);
2787 heap_free(cs->data);
2788 heap_free(cs);