wined3d: Add support for stream output with vertex shaders.
[wine.git] / dlls / wined3d / cs.c
blobb0572ab47c20dd60cf97c97e63ee25e3ca1a2aae
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 const struct wined3d_shader *geometry_shader;
783 struct wined3d_state *state = &cs->state;
784 const struct wined3d_cs_draw *op = data;
785 int load_base_vertex_idx;
786 unsigned int i;
788 /* ARB_draw_indirect always supports a base vertex offset. */
789 if (!op->parameters.indirect && !gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
790 load_base_vertex_idx = op->parameters.u.direct.base_vertex_idx;
791 else
792 load_base_vertex_idx = 0;
794 if (state->load_base_vertex_index != load_base_vertex_idx)
796 state->load_base_vertex_index = load_base_vertex_idx;
797 device_invalidate_state(cs->device, STATE_BASEVERTEXINDEX);
800 if (state->gl_primitive_type != op->primitive_type)
802 if ((geometry_shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]) && !geometry_shader->function)
803 device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY));
804 if (state->gl_primitive_type == GL_POINTS || op->primitive_type == GL_POINTS)
805 device_invalidate_state(cs->device, STATE_POINT_ENABLE);
806 state->gl_primitive_type = op->primitive_type;
808 state->gl_patch_vertices = op->patch_vertex_count;
810 draw_primitive(cs->device, state, &op->parameters);
812 if (op->parameters.indirect)
814 struct wined3d_buffer *buffer = op->parameters.u.indirect.buffer;
815 wined3d_resource_release(&buffer->resource);
818 if (op->parameters.indexed)
819 wined3d_resource_release(&state->index_buffer->resource);
820 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
822 if (state->streams[i].buffer)
823 wined3d_resource_release(&state->streams[i].buffer->resource);
825 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
827 if (state->stream_output[i].buffer)
828 wined3d_resource_release(&state->stream_output[i].buffer->resource);
830 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
832 if (state->textures[i])
833 wined3d_resource_release(&state->textures[i]->resource);
835 for (i = 0; i < gl_info->limits.buffers; ++i)
837 if (state->fb->render_targets[i])
838 wined3d_resource_release(state->fb->render_targets[i]->resource);
840 if (state->fb->depth_stencil)
841 wined3d_resource_release(state->fb->depth_stencil->resource);
842 release_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
843 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
844 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
847 static void acquire_graphics_pipeline_resources(const struct wined3d_state *state,
848 BOOL indexed, const struct wined3d_gl_info *gl_info)
850 unsigned int i;
852 if (indexed)
853 wined3d_resource_acquire(&state->index_buffer->resource);
854 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
856 if (state->streams[i].buffer)
857 wined3d_resource_acquire(&state->streams[i].buffer->resource);
859 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
861 if (state->stream_output[i].buffer)
862 wined3d_resource_acquire(&state->stream_output[i].buffer->resource);
864 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
866 if (state->textures[i])
867 wined3d_resource_acquire(&state->textures[i]->resource);
869 for (i = 0; i < gl_info->limits.buffers; ++i)
871 if (state->fb->render_targets[i])
872 wined3d_resource_acquire(state->fb->render_targets[i]->resource);
874 if (state->fb->depth_stencil)
875 wined3d_resource_acquire(state->fb->depth_stencil->resource);
876 acquire_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
877 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
878 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
881 void wined3d_cs_emit_draw(struct wined3d_cs *cs, GLenum primitive_type, unsigned int patch_vertex_count,
882 int base_vertex_idx, unsigned int start_idx, unsigned int index_count,
883 unsigned int start_instance, unsigned int instance_count, BOOL indexed)
885 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
886 const struct wined3d_state *state = &cs->device->state;
887 struct wined3d_cs_draw *op;
889 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
890 op->opcode = WINED3D_CS_OP_DRAW;
891 op->primitive_type = primitive_type;
892 op->patch_vertex_count = patch_vertex_count;
893 op->parameters.indirect = FALSE;
894 op->parameters.u.direct.base_vertex_idx = base_vertex_idx;
895 op->parameters.u.direct.start_idx = start_idx;
896 op->parameters.u.direct.index_count = index_count;
897 op->parameters.u.direct.start_instance = start_instance;
898 op->parameters.u.direct.instance_count = instance_count;
899 op->parameters.indexed = indexed;
901 acquire_graphics_pipeline_resources(state, indexed, gl_info);
903 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
906 void wined3d_cs_emit_draw_indirect(struct wined3d_cs *cs, GLenum primitive_type, unsigned int patch_vertex_count,
907 struct wined3d_buffer *buffer, unsigned int offset, BOOL indexed)
909 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
910 const struct wined3d_state *state = &cs->device->state;
911 struct wined3d_cs_draw *op;
913 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
914 op->opcode = WINED3D_CS_OP_DRAW;
915 op->primitive_type = primitive_type;
916 op->patch_vertex_count = patch_vertex_count;
917 op->parameters.indirect = TRUE;
918 op->parameters.u.indirect.buffer = buffer;
919 op->parameters.u.indirect.offset = offset;
920 op->parameters.indexed = indexed;
922 acquire_graphics_pipeline_resources(state, indexed, gl_info);
923 wined3d_resource_acquire(&buffer->resource);
925 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
928 static void wined3d_cs_exec_flush(struct wined3d_cs *cs, const void *data)
930 struct wined3d_context *context;
932 context = context_acquire(cs->device, NULL, 0);
933 if (context->valid)
934 context->gl_info->gl_ops.gl.p_glFlush();
935 context_release(context);
938 void wined3d_cs_emit_flush(struct wined3d_cs *cs)
940 struct wined3d_cs_flush *op;
942 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
943 op->opcode = WINED3D_CS_OP_FLUSH;
945 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
946 cs->queries_flushed = TRUE;
949 static void wined3d_cs_exec_set_predication(struct wined3d_cs *cs, const void *data)
951 const struct wined3d_cs_set_predication *op = data;
953 cs->state.predicate = op->predicate;
954 cs->state.predicate_value = op->value;
957 void wined3d_cs_emit_set_predication(struct wined3d_cs *cs, struct wined3d_query *predicate, BOOL value)
959 struct wined3d_cs_set_predication *op;
961 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
962 op->opcode = WINED3D_CS_OP_SET_PREDICATION;
963 op->predicate = predicate;
964 op->value = value;
966 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
969 static void wined3d_cs_exec_set_viewports(struct wined3d_cs *cs, const void *data)
971 const struct wined3d_cs_set_viewports *op = data;
973 if (op->viewport_count)
974 memcpy(cs->state.viewports, op->viewports, op->viewport_count * sizeof(*op->viewports));
975 else
976 memset(cs->state.viewports, 0, sizeof(*cs->state.viewports));
977 cs->state.viewport_count = op->viewport_count;
978 device_invalidate_state(cs->device, STATE_VIEWPORT);
981 void wined3d_cs_emit_set_viewports(struct wined3d_cs *cs, unsigned int viewport_count,
982 const struct wined3d_viewport *viewports)
984 struct wined3d_cs_set_viewports *op;
986 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_set_viewports, viewports[viewport_count]),
987 WINED3D_CS_QUEUE_DEFAULT);
988 op->opcode = WINED3D_CS_OP_SET_VIEWPORTS;
989 if (viewport_count)
990 memcpy(op->viewports, viewports, viewport_count * sizeof(*viewports));
991 op->viewport_count = viewport_count;
993 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
996 static void wined3d_cs_exec_set_scissor_rects(struct wined3d_cs *cs, const void *data)
998 const struct wined3d_cs_set_scissor_rects *op = data;
1000 if (op->rect_count)
1001 memcpy(cs->state.scissor_rects, op->rects, op->rect_count * sizeof(*op->rects));
1002 else
1003 SetRectEmpty(cs->state.scissor_rects);
1004 cs->state.scissor_rect_count = op->rect_count;
1005 device_invalidate_state(cs->device, STATE_SCISSORRECT);
1008 void wined3d_cs_emit_set_scissor_rects(struct wined3d_cs *cs, unsigned int rect_count, const RECT *rects)
1010 struct wined3d_cs_set_scissor_rects *op;
1012 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_set_scissor_rects, rects[rect_count]),
1013 WINED3D_CS_QUEUE_DEFAULT);
1014 op->opcode = WINED3D_CS_OP_SET_SCISSOR_RECTS;
1015 if (rect_count)
1016 memcpy(op->rects, rects, rect_count * sizeof(*rects));
1017 op->rect_count = rect_count;
1019 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1022 static void wined3d_cs_exec_set_rendertarget_view(struct wined3d_cs *cs, const void *data)
1024 const struct wined3d_cs_set_rendertarget_view *op = data;
1026 cs->fb.render_targets[op->view_idx] = op->view;
1027 device_invalidate_state(cs->device, STATE_FRAMEBUFFER);
1030 void wined3d_cs_emit_set_rendertarget_view(struct wined3d_cs *cs, unsigned int view_idx,
1031 struct wined3d_rendertarget_view *view)
1033 struct wined3d_cs_set_rendertarget_view *op;
1035 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1036 op->opcode = WINED3D_CS_OP_SET_RENDERTARGET_VIEW;
1037 op->view_idx = view_idx;
1038 op->view = view;
1040 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1043 static void wined3d_cs_exec_set_depth_stencil_view(struct wined3d_cs *cs, const void *data)
1045 const struct wined3d_cs_set_depth_stencil_view *op = data;
1046 struct wined3d_device *device = cs->device;
1047 struct wined3d_rendertarget_view *prev;
1049 if ((prev = cs->state.fb->depth_stencil) && prev->resource->type != WINED3D_RTYPE_BUFFER)
1051 struct wined3d_texture *prev_texture = texture_from_resource(prev->resource);
1053 if (device->swapchains[0]->desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
1054 || prev_texture->flags & WINED3D_TEXTURE_DISCARD)
1055 wined3d_texture_validate_location(prev_texture,
1056 prev->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
1059 cs->fb.depth_stencil = op->view;
1061 if (!prev != !op->view)
1063 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
1064 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
1065 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
1066 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
1067 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
1069 else if (prev && prev->format->depth_bias_scale != op->view->format->depth_bias_scale)
1071 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
1074 device_invalidate_state(device, STATE_FRAMEBUFFER);
1077 void wined3d_cs_emit_set_depth_stencil_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view)
1079 struct wined3d_cs_set_depth_stencil_view *op;
1081 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1082 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW;
1083 op->view = view;
1085 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1088 static void wined3d_cs_exec_set_vertex_declaration(struct wined3d_cs *cs, const void *data)
1090 const struct wined3d_cs_set_vertex_declaration *op = data;
1092 cs->state.vertex_declaration = op->declaration;
1093 device_invalidate_state(cs->device, STATE_VDECL);
1096 void wined3d_cs_emit_set_vertex_declaration(struct wined3d_cs *cs, struct wined3d_vertex_declaration *declaration)
1098 struct wined3d_cs_set_vertex_declaration *op;
1100 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1101 op->opcode = WINED3D_CS_OP_SET_VERTEX_DECLARATION;
1102 op->declaration = declaration;
1104 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1107 static void wined3d_cs_exec_set_stream_source(struct wined3d_cs *cs, const void *data)
1109 const struct wined3d_cs_set_stream_source *op = data;
1110 struct wined3d_stream_state *stream;
1111 struct wined3d_buffer *prev;
1113 stream = &cs->state.streams[op->stream_idx];
1114 prev = stream->buffer;
1115 stream->buffer = op->buffer;
1116 stream->offset = op->offset;
1117 stream->stride = op->stride;
1119 if (op->buffer)
1120 InterlockedIncrement(&op->buffer->resource.bind_count);
1121 if (prev)
1122 InterlockedDecrement(&prev->resource.bind_count);
1124 device_invalidate_state(cs->device, STATE_STREAMSRC);
1127 void wined3d_cs_emit_set_stream_source(struct wined3d_cs *cs, UINT stream_idx,
1128 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1130 struct wined3d_cs_set_stream_source *op;
1132 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1133 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE;
1134 op->stream_idx = stream_idx;
1135 op->buffer = buffer;
1136 op->offset = offset;
1137 op->stride = stride;
1139 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1142 static void wined3d_cs_exec_set_stream_source_freq(struct wined3d_cs *cs, const void *data)
1144 const struct wined3d_cs_set_stream_source_freq *op = data;
1145 struct wined3d_stream_state *stream;
1147 stream = &cs->state.streams[op->stream_idx];
1148 stream->frequency = op->frequency;
1149 stream->flags = op->flags;
1151 device_invalidate_state(cs->device, STATE_STREAMSRC);
1154 void wined3d_cs_emit_set_stream_source_freq(struct wined3d_cs *cs, UINT stream_idx, UINT frequency, UINT flags)
1156 struct wined3d_cs_set_stream_source_freq *op;
1158 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1159 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ;
1160 op->stream_idx = stream_idx;
1161 op->frequency = frequency;
1162 op->flags = flags;
1164 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1167 static void wined3d_cs_exec_set_stream_output(struct wined3d_cs *cs, const void *data)
1169 const struct wined3d_cs_set_stream_output *op = data;
1170 struct wined3d_stream_output *stream;
1171 struct wined3d_buffer *prev;
1173 stream = &cs->state.stream_output[op->stream_idx];
1174 prev = stream->buffer;
1175 stream->buffer = op->buffer;
1176 stream->offset = op->offset;
1178 if (op->buffer)
1179 InterlockedIncrement(&op->buffer->resource.bind_count);
1180 if (prev)
1181 InterlockedDecrement(&prev->resource.bind_count);
1183 device_invalidate_state(cs->device, STATE_STREAM_OUTPUT);
1186 void wined3d_cs_emit_set_stream_output(struct wined3d_cs *cs, UINT stream_idx,
1187 struct wined3d_buffer *buffer, UINT offset)
1189 struct wined3d_cs_set_stream_output *op;
1191 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1192 op->opcode = WINED3D_CS_OP_SET_STREAM_OUTPUT;
1193 op->stream_idx = stream_idx;
1194 op->buffer = buffer;
1195 op->offset = offset;
1197 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1200 static void wined3d_cs_exec_set_index_buffer(struct wined3d_cs *cs, const void *data)
1202 const struct wined3d_cs_set_index_buffer *op = data;
1203 struct wined3d_buffer *prev;
1205 prev = cs->state.index_buffer;
1206 cs->state.index_buffer = op->buffer;
1207 cs->state.index_format = op->format_id;
1208 cs->state.index_offset = op->offset;
1210 if (op->buffer)
1211 InterlockedIncrement(&op->buffer->resource.bind_count);
1212 if (prev)
1213 InterlockedDecrement(&prev->resource.bind_count);
1215 device_invalidate_state(cs->device, STATE_INDEXBUFFER);
1218 void wined3d_cs_emit_set_index_buffer(struct wined3d_cs *cs, struct wined3d_buffer *buffer,
1219 enum wined3d_format_id format_id, unsigned int offset)
1221 struct wined3d_cs_set_index_buffer *op;
1223 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1224 op->opcode = WINED3D_CS_OP_SET_INDEX_BUFFER;
1225 op->buffer = buffer;
1226 op->format_id = format_id;
1227 op->offset = offset;
1229 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1232 static void wined3d_cs_exec_set_constant_buffer(struct wined3d_cs *cs, const void *data)
1234 const struct wined3d_cs_set_constant_buffer *op = data;
1235 struct wined3d_buffer *prev;
1237 prev = cs->state.cb[op->type][op->cb_idx];
1238 cs->state.cb[op->type][op->cb_idx] = op->buffer;
1240 if (op->buffer)
1241 InterlockedIncrement(&op->buffer->resource.bind_count);
1242 if (prev)
1243 InterlockedDecrement(&prev->resource.bind_count);
1245 device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(op->type));
1248 void wined3d_cs_emit_set_constant_buffer(struct wined3d_cs *cs, enum wined3d_shader_type type,
1249 UINT cb_idx, struct wined3d_buffer *buffer)
1251 struct wined3d_cs_set_constant_buffer *op;
1253 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1254 op->opcode = WINED3D_CS_OP_SET_CONSTANT_BUFFER;
1255 op->type = type;
1256 op->cb_idx = cb_idx;
1257 op->buffer = buffer;
1259 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1262 static void wined3d_cs_exec_set_texture(struct wined3d_cs *cs, const void *data)
1264 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
1265 const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
1266 const struct wined3d_cs_set_texture *op = data;
1267 struct wined3d_texture *prev;
1268 BOOL old_use_color_key = FALSE, new_use_color_key = FALSE;
1270 prev = cs->state.textures[op->stage];
1271 cs->state.textures[op->stage] = op->texture;
1273 if (op->texture)
1275 const struct wined3d_format *new_format = op->texture->resource.format;
1276 const struct wined3d_format *old_format = prev ? prev->resource.format : NULL;
1277 unsigned int old_fmt_flags = prev ? prev->resource.format_flags : 0;
1278 unsigned int new_fmt_flags = op->texture->resource.format_flags;
1280 if (InterlockedIncrement(&op->texture->resource.bind_count) == 1)
1281 op->texture->sampler = op->stage;
1283 if (!prev || op->texture->target != prev->target
1284 || (!is_same_fixup(new_format->color_fixup, old_format->color_fixup)
1285 && !(can_use_texture_swizzle(gl_info, new_format) && can_use_texture_swizzle(gl_info, old_format)))
1286 || (new_fmt_flags & WINED3DFMT_FLAG_SHADOW) != (old_fmt_flags & WINED3DFMT_FLAG_SHADOW))
1287 device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1289 if (!prev && op->stage < d3d_info->limits.ffp_blend_stages)
1291 /* The source arguments for color and alpha ops have different
1292 * meanings when a NULL texture is bound, so the COLOR_OP and
1293 * ALPHA_OP have to be dirtified. */
1294 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1295 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1298 if (!op->stage && op->texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1299 new_use_color_key = TRUE;
1302 if (prev)
1304 if (InterlockedDecrement(&prev->resource.bind_count) && prev->sampler == op->stage)
1306 unsigned int i;
1308 /* Search for other stages the texture is bound to. Shouldn't
1309 * happen if applications bind textures to a single stage only. */
1310 TRACE("Searching for other stages the texture is bound to.\n");
1311 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
1313 if (cs->state.textures[i] == prev)
1315 TRACE("Texture is also bound to stage %u.\n", i);
1316 prev->sampler = i;
1317 break;
1322 if (!op->texture && op->stage < d3d_info->limits.ffp_blend_stages)
1324 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1325 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1328 if (!op->stage && prev->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1329 old_use_color_key = TRUE;
1332 device_invalidate_state(cs->device, STATE_SAMPLER(op->stage));
1334 if (new_use_color_key != old_use_color_key)
1335 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1337 if (new_use_color_key)
1338 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1341 void wined3d_cs_emit_set_texture(struct wined3d_cs *cs, UINT stage, struct wined3d_texture *texture)
1343 struct wined3d_cs_set_texture *op;
1345 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1346 op->opcode = WINED3D_CS_OP_SET_TEXTURE;
1347 op->stage = stage;
1348 op->texture = texture;
1350 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1353 static void wined3d_cs_exec_set_shader_resource_view(struct wined3d_cs *cs, const void *data)
1355 const struct wined3d_cs_set_shader_resource_view *op = data;
1356 struct wined3d_shader_resource_view *prev;
1358 prev = cs->state.shader_resource_view[op->type][op->view_idx];
1359 cs->state.shader_resource_view[op->type][op->view_idx] = op->view;
1361 if (op->view)
1362 InterlockedIncrement(&op->view->resource->bind_count);
1363 if (prev)
1364 InterlockedDecrement(&prev->resource->bind_count);
1366 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1367 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1368 else
1369 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1372 void wined3d_cs_emit_set_shader_resource_view(struct wined3d_cs *cs, enum wined3d_shader_type type,
1373 UINT view_idx, struct wined3d_shader_resource_view *view)
1375 struct wined3d_cs_set_shader_resource_view *op;
1377 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1378 op->opcode = WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW;
1379 op->type = type;
1380 op->view_idx = view_idx;
1381 op->view = view;
1383 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1386 static void wined3d_cs_exec_set_unordered_access_view(struct wined3d_cs *cs, const void *data)
1388 const struct wined3d_cs_set_unordered_access_view *op = data;
1389 struct wined3d_unordered_access_view *prev;
1391 prev = cs->state.unordered_access_view[op->pipeline][op->view_idx];
1392 cs->state.unordered_access_view[op->pipeline][op->view_idx] = op->view;
1394 if (op->view)
1395 InterlockedIncrement(&op->view->resource->bind_count);
1396 if (prev)
1397 InterlockedDecrement(&prev->resource->bind_count);
1399 if (op->view && op->initial_count != ~0u)
1400 wined3d_unordered_access_view_set_counter(op->view, op->initial_count);
1402 device_invalidate_state(cs->device, STATE_UNORDERED_ACCESS_VIEW_BINDING(op->pipeline));
1405 void wined3d_cs_emit_set_unordered_access_view(struct wined3d_cs *cs, enum wined3d_pipeline pipeline,
1406 unsigned int view_idx, struct wined3d_unordered_access_view *view, unsigned int initial_count)
1408 struct wined3d_cs_set_unordered_access_view *op;
1410 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1411 op->opcode = WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW;
1412 op->pipeline = pipeline;
1413 op->view_idx = view_idx;
1414 op->view = view;
1415 op->initial_count = initial_count;
1417 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1420 static void wined3d_cs_exec_set_sampler(struct wined3d_cs *cs, const void *data)
1422 const struct wined3d_cs_set_sampler *op = data;
1424 cs->state.sampler[op->type][op->sampler_idx] = op->sampler;
1425 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1426 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1427 else
1428 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1431 void wined3d_cs_emit_set_sampler(struct wined3d_cs *cs, enum wined3d_shader_type type,
1432 UINT sampler_idx, struct wined3d_sampler *sampler)
1434 struct wined3d_cs_set_sampler *op;
1436 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1437 op->opcode = WINED3D_CS_OP_SET_SAMPLER;
1438 op->type = type;
1439 op->sampler_idx = sampler_idx;
1440 op->sampler = sampler;
1442 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1445 static void wined3d_cs_exec_set_shader(struct wined3d_cs *cs, const void *data)
1447 const struct wined3d_cs_set_shader *op = data;
1449 cs->state.shader[op->type] = op->shader;
1450 device_invalidate_state(cs->device, STATE_SHADER(op->type));
1451 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1452 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1453 else
1454 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1457 void wined3d_cs_emit_set_shader(struct wined3d_cs *cs, enum wined3d_shader_type type, struct wined3d_shader *shader)
1459 struct wined3d_cs_set_shader *op;
1461 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1462 op->opcode = WINED3D_CS_OP_SET_SHADER;
1463 op->type = type;
1464 op->shader = shader;
1466 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1469 static void wined3d_cs_exec_set_blend_state(struct wined3d_cs *cs, const void *data)
1471 const struct wined3d_cs_set_blend_state *op = data;
1473 cs->state.blend_state = op->state;
1474 device_invalidate_state(cs->device, STATE_BLEND);
1477 void wined3d_cs_emit_set_blend_state(struct wined3d_cs *cs, struct wined3d_blend_state *state)
1479 struct wined3d_cs_set_blend_state *op;
1481 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1482 op->opcode = WINED3D_CS_OP_SET_BLEND_STATE;
1483 op->state = state;
1485 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1488 static void wined3d_cs_exec_set_rasterizer_state(struct wined3d_cs *cs, const void *data)
1490 const struct wined3d_cs_set_rasterizer_state *op = data;
1492 cs->state.rasterizer_state = op->state;
1493 device_invalidate_state(cs->device, STATE_FRONTFACE);
1496 void wined3d_cs_emit_set_rasterizer_state(struct wined3d_cs *cs,
1497 struct wined3d_rasterizer_state *rasterizer_state)
1499 struct wined3d_cs_set_rasterizer_state *op;
1501 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1502 op->opcode = WINED3D_CS_OP_SET_RASTERIZER_STATE;
1503 op->state = rasterizer_state;
1505 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1508 static void wined3d_cs_exec_set_render_state(struct wined3d_cs *cs, const void *data)
1510 const struct wined3d_cs_set_render_state *op = data;
1512 cs->state.render_states[op->state] = op->value;
1513 device_invalidate_state(cs->device, STATE_RENDER(op->state));
1516 void wined3d_cs_emit_set_render_state(struct wined3d_cs *cs, enum wined3d_render_state state, DWORD value)
1518 struct wined3d_cs_set_render_state *op;
1520 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1521 op->opcode = WINED3D_CS_OP_SET_RENDER_STATE;
1522 op->state = state;
1523 op->value = value;
1525 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1528 static void wined3d_cs_exec_set_texture_state(struct wined3d_cs *cs, const void *data)
1530 const struct wined3d_cs_set_texture_state *op = data;
1532 cs->state.texture_states[op->stage][op->state] = op->value;
1533 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, op->state));
1536 void wined3d_cs_emit_set_texture_state(struct wined3d_cs *cs, UINT stage,
1537 enum wined3d_texture_stage_state state, DWORD value)
1539 struct wined3d_cs_set_texture_state *op;
1541 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1542 op->opcode = WINED3D_CS_OP_SET_TEXTURE_STATE;
1543 op->stage = stage;
1544 op->state = state;
1545 op->value = value;
1547 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1550 static void wined3d_cs_exec_set_sampler_state(struct wined3d_cs *cs, const void *data)
1552 const struct wined3d_cs_set_sampler_state *op = data;
1554 cs->state.sampler_states[op->sampler_idx][op->state] = op->value;
1555 device_invalidate_state(cs->device, STATE_SAMPLER(op->sampler_idx));
1558 void wined3d_cs_emit_set_sampler_state(struct wined3d_cs *cs, UINT sampler_idx,
1559 enum wined3d_sampler_state state, DWORD value)
1561 struct wined3d_cs_set_sampler_state *op;
1563 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1564 op->opcode = WINED3D_CS_OP_SET_SAMPLER_STATE;
1565 op->sampler_idx = sampler_idx;
1566 op->state = state;
1567 op->value = value;
1569 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1572 static void wined3d_cs_exec_set_transform(struct wined3d_cs *cs, const void *data)
1574 const struct wined3d_cs_set_transform *op = data;
1576 cs->state.transforms[op->state] = op->matrix;
1577 if (op->state < WINED3D_TS_WORLD_MATRIX(cs->device->adapter->d3d_info.limits.ffp_vertex_blend_matrices))
1578 device_invalidate_state(cs->device, STATE_TRANSFORM(op->state));
1581 void wined3d_cs_emit_set_transform(struct wined3d_cs *cs, enum wined3d_transform_state state,
1582 const struct wined3d_matrix *matrix)
1584 struct wined3d_cs_set_transform *op;
1586 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1587 op->opcode = WINED3D_CS_OP_SET_TRANSFORM;
1588 op->state = state;
1589 op->matrix = *matrix;
1591 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1594 static void wined3d_cs_exec_set_clip_plane(struct wined3d_cs *cs, const void *data)
1596 const struct wined3d_cs_set_clip_plane *op = data;
1598 cs->state.clip_planes[op->plane_idx] = op->plane;
1599 device_invalidate_state(cs->device, STATE_CLIPPLANE(op->plane_idx));
1602 void wined3d_cs_emit_set_clip_plane(struct wined3d_cs *cs, UINT plane_idx, const struct wined3d_vec4 *plane)
1604 struct wined3d_cs_set_clip_plane *op;
1606 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1607 op->opcode = WINED3D_CS_OP_SET_CLIP_PLANE;
1608 op->plane_idx = plane_idx;
1609 op->plane = *plane;
1611 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1614 static void wined3d_cs_exec_set_color_key(struct wined3d_cs *cs, const void *data)
1616 const struct wined3d_cs_set_color_key *op = data;
1617 struct wined3d_texture *texture = op->texture;
1619 if (op->set)
1621 switch (op->flags)
1623 case WINED3D_CKEY_DST_BLT:
1624 texture->async.dst_blt_color_key = op->color_key;
1625 texture->async.color_key_flags |= WINED3D_CKEY_DST_BLT;
1626 break;
1628 case WINED3D_CKEY_DST_OVERLAY:
1629 texture->async.dst_overlay_color_key = op->color_key;
1630 texture->async.color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
1631 break;
1633 case WINED3D_CKEY_SRC_BLT:
1634 if (texture == cs->state.textures[0])
1636 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1637 if (!(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1638 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1641 texture->async.src_blt_color_key = op->color_key;
1642 texture->async.color_key_flags |= WINED3D_CKEY_SRC_BLT;
1643 break;
1645 case WINED3D_CKEY_SRC_OVERLAY:
1646 texture->async.src_overlay_color_key = op->color_key;
1647 texture->async.color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
1648 break;
1651 else
1653 switch (op->flags)
1655 case WINED3D_CKEY_DST_BLT:
1656 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_BLT;
1657 break;
1659 case WINED3D_CKEY_DST_OVERLAY:
1660 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
1661 break;
1663 case WINED3D_CKEY_SRC_BLT:
1664 if (texture == cs->state.textures[0] && texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1665 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1667 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
1668 break;
1670 case WINED3D_CKEY_SRC_OVERLAY:
1671 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
1672 break;
1677 void wined3d_cs_emit_set_color_key(struct wined3d_cs *cs, struct wined3d_texture *texture,
1678 WORD flags, const struct wined3d_color_key *color_key)
1680 struct wined3d_cs_set_color_key *op;
1682 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1683 op->opcode = WINED3D_CS_OP_SET_COLOR_KEY;
1684 op->texture = texture;
1685 op->flags = flags;
1686 if (color_key)
1688 op->color_key = *color_key;
1689 op->set = 1;
1691 else
1692 op->set = 0;
1694 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1697 static void wined3d_cs_exec_set_material(struct wined3d_cs *cs, const void *data)
1699 const struct wined3d_cs_set_material *op = data;
1701 cs->state.material = op->material;
1702 device_invalidate_state(cs->device, STATE_MATERIAL);
1705 void wined3d_cs_emit_set_material(struct wined3d_cs *cs, const struct wined3d_material *material)
1707 struct wined3d_cs_set_material *op;
1709 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1710 op->opcode = WINED3D_CS_OP_SET_MATERIAL;
1711 op->material = *material;
1713 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1716 static void wined3d_cs_exec_set_light(struct wined3d_cs *cs, const void *data)
1718 const struct wined3d_cs_set_light *op = data;
1719 struct wined3d_light_info *light_info;
1720 unsigned int light_idx, hash_idx;
1722 light_idx = op->light.OriginalIndex;
1724 if (!(light_info = wined3d_state_get_light(&cs->state, light_idx)))
1726 TRACE("Adding new light.\n");
1727 if (!(light_info = heap_alloc_zero(sizeof(*light_info))))
1729 ERR("Failed to allocate light info.\n");
1730 return;
1733 hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1734 list_add_head(&cs->state.light_map[hash_idx], &light_info->entry);
1735 light_info->glIndex = -1;
1736 light_info->OriginalIndex = light_idx;
1739 if (light_info->glIndex != -1)
1741 if (light_info->OriginalParms.type != op->light.OriginalParms.type)
1742 device_invalidate_state(cs->device, STATE_LIGHT_TYPE);
1743 device_invalidate_state(cs->device, STATE_ACTIVELIGHT(light_info->glIndex));
1746 light_info->OriginalParms = op->light.OriginalParms;
1747 light_info->position = op->light.position;
1748 light_info->direction = op->light.direction;
1749 light_info->exponent = op->light.exponent;
1750 light_info->cutoff = op->light.cutoff;
1753 void wined3d_cs_emit_set_light(struct wined3d_cs *cs, const struct wined3d_light_info *light)
1755 struct wined3d_cs_set_light *op;
1757 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1758 op->opcode = WINED3D_CS_OP_SET_LIGHT;
1759 op->light = *light;
1761 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1764 static void wined3d_cs_exec_set_light_enable(struct wined3d_cs *cs, const void *data)
1766 const struct wined3d_cs_set_light_enable *op = data;
1767 struct wined3d_device *device = cs->device;
1768 struct wined3d_light_info *light_info;
1769 int prev_idx;
1771 if (!(light_info = wined3d_state_get_light(&cs->state, op->idx)))
1773 ERR("Light doesn't exist.\n");
1774 return;
1777 prev_idx = light_info->glIndex;
1778 wined3d_state_enable_light(&cs->state, &device->adapter->d3d_info, light_info, op->enable);
1779 if (light_info->glIndex != prev_idx)
1781 device_invalidate_state(device, STATE_LIGHT_TYPE);
1782 device_invalidate_state(device, STATE_ACTIVELIGHT(op->enable ? light_info->glIndex : prev_idx));
1786 void wined3d_cs_emit_set_light_enable(struct wined3d_cs *cs, unsigned int idx, BOOL enable)
1788 struct wined3d_cs_set_light_enable *op;
1790 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1791 op->opcode = WINED3D_CS_OP_SET_LIGHT_ENABLE;
1792 op->idx = idx;
1793 op->enable = enable;
1795 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1798 static const struct
1800 size_t offset;
1801 size_t size;
1802 DWORD mask;
1804 wined3d_cs_push_constant_info[] =
1806 /* WINED3D_PUSH_CONSTANTS_VS_F */
1807 {FIELD_OFFSET(struct wined3d_state, vs_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_VS_F},
1808 /* WINED3D_PUSH_CONSTANTS_PS_F */
1809 {FIELD_OFFSET(struct wined3d_state, ps_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_PS_F},
1810 /* WINED3D_PUSH_CONSTANTS_VS_I */
1811 {FIELD_OFFSET(struct wined3d_state, vs_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_VS_I},
1812 /* WINED3D_PUSH_CONSTANTS_PS_I */
1813 {FIELD_OFFSET(struct wined3d_state, ps_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_PS_I},
1814 /* WINED3D_PUSH_CONSTANTS_VS_B */
1815 {FIELD_OFFSET(struct wined3d_state, vs_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_VS_B},
1816 /* WINED3D_PUSH_CONSTANTS_PS_B */
1817 {FIELD_OFFSET(struct wined3d_state, ps_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_PS_B},
1820 static void wined3d_cs_st_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1821 unsigned int start_idx, unsigned int count, const void *constants)
1823 struct wined3d_device *device = cs->device;
1824 unsigned int context_count;
1825 unsigned int i;
1826 size_t offset;
1828 if (p == WINED3D_PUSH_CONSTANTS_VS_F)
1829 device->shader_backend->shader_update_float_vertex_constants(device, start_idx, count);
1830 else if (p == WINED3D_PUSH_CONSTANTS_PS_F)
1831 device->shader_backend->shader_update_float_pixel_constants(device, start_idx, count);
1833 offset = wined3d_cs_push_constant_info[p].offset + start_idx * wined3d_cs_push_constant_info[p].size;
1834 memcpy((BYTE *)&cs->state + offset, constants, count * wined3d_cs_push_constant_info[p].size);
1835 for (i = 0, context_count = device->context_count; i < context_count; ++i)
1837 device->contexts[i]->constant_update_mask |= wined3d_cs_push_constant_info[p].mask;
1841 static void wined3d_cs_exec_push_constants(struct wined3d_cs *cs, const void *data)
1843 const struct wined3d_cs_push_constants *op = data;
1845 wined3d_cs_st_push_constants(cs, op->type, op->start_idx, op->count, op->constants);
1848 static void wined3d_cs_mt_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1849 unsigned int start_idx, unsigned int count, const void *constants)
1851 struct wined3d_cs_push_constants *op;
1852 size_t size;
1854 size = count * wined3d_cs_push_constant_info[p].size;
1855 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_push_constants, constants[size]),
1856 WINED3D_CS_QUEUE_DEFAULT);
1857 op->opcode = WINED3D_CS_OP_PUSH_CONSTANTS;
1858 op->type = p;
1859 op->start_idx = start_idx;
1860 op->count = count;
1861 memcpy(op->constants, constants, size);
1863 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1866 static void wined3d_cs_exec_reset_state(struct wined3d_cs *cs, const void *data)
1868 struct wined3d_adapter *adapter = cs->device->adapter;
1870 state_cleanup(&cs->state);
1871 memset(&cs->state, 0, sizeof(cs->state));
1872 state_init(&cs->state, &cs->fb, &adapter->gl_info, &adapter->d3d_info,
1873 WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
1876 void wined3d_cs_emit_reset_state(struct wined3d_cs *cs)
1878 struct wined3d_cs_reset_state *op;
1880 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1881 op->opcode = WINED3D_CS_OP_RESET_STATE;
1883 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1886 static void wined3d_cs_exec_callback(struct wined3d_cs *cs, const void *data)
1888 const struct wined3d_cs_callback *op = data;
1890 op->callback(op->object);
1893 static void wined3d_cs_emit_callback(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1895 struct wined3d_cs_callback *op;
1897 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1898 op->opcode = WINED3D_CS_OP_CALLBACK;
1899 op->callback = callback;
1900 op->object = object;
1902 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1905 void wined3d_cs_destroy_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1907 wined3d_cs_emit_callback(cs, callback, object);
1910 void wined3d_cs_init_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1912 wined3d_cs_emit_callback(cs, callback, object);
1915 static void wined3d_cs_exec_query_issue(struct wined3d_cs *cs, const void *data)
1917 const struct wined3d_cs_query_issue *op = data;
1918 struct wined3d_query *query = op->query;
1919 BOOL poll;
1921 poll = query->query_ops->query_issue(query, op->flags);
1923 if (!cs->thread)
1924 return;
1926 if (poll && list_empty(&query->poll_list_entry))
1928 list_add_tail(&cs->query_poll_list, &query->poll_list_entry);
1929 return;
1932 /* This can happen if occlusion queries are restarted. This discards the
1933 * old result, since polling it could result in a GL error. */
1934 if ((op->flags & WINED3DISSUE_BEGIN) && !poll && !list_empty(&query->poll_list_entry))
1936 list_remove(&query->poll_list_entry);
1937 list_init(&query->poll_list_entry);
1938 InterlockedIncrement(&query->counter_retrieved);
1939 return;
1942 /* This can happen when an occlusion query is ended without being started,
1943 * in which case we don't want to poll, but still have to counter-balance
1944 * the increment of the main counter.
1946 * This can also happen if an event query is re-issued before the first
1947 * fence was reached. In this case the query is already in the list and
1948 * the poll function will check the new fence. We have to counter-balance
1949 * the discarded increment. */
1950 if (op->flags & WINED3DISSUE_END)
1951 InterlockedIncrement(&query->counter_retrieved);
1954 void wined3d_cs_emit_query_issue(struct wined3d_cs *cs, struct wined3d_query *query, DWORD flags)
1956 struct wined3d_cs_query_issue *op;
1958 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1959 op->opcode = WINED3D_CS_OP_QUERY_ISSUE;
1960 op->query = query;
1961 op->flags = flags;
1963 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1964 cs->queries_flushed = FALSE;
1967 static void wined3d_cs_exec_preload_resource(struct wined3d_cs *cs, const void *data)
1969 const struct wined3d_cs_preload_resource *op = data;
1970 struct wined3d_resource *resource = op->resource;
1972 resource->resource_ops->resource_preload(resource);
1973 wined3d_resource_release(resource);
1976 void wined3d_cs_emit_preload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
1978 struct wined3d_cs_preload_resource *op;
1980 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1981 op->opcode = WINED3D_CS_OP_PRELOAD_RESOURCE;
1982 op->resource = resource;
1984 wined3d_resource_acquire(resource);
1986 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1989 static void wined3d_cs_exec_unload_resource(struct wined3d_cs *cs, const void *data)
1991 const struct wined3d_cs_unload_resource *op = data;
1992 struct wined3d_resource *resource = op->resource;
1994 resource->resource_ops->resource_unload(resource);
1995 wined3d_resource_release(resource);
1998 void wined3d_cs_emit_unload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
2000 struct wined3d_cs_unload_resource *op;
2002 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2003 op->opcode = WINED3D_CS_OP_UNLOAD_RESOURCE;
2004 op->resource = resource;
2006 wined3d_resource_acquire(resource);
2008 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2011 static void wined3d_cs_exec_map(struct wined3d_cs *cs, const void *data)
2013 const struct wined3d_cs_map *op = data;
2014 struct wined3d_resource *resource = op->resource;
2016 *op->hr = resource->resource_ops->resource_sub_resource_map(resource,
2017 op->sub_resource_idx, op->map_desc, op->box, op->flags);
2020 HRESULT wined3d_cs_map(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx,
2021 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
2023 struct wined3d_cs_map *op;
2024 HRESULT hr;
2026 /* Mapping resources from the worker thread isn't an issue by itself, but
2027 * increasing the map count would be visible to applications. */
2028 wined3d_not_from_cs(cs);
2030 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2031 op->opcode = WINED3D_CS_OP_MAP;
2032 op->resource = resource;
2033 op->sub_resource_idx = sub_resource_idx;
2034 op->map_desc = map_desc;
2035 op->box = box;
2036 op->flags = flags;
2037 op->hr = &hr;
2039 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2040 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2042 return hr;
2045 static void wined3d_cs_exec_unmap(struct wined3d_cs *cs, const void *data)
2047 const struct wined3d_cs_unmap *op = data;
2048 struct wined3d_resource *resource = op->resource;
2050 *op->hr = resource->resource_ops->resource_sub_resource_unmap(resource, op->sub_resource_idx);
2053 HRESULT wined3d_cs_unmap(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx)
2055 struct wined3d_cs_unmap *op;
2056 HRESULT hr;
2058 wined3d_not_from_cs(cs);
2060 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2061 op->opcode = WINED3D_CS_OP_UNMAP;
2062 op->resource = resource;
2063 op->sub_resource_idx = sub_resource_idx;
2064 op->hr = &hr;
2066 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2067 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2069 return hr;
2072 static void wined3d_cs_exec_blt_sub_resource(struct wined3d_cs *cs, const void *data)
2074 const struct wined3d_cs_blt_sub_resource *op = data;
2076 if (op->dst_resource->type == WINED3D_RTYPE_BUFFER)
2078 wined3d_buffer_copy(buffer_from_resource(op->dst_resource), op->dst_box.left,
2079 buffer_from_resource(op->src_resource), op->src_box.left,
2080 op->src_box.right - op->src_box.left);
2082 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_2D)
2084 if (FAILED(texture2d_blt(texture_from_resource(op->dst_resource), op->dst_sub_resource_idx,
2085 &op->dst_box, texture_from_resource(op->src_resource), op->src_sub_resource_idx,
2086 &op->src_box, op->flags, &op->fx, op->filter)))
2087 FIXME("Blit failed.\n");
2089 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_3D)
2091 struct wined3d_texture *src_texture, *dst_texture;
2092 unsigned int level, update_w, update_h, update_d;
2093 unsigned int row_pitch, slice_pitch;
2094 struct wined3d_context *context;
2095 struct wined3d_bo_address addr;
2097 if (op->flags & ~WINED3D_BLT_RAW)
2099 FIXME("Flags %#x not implemented for %s resources.\n",
2100 op->flags, debug_d3dresourcetype(op->dst_resource->type));
2101 goto error;
2104 if (!(op->flags & WINED3D_BLT_RAW) && op->src_resource->format != op->dst_resource->format)
2106 FIXME("Format conversion not implemented for %s resources.\n",
2107 debug_d3dresourcetype(op->dst_resource->type));
2108 goto error;
2111 update_w = op->dst_box.right - op->dst_box.left;
2112 update_h = op->dst_box.bottom - op->dst_box.top;
2113 update_d = op->dst_box.back - op->dst_box.front;
2114 if (op->src_box.right - op->src_box.left != update_w
2115 || op->src_box.bottom - op->src_box.top != update_h
2116 || op->src_box.back - op->src_box.front != update_d)
2118 FIXME("Stretching not implemented for %s resources.\n",
2119 debug_d3dresourcetype(op->dst_resource->type));
2120 goto error;
2123 dst_texture = texture_from_resource(op->dst_resource);
2124 src_texture = texture_from_resource(op->src_resource);
2126 context = context_acquire(cs->device, NULL, 0);
2128 if (!wined3d_texture_load_location(src_texture, op->src_sub_resource_idx,
2129 context, src_texture->resource.map_binding))
2131 ERR("Failed to load source sub-resource into %s.\n",
2132 wined3d_debug_location(src_texture->resource.map_binding));
2133 context_release(context);
2134 goto error;
2137 level = op->dst_sub_resource_idx % dst_texture->level_count;
2138 if (update_w == wined3d_texture_get_level_width(dst_texture, level)
2139 && update_h == wined3d_texture_get_level_height(dst_texture, level)
2140 && update_d == wined3d_texture_get_level_depth(dst_texture, level))
2142 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
2144 else if (!wined3d_texture_load_location(dst_texture, op->dst_sub_resource_idx,
2145 context, WINED3D_LOCATION_TEXTURE_RGB))
2147 ERR("Failed to load destination sub-resource.\n");
2148 context_release(context);
2149 goto error;
2152 wined3d_texture_get_memory(src_texture, op->src_sub_resource_idx, &addr, src_texture->resource.map_binding);
2153 wined3d_texture_get_pitch(src_texture, op->src_sub_resource_idx % src_texture->level_count,
2154 &row_pitch, &slice_pitch);
2156 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
2157 wined3d_texture_upload_data(dst_texture, op->dst_sub_resource_idx, context,
2158 dst_texture->resource.format, &op->src_box, wined3d_const_bo_address(&addr),
2159 row_pitch, slice_pitch, op->dst_box.left, op->dst_box.top, op->dst_box.front, FALSE);
2160 wined3d_texture_validate_location(dst_texture, op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2161 wined3d_texture_invalidate_location(dst_texture, op->dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2163 context_release(context);
2165 else
2167 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(op->dst_resource->type));
2170 error:
2171 if (op->src_resource)
2172 wined3d_resource_release(op->src_resource);
2173 wined3d_resource_release(op->dst_resource);
2176 void wined3d_cs_emit_blt_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *dst_resource,
2177 unsigned int dst_sub_resource_idx, const struct wined3d_box *dst_box, struct wined3d_resource *src_resource,
2178 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, DWORD flags,
2179 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2181 struct wined3d_cs_blt_sub_resource *op;
2183 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2184 op->opcode = WINED3D_CS_OP_BLT_SUB_RESOURCE;
2185 op->dst_resource = dst_resource;
2186 op->dst_sub_resource_idx = dst_sub_resource_idx;
2187 op->dst_box = *dst_box;
2188 op->src_resource = src_resource;
2189 op->src_sub_resource_idx = src_sub_resource_idx;
2190 op->src_box = *src_box;
2191 op->flags = flags;
2192 if (fx)
2193 op->fx = *fx;
2194 else
2195 memset(&op->fx, 0, sizeof(op->fx));
2196 op->filter = filter;
2198 wined3d_resource_acquire(dst_resource);
2199 if (src_resource)
2200 wined3d_resource_acquire(src_resource);
2202 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2203 if (flags & WINED3D_BLT_SYNCHRONOUS)
2204 cs->ops->finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2207 static void wined3d_cs_exec_update_sub_resource(struct wined3d_cs *cs, const void *data)
2209 const struct wined3d_cs_update_sub_resource *op = data;
2210 struct wined3d_resource *resource = op->resource;
2211 const struct wined3d_box *box = &op->box;
2212 unsigned int width, height, depth, level;
2213 struct wined3d_const_bo_address addr;
2214 struct wined3d_context *context;
2215 struct wined3d_texture *texture;
2216 struct wined3d_box src_box;
2218 context = context_acquire(cs->device, NULL, 0);
2220 if (resource->type == WINED3D_RTYPE_BUFFER)
2222 struct wined3d_buffer *buffer = buffer_from_resource(resource);
2224 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
2226 ERR("Failed to load buffer location.\n");
2227 goto done;
2230 wined3d_buffer_upload_data(buffer, context, box, op->data.data);
2231 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
2232 goto done;
2235 texture = wined3d_texture_from_resource(resource);
2237 level = op->sub_resource_idx % texture->level_count;
2238 width = wined3d_texture_get_level_width(texture, level);
2239 height = wined3d_texture_get_level_height(texture, level);
2240 depth = wined3d_texture_get_level_depth(texture, level);
2242 addr.buffer_object = 0;
2243 addr.addr = op->data.data;
2245 /* Only load the sub-resource for partial updates. */
2246 if (!box->left && !box->top && !box->front
2247 && box->right == width && box->bottom == height && box->back == depth)
2248 wined3d_texture_prepare_texture(texture, context, FALSE);
2249 else
2250 wined3d_texture_load_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2251 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2253 wined3d_box_set(&src_box, 0, 0, box->right - box->left, box->bottom - box->top, 0, box->back - box->front);
2254 wined3d_texture_upload_data(texture, op->sub_resource_idx, context, texture->resource.format, &src_box,
2255 &addr, op->data.row_pitch, op->data.slice_pitch, box->left, box->top, box->front, FALSE);
2257 wined3d_texture_validate_location(texture, op->sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2258 wined3d_texture_invalidate_location(texture, op->sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2260 done:
2261 context_release(context);
2263 wined3d_resource_release(resource);
2266 void wined3d_cs_emit_update_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *resource,
2267 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
2268 unsigned int slice_pitch)
2270 struct wined3d_cs_update_sub_resource *op;
2272 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2273 op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2274 op->resource = resource;
2275 op->sub_resource_idx = sub_resource_idx;
2276 op->box = *box;
2277 op->data.row_pitch = row_pitch;
2278 op->data.slice_pitch = slice_pitch;
2279 op->data.data = data;
2281 wined3d_resource_acquire(resource);
2283 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2284 /* The data pointer may go away, so we need to wait until it is read.
2285 * Copying the data may be faster if it's small. */
2286 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2289 static void wined3d_cs_exec_add_dirty_texture_region(struct wined3d_cs *cs, const void *data)
2291 const struct wined3d_cs_add_dirty_texture_region *op = data;
2292 struct wined3d_texture *texture = op->texture;
2293 unsigned int sub_resource_idx, i;
2294 struct wined3d_context *context;
2296 context = context_acquire(cs->device, NULL, 0);
2297 sub_resource_idx = op->layer * texture->level_count;
2298 for (i = 0; i < texture->level_count; ++i, ++sub_resource_idx)
2300 if (wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
2301 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2302 else
2303 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
2305 context_release(context);
2307 wined3d_resource_release(&texture->resource);
2310 void wined3d_cs_emit_add_dirty_texture_region(struct wined3d_cs *cs,
2311 struct wined3d_texture *texture, unsigned int layer)
2313 struct wined3d_cs_add_dirty_texture_region *op;
2315 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2316 op->opcode = WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION;
2317 op->texture = texture;
2318 op->layer = layer;
2320 wined3d_resource_acquire(&texture->resource);
2322 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2325 static void wined3d_cs_exec_clear_unordered_access_view(struct wined3d_cs *cs, const void *data)
2327 const struct wined3d_cs_clear_unordered_access_view *op = data;
2328 struct wined3d_unordered_access_view *view = op->view;
2329 struct wined3d_context *context;
2331 context = context_acquire(cs->device, NULL, 0);
2332 wined3d_unordered_access_view_clear_uint(view, &op->clear_value, context);
2333 context_release(context);
2335 wined3d_resource_release(view->resource);
2338 void wined3d_cs_emit_clear_unordered_access_view_uint(struct wined3d_cs *cs,
2339 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
2341 struct wined3d_cs_clear_unordered_access_view *op;
2343 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2344 op->opcode = WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW;
2345 op->view = view;
2346 op->clear_value = *clear_value;
2348 wined3d_resource_acquire(view->resource);
2350 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2353 static void wined3d_cs_exec_copy_uav_counter(struct wined3d_cs *cs, const void *data)
2355 const struct wined3d_cs_copy_uav_counter *op = data;
2356 struct wined3d_unordered_access_view *view = op->view;
2357 struct wined3d_context *context;
2359 context = context_acquire(cs->device, NULL, 0);
2360 wined3d_unordered_access_view_copy_counter(view, op->buffer, op->offset, context);
2361 context_release(context);
2363 wined3d_resource_release(&op->buffer->resource);
2366 void wined3d_cs_emit_copy_uav_counter(struct wined3d_cs *cs, struct wined3d_buffer *dst_buffer,
2367 unsigned int offset, struct wined3d_unordered_access_view *uav)
2369 struct wined3d_cs_copy_uav_counter *op;
2371 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2372 op->opcode = WINED3D_CS_OP_COPY_UAV_COUNTER;
2373 op->buffer = dst_buffer;
2374 op->offset = offset;
2375 op->view = uav;
2377 wined3d_resource_acquire(&dst_buffer->resource);
2379 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2382 static void wined3d_cs_exec_generate_mipmaps(struct wined3d_cs *cs, const void *data)
2384 const struct wined3d_cs_generate_mipmaps *op = data;
2385 struct wined3d_shader_resource_view *view = op->view;
2387 shader_resource_view_generate_mipmaps(view);
2388 wined3d_resource_release(view->resource);
2391 void wined3d_cs_emit_generate_mipmaps(struct wined3d_cs *cs, struct wined3d_shader_resource_view *view)
2393 struct wined3d_cs_generate_mipmaps *op;
2395 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2396 op->opcode = WINED3D_CS_OP_GENERATE_MIPMAPS;
2397 op->view = view;
2399 wined3d_resource_acquire(view->resource);
2401 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2404 static void wined3d_cs_emit_stop(struct wined3d_cs *cs)
2406 struct wined3d_cs_stop *op;
2408 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2409 op->opcode = WINED3D_CS_OP_STOP;
2411 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2412 cs->ops->finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2415 static void (* const wined3d_cs_op_handlers[])(struct wined3d_cs *cs, const void *data) =
2417 /* WINED3D_CS_OP_NOP */ wined3d_cs_exec_nop,
2418 /* WINED3D_CS_OP_PRESENT */ wined3d_cs_exec_present,
2419 /* WINED3D_CS_OP_CLEAR */ wined3d_cs_exec_clear,
2420 /* WINED3D_CS_OP_DISPATCH */ wined3d_cs_exec_dispatch,
2421 /* WINED3D_CS_OP_DRAW */ wined3d_cs_exec_draw,
2422 /* WINED3D_CS_OP_FLUSH */ wined3d_cs_exec_flush,
2423 /* WINED3D_CS_OP_SET_PREDICATION */ wined3d_cs_exec_set_predication,
2424 /* WINED3D_CS_OP_SET_VIEWPORTS */ wined3d_cs_exec_set_viewports,
2425 /* WINED3D_CS_OP_SET_SCISSOR_RECTS */ wined3d_cs_exec_set_scissor_rects,
2426 /* WINED3D_CS_OP_SET_RENDERTARGET_VIEW */ wined3d_cs_exec_set_rendertarget_view,
2427 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW */ wined3d_cs_exec_set_depth_stencil_view,
2428 /* WINED3D_CS_OP_SET_VERTEX_DECLARATION */ wined3d_cs_exec_set_vertex_declaration,
2429 /* WINED3D_CS_OP_SET_STREAM_SOURCE */ wined3d_cs_exec_set_stream_source,
2430 /* WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ */ wined3d_cs_exec_set_stream_source_freq,
2431 /* WINED3D_CS_OP_SET_STREAM_OUTPUT */ wined3d_cs_exec_set_stream_output,
2432 /* WINED3D_CS_OP_SET_INDEX_BUFFER */ wined3d_cs_exec_set_index_buffer,
2433 /* WINED3D_CS_OP_SET_CONSTANT_BUFFER */ wined3d_cs_exec_set_constant_buffer,
2434 /* WINED3D_CS_OP_SET_TEXTURE */ wined3d_cs_exec_set_texture,
2435 /* WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW */ wined3d_cs_exec_set_shader_resource_view,
2436 /* WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_set_unordered_access_view,
2437 /* WINED3D_CS_OP_SET_SAMPLER */ wined3d_cs_exec_set_sampler,
2438 /* WINED3D_CS_OP_SET_SHADER */ wined3d_cs_exec_set_shader,
2439 /* WINED3D_CS_OP_SET_BLEND_STATE */ wined3d_cs_exec_set_blend_state,
2440 /* WINED3D_CS_OP_SET_RASTERIZER_STATE */ wined3d_cs_exec_set_rasterizer_state,
2441 /* WINED3D_CS_OP_SET_RENDER_STATE */ wined3d_cs_exec_set_render_state,
2442 /* WINED3D_CS_OP_SET_TEXTURE_STATE */ wined3d_cs_exec_set_texture_state,
2443 /* WINED3D_CS_OP_SET_SAMPLER_STATE */ wined3d_cs_exec_set_sampler_state,
2444 /* WINED3D_CS_OP_SET_TRANSFORM */ wined3d_cs_exec_set_transform,
2445 /* WINED3D_CS_OP_SET_CLIP_PLANE */ wined3d_cs_exec_set_clip_plane,
2446 /* WINED3D_CS_OP_SET_COLOR_KEY */ wined3d_cs_exec_set_color_key,
2447 /* WINED3D_CS_OP_SET_MATERIAL */ wined3d_cs_exec_set_material,
2448 /* WINED3D_CS_OP_SET_LIGHT */ wined3d_cs_exec_set_light,
2449 /* WINED3D_CS_OP_SET_LIGHT_ENABLE */ wined3d_cs_exec_set_light_enable,
2450 /* WINED3D_CS_OP_PUSH_CONSTANTS */ wined3d_cs_exec_push_constants,
2451 /* WINED3D_CS_OP_RESET_STATE */ wined3d_cs_exec_reset_state,
2452 /* WINED3D_CS_OP_CALLBACK */ wined3d_cs_exec_callback,
2453 /* WINED3D_CS_OP_QUERY_ISSUE */ wined3d_cs_exec_query_issue,
2454 /* WINED3D_CS_OP_PRELOAD_RESOURCE */ wined3d_cs_exec_preload_resource,
2455 /* WINED3D_CS_OP_UNLOAD_RESOURCE */ wined3d_cs_exec_unload_resource,
2456 /* WINED3D_CS_OP_MAP */ wined3d_cs_exec_map,
2457 /* WINED3D_CS_OP_UNMAP */ wined3d_cs_exec_unmap,
2458 /* WINED3D_CS_OP_BLT_SUB_RESOURCE */ wined3d_cs_exec_blt_sub_resource,
2459 /* WINED3D_CS_OP_UPDATE_SUB_RESOURCE */ wined3d_cs_exec_update_sub_resource,
2460 /* WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION */ wined3d_cs_exec_add_dirty_texture_region,
2461 /* WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_clear_unordered_access_view,
2462 /* WINED3D_CS_OP_COPY_UAV_COUNTER */ wined3d_cs_exec_copy_uav_counter,
2463 /* WINED3D_CS_OP_GENERATE_MIPMAPS */ wined3d_cs_exec_generate_mipmaps,
2466 static void *wined3d_cs_st_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2468 if (size > (cs->data_size - cs->end))
2470 size_t new_size;
2471 void *new_data;
2473 new_size = max(size, cs->data_size * 2);
2474 if (!cs->end)
2475 new_data = heap_realloc(cs->data, new_size);
2476 else
2477 new_data = heap_alloc(new_size);
2478 if (!new_data)
2479 return NULL;
2481 cs->data_size = new_size;
2482 cs->start = cs->end = 0;
2483 cs->data = new_data;
2486 cs->end += size;
2488 return (BYTE *)cs->data + cs->start;
2491 static void wined3d_cs_st_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2493 enum wined3d_cs_op opcode;
2494 size_t start;
2495 BYTE *data;
2497 data = cs->data;
2498 start = cs->start;
2499 cs->start = cs->end;
2501 opcode = *(const enum wined3d_cs_op *)&data[start];
2502 if (opcode >= WINED3D_CS_OP_STOP)
2503 ERR("Invalid opcode %#x.\n", opcode);
2504 else
2505 wined3d_cs_op_handlers[opcode](cs, &data[start]);
2507 if (cs->data == data)
2508 cs->start = cs->end = start;
2509 else if (!start)
2510 heap_free(data);
2513 static void wined3d_cs_st_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2517 static const struct wined3d_cs_ops wined3d_cs_st_ops =
2519 wined3d_cs_st_require_space,
2520 wined3d_cs_st_submit,
2521 wined3d_cs_st_finish,
2522 wined3d_cs_st_push_constants,
2525 static BOOL wined3d_cs_queue_is_empty(const struct wined3d_cs *cs, const struct wined3d_cs_queue *queue)
2527 wined3d_from_cs(cs);
2528 return *(volatile LONG *)&queue->head == queue->tail;
2531 static void wined3d_cs_queue_submit(struct wined3d_cs_queue *queue, struct wined3d_cs *cs)
2533 struct wined3d_cs_packet *packet;
2534 size_t packet_size;
2536 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2537 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2538 InterlockedExchange(&queue->head, (queue->head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1));
2540 if (InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2541 SetEvent(cs->event);
2544 static void wined3d_cs_mt_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2546 if (cs->thread_id == GetCurrentThreadId())
2547 return wined3d_cs_st_submit(cs, queue_id);
2549 wined3d_cs_queue_submit(&cs->queue[queue_id], cs);
2552 static void *wined3d_cs_queue_require_space(struct wined3d_cs_queue *queue, size_t size, struct wined3d_cs *cs)
2554 size_t queue_size = ARRAY_SIZE(queue->data);
2555 size_t header_size, packet_size, remaining;
2556 struct wined3d_cs_packet *packet;
2558 header_size = FIELD_OFFSET(struct wined3d_cs_packet, data[0]);
2559 size = (size + header_size - 1) & ~(header_size - 1);
2560 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[size]);
2561 if (packet_size >= WINED3D_CS_QUEUE_SIZE)
2563 ERR("Packet size %lu >= queue size %u.\n",
2564 (unsigned long)packet_size, WINED3D_CS_QUEUE_SIZE);
2565 return NULL;
2568 remaining = queue_size - queue->head;
2569 if (remaining < packet_size)
2571 size_t nop_size = remaining - header_size;
2572 struct wined3d_cs_nop *nop;
2574 TRACE("Inserting a nop for %lu + %lu bytes.\n",
2575 (unsigned long)header_size, (unsigned long)nop_size);
2577 nop = wined3d_cs_queue_require_space(queue, nop_size, cs);
2578 if (nop_size)
2579 nop->opcode = WINED3D_CS_OP_NOP;
2581 wined3d_cs_queue_submit(queue, cs);
2582 assert(!queue->head);
2585 for (;;)
2587 LONG tail = *(volatile LONG *)&queue->tail;
2588 LONG head = queue->head;
2589 LONG new_pos;
2591 /* Empty. */
2592 if (head == tail)
2593 break;
2594 new_pos = (head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1);
2595 /* Head ahead of tail. We checked the remaining size above, so we only
2596 * need to make sure we don't make head equal to tail. */
2597 if (head > tail && (new_pos != tail))
2598 break;
2599 /* Tail ahead of head. Make sure the new head is before the tail as
2600 * well. Note that new_pos is 0 when it's at the end of the queue. */
2601 if (new_pos < tail && new_pos)
2602 break;
2604 TRACE("Waiting for free space. Head %u, tail %u, packet size %lu.\n",
2605 head, tail, (unsigned long)packet_size);
2608 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2609 packet->size = size;
2610 return packet->data;
2613 static void *wined3d_cs_mt_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2615 if (cs->thread_id == GetCurrentThreadId())
2616 return wined3d_cs_st_require_space(cs, size, queue_id);
2618 return wined3d_cs_queue_require_space(&cs->queue[queue_id], size, cs);
2621 static void wined3d_cs_mt_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2623 if (cs->thread_id == GetCurrentThreadId())
2624 return wined3d_cs_st_finish(cs, queue_id);
2626 while (cs->queue[queue_id].head != *(volatile LONG *)&cs->queue[queue_id].tail)
2627 wined3d_pause();
2630 static const struct wined3d_cs_ops wined3d_cs_mt_ops =
2632 wined3d_cs_mt_require_space,
2633 wined3d_cs_mt_submit,
2634 wined3d_cs_mt_finish,
2635 wined3d_cs_mt_push_constants,
2638 static void poll_queries(struct wined3d_cs *cs)
2640 struct wined3d_query *query, *cursor;
2642 LIST_FOR_EACH_ENTRY_SAFE(query, cursor, &cs->query_poll_list, struct wined3d_query, poll_list_entry)
2644 if (!query->query_ops->query_poll(query, 0))
2645 continue;
2647 list_remove(&query->poll_list_entry);
2648 list_init(&query->poll_list_entry);
2649 InterlockedIncrement(&query->counter_retrieved);
2653 static void wined3d_cs_wait_event(struct wined3d_cs *cs)
2655 InterlockedExchange(&cs->waiting_for_event, TRUE);
2657 /* The main thread might have enqueued a command and blocked on it after
2658 * the CS thread decided to enter wined3d_cs_wait_event(), but before
2659 * "waiting_for_event" was set.
2661 * Likewise, we can race with the main thread when resetting
2662 * "waiting_for_event", in which case we would need to call
2663 * WaitForSingleObject() because the main thread called SetEvent(). */
2664 if (!(wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_DEFAULT])
2665 && wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_MAP]))
2666 && InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2667 return;
2669 WaitForSingleObject(cs->event, INFINITE);
2672 static DWORD WINAPI wined3d_cs_run(void *ctx)
2674 struct wined3d_cs_packet *packet;
2675 struct wined3d_cs_queue *queue;
2676 unsigned int spin_count = 0;
2677 struct wined3d_cs *cs = ctx;
2678 enum wined3d_cs_op opcode;
2679 HMODULE wined3d_module;
2680 unsigned int poll = 0;
2681 LONG tail;
2683 TRACE("Started.\n");
2685 /* Copy the module handle to a local variable to avoid racing with the
2686 * thread freeing "cs" before the FreeLibraryAndExitThread() call. */
2687 wined3d_module = cs->wined3d_module;
2689 list_init(&cs->query_poll_list);
2690 cs->thread_id = GetCurrentThreadId();
2691 for (;;)
2693 if (++poll == WINED3D_CS_QUERY_POLL_INTERVAL)
2695 poll_queries(cs);
2696 poll = 0;
2699 queue = &cs->queue[WINED3D_CS_QUEUE_MAP];
2700 if (wined3d_cs_queue_is_empty(cs, queue))
2702 queue = &cs->queue[WINED3D_CS_QUEUE_DEFAULT];
2703 if (wined3d_cs_queue_is_empty(cs, queue))
2705 if (++spin_count >= WINED3D_CS_SPIN_COUNT && list_empty(&cs->query_poll_list))
2706 wined3d_cs_wait_event(cs);
2707 continue;
2710 spin_count = 0;
2712 tail = queue->tail;
2713 packet = (struct wined3d_cs_packet *)&queue->data[tail];
2714 if (packet->size)
2716 opcode = *(const enum wined3d_cs_op *)packet->data;
2718 if (opcode >= WINED3D_CS_OP_STOP)
2720 if (opcode > WINED3D_CS_OP_STOP)
2721 ERR("Invalid opcode %#x.\n", opcode);
2722 break;
2725 wined3d_cs_op_handlers[opcode](cs, packet->data);
2728 tail += FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2729 tail &= (WINED3D_CS_QUEUE_SIZE - 1);
2730 InterlockedExchange(&queue->tail, tail);
2733 cs->queue[WINED3D_CS_QUEUE_MAP].tail = cs->queue[WINED3D_CS_QUEUE_MAP].head;
2734 cs->queue[WINED3D_CS_QUEUE_DEFAULT].tail = cs->queue[WINED3D_CS_QUEUE_DEFAULT].head;
2735 TRACE("Stopped.\n");
2736 FreeLibraryAndExitThread(wined3d_module, 0);
2739 struct wined3d_cs *wined3d_cs_create(struct wined3d_device *device)
2741 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2742 struct wined3d_cs *cs;
2744 if (!(cs = heap_alloc_zero(sizeof(*cs))))
2745 return NULL;
2747 cs->ops = &wined3d_cs_st_ops;
2748 cs->device = device;
2750 state_init(&cs->state, &cs->fb, gl_info, &device->adapter->d3d_info,
2751 WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
2753 cs->data_size = WINED3D_INITIAL_CS_SIZE;
2754 if (!(cs->data = heap_alloc(cs->data_size)))
2755 goto fail;
2757 if (wined3d_settings.cs_multithreaded
2758 && !RtlIsCriticalSectionLockedByThread(NtCurrentTeb()->Peb->LoaderLock))
2760 cs->ops = &wined3d_cs_mt_ops;
2762 if (!(cs->event = CreateEventW(NULL, FALSE, FALSE, NULL)))
2764 ERR("Failed to create command stream event.\n");
2765 heap_free(cs->data);
2766 goto fail;
2769 if (!(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2770 (const WCHAR *)wined3d_cs_run, &cs->wined3d_module)))
2772 ERR("Failed to get wined3d module handle.\n");
2773 CloseHandle(cs->event);
2774 heap_free(cs->data);
2775 goto fail;
2778 if (!(cs->thread = CreateThread(NULL, 0, wined3d_cs_run, cs, 0, NULL)))
2780 ERR("Failed to create wined3d command stream thread.\n");
2781 FreeLibrary(cs->wined3d_module);
2782 CloseHandle(cs->event);
2783 heap_free(cs->data);
2784 goto fail;
2788 return cs;
2790 fail:
2791 state_cleanup(&cs->state);
2792 heap_free(cs);
2793 return NULL;
2796 void wined3d_cs_destroy(struct wined3d_cs *cs)
2798 if (cs->thread)
2800 wined3d_cs_emit_stop(cs);
2801 CloseHandle(cs->thread);
2802 if (!CloseHandle(cs->event))
2803 ERR("Closing event failed.\n");
2806 state_cleanup(&cs->state);
2807 heap_free(cs->data);
2808 heap_free(cs);