wined3d: Add support for setting multiple scissor rectangles.
[wine.git] / dlls / wined3d / cs.c
blob517b59979050b8a14d6309822bce66dfb1bdf9cb
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_VIEWPORTS,
37 WINED3D_CS_OP_SET_SCISSOR_RECTS,
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_viewports
143 enum wined3d_cs_op opcode;
144 unsigned int viewport_count;
145 struct wined3d_viewport viewports[1];
148 struct wined3d_cs_set_scissor_rects
150 enum wined3d_cs_op opcode;
151 unsigned int rect_count;
152 RECT rects[1];
155 struct wined3d_cs_set_rendertarget_view
157 enum wined3d_cs_op opcode;
158 unsigned int view_idx;
159 struct wined3d_rendertarget_view *view;
162 struct wined3d_cs_set_depth_stencil_view
164 enum wined3d_cs_op opcode;
165 struct wined3d_rendertarget_view *view;
168 struct wined3d_cs_set_vertex_declaration
170 enum wined3d_cs_op opcode;
171 struct wined3d_vertex_declaration *declaration;
174 struct wined3d_cs_set_stream_source
176 enum wined3d_cs_op opcode;
177 UINT stream_idx;
178 struct wined3d_buffer *buffer;
179 UINT offset;
180 UINT stride;
183 struct wined3d_cs_set_stream_source_freq
185 enum wined3d_cs_op opcode;
186 UINT stream_idx;
187 UINT frequency;
188 UINT flags;
191 struct wined3d_cs_set_stream_output
193 enum wined3d_cs_op opcode;
194 UINT stream_idx;
195 struct wined3d_buffer *buffer;
196 UINT offset;
199 struct wined3d_cs_set_index_buffer
201 enum wined3d_cs_op opcode;
202 struct wined3d_buffer *buffer;
203 enum wined3d_format_id format_id;
204 unsigned int offset;
207 struct wined3d_cs_set_constant_buffer
209 enum wined3d_cs_op opcode;
210 enum wined3d_shader_type type;
211 UINT cb_idx;
212 struct wined3d_buffer *buffer;
215 struct wined3d_cs_set_texture
217 enum wined3d_cs_op opcode;
218 UINT stage;
219 struct wined3d_texture *texture;
222 struct wined3d_cs_set_color_key
224 enum wined3d_cs_op opcode;
225 struct wined3d_texture *texture;
226 WORD flags;
227 WORD set;
228 struct wined3d_color_key color_key;
231 struct wined3d_cs_set_shader_resource_view
233 enum wined3d_cs_op opcode;
234 enum wined3d_shader_type type;
235 UINT view_idx;
236 struct wined3d_shader_resource_view *view;
239 struct wined3d_cs_set_unordered_access_view
241 enum wined3d_cs_op opcode;
242 enum wined3d_pipeline pipeline;
243 unsigned int view_idx;
244 struct wined3d_unordered_access_view *view;
245 unsigned int initial_count;
248 struct wined3d_cs_set_sampler
250 enum wined3d_cs_op opcode;
251 enum wined3d_shader_type type;
252 UINT sampler_idx;
253 struct wined3d_sampler *sampler;
256 struct wined3d_cs_set_shader
258 enum wined3d_cs_op opcode;
259 enum wined3d_shader_type type;
260 struct wined3d_shader *shader;
263 struct wined3d_cs_set_blend_state
265 enum wined3d_cs_op opcode;
266 struct wined3d_blend_state *state;
269 struct wined3d_cs_set_rasterizer_state
271 enum wined3d_cs_op opcode;
272 struct wined3d_rasterizer_state *state;
275 struct wined3d_cs_set_render_state
277 enum wined3d_cs_op opcode;
278 enum wined3d_render_state state;
279 DWORD value;
282 struct wined3d_cs_set_texture_state
284 enum wined3d_cs_op opcode;
285 UINT stage;
286 enum wined3d_texture_stage_state state;
287 DWORD value;
290 struct wined3d_cs_set_sampler_state
292 enum wined3d_cs_op opcode;
293 UINT sampler_idx;
294 enum wined3d_sampler_state state;
295 DWORD value;
298 struct wined3d_cs_set_transform
300 enum wined3d_cs_op opcode;
301 enum wined3d_transform_state state;
302 struct wined3d_matrix matrix;
305 struct wined3d_cs_set_clip_plane
307 enum wined3d_cs_op opcode;
308 UINT plane_idx;
309 struct wined3d_vec4 plane;
312 struct wined3d_cs_set_material
314 enum wined3d_cs_op opcode;
315 struct wined3d_material material;
318 struct wined3d_cs_set_light
320 enum wined3d_cs_op opcode;
321 struct wined3d_light_info light;
324 struct wined3d_cs_set_light_enable
326 enum wined3d_cs_op opcode;
327 unsigned int idx;
328 BOOL enable;
331 struct wined3d_cs_push_constants
333 enum wined3d_cs_op opcode;
334 enum wined3d_push_constants type;
335 unsigned int start_idx;
336 unsigned int count;
337 BYTE constants[1];
340 struct wined3d_cs_reset_state
342 enum wined3d_cs_op opcode;
345 struct wined3d_cs_callback
347 enum wined3d_cs_op opcode;
348 void (*callback)(void *object);
349 void *object;
352 struct wined3d_cs_query_issue
354 enum wined3d_cs_op opcode;
355 struct wined3d_query *query;
356 DWORD flags;
359 struct wined3d_cs_preload_resource
361 enum wined3d_cs_op opcode;
362 struct wined3d_resource *resource;
365 struct wined3d_cs_unload_resource
367 enum wined3d_cs_op opcode;
368 struct wined3d_resource *resource;
371 struct wined3d_cs_map
373 enum wined3d_cs_op opcode;
374 struct wined3d_resource *resource;
375 unsigned int sub_resource_idx;
376 struct wined3d_map_desc *map_desc;
377 const struct wined3d_box *box;
378 DWORD flags;
379 HRESULT *hr;
382 struct wined3d_cs_unmap
384 enum wined3d_cs_op opcode;
385 struct wined3d_resource *resource;
386 unsigned int sub_resource_idx;
387 HRESULT *hr;
390 struct wined3d_cs_blt_sub_resource
392 enum wined3d_cs_op opcode;
393 struct wined3d_resource *dst_resource;
394 unsigned int dst_sub_resource_idx;
395 struct wined3d_box dst_box;
396 struct wined3d_resource *src_resource;
397 unsigned int src_sub_resource_idx;
398 struct wined3d_box src_box;
399 DWORD flags;
400 struct wined3d_blt_fx fx;
401 enum wined3d_texture_filter_type filter;
404 struct wined3d_cs_update_sub_resource
406 enum wined3d_cs_op opcode;
407 struct wined3d_resource *resource;
408 unsigned int sub_resource_idx;
409 struct wined3d_box box;
410 struct wined3d_sub_resource_data data;
413 struct wined3d_cs_add_dirty_texture_region
415 enum wined3d_cs_op opcode;
416 struct wined3d_texture *texture;
417 unsigned int layer;
420 struct wined3d_cs_clear_unordered_access_view
422 enum wined3d_cs_op opcode;
423 struct wined3d_unordered_access_view *view;
424 struct wined3d_uvec4 clear_value;
427 struct wined3d_cs_copy_uav_counter
429 enum wined3d_cs_op opcode;
430 struct wined3d_buffer *buffer;
431 unsigned int offset;
432 struct wined3d_unordered_access_view *view;
435 struct wined3d_cs_generate_mipmaps
437 enum wined3d_cs_op opcode;
438 struct wined3d_shader_resource_view *view;
441 struct wined3d_cs_stop
443 enum wined3d_cs_op opcode;
446 static void wined3d_cs_exec_nop(struct wined3d_cs *cs, const void *data)
450 static void wined3d_cs_exec_present(struct wined3d_cs *cs, const void *data)
452 const struct wined3d_cs_present *op = data;
453 struct wined3d_swapchain *swapchain;
454 unsigned int i;
456 swapchain = op->swapchain;
457 wined3d_swapchain_set_window(swapchain, op->dst_window_override);
458 wined3d_swapchain_set_swap_interval(swapchain, op->swap_interval);
460 swapchain->swapchain_ops->swapchain_present(swapchain, &op->src_rect, &op->dst_rect, op->flags);
462 wined3d_resource_release(&swapchain->front_buffer->resource);
463 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
465 wined3d_resource_release(&swapchain->back_buffers[i]->resource);
468 InterlockedDecrement(&cs->pending_presents);
471 void wined3d_cs_emit_present(struct wined3d_cs *cs, struct wined3d_swapchain *swapchain,
472 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
473 unsigned int swap_interval, DWORD flags)
475 struct wined3d_cs_present *op;
476 unsigned int i;
477 LONG pending;
479 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
480 op->opcode = WINED3D_CS_OP_PRESENT;
481 op->dst_window_override = dst_window_override;
482 op->swapchain = swapchain;
483 op->src_rect = *src_rect;
484 op->dst_rect = *dst_rect;
485 op->swap_interval = swap_interval;
486 op->flags = flags;
488 pending = InterlockedIncrement(&cs->pending_presents);
490 wined3d_resource_acquire(&swapchain->front_buffer->resource);
491 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
493 wined3d_resource_acquire(&swapchain->back_buffers[i]->resource);
496 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
498 /* Limit input latency by limiting the number of presents that we can get
499 * ahead of the worker thread. We have a constant limit here, but
500 * IDXGIDevice1 allows tuning this. */
501 while (pending > 1)
503 wined3d_pause();
504 pending = InterlockedCompareExchange(&cs->pending_presents, 0, 0);
508 static void wined3d_cs_exec_clear(struct wined3d_cs *cs, const void *data)
510 const struct wined3d_cs_clear *op = data;
511 struct wined3d_device *device;
512 unsigned int i;
514 device = cs->device;
515 device->blitter->ops->blitter_clear(device->blitter, device, op->rt_count, op->fb,
516 op->rect_count, op->rects, &op->draw_rect, op->flags, &op->color, op->depth, op->stencil);
518 if (op->flags & WINED3DCLEAR_TARGET)
520 for (i = 0; i < op->rt_count; ++i)
522 if (op->fb->render_targets[i])
523 wined3d_resource_release(op->fb->render_targets[i]->resource);
526 if (op->flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
527 wined3d_resource_release(op->fb->depth_stencil->resource);
530 void wined3d_cs_emit_clear(struct wined3d_cs *cs, DWORD rect_count, const RECT *rects,
531 DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
533 unsigned int rt_count = cs->device->adapter->gl_info.limits.buffers;
534 const struct wined3d_state *state = &cs->device->state;
535 const struct wined3d_viewport *vp = &state->viewports[0];
536 struct wined3d_rendertarget_view *view;
537 struct wined3d_cs_clear *op;
538 RECT view_rect;
539 unsigned int i;
541 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_clear, rects[rect_count]),
542 WINED3D_CS_QUEUE_DEFAULT);
543 op->opcode = WINED3D_CS_OP_CLEAR;
544 op->flags = flags;
545 op->rt_count = rt_count;
546 op->fb = &cs->fb;
547 SetRect(&op->draw_rect, vp->x, vp->y, vp->x + vp->width, vp->y + vp->height);
548 if (state->render_states[WINED3D_RS_SCISSORTESTENABLE])
549 IntersectRect(&op->draw_rect, &op->draw_rect, &state->scissor_rects[0]);
550 op->color = *color;
551 op->depth = depth;
552 op->stencil = stencil;
553 op->rect_count = rect_count;
554 memcpy(op->rects, rects, sizeof(*rects) * rect_count);
556 if (flags & WINED3DCLEAR_TARGET)
558 for (i = 0; i < rt_count; ++i)
560 if ((view = state->fb->render_targets[i]))
562 SetRect(&view_rect, 0, 0, view->width, view->height);
563 IntersectRect(&op->draw_rect, &op->draw_rect, &view_rect);
564 wined3d_resource_acquire(view->resource);
568 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
570 view = state->fb->depth_stencil;
571 SetRect(&view_rect, 0, 0, view->width, view->height);
572 IntersectRect(&op->draw_rect, &op->draw_rect, &view_rect);
573 wined3d_resource_acquire(view->resource);
576 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
579 void wined3d_cs_emit_clear_rendertarget_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view,
580 const RECT *rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
582 struct wined3d_cs_clear *op;
583 size_t size;
585 size = FIELD_OFFSET(struct wined3d_cs_clear, rects[1]) + sizeof(struct wined3d_fb_state);
586 op = cs->ops->require_space(cs, size, WINED3D_CS_QUEUE_DEFAULT);
587 op->fb = (void *)&op->rects[1];
589 op->opcode = WINED3D_CS_OP_CLEAR;
590 op->flags = flags;
591 if (flags & WINED3DCLEAR_TARGET)
593 op->rt_count = 1;
594 op->fb->render_targets[0] = view;
595 op->fb->depth_stencil = NULL;
596 op->color = *color;
598 else
600 op->rt_count = 0;
601 op->fb->render_targets[0] = NULL;
602 op->fb->depth_stencil = view;
603 op->depth = depth;
604 op->stencil = stencil;
606 SetRect(&op->draw_rect, 0, 0, view->width, view->height);
607 op->rect_count = 1;
608 op->rects[0] = *rect;
610 wined3d_resource_acquire(view->resource);
612 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
615 static void acquire_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
617 struct wined3d_shader_sampler_map_entry *entry;
618 struct wined3d_shader_resource_view *view;
619 struct wined3d_shader *shader;
620 unsigned int i, j;
622 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
624 if (!(shader_mask & (1u << i)))
625 continue;
627 if (!(shader = state->shader[i]))
628 continue;
630 for (j = 0; j < WINED3D_MAX_CBS; ++j)
632 if (state->cb[i][j])
633 wined3d_resource_acquire(&state->cb[i][j]->resource);
636 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
638 entry = &shader->reg_maps.sampler_map.entries[j];
640 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
641 continue;
643 wined3d_resource_acquire(view->resource);
648 static void release_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
650 struct wined3d_shader_sampler_map_entry *entry;
651 struct wined3d_shader_resource_view *view;
652 struct wined3d_shader *shader;
653 unsigned int i, j;
655 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
657 if (!(shader_mask & (1u << i)))
658 continue;
660 if (!(shader = state->shader[i]))
661 continue;
663 for (j = 0; j < WINED3D_MAX_CBS; ++j)
665 if (state->cb[i][j])
666 wined3d_resource_release(&state->cb[i][j]->resource);
669 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
671 entry = &shader->reg_maps.sampler_map.entries[j];
673 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
674 continue;
676 wined3d_resource_release(view->resource);
681 static void acquire_unordered_access_resources(const struct wined3d_shader *shader,
682 struct wined3d_unordered_access_view * const *views)
684 unsigned int i;
686 if (!shader)
687 return;
689 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
691 if (!shader->reg_maps.uav_resource_info[i].type)
692 continue;
694 if (!views[i])
695 continue;
697 wined3d_resource_acquire(views[i]->resource);
701 static void release_unordered_access_resources(const struct wined3d_shader *shader,
702 struct wined3d_unordered_access_view * const *views)
704 unsigned int i;
706 if (!shader)
707 return;
709 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
711 if (!shader->reg_maps.uav_resource_info[i].type)
712 continue;
714 if (!views[i])
715 continue;
717 wined3d_resource_release(views[i]->resource);
721 static void wined3d_cs_exec_dispatch(struct wined3d_cs *cs, const void *data)
723 const struct wined3d_cs_dispatch *op = data;
724 struct wined3d_state *state = &cs->state;
726 dispatch_compute(cs->device, state, &op->parameters);
728 if (op->parameters.indirect)
729 wined3d_resource_release(&op->parameters.u.indirect.buffer->resource);
731 release_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
732 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
733 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
736 static void acquire_compute_pipeline_resources(const struct wined3d_state *state)
738 acquire_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
739 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
740 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
743 void wined3d_cs_emit_dispatch(struct wined3d_cs *cs,
744 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
746 const struct wined3d_state *state = &cs->device->state;
747 struct wined3d_cs_dispatch *op;
749 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
750 op->opcode = WINED3D_CS_OP_DISPATCH;
751 op->parameters.indirect = FALSE;
752 op->parameters.u.direct.group_count_x = group_count_x;
753 op->parameters.u.direct.group_count_y = group_count_y;
754 op->parameters.u.direct.group_count_z = group_count_z;
756 acquire_compute_pipeline_resources(state);
758 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
761 void wined3d_cs_emit_dispatch_indirect(struct wined3d_cs *cs,
762 struct wined3d_buffer *buffer, unsigned int offset)
764 const struct wined3d_state *state = &cs->device->state;
765 struct wined3d_cs_dispatch *op;
767 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
768 op->opcode = WINED3D_CS_OP_DISPATCH;
769 op->parameters.indirect = TRUE;
770 op->parameters.u.indirect.buffer = buffer;
771 op->parameters.u.indirect.offset = offset;
773 acquire_compute_pipeline_resources(state);
774 wined3d_resource_acquire(&buffer->resource);
776 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
779 static void wined3d_cs_exec_draw(struct wined3d_cs *cs, const void *data)
781 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
782 struct wined3d_state *state = &cs->state;
783 const struct wined3d_cs_draw *op = data;
784 int load_base_vertex_idx;
785 unsigned int i;
787 /* ARB_draw_indirect always supports a base vertex offset. */
788 if (!op->parameters.indirect && !gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
789 load_base_vertex_idx = op->parameters.u.direct.base_vertex_idx;
790 else
791 load_base_vertex_idx = 0;
793 if (state->load_base_vertex_index != load_base_vertex_idx)
795 state->load_base_vertex_index = load_base_vertex_idx;
796 device_invalidate_state(cs->device, STATE_BASEVERTEXINDEX);
799 if (state->gl_primitive_type != op->primitive_type)
801 if (state->gl_primitive_type == GL_POINTS || op->primitive_type == GL_POINTS)
802 device_invalidate_state(cs->device, STATE_POINT_ENABLE);
803 state->gl_primitive_type = op->primitive_type;
805 state->gl_patch_vertices = op->patch_vertex_count;
807 draw_primitive(cs->device, state, &op->parameters);
809 if (op->parameters.indirect)
811 struct wined3d_buffer *buffer = op->parameters.u.indirect.buffer;
812 wined3d_resource_release(&buffer->resource);
815 if (op->parameters.indexed)
816 wined3d_resource_release(&state->index_buffer->resource);
817 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
819 if (state->streams[i].buffer)
820 wined3d_resource_release(&state->streams[i].buffer->resource);
822 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
824 if (state->stream_output[i].buffer)
825 wined3d_resource_release(&state->stream_output[i].buffer->resource);
827 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
829 if (state->textures[i])
830 wined3d_resource_release(&state->textures[i]->resource);
832 for (i = 0; i < gl_info->limits.buffers; ++i)
834 if (state->fb->render_targets[i])
835 wined3d_resource_release(state->fb->render_targets[i]->resource);
837 if (state->fb->depth_stencil)
838 wined3d_resource_release(state->fb->depth_stencil->resource);
839 release_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
840 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
841 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
844 static void acquire_graphics_pipeline_resources(const struct wined3d_state *state,
845 BOOL indexed, const struct wined3d_gl_info *gl_info)
847 unsigned int i;
849 if (indexed)
850 wined3d_resource_acquire(&state->index_buffer->resource);
851 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
853 if (state->streams[i].buffer)
854 wined3d_resource_acquire(&state->streams[i].buffer->resource);
856 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
858 if (state->stream_output[i].buffer)
859 wined3d_resource_acquire(&state->stream_output[i].buffer->resource);
861 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
863 if (state->textures[i])
864 wined3d_resource_acquire(&state->textures[i]->resource);
866 for (i = 0; i < gl_info->limits.buffers; ++i)
868 if (state->fb->render_targets[i])
869 wined3d_resource_acquire(state->fb->render_targets[i]->resource);
871 if (state->fb->depth_stencil)
872 wined3d_resource_acquire(state->fb->depth_stencil->resource);
873 acquire_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
874 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
875 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
878 void wined3d_cs_emit_draw(struct wined3d_cs *cs, GLenum primitive_type, unsigned int patch_vertex_count,
879 int base_vertex_idx, unsigned int start_idx, unsigned int index_count,
880 unsigned int start_instance, unsigned int instance_count, BOOL indexed)
882 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
883 const struct wined3d_state *state = &cs->device->state;
884 struct wined3d_cs_draw *op;
886 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
887 op->opcode = WINED3D_CS_OP_DRAW;
888 op->primitive_type = primitive_type;
889 op->patch_vertex_count = patch_vertex_count;
890 op->parameters.indirect = FALSE;
891 op->parameters.u.direct.base_vertex_idx = base_vertex_idx;
892 op->parameters.u.direct.start_idx = start_idx;
893 op->parameters.u.direct.index_count = index_count;
894 op->parameters.u.direct.start_instance = start_instance;
895 op->parameters.u.direct.instance_count = instance_count;
896 op->parameters.indexed = indexed;
898 acquire_graphics_pipeline_resources(state, indexed, gl_info);
900 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
903 void wined3d_cs_emit_draw_indirect(struct wined3d_cs *cs, GLenum primitive_type, unsigned int patch_vertex_count,
904 struct wined3d_buffer *buffer, unsigned int offset, BOOL indexed)
906 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
907 const struct wined3d_state *state = &cs->device->state;
908 struct wined3d_cs_draw *op;
910 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
911 op->opcode = WINED3D_CS_OP_DRAW;
912 op->primitive_type = primitive_type;
913 op->patch_vertex_count = patch_vertex_count;
914 op->parameters.indirect = TRUE;
915 op->parameters.u.indirect.buffer = buffer;
916 op->parameters.u.indirect.offset = offset;
917 op->parameters.indexed = indexed;
919 acquire_graphics_pipeline_resources(state, indexed, gl_info);
920 wined3d_resource_acquire(&buffer->resource);
922 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
925 static void wined3d_cs_exec_flush(struct wined3d_cs *cs, const void *data)
927 struct wined3d_context *context;
929 context = context_acquire(cs->device, NULL, 0);
930 if (context->valid)
931 context->gl_info->gl_ops.gl.p_glFlush();
932 context_release(context);
935 void wined3d_cs_emit_flush(struct wined3d_cs *cs)
937 struct wined3d_cs_flush *op;
939 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
940 op->opcode = WINED3D_CS_OP_FLUSH;
942 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
943 cs->queries_flushed = TRUE;
946 static void wined3d_cs_exec_set_predication(struct wined3d_cs *cs, const void *data)
948 const struct wined3d_cs_set_predication *op = data;
950 cs->state.predicate = op->predicate;
951 cs->state.predicate_value = op->value;
954 void wined3d_cs_emit_set_predication(struct wined3d_cs *cs, struct wined3d_query *predicate, BOOL value)
956 struct wined3d_cs_set_predication *op;
958 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
959 op->opcode = WINED3D_CS_OP_SET_PREDICATION;
960 op->predicate = predicate;
961 op->value = value;
963 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
966 static void wined3d_cs_exec_set_viewports(struct wined3d_cs *cs, const void *data)
968 const struct wined3d_cs_set_viewports *op = data;
970 if (op->viewport_count)
971 memcpy(cs->state.viewports, op->viewports, op->viewport_count * sizeof(*op->viewports));
972 else
973 memset(cs->state.viewports, 0, sizeof(*cs->state.viewports));
974 cs->state.viewport_count = op->viewport_count;
975 device_invalidate_state(cs->device, STATE_VIEWPORT);
978 void wined3d_cs_emit_set_viewports(struct wined3d_cs *cs, unsigned int viewport_count,
979 const struct wined3d_viewport *viewports)
981 struct wined3d_cs_set_viewports *op;
983 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_set_viewports, viewports[viewport_count]),
984 WINED3D_CS_QUEUE_DEFAULT);
985 op->opcode = WINED3D_CS_OP_SET_VIEWPORTS;
986 if (viewport_count)
987 memcpy(op->viewports, viewports, viewport_count * sizeof(*viewports));
988 op->viewport_count = viewport_count;
990 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
993 static void wined3d_cs_exec_set_scissor_rects(struct wined3d_cs *cs, const void *data)
995 const struct wined3d_cs_set_scissor_rects *op = data;
997 if (op->rect_count)
998 memcpy(cs->state.scissor_rects, op->rects, op->rect_count * sizeof(*op->rects));
999 else
1000 SetRectEmpty(cs->state.scissor_rects);
1001 cs->state.scissor_rect_count = op->rect_count;
1002 device_invalidate_state(cs->device, STATE_SCISSORRECT);
1005 void wined3d_cs_emit_set_scissor_rects(struct wined3d_cs *cs, unsigned int rect_count, const RECT *rects)
1007 struct wined3d_cs_set_scissor_rects *op;
1009 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_set_scissor_rects, rects[rect_count]),
1010 WINED3D_CS_QUEUE_DEFAULT);
1011 op->opcode = WINED3D_CS_OP_SET_SCISSOR_RECTS;
1012 if (rect_count)
1013 memcpy(op->rects, rects, rect_count * sizeof(*rects));
1014 op->rect_count = rect_count;
1016 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1019 static void wined3d_cs_exec_set_rendertarget_view(struct wined3d_cs *cs, const void *data)
1021 const struct wined3d_cs_set_rendertarget_view *op = data;
1023 cs->fb.render_targets[op->view_idx] = op->view;
1024 device_invalidate_state(cs->device, STATE_FRAMEBUFFER);
1027 void wined3d_cs_emit_set_rendertarget_view(struct wined3d_cs *cs, unsigned int view_idx,
1028 struct wined3d_rendertarget_view *view)
1030 struct wined3d_cs_set_rendertarget_view *op;
1032 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1033 op->opcode = WINED3D_CS_OP_SET_RENDERTARGET_VIEW;
1034 op->view_idx = view_idx;
1035 op->view = view;
1037 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1040 static void wined3d_cs_exec_set_depth_stencil_view(struct wined3d_cs *cs, const void *data)
1042 const struct wined3d_cs_set_depth_stencil_view *op = data;
1043 struct wined3d_device *device = cs->device;
1044 struct wined3d_rendertarget_view *prev;
1046 if ((prev = cs->state.fb->depth_stencil) && prev->resource->type != WINED3D_RTYPE_BUFFER)
1048 struct wined3d_texture *prev_texture = texture_from_resource(prev->resource);
1050 if (device->swapchains[0]->desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
1051 || prev_texture->flags & WINED3D_TEXTURE_DISCARD)
1052 wined3d_texture_validate_location(prev_texture,
1053 prev->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
1056 cs->fb.depth_stencil = op->view;
1058 if (!prev != !op->view)
1060 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
1061 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
1062 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
1063 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
1064 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
1066 else if (prev && prev->format->depth_bias_scale != op->view->format->depth_bias_scale)
1068 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
1071 device_invalidate_state(device, STATE_FRAMEBUFFER);
1074 void wined3d_cs_emit_set_depth_stencil_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view)
1076 struct wined3d_cs_set_depth_stencil_view *op;
1078 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1079 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW;
1080 op->view = view;
1082 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1085 static void wined3d_cs_exec_set_vertex_declaration(struct wined3d_cs *cs, const void *data)
1087 const struct wined3d_cs_set_vertex_declaration *op = data;
1089 cs->state.vertex_declaration = op->declaration;
1090 device_invalidate_state(cs->device, STATE_VDECL);
1093 void wined3d_cs_emit_set_vertex_declaration(struct wined3d_cs *cs, struct wined3d_vertex_declaration *declaration)
1095 struct wined3d_cs_set_vertex_declaration *op;
1097 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1098 op->opcode = WINED3D_CS_OP_SET_VERTEX_DECLARATION;
1099 op->declaration = declaration;
1101 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1104 static void wined3d_cs_exec_set_stream_source(struct wined3d_cs *cs, const void *data)
1106 const struct wined3d_cs_set_stream_source *op = data;
1107 struct wined3d_stream_state *stream;
1108 struct wined3d_buffer *prev;
1110 stream = &cs->state.streams[op->stream_idx];
1111 prev = stream->buffer;
1112 stream->buffer = op->buffer;
1113 stream->offset = op->offset;
1114 stream->stride = op->stride;
1116 if (op->buffer)
1117 InterlockedIncrement(&op->buffer->resource.bind_count);
1118 if (prev)
1119 InterlockedDecrement(&prev->resource.bind_count);
1121 device_invalidate_state(cs->device, STATE_STREAMSRC);
1124 void wined3d_cs_emit_set_stream_source(struct wined3d_cs *cs, UINT stream_idx,
1125 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1127 struct wined3d_cs_set_stream_source *op;
1129 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1130 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE;
1131 op->stream_idx = stream_idx;
1132 op->buffer = buffer;
1133 op->offset = offset;
1134 op->stride = stride;
1136 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1139 static void wined3d_cs_exec_set_stream_source_freq(struct wined3d_cs *cs, const void *data)
1141 const struct wined3d_cs_set_stream_source_freq *op = data;
1142 struct wined3d_stream_state *stream;
1144 stream = &cs->state.streams[op->stream_idx];
1145 stream->frequency = op->frequency;
1146 stream->flags = op->flags;
1148 device_invalidate_state(cs->device, STATE_STREAMSRC);
1151 void wined3d_cs_emit_set_stream_source_freq(struct wined3d_cs *cs, UINT stream_idx, UINT frequency, UINT flags)
1153 struct wined3d_cs_set_stream_source_freq *op;
1155 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1156 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ;
1157 op->stream_idx = stream_idx;
1158 op->frequency = frequency;
1159 op->flags = flags;
1161 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1164 static void wined3d_cs_exec_set_stream_output(struct wined3d_cs *cs, const void *data)
1166 const struct wined3d_cs_set_stream_output *op = data;
1167 struct wined3d_stream_output *stream;
1168 struct wined3d_buffer *prev;
1170 stream = &cs->state.stream_output[op->stream_idx];
1171 prev = stream->buffer;
1172 stream->buffer = op->buffer;
1173 stream->offset = op->offset;
1175 if (op->buffer)
1176 InterlockedIncrement(&op->buffer->resource.bind_count);
1177 if (prev)
1178 InterlockedDecrement(&prev->resource.bind_count);
1180 device_invalidate_state(cs->device, STATE_STREAM_OUTPUT);
1183 void wined3d_cs_emit_set_stream_output(struct wined3d_cs *cs, UINT stream_idx,
1184 struct wined3d_buffer *buffer, UINT offset)
1186 struct wined3d_cs_set_stream_output *op;
1188 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1189 op->opcode = WINED3D_CS_OP_SET_STREAM_OUTPUT;
1190 op->stream_idx = stream_idx;
1191 op->buffer = buffer;
1192 op->offset = offset;
1194 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1197 static void wined3d_cs_exec_set_index_buffer(struct wined3d_cs *cs, const void *data)
1199 const struct wined3d_cs_set_index_buffer *op = data;
1200 struct wined3d_buffer *prev;
1202 prev = cs->state.index_buffer;
1203 cs->state.index_buffer = op->buffer;
1204 cs->state.index_format = op->format_id;
1205 cs->state.index_offset = op->offset;
1207 if (op->buffer)
1208 InterlockedIncrement(&op->buffer->resource.bind_count);
1209 if (prev)
1210 InterlockedDecrement(&prev->resource.bind_count);
1212 device_invalidate_state(cs->device, STATE_INDEXBUFFER);
1215 void wined3d_cs_emit_set_index_buffer(struct wined3d_cs *cs, struct wined3d_buffer *buffer,
1216 enum wined3d_format_id format_id, unsigned int offset)
1218 struct wined3d_cs_set_index_buffer *op;
1220 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1221 op->opcode = WINED3D_CS_OP_SET_INDEX_BUFFER;
1222 op->buffer = buffer;
1223 op->format_id = format_id;
1224 op->offset = offset;
1226 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1229 static void wined3d_cs_exec_set_constant_buffer(struct wined3d_cs *cs, const void *data)
1231 const struct wined3d_cs_set_constant_buffer *op = data;
1232 struct wined3d_buffer *prev;
1234 prev = cs->state.cb[op->type][op->cb_idx];
1235 cs->state.cb[op->type][op->cb_idx] = op->buffer;
1237 if (op->buffer)
1238 InterlockedIncrement(&op->buffer->resource.bind_count);
1239 if (prev)
1240 InterlockedDecrement(&prev->resource.bind_count);
1242 device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(op->type));
1245 void wined3d_cs_emit_set_constant_buffer(struct wined3d_cs *cs, enum wined3d_shader_type type,
1246 UINT cb_idx, struct wined3d_buffer *buffer)
1248 struct wined3d_cs_set_constant_buffer *op;
1250 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1251 op->opcode = WINED3D_CS_OP_SET_CONSTANT_BUFFER;
1252 op->type = type;
1253 op->cb_idx = cb_idx;
1254 op->buffer = buffer;
1256 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1259 static void wined3d_cs_exec_set_texture(struct wined3d_cs *cs, const void *data)
1261 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
1262 const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
1263 const struct wined3d_cs_set_texture *op = data;
1264 struct wined3d_texture *prev;
1265 BOOL old_use_color_key = FALSE, new_use_color_key = FALSE;
1267 prev = cs->state.textures[op->stage];
1268 cs->state.textures[op->stage] = op->texture;
1270 if (op->texture)
1272 const struct wined3d_format *new_format = op->texture->resource.format;
1273 const struct wined3d_format *old_format = prev ? prev->resource.format : NULL;
1274 unsigned int old_fmt_flags = prev ? prev->resource.format_flags : 0;
1275 unsigned int new_fmt_flags = op->texture->resource.format_flags;
1277 if (InterlockedIncrement(&op->texture->resource.bind_count) == 1)
1278 op->texture->sampler = op->stage;
1280 if (!prev || op->texture->target != prev->target
1281 || (!is_same_fixup(new_format->color_fixup, old_format->color_fixup)
1282 && !(can_use_texture_swizzle(gl_info, new_format) && can_use_texture_swizzle(gl_info, old_format)))
1283 || (new_fmt_flags & WINED3DFMT_FLAG_SHADOW) != (old_fmt_flags & WINED3DFMT_FLAG_SHADOW))
1284 device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1286 if (!prev && op->stage < d3d_info->limits.ffp_blend_stages)
1288 /* The source arguments for color and alpha ops have different
1289 * meanings when a NULL texture is bound, so the COLOR_OP and
1290 * ALPHA_OP have to be dirtified. */
1291 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1292 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1295 if (!op->stage && op->texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1296 new_use_color_key = TRUE;
1299 if (prev)
1301 if (InterlockedDecrement(&prev->resource.bind_count) && prev->sampler == op->stage)
1303 unsigned int i;
1305 /* Search for other stages the texture is bound to. Shouldn't
1306 * happen if applications bind textures to a single stage only. */
1307 TRACE("Searching for other stages the texture is bound to.\n");
1308 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
1310 if (cs->state.textures[i] == prev)
1312 TRACE("Texture is also bound to stage %u.\n", i);
1313 prev->sampler = i;
1314 break;
1319 if (!op->texture && op->stage < d3d_info->limits.ffp_blend_stages)
1321 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1322 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1325 if (!op->stage && prev->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1326 old_use_color_key = TRUE;
1329 device_invalidate_state(cs->device, STATE_SAMPLER(op->stage));
1331 if (new_use_color_key != old_use_color_key)
1332 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1334 if (new_use_color_key)
1335 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1338 void wined3d_cs_emit_set_texture(struct wined3d_cs *cs, UINT stage, struct wined3d_texture *texture)
1340 struct wined3d_cs_set_texture *op;
1342 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1343 op->opcode = WINED3D_CS_OP_SET_TEXTURE;
1344 op->stage = stage;
1345 op->texture = texture;
1347 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1350 static void wined3d_cs_exec_set_shader_resource_view(struct wined3d_cs *cs, const void *data)
1352 const struct wined3d_cs_set_shader_resource_view *op = data;
1353 struct wined3d_shader_resource_view *prev;
1355 prev = cs->state.shader_resource_view[op->type][op->view_idx];
1356 cs->state.shader_resource_view[op->type][op->view_idx] = op->view;
1358 if (op->view)
1359 InterlockedIncrement(&op->view->resource->bind_count);
1360 if (prev)
1361 InterlockedDecrement(&prev->resource->bind_count);
1363 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1364 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1365 else
1366 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1369 void wined3d_cs_emit_set_shader_resource_view(struct wined3d_cs *cs, enum wined3d_shader_type type,
1370 UINT view_idx, struct wined3d_shader_resource_view *view)
1372 struct wined3d_cs_set_shader_resource_view *op;
1374 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1375 op->opcode = WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW;
1376 op->type = type;
1377 op->view_idx = view_idx;
1378 op->view = view;
1380 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1383 static void wined3d_cs_exec_set_unordered_access_view(struct wined3d_cs *cs, const void *data)
1385 const struct wined3d_cs_set_unordered_access_view *op = data;
1386 struct wined3d_unordered_access_view *prev;
1388 prev = cs->state.unordered_access_view[op->pipeline][op->view_idx];
1389 cs->state.unordered_access_view[op->pipeline][op->view_idx] = op->view;
1391 if (op->view)
1392 InterlockedIncrement(&op->view->resource->bind_count);
1393 if (prev)
1394 InterlockedDecrement(&prev->resource->bind_count);
1396 if (op->view && op->initial_count != ~0u)
1397 wined3d_unordered_access_view_set_counter(op->view, op->initial_count);
1399 device_invalidate_state(cs->device, STATE_UNORDERED_ACCESS_VIEW_BINDING(op->pipeline));
1402 void wined3d_cs_emit_set_unordered_access_view(struct wined3d_cs *cs, enum wined3d_pipeline pipeline,
1403 unsigned int view_idx, struct wined3d_unordered_access_view *view, unsigned int initial_count)
1405 struct wined3d_cs_set_unordered_access_view *op;
1407 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1408 op->opcode = WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW;
1409 op->pipeline = pipeline;
1410 op->view_idx = view_idx;
1411 op->view = view;
1412 op->initial_count = initial_count;
1414 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1417 static void wined3d_cs_exec_set_sampler(struct wined3d_cs *cs, const void *data)
1419 const struct wined3d_cs_set_sampler *op = data;
1421 cs->state.sampler[op->type][op->sampler_idx] = op->sampler;
1422 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1423 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1424 else
1425 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1428 void wined3d_cs_emit_set_sampler(struct wined3d_cs *cs, enum wined3d_shader_type type,
1429 UINT sampler_idx, struct wined3d_sampler *sampler)
1431 struct wined3d_cs_set_sampler *op;
1433 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1434 op->opcode = WINED3D_CS_OP_SET_SAMPLER;
1435 op->type = type;
1436 op->sampler_idx = sampler_idx;
1437 op->sampler = sampler;
1439 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1442 static void wined3d_cs_exec_set_shader(struct wined3d_cs *cs, const void *data)
1444 const struct wined3d_cs_set_shader *op = data;
1446 cs->state.shader[op->type] = op->shader;
1447 device_invalidate_state(cs->device, STATE_SHADER(op->type));
1448 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1449 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1450 else
1451 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1454 void wined3d_cs_emit_set_shader(struct wined3d_cs *cs, enum wined3d_shader_type type, struct wined3d_shader *shader)
1456 struct wined3d_cs_set_shader *op;
1458 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1459 op->opcode = WINED3D_CS_OP_SET_SHADER;
1460 op->type = type;
1461 op->shader = shader;
1463 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1466 static void wined3d_cs_exec_set_blend_state(struct wined3d_cs *cs, const void *data)
1468 const struct wined3d_cs_set_blend_state *op = data;
1470 cs->state.blend_state = op->state;
1471 device_invalidate_state(cs->device, STATE_BLEND);
1474 void wined3d_cs_emit_set_blend_state(struct wined3d_cs *cs, struct wined3d_blend_state *state)
1476 struct wined3d_cs_set_blend_state *op;
1478 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1479 op->opcode = WINED3D_CS_OP_SET_BLEND_STATE;
1480 op->state = state;
1482 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1485 static void wined3d_cs_exec_set_rasterizer_state(struct wined3d_cs *cs, const void *data)
1487 const struct wined3d_cs_set_rasterizer_state *op = data;
1489 cs->state.rasterizer_state = op->state;
1490 device_invalidate_state(cs->device, STATE_FRONTFACE);
1493 void wined3d_cs_emit_set_rasterizer_state(struct wined3d_cs *cs,
1494 struct wined3d_rasterizer_state *rasterizer_state)
1496 struct wined3d_cs_set_rasterizer_state *op;
1498 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1499 op->opcode = WINED3D_CS_OP_SET_RASTERIZER_STATE;
1500 op->state = rasterizer_state;
1502 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1505 static void wined3d_cs_exec_set_render_state(struct wined3d_cs *cs, const void *data)
1507 const struct wined3d_cs_set_render_state *op = data;
1509 cs->state.render_states[op->state] = op->value;
1510 device_invalidate_state(cs->device, STATE_RENDER(op->state));
1513 void wined3d_cs_emit_set_render_state(struct wined3d_cs *cs, enum wined3d_render_state state, DWORD value)
1515 struct wined3d_cs_set_render_state *op;
1517 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1518 op->opcode = WINED3D_CS_OP_SET_RENDER_STATE;
1519 op->state = state;
1520 op->value = value;
1522 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1525 static void wined3d_cs_exec_set_texture_state(struct wined3d_cs *cs, const void *data)
1527 const struct wined3d_cs_set_texture_state *op = data;
1529 cs->state.texture_states[op->stage][op->state] = op->value;
1530 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, op->state));
1533 void wined3d_cs_emit_set_texture_state(struct wined3d_cs *cs, UINT stage,
1534 enum wined3d_texture_stage_state state, DWORD value)
1536 struct wined3d_cs_set_texture_state *op;
1538 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1539 op->opcode = WINED3D_CS_OP_SET_TEXTURE_STATE;
1540 op->stage = stage;
1541 op->state = state;
1542 op->value = value;
1544 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1547 static void wined3d_cs_exec_set_sampler_state(struct wined3d_cs *cs, const void *data)
1549 const struct wined3d_cs_set_sampler_state *op = data;
1551 cs->state.sampler_states[op->sampler_idx][op->state] = op->value;
1552 device_invalidate_state(cs->device, STATE_SAMPLER(op->sampler_idx));
1555 void wined3d_cs_emit_set_sampler_state(struct wined3d_cs *cs, UINT sampler_idx,
1556 enum wined3d_sampler_state state, DWORD value)
1558 struct wined3d_cs_set_sampler_state *op;
1560 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1561 op->opcode = WINED3D_CS_OP_SET_SAMPLER_STATE;
1562 op->sampler_idx = sampler_idx;
1563 op->state = state;
1564 op->value = value;
1566 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1569 static void wined3d_cs_exec_set_transform(struct wined3d_cs *cs, const void *data)
1571 const struct wined3d_cs_set_transform *op = data;
1573 cs->state.transforms[op->state] = op->matrix;
1574 if (op->state < WINED3D_TS_WORLD_MATRIX(cs->device->adapter->d3d_info.limits.ffp_vertex_blend_matrices))
1575 device_invalidate_state(cs->device, STATE_TRANSFORM(op->state));
1578 void wined3d_cs_emit_set_transform(struct wined3d_cs *cs, enum wined3d_transform_state state,
1579 const struct wined3d_matrix *matrix)
1581 struct wined3d_cs_set_transform *op;
1583 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1584 op->opcode = WINED3D_CS_OP_SET_TRANSFORM;
1585 op->state = state;
1586 op->matrix = *matrix;
1588 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1591 static void wined3d_cs_exec_set_clip_plane(struct wined3d_cs *cs, const void *data)
1593 const struct wined3d_cs_set_clip_plane *op = data;
1595 cs->state.clip_planes[op->plane_idx] = op->plane;
1596 device_invalidate_state(cs->device, STATE_CLIPPLANE(op->plane_idx));
1599 void wined3d_cs_emit_set_clip_plane(struct wined3d_cs *cs, UINT plane_idx, const struct wined3d_vec4 *plane)
1601 struct wined3d_cs_set_clip_plane *op;
1603 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1604 op->opcode = WINED3D_CS_OP_SET_CLIP_PLANE;
1605 op->plane_idx = plane_idx;
1606 op->plane = *plane;
1608 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1611 static void wined3d_cs_exec_set_color_key(struct wined3d_cs *cs, const void *data)
1613 const struct wined3d_cs_set_color_key *op = data;
1614 struct wined3d_texture *texture = op->texture;
1616 if (op->set)
1618 switch (op->flags)
1620 case WINED3D_CKEY_DST_BLT:
1621 texture->async.dst_blt_color_key = op->color_key;
1622 texture->async.color_key_flags |= WINED3D_CKEY_DST_BLT;
1623 break;
1625 case WINED3D_CKEY_DST_OVERLAY:
1626 texture->async.dst_overlay_color_key = op->color_key;
1627 texture->async.color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
1628 break;
1630 case WINED3D_CKEY_SRC_BLT:
1631 if (texture == cs->state.textures[0])
1633 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1634 if (!(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1635 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1638 texture->async.src_blt_color_key = op->color_key;
1639 texture->async.color_key_flags |= WINED3D_CKEY_SRC_BLT;
1640 break;
1642 case WINED3D_CKEY_SRC_OVERLAY:
1643 texture->async.src_overlay_color_key = op->color_key;
1644 texture->async.color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
1645 break;
1648 else
1650 switch (op->flags)
1652 case WINED3D_CKEY_DST_BLT:
1653 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_BLT;
1654 break;
1656 case WINED3D_CKEY_DST_OVERLAY:
1657 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
1658 break;
1660 case WINED3D_CKEY_SRC_BLT:
1661 if (texture == cs->state.textures[0] && texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1662 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1664 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
1665 break;
1667 case WINED3D_CKEY_SRC_OVERLAY:
1668 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
1669 break;
1674 void wined3d_cs_emit_set_color_key(struct wined3d_cs *cs, struct wined3d_texture *texture,
1675 WORD flags, const struct wined3d_color_key *color_key)
1677 struct wined3d_cs_set_color_key *op;
1679 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1680 op->opcode = WINED3D_CS_OP_SET_COLOR_KEY;
1681 op->texture = texture;
1682 op->flags = flags;
1683 if (color_key)
1685 op->color_key = *color_key;
1686 op->set = 1;
1688 else
1689 op->set = 0;
1691 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1694 static void wined3d_cs_exec_set_material(struct wined3d_cs *cs, const void *data)
1696 const struct wined3d_cs_set_material *op = data;
1698 cs->state.material = op->material;
1699 device_invalidate_state(cs->device, STATE_MATERIAL);
1702 void wined3d_cs_emit_set_material(struct wined3d_cs *cs, const struct wined3d_material *material)
1704 struct wined3d_cs_set_material *op;
1706 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1707 op->opcode = WINED3D_CS_OP_SET_MATERIAL;
1708 op->material = *material;
1710 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1713 static void wined3d_cs_exec_set_light(struct wined3d_cs *cs, const void *data)
1715 const struct wined3d_cs_set_light *op = data;
1716 struct wined3d_light_info *light_info;
1717 unsigned int light_idx, hash_idx;
1719 light_idx = op->light.OriginalIndex;
1721 if (!(light_info = wined3d_state_get_light(&cs->state, light_idx)))
1723 TRACE("Adding new light.\n");
1724 if (!(light_info = heap_alloc_zero(sizeof(*light_info))))
1726 ERR("Failed to allocate light info.\n");
1727 return;
1730 hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1731 list_add_head(&cs->state.light_map[hash_idx], &light_info->entry);
1732 light_info->glIndex = -1;
1733 light_info->OriginalIndex = light_idx;
1736 if (light_info->glIndex != -1)
1738 if (light_info->OriginalParms.type != op->light.OriginalParms.type)
1739 device_invalidate_state(cs->device, STATE_LIGHT_TYPE);
1740 device_invalidate_state(cs->device, STATE_ACTIVELIGHT(light_info->glIndex));
1743 light_info->OriginalParms = op->light.OriginalParms;
1744 light_info->position = op->light.position;
1745 light_info->direction = op->light.direction;
1746 light_info->exponent = op->light.exponent;
1747 light_info->cutoff = op->light.cutoff;
1750 void wined3d_cs_emit_set_light(struct wined3d_cs *cs, const struct wined3d_light_info *light)
1752 struct wined3d_cs_set_light *op;
1754 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1755 op->opcode = WINED3D_CS_OP_SET_LIGHT;
1756 op->light = *light;
1758 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1761 static void wined3d_cs_exec_set_light_enable(struct wined3d_cs *cs, const void *data)
1763 const struct wined3d_cs_set_light_enable *op = data;
1764 struct wined3d_device *device = cs->device;
1765 struct wined3d_light_info *light_info;
1766 int prev_idx;
1768 if (!(light_info = wined3d_state_get_light(&cs->state, op->idx)))
1770 ERR("Light doesn't exist.\n");
1771 return;
1774 prev_idx = light_info->glIndex;
1775 wined3d_state_enable_light(&cs->state, &device->adapter->d3d_info, light_info, op->enable);
1776 if (light_info->glIndex != prev_idx)
1778 device_invalidate_state(device, STATE_LIGHT_TYPE);
1779 device_invalidate_state(device, STATE_ACTIVELIGHT(op->enable ? light_info->glIndex : prev_idx));
1783 void wined3d_cs_emit_set_light_enable(struct wined3d_cs *cs, unsigned int idx, BOOL enable)
1785 struct wined3d_cs_set_light_enable *op;
1787 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1788 op->opcode = WINED3D_CS_OP_SET_LIGHT_ENABLE;
1789 op->idx = idx;
1790 op->enable = enable;
1792 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1795 static const struct
1797 size_t offset;
1798 size_t size;
1799 DWORD mask;
1801 wined3d_cs_push_constant_info[] =
1803 /* WINED3D_PUSH_CONSTANTS_VS_F */
1804 {FIELD_OFFSET(struct wined3d_state, vs_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_VS_F},
1805 /* WINED3D_PUSH_CONSTANTS_PS_F */
1806 {FIELD_OFFSET(struct wined3d_state, ps_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_PS_F},
1807 /* WINED3D_PUSH_CONSTANTS_VS_I */
1808 {FIELD_OFFSET(struct wined3d_state, vs_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_VS_I},
1809 /* WINED3D_PUSH_CONSTANTS_PS_I */
1810 {FIELD_OFFSET(struct wined3d_state, ps_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_PS_I},
1811 /* WINED3D_PUSH_CONSTANTS_VS_B */
1812 {FIELD_OFFSET(struct wined3d_state, vs_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_VS_B},
1813 /* WINED3D_PUSH_CONSTANTS_PS_B */
1814 {FIELD_OFFSET(struct wined3d_state, ps_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_PS_B},
1817 static void wined3d_cs_st_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1818 unsigned int start_idx, unsigned int count, const void *constants)
1820 struct wined3d_device *device = cs->device;
1821 unsigned int context_count;
1822 unsigned int i;
1823 size_t offset;
1825 if (p == WINED3D_PUSH_CONSTANTS_VS_F)
1826 device->shader_backend->shader_update_float_vertex_constants(device, start_idx, count);
1827 else if (p == WINED3D_PUSH_CONSTANTS_PS_F)
1828 device->shader_backend->shader_update_float_pixel_constants(device, start_idx, count);
1830 offset = wined3d_cs_push_constant_info[p].offset + start_idx * wined3d_cs_push_constant_info[p].size;
1831 memcpy((BYTE *)&cs->state + offset, constants, count * wined3d_cs_push_constant_info[p].size);
1832 for (i = 0, context_count = device->context_count; i < context_count; ++i)
1834 device->contexts[i]->constant_update_mask |= wined3d_cs_push_constant_info[p].mask;
1838 static void wined3d_cs_exec_push_constants(struct wined3d_cs *cs, const void *data)
1840 const struct wined3d_cs_push_constants *op = data;
1842 wined3d_cs_st_push_constants(cs, op->type, op->start_idx, op->count, op->constants);
1845 static void wined3d_cs_mt_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1846 unsigned int start_idx, unsigned int count, const void *constants)
1848 struct wined3d_cs_push_constants *op;
1849 size_t size;
1851 size = count * wined3d_cs_push_constant_info[p].size;
1852 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_push_constants, constants[size]),
1853 WINED3D_CS_QUEUE_DEFAULT);
1854 op->opcode = WINED3D_CS_OP_PUSH_CONSTANTS;
1855 op->type = p;
1856 op->start_idx = start_idx;
1857 op->count = count;
1858 memcpy(op->constants, constants, size);
1860 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1863 static void wined3d_cs_exec_reset_state(struct wined3d_cs *cs, const void *data)
1865 struct wined3d_adapter *adapter = cs->device->adapter;
1867 state_cleanup(&cs->state);
1868 memset(&cs->state, 0, sizeof(cs->state));
1869 state_init(&cs->state, &cs->fb, &adapter->gl_info, &adapter->d3d_info,
1870 WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
1873 void wined3d_cs_emit_reset_state(struct wined3d_cs *cs)
1875 struct wined3d_cs_reset_state *op;
1877 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1878 op->opcode = WINED3D_CS_OP_RESET_STATE;
1880 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1883 static void wined3d_cs_exec_callback(struct wined3d_cs *cs, const void *data)
1885 const struct wined3d_cs_callback *op = data;
1887 op->callback(op->object);
1890 static void wined3d_cs_emit_callback(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1892 struct wined3d_cs_callback *op;
1894 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1895 op->opcode = WINED3D_CS_OP_CALLBACK;
1896 op->callback = callback;
1897 op->object = object;
1899 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1902 void wined3d_cs_destroy_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1904 wined3d_cs_emit_callback(cs, callback, object);
1907 void wined3d_cs_init_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1909 wined3d_cs_emit_callback(cs, callback, object);
1912 static void wined3d_cs_exec_query_issue(struct wined3d_cs *cs, const void *data)
1914 const struct wined3d_cs_query_issue *op = data;
1915 struct wined3d_query *query = op->query;
1916 BOOL poll;
1918 poll = query->query_ops->query_issue(query, op->flags);
1920 if (!cs->thread)
1921 return;
1923 if (poll && list_empty(&query->poll_list_entry))
1925 list_add_tail(&cs->query_poll_list, &query->poll_list_entry);
1926 return;
1929 /* This can happen if occlusion queries are restarted. This discards the
1930 * old result, since polling it could result in a GL error. */
1931 if ((op->flags & WINED3DISSUE_BEGIN) && !poll && !list_empty(&query->poll_list_entry))
1933 list_remove(&query->poll_list_entry);
1934 list_init(&query->poll_list_entry);
1935 InterlockedIncrement(&query->counter_retrieved);
1936 return;
1939 /* This can happen when an occlusion query is ended without being started,
1940 * in which case we don't want to poll, but still have to counter-balance
1941 * the increment of the main counter.
1943 * This can also happen if an event query is re-issued before the first
1944 * fence was reached. In this case the query is already in the list and
1945 * the poll function will check the new fence. We have to counter-balance
1946 * the discarded increment. */
1947 if (op->flags & WINED3DISSUE_END)
1948 InterlockedIncrement(&query->counter_retrieved);
1951 void wined3d_cs_emit_query_issue(struct wined3d_cs *cs, struct wined3d_query *query, DWORD flags)
1953 struct wined3d_cs_query_issue *op;
1955 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1956 op->opcode = WINED3D_CS_OP_QUERY_ISSUE;
1957 op->query = query;
1958 op->flags = flags;
1960 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1961 cs->queries_flushed = FALSE;
1964 static void wined3d_cs_exec_preload_resource(struct wined3d_cs *cs, const void *data)
1966 const struct wined3d_cs_preload_resource *op = data;
1967 struct wined3d_resource *resource = op->resource;
1969 resource->resource_ops->resource_preload(resource);
1970 wined3d_resource_release(resource);
1973 void wined3d_cs_emit_preload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
1975 struct wined3d_cs_preload_resource *op;
1977 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1978 op->opcode = WINED3D_CS_OP_PRELOAD_RESOURCE;
1979 op->resource = resource;
1981 wined3d_resource_acquire(resource);
1983 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1986 static void wined3d_cs_exec_unload_resource(struct wined3d_cs *cs, const void *data)
1988 const struct wined3d_cs_unload_resource *op = data;
1989 struct wined3d_resource *resource = op->resource;
1991 resource->resource_ops->resource_unload(resource);
1992 wined3d_resource_release(resource);
1995 void wined3d_cs_emit_unload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
1997 struct wined3d_cs_unload_resource *op;
1999 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2000 op->opcode = WINED3D_CS_OP_UNLOAD_RESOURCE;
2001 op->resource = resource;
2003 wined3d_resource_acquire(resource);
2005 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2008 static void wined3d_cs_exec_map(struct wined3d_cs *cs, const void *data)
2010 const struct wined3d_cs_map *op = data;
2011 struct wined3d_resource *resource = op->resource;
2013 *op->hr = resource->resource_ops->resource_sub_resource_map(resource,
2014 op->sub_resource_idx, op->map_desc, op->box, op->flags);
2017 HRESULT wined3d_cs_map(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx,
2018 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
2020 struct wined3d_cs_map *op;
2021 HRESULT hr;
2023 /* Mapping resources from the worker thread isn't an issue by itself, but
2024 * increasing the map count would be visible to applications. */
2025 wined3d_not_from_cs(cs);
2027 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2028 op->opcode = WINED3D_CS_OP_MAP;
2029 op->resource = resource;
2030 op->sub_resource_idx = sub_resource_idx;
2031 op->map_desc = map_desc;
2032 op->box = box;
2033 op->flags = flags;
2034 op->hr = &hr;
2036 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2037 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2039 return hr;
2042 static void wined3d_cs_exec_unmap(struct wined3d_cs *cs, const void *data)
2044 const struct wined3d_cs_unmap *op = data;
2045 struct wined3d_resource *resource = op->resource;
2047 *op->hr = resource->resource_ops->resource_sub_resource_unmap(resource, op->sub_resource_idx);
2050 HRESULT wined3d_cs_unmap(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx)
2052 struct wined3d_cs_unmap *op;
2053 HRESULT hr;
2055 wined3d_not_from_cs(cs);
2057 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2058 op->opcode = WINED3D_CS_OP_UNMAP;
2059 op->resource = resource;
2060 op->sub_resource_idx = sub_resource_idx;
2061 op->hr = &hr;
2063 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2064 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2066 return hr;
2069 static void wined3d_cs_exec_blt_sub_resource(struct wined3d_cs *cs, const void *data)
2071 const struct wined3d_cs_blt_sub_resource *op = data;
2073 if (op->dst_resource->type == WINED3D_RTYPE_BUFFER)
2075 wined3d_buffer_copy(buffer_from_resource(op->dst_resource), op->dst_box.left,
2076 buffer_from_resource(op->src_resource), op->src_box.left,
2077 op->src_box.right - op->src_box.left);
2079 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_2D)
2081 if (FAILED(texture2d_blt(texture_from_resource(op->dst_resource), op->dst_sub_resource_idx,
2082 &op->dst_box, texture_from_resource(op->src_resource), op->src_sub_resource_idx,
2083 &op->src_box, op->flags, &op->fx, op->filter)))
2084 FIXME("Blit failed.\n");
2086 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_3D)
2088 struct wined3d_texture *src_texture, *dst_texture;
2089 unsigned int level, update_w, update_h, update_d;
2090 unsigned int row_pitch, slice_pitch;
2091 struct wined3d_context *context;
2092 struct wined3d_bo_address addr;
2094 if (op->flags & ~WINED3D_BLT_RAW)
2096 FIXME("Flags %#x not implemented for %s resources.\n",
2097 op->flags, debug_d3dresourcetype(op->dst_resource->type));
2098 goto error;
2101 if (!(op->flags & WINED3D_BLT_RAW) && op->src_resource->format != op->dst_resource->format)
2103 FIXME("Format conversion not implemented for %s resources.\n",
2104 debug_d3dresourcetype(op->dst_resource->type));
2105 goto error;
2108 update_w = op->dst_box.right - op->dst_box.left;
2109 update_h = op->dst_box.bottom - op->dst_box.top;
2110 update_d = op->dst_box.back - op->dst_box.front;
2111 if (op->src_box.right - op->src_box.left != update_w
2112 || op->src_box.bottom - op->src_box.top != update_h
2113 || op->src_box.back - op->src_box.front != update_d)
2115 FIXME("Stretching not implemented for %s resources.\n",
2116 debug_d3dresourcetype(op->dst_resource->type));
2117 goto error;
2120 dst_texture = texture_from_resource(op->dst_resource);
2121 src_texture = texture_from_resource(op->src_resource);
2123 context = context_acquire(cs->device, NULL, 0);
2125 if (!wined3d_texture_load_location(src_texture, op->src_sub_resource_idx,
2126 context, src_texture->resource.map_binding))
2128 ERR("Failed to load source sub-resource into %s.\n",
2129 wined3d_debug_location(src_texture->resource.map_binding));
2130 context_release(context);
2131 goto error;
2134 level = op->dst_sub_resource_idx % dst_texture->level_count;
2135 if (update_w == wined3d_texture_get_level_width(dst_texture, level)
2136 && update_h == wined3d_texture_get_level_height(dst_texture, level)
2137 && update_d == wined3d_texture_get_level_depth(dst_texture, level))
2139 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
2141 else if (!wined3d_texture_load_location(dst_texture, op->dst_sub_resource_idx,
2142 context, WINED3D_LOCATION_TEXTURE_RGB))
2144 ERR("Failed to load destination sub-resource.\n");
2145 context_release(context);
2146 goto error;
2149 wined3d_texture_get_memory(src_texture, op->src_sub_resource_idx, &addr, src_texture->resource.map_binding);
2150 wined3d_texture_get_pitch(src_texture, op->src_sub_resource_idx % src_texture->level_count,
2151 &row_pitch, &slice_pitch);
2153 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
2154 wined3d_texture_upload_data(dst_texture, op->dst_sub_resource_idx, context,
2155 dst_texture->resource.format, &op->src_box, wined3d_const_bo_address(&addr),
2156 row_pitch, slice_pitch, op->dst_box.left, op->dst_box.top, op->dst_box.front, FALSE);
2157 wined3d_texture_validate_location(dst_texture, op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2158 wined3d_texture_invalidate_location(dst_texture, op->dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2160 context_release(context);
2162 else
2164 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(op->dst_resource->type));
2167 error:
2168 if (op->src_resource)
2169 wined3d_resource_release(op->src_resource);
2170 wined3d_resource_release(op->dst_resource);
2173 void wined3d_cs_emit_blt_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *dst_resource,
2174 unsigned int dst_sub_resource_idx, const struct wined3d_box *dst_box, struct wined3d_resource *src_resource,
2175 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, DWORD flags,
2176 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2178 struct wined3d_cs_blt_sub_resource *op;
2180 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2181 op->opcode = WINED3D_CS_OP_BLT_SUB_RESOURCE;
2182 op->dst_resource = dst_resource;
2183 op->dst_sub_resource_idx = dst_sub_resource_idx;
2184 op->dst_box = *dst_box;
2185 op->src_resource = src_resource;
2186 op->src_sub_resource_idx = src_sub_resource_idx;
2187 op->src_box = *src_box;
2188 op->flags = flags;
2189 if (fx)
2190 op->fx = *fx;
2191 else
2192 memset(&op->fx, 0, sizeof(op->fx));
2193 op->filter = filter;
2195 wined3d_resource_acquire(dst_resource);
2196 if (src_resource)
2197 wined3d_resource_acquire(src_resource);
2199 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2200 if (flags & WINED3D_BLT_SYNCHRONOUS)
2201 cs->ops->finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2204 static void wined3d_cs_exec_update_sub_resource(struct wined3d_cs *cs, const void *data)
2206 const struct wined3d_cs_update_sub_resource *op = data;
2207 struct wined3d_resource *resource = op->resource;
2208 const struct wined3d_box *box = &op->box;
2209 unsigned int width, height, depth, level;
2210 struct wined3d_const_bo_address addr;
2211 struct wined3d_context *context;
2212 struct wined3d_texture *texture;
2213 struct wined3d_box src_box;
2215 context = context_acquire(cs->device, NULL, 0);
2217 if (resource->type == WINED3D_RTYPE_BUFFER)
2219 struct wined3d_buffer *buffer = buffer_from_resource(resource);
2221 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
2223 ERR("Failed to load buffer location.\n");
2224 goto done;
2227 wined3d_buffer_upload_data(buffer, context, box, op->data.data);
2228 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
2229 goto done;
2232 texture = wined3d_texture_from_resource(resource);
2234 level = op->sub_resource_idx % texture->level_count;
2235 width = wined3d_texture_get_level_width(texture, level);
2236 height = wined3d_texture_get_level_height(texture, level);
2237 depth = wined3d_texture_get_level_depth(texture, level);
2239 addr.buffer_object = 0;
2240 addr.addr = op->data.data;
2242 /* Only load the sub-resource for partial updates. */
2243 if (!box->left && !box->top && !box->front
2244 && box->right == width && box->bottom == height && box->back == depth)
2245 wined3d_texture_prepare_texture(texture, context, FALSE);
2246 else
2247 wined3d_texture_load_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2248 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2250 wined3d_box_set(&src_box, 0, 0, box->right - box->left, box->bottom - box->top, 0, box->back - box->front);
2251 wined3d_texture_upload_data(texture, op->sub_resource_idx, context, texture->resource.format, &src_box,
2252 &addr, op->data.row_pitch, op->data.slice_pitch, box->left, box->top, box->front, FALSE);
2254 wined3d_texture_validate_location(texture, op->sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2255 wined3d_texture_invalidate_location(texture, op->sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2257 done:
2258 context_release(context);
2260 wined3d_resource_release(resource);
2263 void wined3d_cs_emit_update_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *resource,
2264 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
2265 unsigned int slice_pitch)
2267 struct wined3d_cs_update_sub_resource *op;
2269 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2270 op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2271 op->resource = resource;
2272 op->sub_resource_idx = sub_resource_idx;
2273 op->box = *box;
2274 op->data.row_pitch = row_pitch;
2275 op->data.slice_pitch = slice_pitch;
2276 op->data.data = data;
2278 wined3d_resource_acquire(resource);
2280 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2281 /* The data pointer may go away, so we need to wait until it is read.
2282 * Copying the data may be faster if it's small. */
2283 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2286 static void wined3d_cs_exec_add_dirty_texture_region(struct wined3d_cs *cs, const void *data)
2288 const struct wined3d_cs_add_dirty_texture_region *op = data;
2289 struct wined3d_texture *texture = op->texture;
2290 unsigned int sub_resource_idx, i;
2291 struct wined3d_context *context;
2293 context = context_acquire(cs->device, NULL, 0);
2294 sub_resource_idx = op->layer * texture->level_count;
2295 for (i = 0; i < texture->level_count; ++i, ++sub_resource_idx)
2297 if (wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
2298 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2299 else
2300 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
2302 context_release(context);
2304 wined3d_resource_release(&texture->resource);
2307 void wined3d_cs_emit_add_dirty_texture_region(struct wined3d_cs *cs,
2308 struct wined3d_texture *texture, unsigned int layer)
2310 struct wined3d_cs_add_dirty_texture_region *op;
2312 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2313 op->opcode = WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION;
2314 op->texture = texture;
2315 op->layer = layer;
2317 wined3d_resource_acquire(&texture->resource);
2319 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2322 static void wined3d_cs_exec_clear_unordered_access_view(struct wined3d_cs *cs, const void *data)
2324 const struct wined3d_cs_clear_unordered_access_view *op = data;
2325 struct wined3d_unordered_access_view *view = op->view;
2326 struct wined3d_context *context;
2328 context = context_acquire(cs->device, NULL, 0);
2329 wined3d_unordered_access_view_clear_uint(view, &op->clear_value, context);
2330 context_release(context);
2332 wined3d_resource_release(view->resource);
2335 void wined3d_cs_emit_clear_unordered_access_view_uint(struct wined3d_cs *cs,
2336 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
2338 struct wined3d_cs_clear_unordered_access_view *op;
2340 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2341 op->opcode = WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW;
2342 op->view = view;
2343 op->clear_value = *clear_value;
2345 wined3d_resource_acquire(view->resource);
2347 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2350 static void wined3d_cs_exec_copy_uav_counter(struct wined3d_cs *cs, const void *data)
2352 const struct wined3d_cs_copy_uav_counter *op = data;
2353 struct wined3d_unordered_access_view *view = op->view;
2354 struct wined3d_context *context;
2356 context = context_acquire(cs->device, NULL, 0);
2357 wined3d_unordered_access_view_copy_counter(view, op->buffer, op->offset, context);
2358 context_release(context);
2360 wined3d_resource_release(&op->buffer->resource);
2363 void wined3d_cs_emit_copy_uav_counter(struct wined3d_cs *cs, struct wined3d_buffer *dst_buffer,
2364 unsigned int offset, struct wined3d_unordered_access_view *uav)
2366 struct wined3d_cs_copy_uav_counter *op;
2368 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2369 op->opcode = WINED3D_CS_OP_COPY_UAV_COUNTER;
2370 op->buffer = dst_buffer;
2371 op->offset = offset;
2372 op->view = uav;
2374 wined3d_resource_acquire(&dst_buffer->resource);
2376 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2379 static void wined3d_cs_exec_generate_mipmaps(struct wined3d_cs *cs, const void *data)
2381 const struct wined3d_cs_generate_mipmaps *op = data;
2382 struct wined3d_shader_resource_view *view = op->view;
2384 shader_resource_view_generate_mipmaps(view);
2385 wined3d_resource_release(view->resource);
2388 void wined3d_cs_emit_generate_mipmaps(struct wined3d_cs *cs, struct wined3d_shader_resource_view *view)
2390 struct wined3d_cs_generate_mipmaps *op;
2392 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2393 op->opcode = WINED3D_CS_OP_GENERATE_MIPMAPS;
2394 op->view = view;
2396 wined3d_resource_acquire(view->resource);
2398 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2401 static void wined3d_cs_emit_stop(struct wined3d_cs *cs)
2403 struct wined3d_cs_stop *op;
2405 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2406 op->opcode = WINED3D_CS_OP_STOP;
2408 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2409 cs->ops->finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2412 static void (* const wined3d_cs_op_handlers[])(struct wined3d_cs *cs, const void *data) =
2414 /* WINED3D_CS_OP_NOP */ wined3d_cs_exec_nop,
2415 /* WINED3D_CS_OP_PRESENT */ wined3d_cs_exec_present,
2416 /* WINED3D_CS_OP_CLEAR */ wined3d_cs_exec_clear,
2417 /* WINED3D_CS_OP_DISPATCH */ wined3d_cs_exec_dispatch,
2418 /* WINED3D_CS_OP_DRAW */ wined3d_cs_exec_draw,
2419 /* WINED3D_CS_OP_FLUSH */ wined3d_cs_exec_flush,
2420 /* WINED3D_CS_OP_SET_PREDICATION */ wined3d_cs_exec_set_predication,
2421 /* WINED3D_CS_OP_SET_VIEWPORTS */ wined3d_cs_exec_set_viewports,
2422 /* WINED3D_CS_OP_SET_SCISSOR_RECTS */ wined3d_cs_exec_set_scissor_rects,
2423 /* WINED3D_CS_OP_SET_RENDERTARGET_VIEW */ wined3d_cs_exec_set_rendertarget_view,
2424 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW */ wined3d_cs_exec_set_depth_stencil_view,
2425 /* WINED3D_CS_OP_SET_VERTEX_DECLARATION */ wined3d_cs_exec_set_vertex_declaration,
2426 /* WINED3D_CS_OP_SET_STREAM_SOURCE */ wined3d_cs_exec_set_stream_source,
2427 /* WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ */ wined3d_cs_exec_set_stream_source_freq,
2428 /* WINED3D_CS_OP_SET_STREAM_OUTPUT */ wined3d_cs_exec_set_stream_output,
2429 /* WINED3D_CS_OP_SET_INDEX_BUFFER */ wined3d_cs_exec_set_index_buffer,
2430 /* WINED3D_CS_OP_SET_CONSTANT_BUFFER */ wined3d_cs_exec_set_constant_buffer,
2431 /* WINED3D_CS_OP_SET_TEXTURE */ wined3d_cs_exec_set_texture,
2432 /* WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW */ wined3d_cs_exec_set_shader_resource_view,
2433 /* WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_set_unordered_access_view,
2434 /* WINED3D_CS_OP_SET_SAMPLER */ wined3d_cs_exec_set_sampler,
2435 /* WINED3D_CS_OP_SET_SHADER */ wined3d_cs_exec_set_shader,
2436 /* WINED3D_CS_OP_SET_BLEND_STATE */ wined3d_cs_exec_set_blend_state,
2437 /* WINED3D_CS_OP_SET_RASTERIZER_STATE */ wined3d_cs_exec_set_rasterizer_state,
2438 /* WINED3D_CS_OP_SET_RENDER_STATE */ wined3d_cs_exec_set_render_state,
2439 /* WINED3D_CS_OP_SET_TEXTURE_STATE */ wined3d_cs_exec_set_texture_state,
2440 /* WINED3D_CS_OP_SET_SAMPLER_STATE */ wined3d_cs_exec_set_sampler_state,
2441 /* WINED3D_CS_OP_SET_TRANSFORM */ wined3d_cs_exec_set_transform,
2442 /* WINED3D_CS_OP_SET_CLIP_PLANE */ wined3d_cs_exec_set_clip_plane,
2443 /* WINED3D_CS_OP_SET_COLOR_KEY */ wined3d_cs_exec_set_color_key,
2444 /* WINED3D_CS_OP_SET_MATERIAL */ wined3d_cs_exec_set_material,
2445 /* WINED3D_CS_OP_SET_LIGHT */ wined3d_cs_exec_set_light,
2446 /* WINED3D_CS_OP_SET_LIGHT_ENABLE */ wined3d_cs_exec_set_light_enable,
2447 /* WINED3D_CS_OP_PUSH_CONSTANTS */ wined3d_cs_exec_push_constants,
2448 /* WINED3D_CS_OP_RESET_STATE */ wined3d_cs_exec_reset_state,
2449 /* WINED3D_CS_OP_CALLBACK */ wined3d_cs_exec_callback,
2450 /* WINED3D_CS_OP_QUERY_ISSUE */ wined3d_cs_exec_query_issue,
2451 /* WINED3D_CS_OP_PRELOAD_RESOURCE */ wined3d_cs_exec_preload_resource,
2452 /* WINED3D_CS_OP_UNLOAD_RESOURCE */ wined3d_cs_exec_unload_resource,
2453 /* WINED3D_CS_OP_MAP */ wined3d_cs_exec_map,
2454 /* WINED3D_CS_OP_UNMAP */ wined3d_cs_exec_unmap,
2455 /* WINED3D_CS_OP_BLT_SUB_RESOURCE */ wined3d_cs_exec_blt_sub_resource,
2456 /* WINED3D_CS_OP_UPDATE_SUB_RESOURCE */ wined3d_cs_exec_update_sub_resource,
2457 /* WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION */ wined3d_cs_exec_add_dirty_texture_region,
2458 /* WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_clear_unordered_access_view,
2459 /* WINED3D_CS_OP_COPY_UAV_COUNTER */ wined3d_cs_exec_copy_uav_counter,
2460 /* WINED3D_CS_OP_GENERATE_MIPMAPS */ wined3d_cs_exec_generate_mipmaps,
2463 static void *wined3d_cs_st_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2465 if (size > (cs->data_size - cs->end))
2467 size_t new_size;
2468 void *new_data;
2470 new_size = max(size, cs->data_size * 2);
2471 if (!cs->end)
2472 new_data = heap_realloc(cs->data, new_size);
2473 else
2474 new_data = heap_alloc(new_size);
2475 if (!new_data)
2476 return NULL;
2478 cs->data_size = new_size;
2479 cs->start = cs->end = 0;
2480 cs->data = new_data;
2483 cs->end += size;
2485 return (BYTE *)cs->data + cs->start;
2488 static void wined3d_cs_st_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2490 enum wined3d_cs_op opcode;
2491 size_t start;
2492 BYTE *data;
2494 data = cs->data;
2495 start = cs->start;
2496 cs->start = cs->end;
2498 opcode = *(const enum wined3d_cs_op *)&data[start];
2499 if (opcode >= WINED3D_CS_OP_STOP)
2500 ERR("Invalid opcode %#x.\n", opcode);
2501 else
2502 wined3d_cs_op_handlers[opcode](cs, &data[start]);
2504 if (cs->data == data)
2505 cs->start = cs->end = start;
2506 else if (!start)
2507 heap_free(data);
2510 static void wined3d_cs_st_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2514 static const struct wined3d_cs_ops wined3d_cs_st_ops =
2516 wined3d_cs_st_require_space,
2517 wined3d_cs_st_submit,
2518 wined3d_cs_st_finish,
2519 wined3d_cs_st_push_constants,
2522 static BOOL wined3d_cs_queue_is_empty(const struct wined3d_cs *cs, const struct wined3d_cs_queue *queue)
2524 wined3d_from_cs(cs);
2525 return *(volatile LONG *)&queue->head == queue->tail;
2528 static void wined3d_cs_queue_submit(struct wined3d_cs_queue *queue, struct wined3d_cs *cs)
2530 struct wined3d_cs_packet *packet;
2531 size_t packet_size;
2533 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2534 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2535 InterlockedExchange(&queue->head, (queue->head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1));
2537 if (InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2538 SetEvent(cs->event);
2541 static void wined3d_cs_mt_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2543 if (cs->thread_id == GetCurrentThreadId())
2544 return wined3d_cs_st_submit(cs, queue_id);
2546 wined3d_cs_queue_submit(&cs->queue[queue_id], cs);
2549 static void *wined3d_cs_queue_require_space(struct wined3d_cs_queue *queue, size_t size, struct wined3d_cs *cs)
2551 size_t queue_size = ARRAY_SIZE(queue->data);
2552 size_t header_size, packet_size, remaining;
2553 struct wined3d_cs_packet *packet;
2555 header_size = FIELD_OFFSET(struct wined3d_cs_packet, data[0]);
2556 size = (size + header_size - 1) & ~(header_size - 1);
2557 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[size]);
2558 if (packet_size >= WINED3D_CS_QUEUE_SIZE)
2560 ERR("Packet size %lu >= queue size %u.\n",
2561 (unsigned long)packet_size, WINED3D_CS_QUEUE_SIZE);
2562 return NULL;
2565 remaining = queue_size - queue->head;
2566 if (remaining < packet_size)
2568 size_t nop_size = remaining - header_size;
2569 struct wined3d_cs_nop *nop;
2571 TRACE("Inserting a nop for %lu + %lu bytes.\n",
2572 (unsigned long)header_size, (unsigned long)nop_size);
2574 nop = wined3d_cs_queue_require_space(queue, nop_size, cs);
2575 if (nop_size)
2576 nop->opcode = WINED3D_CS_OP_NOP;
2578 wined3d_cs_queue_submit(queue, cs);
2579 assert(!queue->head);
2582 for (;;)
2584 LONG tail = *(volatile LONG *)&queue->tail;
2585 LONG head = queue->head;
2586 LONG new_pos;
2588 /* Empty. */
2589 if (head == tail)
2590 break;
2591 new_pos = (head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1);
2592 /* Head ahead of tail. We checked the remaining size above, so we only
2593 * need to make sure we don't make head equal to tail. */
2594 if (head > tail && (new_pos != tail))
2595 break;
2596 /* Tail ahead of head. Make sure the new head is before the tail as
2597 * well. Note that new_pos is 0 when it's at the end of the queue. */
2598 if (new_pos < tail && new_pos)
2599 break;
2601 TRACE("Waiting for free space. Head %u, tail %u, packet size %lu.\n",
2602 head, tail, (unsigned long)packet_size);
2605 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2606 packet->size = size;
2607 return packet->data;
2610 static void *wined3d_cs_mt_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2612 if (cs->thread_id == GetCurrentThreadId())
2613 return wined3d_cs_st_require_space(cs, size, queue_id);
2615 return wined3d_cs_queue_require_space(&cs->queue[queue_id], size, cs);
2618 static void wined3d_cs_mt_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2620 if (cs->thread_id == GetCurrentThreadId())
2621 return wined3d_cs_st_finish(cs, queue_id);
2623 while (cs->queue[queue_id].head != *(volatile LONG *)&cs->queue[queue_id].tail)
2624 wined3d_pause();
2627 static const struct wined3d_cs_ops wined3d_cs_mt_ops =
2629 wined3d_cs_mt_require_space,
2630 wined3d_cs_mt_submit,
2631 wined3d_cs_mt_finish,
2632 wined3d_cs_mt_push_constants,
2635 static void poll_queries(struct wined3d_cs *cs)
2637 struct wined3d_query *query, *cursor;
2639 LIST_FOR_EACH_ENTRY_SAFE(query, cursor, &cs->query_poll_list, struct wined3d_query, poll_list_entry)
2641 if (!query->query_ops->query_poll(query, 0))
2642 continue;
2644 list_remove(&query->poll_list_entry);
2645 list_init(&query->poll_list_entry);
2646 InterlockedIncrement(&query->counter_retrieved);
2650 static void wined3d_cs_wait_event(struct wined3d_cs *cs)
2652 InterlockedExchange(&cs->waiting_for_event, TRUE);
2654 /* The main thread might have enqueued a command and blocked on it after
2655 * the CS thread decided to enter wined3d_cs_wait_event(), but before
2656 * "waiting_for_event" was set.
2658 * Likewise, we can race with the main thread when resetting
2659 * "waiting_for_event", in which case we would need to call
2660 * WaitForSingleObject() because the main thread called SetEvent(). */
2661 if (!(wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_DEFAULT])
2662 && wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_MAP]))
2663 && InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2664 return;
2666 WaitForSingleObject(cs->event, INFINITE);
2669 static DWORD WINAPI wined3d_cs_run(void *ctx)
2671 struct wined3d_cs_packet *packet;
2672 struct wined3d_cs_queue *queue;
2673 unsigned int spin_count = 0;
2674 struct wined3d_cs *cs = ctx;
2675 enum wined3d_cs_op opcode;
2676 HMODULE wined3d_module;
2677 unsigned int poll = 0;
2678 LONG tail;
2680 TRACE("Started.\n");
2682 /* Copy the module handle to a local variable to avoid racing with the
2683 * thread freeing "cs" before the FreeLibraryAndExitThread() call. */
2684 wined3d_module = cs->wined3d_module;
2686 list_init(&cs->query_poll_list);
2687 cs->thread_id = GetCurrentThreadId();
2688 for (;;)
2690 if (++poll == WINED3D_CS_QUERY_POLL_INTERVAL)
2692 poll_queries(cs);
2693 poll = 0;
2696 queue = &cs->queue[WINED3D_CS_QUEUE_MAP];
2697 if (wined3d_cs_queue_is_empty(cs, queue))
2699 queue = &cs->queue[WINED3D_CS_QUEUE_DEFAULT];
2700 if (wined3d_cs_queue_is_empty(cs, queue))
2702 if (++spin_count >= WINED3D_CS_SPIN_COUNT && list_empty(&cs->query_poll_list))
2703 wined3d_cs_wait_event(cs);
2704 continue;
2707 spin_count = 0;
2709 tail = queue->tail;
2710 packet = (struct wined3d_cs_packet *)&queue->data[tail];
2711 if (packet->size)
2713 opcode = *(const enum wined3d_cs_op *)packet->data;
2715 if (opcode >= WINED3D_CS_OP_STOP)
2717 if (opcode > WINED3D_CS_OP_STOP)
2718 ERR("Invalid opcode %#x.\n", opcode);
2719 break;
2722 wined3d_cs_op_handlers[opcode](cs, packet->data);
2725 tail += FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2726 tail &= (WINED3D_CS_QUEUE_SIZE - 1);
2727 InterlockedExchange(&queue->tail, tail);
2730 cs->queue[WINED3D_CS_QUEUE_MAP].tail = cs->queue[WINED3D_CS_QUEUE_MAP].head;
2731 cs->queue[WINED3D_CS_QUEUE_DEFAULT].tail = cs->queue[WINED3D_CS_QUEUE_DEFAULT].head;
2732 TRACE("Stopped.\n");
2733 FreeLibraryAndExitThread(wined3d_module, 0);
2736 struct wined3d_cs *wined3d_cs_create(struct wined3d_device *device)
2738 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2739 struct wined3d_cs *cs;
2741 if (!(cs = heap_alloc_zero(sizeof(*cs))))
2742 return NULL;
2744 cs->ops = &wined3d_cs_st_ops;
2745 cs->device = device;
2747 state_init(&cs->state, &cs->fb, gl_info, &device->adapter->d3d_info,
2748 WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
2750 cs->data_size = WINED3D_INITIAL_CS_SIZE;
2751 if (!(cs->data = heap_alloc(cs->data_size)))
2752 goto fail;
2754 if (wined3d_settings.cs_multithreaded
2755 && !RtlIsCriticalSectionLockedByThread(NtCurrentTeb()->Peb->LoaderLock))
2757 cs->ops = &wined3d_cs_mt_ops;
2759 if (!(cs->event = CreateEventW(NULL, FALSE, FALSE, NULL)))
2761 ERR("Failed to create command stream event.\n");
2762 heap_free(cs->data);
2763 goto fail;
2766 if (!(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2767 (const WCHAR *)wined3d_cs_run, &cs->wined3d_module)))
2769 ERR("Failed to get wined3d module handle.\n");
2770 CloseHandle(cs->event);
2771 heap_free(cs->data);
2772 goto fail;
2775 if (!(cs->thread = CreateThread(NULL, 0, wined3d_cs_run, cs, 0, NULL)))
2777 ERR("Failed to create wined3d command stream thread.\n");
2778 FreeLibrary(cs->wined3d_module);
2779 CloseHandle(cs->event);
2780 heap_free(cs->data);
2781 goto fail;
2785 return cs;
2787 fail:
2788 state_cleanup(&cs->state);
2789 heap_free(cs);
2790 return NULL;
2793 void wined3d_cs_destroy(struct wined3d_cs *cs)
2795 if (cs->thread)
2797 wined3d_cs_emit_stop(cs);
2798 CloseHandle(cs->thread);
2799 if (!CloseHandle(cs->event))
2800 ERR("Closing event failed.\n");
2803 state_cleanup(&cs->state);
2804 heap_free(cs->data);
2805 heap_free(cs);