wmp: Add seeking and duration.
[wine.git] / dlls / wined3d / cs.c
blob221e51eb94bfa0fe9b572faeeff5048b2d2cf2cd
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_RECT,
38 WINED3D_CS_OP_SET_RENDERTARGET_VIEW,
39 WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW,
40 WINED3D_CS_OP_SET_VERTEX_DECLARATION,
41 WINED3D_CS_OP_SET_STREAM_SOURCE,
42 WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ,
43 WINED3D_CS_OP_SET_STREAM_OUTPUT,
44 WINED3D_CS_OP_SET_INDEX_BUFFER,
45 WINED3D_CS_OP_SET_CONSTANT_BUFFER,
46 WINED3D_CS_OP_SET_TEXTURE,
47 WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW,
48 WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW,
49 WINED3D_CS_OP_SET_SAMPLER,
50 WINED3D_CS_OP_SET_SHADER,
51 WINED3D_CS_OP_SET_BLEND_STATE,
52 WINED3D_CS_OP_SET_RASTERIZER_STATE,
53 WINED3D_CS_OP_SET_RENDER_STATE,
54 WINED3D_CS_OP_SET_TEXTURE_STATE,
55 WINED3D_CS_OP_SET_SAMPLER_STATE,
56 WINED3D_CS_OP_SET_TRANSFORM,
57 WINED3D_CS_OP_SET_CLIP_PLANE,
58 WINED3D_CS_OP_SET_COLOR_KEY,
59 WINED3D_CS_OP_SET_MATERIAL,
60 WINED3D_CS_OP_SET_LIGHT,
61 WINED3D_CS_OP_SET_LIGHT_ENABLE,
62 WINED3D_CS_OP_PUSH_CONSTANTS,
63 WINED3D_CS_OP_RESET_STATE,
64 WINED3D_CS_OP_CALLBACK,
65 WINED3D_CS_OP_QUERY_ISSUE,
66 WINED3D_CS_OP_PRELOAD_RESOURCE,
67 WINED3D_CS_OP_UNLOAD_RESOURCE,
68 WINED3D_CS_OP_MAP,
69 WINED3D_CS_OP_UNMAP,
70 WINED3D_CS_OP_BLT_SUB_RESOURCE,
71 WINED3D_CS_OP_UPDATE_SUB_RESOURCE,
72 WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION,
73 WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW,
74 WINED3D_CS_OP_COPY_UAV_COUNTER,
75 WINED3D_CS_OP_GENERATE_MIPMAPS,
76 WINED3D_CS_OP_STOP,
79 struct wined3d_cs_packet
81 size_t size;
82 BYTE data[1];
85 struct wined3d_cs_nop
87 enum wined3d_cs_op opcode;
90 struct wined3d_cs_present
92 enum wined3d_cs_op opcode;
93 HWND dst_window_override;
94 struct wined3d_swapchain *swapchain;
95 RECT src_rect;
96 RECT dst_rect;
97 unsigned int swap_interval;
98 DWORD flags;
101 struct wined3d_cs_clear
103 enum wined3d_cs_op opcode;
104 DWORD flags;
105 unsigned int rt_count;
106 struct wined3d_fb_state *fb;
107 RECT draw_rect;
108 struct wined3d_color color;
109 float depth;
110 DWORD stencil;
111 unsigned int rect_count;
112 RECT rects[1];
115 struct wined3d_cs_dispatch
117 enum wined3d_cs_op opcode;
118 struct wined3d_dispatch_parameters parameters;
121 struct wined3d_cs_draw
123 enum wined3d_cs_op opcode;
124 GLenum primitive_type;
125 GLint patch_vertex_count;
126 struct wined3d_draw_parameters parameters;
129 struct wined3d_cs_flush
131 enum wined3d_cs_op opcode;
134 struct wined3d_cs_set_predication
136 enum wined3d_cs_op opcode;
137 struct wined3d_query *predicate;
138 BOOL value;
141 struct wined3d_cs_set_viewports
143 enum wined3d_cs_op opcode;
144 unsigned int viewport_count;
145 struct wined3d_viewport viewports[1];
148 struct wined3d_cs_set_scissor_rect
150 enum wined3d_cs_op opcode;
151 RECT rect;
154 struct wined3d_cs_set_rendertarget_view
156 enum wined3d_cs_op opcode;
157 unsigned int view_idx;
158 struct wined3d_rendertarget_view *view;
161 struct wined3d_cs_set_depth_stencil_view
163 enum wined3d_cs_op opcode;
164 struct wined3d_rendertarget_view *view;
167 struct wined3d_cs_set_vertex_declaration
169 enum wined3d_cs_op opcode;
170 struct wined3d_vertex_declaration *declaration;
173 struct wined3d_cs_set_stream_source
175 enum wined3d_cs_op opcode;
176 UINT stream_idx;
177 struct wined3d_buffer *buffer;
178 UINT offset;
179 UINT stride;
182 struct wined3d_cs_set_stream_source_freq
184 enum wined3d_cs_op opcode;
185 UINT stream_idx;
186 UINT frequency;
187 UINT flags;
190 struct wined3d_cs_set_stream_output
192 enum wined3d_cs_op opcode;
193 UINT stream_idx;
194 struct wined3d_buffer *buffer;
195 UINT offset;
198 struct wined3d_cs_set_index_buffer
200 enum wined3d_cs_op opcode;
201 struct wined3d_buffer *buffer;
202 enum wined3d_format_id format_id;
203 unsigned int offset;
206 struct wined3d_cs_set_constant_buffer
208 enum wined3d_cs_op opcode;
209 enum wined3d_shader_type type;
210 UINT cb_idx;
211 struct wined3d_buffer *buffer;
214 struct wined3d_cs_set_texture
216 enum wined3d_cs_op opcode;
217 UINT stage;
218 struct wined3d_texture *texture;
221 struct wined3d_cs_set_color_key
223 enum wined3d_cs_op opcode;
224 struct wined3d_texture *texture;
225 WORD flags;
226 WORD set;
227 struct wined3d_color_key color_key;
230 struct wined3d_cs_set_shader_resource_view
232 enum wined3d_cs_op opcode;
233 enum wined3d_shader_type type;
234 UINT view_idx;
235 struct wined3d_shader_resource_view *view;
238 struct wined3d_cs_set_unordered_access_view
240 enum wined3d_cs_op opcode;
241 enum wined3d_pipeline pipeline;
242 unsigned int view_idx;
243 struct wined3d_unordered_access_view *view;
244 unsigned int initial_count;
247 struct wined3d_cs_set_sampler
249 enum wined3d_cs_op opcode;
250 enum wined3d_shader_type type;
251 UINT sampler_idx;
252 struct wined3d_sampler *sampler;
255 struct wined3d_cs_set_shader
257 enum wined3d_cs_op opcode;
258 enum wined3d_shader_type type;
259 struct wined3d_shader *shader;
262 struct wined3d_cs_set_blend_state
264 enum wined3d_cs_op opcode;
265 struct wined3d_blend_state *state;
268 struct wined3d_cs_set_rasterizer_state
270 enum wined3d_cs_op opcode;
271 struct wined3d_rasterizer_state *state;
274 struct wined3d_cs_set_render_state
276 enum wined3d_cs_op opcode;
277 enum wined3d_render_state state;
278 DWORD value;
281 struct wined3d_cs_set_texture_state
283 enum wined3d_cs_op opcode;
284 UINT stage;
285 enum wined3d_texture_stage_state state;
286 DWORD value;
289 struct wined3d_cs_set_sampler_state
291 enum wined3d_cs_op opcode;
292 UINT sampler_idx;
293 enum wined3d_sampler_state state;
294 DWORD value;
297 struct wined3d_cs_set_transform
299 enum wined3d_cs_op opcode;
300 enum wined3d_transform_state state;
301 struct wined3d_matrix matrix;
304 struct wined3d_cs_set_clip_plane
306 enum wined3d_cs_op opcode;
307 UINT plane_idx;
308 struct wined3d_vec4 plane;
311 struct wined3d_cs_set_material
313 enum wined3d_cs_op opcode;
314 struct wined3d_material material;
317 struct wined3d_cs_set_light
319 enum wined3d_cs_op opcode;
320 struct wined3d_light_info light;
323 struct wined3d_cs_set_light_enable
325 enum wined3d_cs_op opcode;
326 unsigned int idx;
327 BOOL enable;
330 struct wined3d_cs_push_constants
332 enum wined3d_cs_op opcode;
333 enum wined3d_push_constants type;
334 unsigned int start_idx;
335 unsigned int count;
336 BYTE constants[1];
339 struct wined3d_cs_reset_state
341 enum wined3d_cs_op opcode;
344 struct wined3d_cs_callback
346 enum wined3d_cs_op opcode;
347 void (*callback)(void *object);
348 void *object;
351 struct wined3d_cs_query_issue
353 enum wined3d_cs_op opcode;
354 struct wined3d_query *query;
355 DWORD flags;
358 struct wined3d_cs_preload_resource
360 enum wined3d_cs_op opcode;
361 struct wined3d_resource *resource;
364 struct wined3d_cs_unload_resource
366 enum wined3d_cs_op opcode;
367 struct wined3d_resource *resource;
370 struct wined3d_cs_map
372 enum wined3d_cs_op opcode;
373 struct wined3d_resource *resource;
374 unsigned int sub_resource_idx;
375 struct wined3d_map_desc *map_desc;
376 const struct wined3d_box *box;
377 DWORD flags;
378 HRESULT *hr;
381 struct wined3d_cs_unmap
383 enum wined3d_cs_op opcode;
384 struct wined3d_resource *resource;
385 unsigned int sub_resource_idx;
386 HRESULT *hr;
389 struct wined3d_cs_blt_sub_resource
391 enum wined3d_cs_op opcode;
392 struct wined3d_resource *dst_resource;
393 unsigned int dst_sub_resource_idx;
394 struct wined3d_box dst_box;
395 struct wined3d_resource *src_resource;
396 unsigned int src_sub_resource_idx;
397 struct wined3d_box src_box;
398 DWORD flags;
399 struct wined3d_blt_fx fx;
400 enum wined3d_texture_filter_type filter;
403 struct wined3d_cs_update_sub_resource
405 enum wined3d_cs_op opcode;
406 struct wined3d_resource *resource;
407 unsigned int sub_resource_idx;
408 struct wined3d_box box;
409 struct wined3d_sub_resource_data data;
412 struct wined3d_cs_add_dirty_texture_region
414 enum wined3d_cs_op opcode;
415 struct wined3d_texture *texture;
416 unsigned int layer;
419 struct wined3d_cs_clear_unordered_access_view
421 enum wined3d_cs_op opcode;
422 struct wined3d_unordered_access_view *view;
423 struct wined3d_uvec4 clear_value;
426 struct wined3d_cs_copy_uav_counter
428 enum wined3d_cs_op opcode;
429 struct wined3d_buffer *buffer;
430 unsigned int offset;
431 struct wined3d_unordered_access_view *view;
434 struct wined3d_cs_generate_mipmaps
436 enum wined3d_cs_op opcode;
437 struct wined3d_shader_resource_view *view;
440 struct wined3d_cs_stop
442 enum wined3d_cs_op opcode;
445 static void wined3d_cs_exec_nop(struct wined3d_cs *cs, const void *data)
449 static void wined3d_cs_exec_present(struct wined3d_cs *cs, const void *data)
451 const struct wined3d_cs_present *op = data;
452 struct wined3d_swapchain *swapchain;
453 unsigned int i;
455 swapchain = op->swapchain;
456 wined3d_swapchain_set_window(swapchain, op->dst_window_override);
457 wined3d_swapchain_set_swap_interval(swapchain, op->swap_interval);
459 swapchain->swapchain_ops->swapchain_present(swapchain, &op->src_rect, &op->dst_rect, op->flags);
461 wined3d_resource_release(&swapchain->front_buffer->resource);
462 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
464 wined3d_resource_release(&swapchain->back_buffers[i]->resource);
467 InterlockedDecrement(&cs->pending_presents);
470 void wined3d_cs_emit_present(struct wined3d_cs *cs, struct wined3d_swapchain *swapchain,
471 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
472 unsigned int swap_interval, DWORD flags)
474 struct wined3d_cs_present *op;
475 unsigned int i;
476 LONG pending;
478 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
479 op->opcode = WINED3D_CS_OP_PRESENT;
480 op->dst_window_override = dst_window_override;
481 op->swapchain = swapchain;
482 op->src_rect = *src_rect;
483 op->dst_rect = *dst_rect;
484 op->swap_interval = swap_interval;
485 op->flags = flags;
487 pending = InterlockedIncrement(&cs->pending_presents);
489 wined3d_resource_acquire(&swapchain->front_buffer->resource);
490 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
492 wined3d_resource_acquire(&swapchain->back_buffers[i]->resource);
495 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
497 /* Limit input latency by limiting the number of presents that we can get
498 * ahead of the worker thread. We have a constant limit here, but
499 * IDXGIDevice1 allows tuning this. */
500 while (pending > 1)
502 wined3d_pause();
503 pending = InterlockedCompareExchange(&cs->pending_presents, 0, 0);
507 static void wined3d_cs_exec_clear(struct wined3d_cs *cs, const void *data)
509 const struct wined3d_cs_clear *op = data;
510 struct wined3d_device *device;
511 unsigned int i;
513 device = cs->device;
514 device->blitter->ops->blitter_clear(device->blitter, device, op->rt_count, op->fb,
515 op->rect_count, op->rects, &op->draw_rect, op->flags, &op->color, op->depth, op->stencil);
517 if (op->flags & WINED3DCLEAR_TARGET)
519 for (i = 0; i < op->rt_count; ++i)
521 if (op->fb->render_targets[i])
522 wined3d_resource_release(op->fb->render_targets[i]->resource);
525 if (op->flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
526 wined3d_resource_release(op->fb->depth_stencil->resource);
529 void wined3d_cs_emit_clear(struct wined3d_cs *cs, DWORD rect_count, const RECT *rects,
530 DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
532 unsigned int rt_count = cs->device->adapter->gl_info.limits.buffers;
533 const struct wined3d_state *state = &cs->device->state;
534 const struct wined3d_viewport *vp = &state->viewports[0];
535 struct wined3d_rendertarget_view *view;
536 struct wined3d_cs_clear *op;
537 RECT view_rect;
538 unsigned int i;
540 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_clear, rects[rect_count]),
541 WINED3D_CS_QUEUE_DEFAULT);
542 op->opcode = WINED3D_CS_OP_CLEAR;
543 op->flags = flags;
544 op->rt_count = rt_count;
545 op->fb = &cs->fb;
546 SetRect(&op->draw_rect, vp->x, vp->y, vp->x + vp->width, vp->y + vp->height);
547 if (state->render_states[WINED3D_RS_SCISSORTESTENABLE])
548 IntersectRect(&op->draw_rect, &op->draw_rect, &state->scissor_rect);
549 op->color = *color;
550 op->depth = depth;
551 op->stencil = stencil;
552 op->rect_count = rect_count;
553 memcpy(op->rects, rects, sizeof(*rects) * rect_count);
555 if (flags & WINED3DCLEAR_TARGET)
557 for (i = 0; i < rt_count; ++i)
559 if ((view = state->fb->render_targets[i]))
561 SetRect(&view_rect, 0, 0, view->width, view->height);
562 IntersectRect(&op->draw_rect, &op->draw_rect, &view_rect);
563 wined3d_resource_acquire(view->resource);
567 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
569 view = state->fb->depth_stencil;
570 SetRect(&view_rect, 0, 0, view->width, view->height);
571 IntersectRect(&op->draw_rect, &op->draw_rect, &view_rect);
572 wined3d_resource_acquire(view->resource);
575 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
578 void wined3d_cs_emit_clear_rendertarget_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view,
579 const RECT *rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
581 struct wined3d_cs_clear *op;
582 size_t size;
584 size = FIELD_OFFSET(struct wined3d_cs_clear, rects[1]) + sizeof(struct wined3d_fb_state);
585 op = cs->ops->require_space(cs, size, WINED3D_CS_QUEUE_DEFAULT);
586 op->fb = (void *)&op->rects[1];
588 op->opcode = WINED3D_CS_OP_CLEAR;
589 op->flags = flags;
590 if (flags & WINED3DCLEAR_TARGET)
592 op->rt_count = 1;
593 op->fb->render_targets[0] = view;
594 op->fb->depth_stencil = NULL;
595 op->color = *color;
597 else
599 op->rt_count = 0;
600 op->fb->render_targets[0] = NULL;
601 op->fb->depth_stencil = view;
602 op->depth = depth;
603 op->stencil = stencil;
605 SetRect(&op->draw_rect, 0, 0, view->width, view->height);
606 op->rect_count = 1;
607 op->rects[0] = *rect;
609 wined3d_resource_acquire(view->resource);
611 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
614 static void acquire_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
616 struct wined3d_shader_sampler_map_entry *entry;
617 struct wined3d_shader_resource_view *view;
618 struct wined3d_shader *shader;
619 unsigned int i, j;
621 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
623 if (!(shader_mask & (1u << i)))
624 continue;
626 if (!(shader = state->shader[i]))
627 continue;
629 for (j = 0; j < WINED3D_MAX_CBS; ++j)
631 if (state->cb[i][j])
632 wined3d_resource_acquire(&state->cb[i][j]->resource);
635 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
637 entry = &shader->reg_maps.sampler_map.entries[j];
639 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
640 continue;
642 wined3d_resource_acquire(view->resource);
647 static void release_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
649 struct wined3d_shader_sampler_map_entry *entry;
650 struct wined3d_shader_resource_view *view;
651 struct wined3d_shader *shader;
652 unsigned int i, j;
654 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
656 if (!(shader_mask & (1u << i)))
657 continue;
659 if (!(shader = state->shader[i]))
660 continue;
662 for (j = 0; j < WINED3D_MAX_CBS; ++j)
664 if (state->cb[i][j])
665 wined3d_resource_release(&state->cb[i][j]->resource);
668 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
670 entry = &shader->reg_maps.sampler_map.entries[j];
672 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
673 continue;
675 wined3d_resource_release(view->resource);
680 static void acquire_unordered_access_resources(const struct wined3d_shader *shader,
681 struct wined3d_unordered_access_view * const *views)
683 unsigned int i;
685 if (!shader)
686 return;
688 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
690 if (!shader->reg_maps.uav_resource_info[i].type)
691 continue;
693 if (!views[i])
694 continue;
696 wined3d_resource_acquire(views[i]->resource);
700 static void release_unordered_access_resources(const struct wined3d_shader *shader,
701 struct wined3d_unordered_access_view * const *views)
703 unsigned int i;
705 if (!shader)
706 return;
708 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
710 if (!shader->reg_maps.uav_resource_info[i].type)
711 continue;
713 if (!views[i])
714 continue;
716 wined3d_resource_release(views[i]->resource);
720 static void wined3d_cs_exec_dispatch(struct wined3d_cs *cs, const void *data)
722 const struct wined3d_cs_dispatch *op = data;
723 struct wined3d_state *state = &cs->state;
725 dispatch_compute(cs->device, state, &op->parameters);
727 if (op->parameters.indirect)
728 wined3d_resource_release(&op->parameters.u.indirect.buffer->resource);
730 release_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
731 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
732 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
735 static void acquire_compute_pipeline_resources(const struct wined3d_state *state)
737 acquire_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
738 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
739 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
742 void wined3d_cs_emit_dispatch(struct wined3d_cs *cs,
743 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
745 const struct wined3d_state *state = &cs->device->state;
746 struct wined3d_cs_dispatch *op;
748 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
749 op->opcode = WINED3D_CS_OP_DISPATCH;
750 op->parameters.indirect = FALSE;
751 op->parameters.u.direct.group_count_x = group_count_x;
752 op->parameters.u.direct.group_count_y = group_count_y;
753 op->parameters.u.direct.group_count_z = group_count_z;
755 acquire_compute_pipeline_resources(state);
757 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
760 void wined3d_cs_emit_dispatch_indirect(struct wined3d_cs *cs,
761 struct wined3d_buffer *buffer, unsigned int offset)
763 const struct wined3d_state *state = &cs->device->state;
764 struct wined3d_cs_dispatch *op;
766 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
767 op->opcode = WINED3D_CS_OP_DISPATCH;
768 op->parameters.indirect = TRUE;
769 op->parameters.u.indirect.buffer = buffer;
770 op->parameters.u.indirect.offset = offset;
772 acquire_compute_pipeline_resources(state);
773 wined3d_resource_acquire(&buffer->resource);
775 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
778 static void wined3d_cs_exec_draw(struct wined3d_cs *cs, const void *data)
780 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
781 struct wined3d_state *state = &cs->state;
782 const struct wined3d_cs_draw *op = data;
783 int load_base_vertex_idx;
784 unsigned int i;
786 /* ARB_draw_indirect always supports a base vertex offset. */
787 if (!op->parameters.indirect && !gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
788 load_base_vertex_idx = op->parameters.u.direct.base_vertex_idx;
789 else
790 load_base_vertex_idx = 0;
792 if (state->load_base_vertex_index != load_base_vertex_idx)
794 state->load_base_vertex_index = load_base_vertex_idx;
795 device_invalidate_state(cs->device, STATE_BASEVERTEXINDEX);
798 if (state->gl_primitive_type != op->primitive_type)
800 if (state->gl_primitive_type == GL_POINTS || op->primitive_type == GL_POINTS)
801 device_invalidate_state(cs->device, STATE_POINT_ENABLE);
802 state->gl_primitive_type = op->primitive_type;
804 state->gl_patch_vertices = op->patch_vertex_count;
806 draw_primitive(cs->device, state, &op->parameters);
808 if (op->parameters.indirect)
810 struct wined3d_buffer *buffer = op->parameters.u.indirect.buffer;
811 wined3d_resource_release(&buffer->resource);
814 if (op->parameters.indexed)
815 wined3d_resource_release(&state->index_buffer->resource);
816 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
818 if (state->streams[i].buffer)
819 wined3d_resource_release(&state->streams[i].buffer->resource);
821 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
823 if (state->stream_output[i].buffer)
824 wined3d_resource_release(&state->stream_output[i].buffer->resource);
826 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
828 if (state->textures[i])
829 wined3d_resource_release(&state->textures[i]->resource);
831 for (i = 0; i < gl_info->limits.buffers; ++i)
833 if (state->fb->render_targets[i])
834 wined3d_resource_release(state->fb->render_targets[i]->resource);
836 if (state->fb->depth_stencil)
837 wined3d_resource_release(state->fb->depth_stencil->resource);
838 release_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
839 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
840 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
843 static void acquire_graphics_pipeline_resources(const struct wined3d_state *state,
844 BOOL indexed, const struct wined3d_gl_info *gl_info)
846 unsigned int i;
848 if (indexed)
849 wined3d_resource_acquire(&state->index_buffer->resource);
850 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
852 if (state->streams[i].buffer)
853 wined3d_resource_acquire(&state->streams[i].buffer->resource);
855 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
857 if (state->stream_output[i].buffer)
858 wined3d_resource_acquire(&state->stream_output[i].buffer->resource);
860 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
862 if (state->textures[i])
863 wined3d_resource_acquire(&state->textures[i]->resource);
865 for (i = 0; i < gl_info->limits.buffers; ++i)
867 if (state->fb->render_targets[i])
868 wined3d_resource_acquire(state->fb->render_targets[i]->resource);
870 if (state->fb->depth_stencil)
871 wined3d_resource_acquire(state->fb->depth_stencil->resource);
872 acquire_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
873 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
874 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
877 void wined3d_cs_emit_draw(struct wined3d_cs *cs, GLenum primitive_type, unsigned int patch_vertex_count,
878 int base_vertex_idx, unsigned int start_idx, unsigned int index_count,
879 unsigned int start_instance, unsigned int instance_count, BOOL indexed)
881 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
882 const struct wined3d_state *state = &cs->device->state;
883 struct wined3d_cs_draw *op;
885 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
886 op->opcode = WINED3D_CS_OP_DRAW;
887 op->primitive_type = primitive_type;
888 op->patch_vertex_count = patch_vertex_count;
889 op->parameters.indirect = FALSE;
890 op->parameters.u.direct.base_vertex_idx = base_vertex_idx;
891 op->parameters.u.direct.start_idx = start_idx;
892 op->parameters.u.direct.index_count = index_count;
893 op->parameters.u.direct.start_instance = start_instance;
894 op->parameters.u.direct.instance_count = instance_count;
895 op->parameters.indexed = indexed;
897 acquire_graphics_pipeline_resources(state, indexed, gl_info);
899 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
902 void wined3d_cs_emit_draw_indirect(struct wined3d_cs *cs, GLenum primitive_type, unsigned int patch_vertex_count,
903 struct wined3d_buffer *buffer, unsigned int offset, BOOL indexed)
905 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
906 const struct wined3d_state *state = &cs->device->state;
907 struct wined3d_cs_draw *op;
909 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
910 op->opcode = WINED3D_CS_OP_DRAW;
911 op->primitive_type = primitive_type;
912 op->patch_vertex_count = patch_vertex_count;
913 op->parameters.indirect = TRUE;
914 op->parameters.u.indirect.buffer = buffer;
915 op->parameters.u.indirect.offset = offset;
916 op->parameters.indexed = indexed;
918 acquire_graphics_pipeline_resources(state, indexed, gl_info);
919 wined3d_resource_acquire(&buffer->resource);
921 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
924 static void wined3d_cs_exec_flush(struct wined3d_cs *cs, const void *data)
926 struct wined3d_context *context;
928 context = context_acquire(cs->device, NULL, 0);
929 if (context->valid)
930 context->gl_info->gl_ops.gl.p_glFlush();
931 context_release(context);
934 void wined3d_cs_emit_flush(struct wined3d_cs *cs)
936 struct wined3d_cs_flush *op;
938 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
939 op->opcode = WINED3D_CS_OP_FLUSH;
941 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
942 cs->queries_flushed = TRUE;
945 static void wined3d_cs_exec_set_predication(struct wined3d_cs *cs, const void *data)
947 const struct wined3d_cs_set_predication *op = data;
949 cs->state.predicate = op->predicate;
950 cs->state.predicate_value = op->value;
953 void wined3d_cs_emit_set_predication(struct wined3d_cs *cs, struct wined3d_query *predicate, BOOL value)
955 struct wined3d_cs_set_predication *op;
957 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
958 op->opcode = WINED3D_CS_OP_SET_PREDICATION;
959 op->predicate = predicate;
960 op->value = value;
962 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
965 static void wined3d_cs_exec_set_viewports(struct wined3d_cs *cs, const void *data)
967 const struct wined3d_cs_set_viewports *op = data;
969 if (op->viewport_count)
970 memcpy(cs->state.viewports, op->viewports, op->viewport_count * sizeof(*op->viewports));
971 else
972 memset(cs->state.viewports, 0, sizeof(*cs->state.viewports));
973 cs->state.viewport_count = op->viewport_count;
974 device_invalidate_state(cs->device, STATE_VIEWPORT);
977 void wined3d_cs_emit_set_viewports(struct wined3d_cs *cs, unsigned int viewport_count,
978 const struct wined3d_viewport *viewports)
980 struct wined3d_cs_set_viewports *op;
982 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_set_viewports, viewports[viewport_count]),
983 WINED3D_CS_QUEUE_DEFAULT);
984 op->opcode = WINED3D_CS_OP_SET_VIEWPORTS;
985 if (viewport_count)
986 memcpy(op->viewports, viewports, viewport_count * sizeof(*viewports));
987 op->viewport_count = viewport_count;
989 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
992 static void wined3d_cs_exec_set_scissor_rect(struct wined3d_cs *cs, const void *data)
994 const struct wined3d_cs_set_scissor_rect *op = data;
996 cs->state.scissor_rect = op->rect;
997 device_invalidate_state(cs->device, STATE_SCISSORRECT);
1000 void wined3d_cs_emit_set_scissor_rect(struct wined3d_cs *cs, const RECT *rect)
1002 struct wined3d_cs_set_scissor_rect *op;
1004 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1005 op->opcode = WINED3D_CS_OP_SET_SCISSOR_RECT;
1006 op->rect = *rect;
1008 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1011 static void wined3d_cs_exec_set_rendertarget_view(struct wined3d_cs *cs, const void *data)
1013 const struct wined3d_cs_set_rendertarget_view *op = data;
1015 cs->fb.render_targets[op->view_idx] = op->view;
1016 device_invalidate_state(cs->device, STATE_FRAMEBUFFER);
1019 void wined3d_cs_emit_set_rendertarget_view(struct wined3d_cs *cs, unsigned int view_idx,
1020 struct wined3d_rendertarget_view *view)
1022 struct wined3d_cs_set_rendertarget_view *op;
1024 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1025 op->opcode = WINED3D_CS_OP_SET_RENDERTARGET_VIEW;
1026 op->view_idx = view_idx;
1027 op->view = view;
1029 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1032 static void wined3d_cs_exec_set_depth_stencil_view(struct wined3d_cs *cs, const void *data)
1034 const struct wined3d_cs_set_depth_stencil_view *op = data;
1035 struct wined3d_device *device = cs->device;
1036 struct wined3d_rendertarget_view *prev;
1038 if ((prev = cs->state.fb->depth_stencil) && prev->resource->type != WINED3D_RTYPE_BUFFER)
1040 struct wined3d_texture *prev_texture = texture_from_resource(prev->resource);
1042 if (device->swapchains[0]->desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
1043 || prev_texture->flags & WINED3D_TEXTURE_DISCARD)
1044 wined3d_texture_validate_location(prev_texture,
1045 prev->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
1048 cs->fb.depth_stencil = op->view;
1050 if (!prev != !op->view)
1052 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
1053 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
1054 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
1055 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
1056 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
1058 else if (prev && prev->format->depth_bias_scale != op->view->format->depth_bias_scale)
1060 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
1063 device_invalidate_state(device, STATE_FRAMEBUFFER);
1066 void wined3d_cs_emit_set_depth_stencil_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view)
1068 struct wined3d_cs_set_depth_stencil_view *op;
1070 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1071 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW;
1072 op->view = view;
1074 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1077 static void wined3d_cs_exec_set_vertex_declaration(struct wined3d_cs *cs, const void *data)
1079 const struct wined3d_cs_set_vertex_declaration *op = data;
1081 cs->state.vertex_declaration = op->declaration;
1082 device_invalidate_state(cs->device, STATE_VDECL);
1085 void wined3d_cs_emit_set_vertex_declaration(struct wined3d_cs *cs, struct wined3d_vertex_declaration *declaration)
1087 struct wined3d_cs_set_vertex_declaration *op;
1089 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1090 op->opcode = WINED3D_CS_OP_SET_VERTEX_DECLARATION;
1091 op->declaration = declaration;
1093 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1096 static void wined3d_cs_exec_set_stream_source(struct wined3d_cs *cs, const void *data)
1098 const struct wined3d_cs_set_stream_source *op = data;
1099 struct wined3d_stream_state *stream;
1100 struct wined3d_buffer *prev;
1102 stream = &cs->state.streams[op->stream_idx];
1103 prev = stream->buffer;
1104 stream->buffer = op->buffer;
1105 stream->offset = op->offset;
1106 stream->stride = op->stride;
1108 if (op->buffer)
1109 InterlockedIncrement(&op->buffer->resource.bind_count);
1110 if (prev)
1111 InterlockedDecrement(&prev->resource.bind_count);
1113 device_invalidate_state(cs->device, STATE_STREAMSRC);
1116 void wined3d_cs_emit_set_stream_source(struct wined3d_cs *cs, UINT stream_idx,
1117 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1119 struct wined3d_cs_set_stream_source *op;
1121 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1122 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE;
1123 op->stream_idx = stream_idx;
1124 op->buffer = buffer;
1125 op->offset = offset;
1126 op->stride = stride;
1128 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1131 static void wined3d_cs_exec_set_stream_source_freq(struct wined3d_cs *cs, const void *data)
1133 const struct wined3d_cs_set_stream_source_freq *op = data;
1134 struct wined3d_stream_state *stream;
1136 stream = &cs->state.streams[op->stream_idx];
1137 stream->frequency = op->frequency;
1138 stream->flags = op->flags;
1140 device_invalidate_state(cs->device, STATE_STREAMSRC);
1143 void wined3d_cs_emit_set_stream_source_freq(struct wined3d_cs *cs, UINT stream_idx, UINT frequency, UINT flags)
1145 struct wined3d_cs_set_stream_source_freq *op;
1147 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1148 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ;
1149 op->stream_idx = stream_idx;
1150 op->frequency = frequency;
1151 op->flags = flags;
1153 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1156 static void wined3d_cs_exec_set_stream_output(struct wined3d_cs *cs, const void *data)
1158 const struct wined3d_cs_set_stream_output *op = data;
1159 struct wined3d_stream_output *stream;
1160 struct wined3d_buffer *prev;
1162 stream = &cs->state.stream_output[op->stream_idx];
1163 prev = stream->buffer;
1164 stream->buffer = op->buffer;
1165 stream->offset = op->offset;
1167 if (op->buffer)
1168 InterlockedIncrement(&op->buffer->resource.bind_count);
1169 if (prev)
1170 InterlockedDecrement(&prev->resource.bind_count);
1172 device_invalidate_state(cs->device, STATE_STREAM_OUTPUT);
1175 void wined3d_cs_emit_set_stream_output(struct wined3d_cs *cs, UINT stream_idx,
1176 struct wined3d_buffer *buffer, UINT offset)
1178 struct wined3d_cs_set_stream_output *op;
1180 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1181 op->opcode = WINED3D_CS_OP_SET_STREAM_OUTPUT;
1182 op->stream_idx = stream_idx;
1183 op->buffer = buffer;
1184 op->offset = offset;
1186 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1189 static void wined3d_cs_exec_set_index_buffer(struct wined3d_cs *cs, const void *data)
1191 const struct wined3d_cs_set_index_buffer *op = data;
1192 struct wined3d_buffer *prev;
1194 prev = cs->state.index_buffer;
1195 cs->state.index_buffer = op->buffer;
1196 cs->state.index_format = op->format_id;
1197 cs->state.index_offset = op->offset;
1199 if (op->buffer)
1200 InterlockedIncrement(&op->buffer->resource.bind_count);
1201 if (prev)
1202 InterlockedDecrement(&prev->resource.bind_count);
1204 device_invalidate_state(cs->device, STATE_INDEXBUFFER);
1207 void wined3d_cs_emit_set_index_buffer(struct wined3d_cs *cs, struct wined3d_buffer *buffer,
1208 enum wined3d_format_id format_id, unsigned int offset)
1210 struct wined3d_cs_set_index_buffer *op;
1212 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1213 op->opcode = WINED3D_CS_OP_SET_INDEX_BUFFER;
1214 op->buffer = buffer;
1215 op->format_id = format_id;
1216 op->offset = offset;
1218 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1221 static void wined3d_cs_exec_set_constant_buffer(struct wined3d_cs *cs, const void *data)
1223 const struct wined3d_cs_set_constant_buffer *op = data;
1224 struct wined3d_buffer *prev;
1226 prev = cs->state.cb[op->type][op->cb_idx];
1227 cs->state.cb[op->type][op->cb_idx] = op->buffer;
1229 if (op->buffer)
1230 InterlockedIncrement(&op->buffer->resource.bind_count);
1231 if (prev)
1232 InterlockedDecrement(&prev->resource.bind_count);
1234 device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(op->type));
1237 void wined3d_cs_emit_set_constant_buffer(struct wined3d_cs *cs, enum wined3d_shader_type type,
1238 UINT cb_idx, struct wined3d_buffer *buffer)
1240 struct wined3d_cs_set_constant_buffer *op;
1242 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1243 op->opcode = WINED3D_CS_OP_SET_CONSTANT_BUFFER;
1244 op->type = type;
1245 op->cb_idx = cb_idx;
1246 op->buffer = buffer;
1248 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1251 static void wined3d_cs_exec_set_texture(struct wined3d_cs *cs, const void *data)
1253 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
1254 const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
1255 const struct wined3d_cs_set_texture *op = data;
1256 struct wined3d_texture *prev;
1257 BOOL old_use_color_key = FALSE, new_use_color_key = FALSE;
1259 prev = cs->state.textures[op->stage];
1260 cs->state.textures[op->stage] = op->texture;
1262 if (op->texture)
1264 const struct wined3d_format *new_format = op->texture->resource.format;
1265 const struct wined3d_format *old_format = prev ? prev->resource.format : NULL;
1266 unsigned int old_fmt_flags = prev ? prev->resource.format_flags : 0;
1267 unsigned int new_fmt_flags = op->texture->resource.format_flags;
1269 if (InterlockedIncrement(&op->texture->resource.bind_count) == 1)
1270 op->texture->sampler = op->stage;
1272 if (!prev || op->texture->target != prev->target
1273 || (!is_same_fixup(new_format->color_fixup, old_format->color_fixup)
1274 && !(can_use_texture_swizzle(gl_info, new_format) && can_use_texture_swizzle(gl_info, old_format)))
1275 || (new_fmt_flags & WINED3DFMT_FLAG_SHADOW) != (old_fmt_flags & WINED3DFMT_FLAG_SHADOW))
1276 device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1278 if (!prev && op->stage < d3d_info->limits.ffp_blend_stages)
1280 /* The source arguments for color and alpha ops have different
1281 * meanings when a NULL texture is bound, so the COLOR_OP and
1282 * ALPHA_OP have to be dirtified. */
1283 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1284 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1287 if (!op->stage && op->texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1288 new_use_color_key = TRUE;
1291 if (prev)
1293 if (InterlockedDecrement(&prev->resource.bind_count) && prev->sampler == op->stage)
1295 unsigned int i;
1297 /* Search for other stages the texture is bound to. Shouldn't
1298 * happen if applications bind textures to a single stage only. */
1299 TRACE("Searching for other stages the texture is bound to.\n");
1300 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
1302 if (cs->state.textures[i] == prev)
1304 TRACE("Texture is also bound to stage %u.\n", i);
1305 prev->sampler = i;
1306 break;
1311 if (!op->texture && op->stage < d3d_info->limits.ffp_blend_stages)
1313 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1314 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1317 if (!op->stage && prev->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1318 old_use_color_key = TRUE;
1321 device_invalidate_state(cs->device, STATE_SAMPLER(op->stage));
1323 if (new_use_color_key != old_use_color_key)
1324 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1326 if (new_use_color_key)
1327 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1330 void wined3d_cs_emit_set_texture(struct wined3d_cs *cs, UINT stage, struct wined3d_texture *texture)
1332 struct wined3d_cs_set_texture *op;
1334 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1335 op->opcode = WINED3D_CS_OP_SET_TEXTURE;
1336 op->stage = stage;
1337 op->texture = texture;
1339 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1342 static void wined3d_cs_exec_set_shader_resource_view(struct wined3d_cs *cs, const void *data)
1344 const struct wined3d_cs_set_shader_resource_view *op = data;
1345 struct wined3d_shader_resource_view *prev;
1347 prev = cs->state.shader_resource_view[op->type][op->view_idx];
1348 cs->state.shader_resource_view[op->type][op->view_idx] = op->view;
1350 if (op->view)
1351 InterlockedIncrement(&op->view->resource->bind_count);
1352 if (prev)
1353 InterlockedDecrement(&prev->resource->bind_count);
1355 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1356 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1357 else
1358 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1361 void wined3d_cs_emit_set_shader_resource_view(struct wined3d_cs *cs, enum wined3d_shader_type type,
1362 UINT view_idx, struct wined3d_shader_resource_view *view)
1364 struct wined3d_cs_set_shader_resource_view *op;
1366 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1367 op->opcode = WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW;
1368 op->type = type;
1369 op->view_idx = view_idx;
1370 op->view = view;
1372 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1375 static void wined3d_cs_exec_set_unordered_access_view(struct wined3d_cs *cs, const void *data)
1377 const struct wined3d_cs_set_unordered_access_view *op = data;
1378 struct wined3d_unordered_access_view *prev;
1380 prev = cs->state.unordered_access_view[op->pipeline][op->view_idx];
1381 cs->state.unordered_access_view[op->pipeline][op->view_idx] = op->view;
1383 if (op->view)
1384 InterlockedIncrement(&op->view->resource->bind_count);
1385 if (prev)
1386 InterlockedDecrement(&prev->resource->bind_count);
1388 if (op->view && op->initial_count != ~0u)
1389 wined3d_unordered_access_view_set_counter(op->view, op->initial_count);
1391 device_invalidate_state(cs->device, STATE_UNORDERED_ACCESS_VIEW_BINDING(op->pipeline));
1394 void wined3d_cs_emit_set_unordered_access_view(struct wined3d_cs *cs, enum wined3d_pipeline pipeline,
1395 unsigned int view_idx, struct wined3d_unordered_access_view *view, unsigned int initial_count)
1397 struct wined3d_cs_set_unordered_access_view *op;
1399 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1400 op->opcode = WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW;
1401 op->pipeline = pipeline;
1402 op->view_idx = view_idx;
1403 op->view = view;
1404 op->initial_count = initial_count;
1406 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1409 static void wined3d_cs_exec_set_sampler(struct wined3d_cs *cs, const void *data)
1411 const struct wined3d_cs_set_sampler *op = data;
1413 cs->state.sampler[op->type][op->sampler_idx] = op->sampler;
1414 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1415 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1416 else
1417 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1420 void wined3d_cs_emit_set_sampler(struct wined3d_cs *cs, enum wined3d_shader_type type,
1421 UINT sampler_idx, struct wined3d_sampler *sampler)
1423 struct wined3d_cs_set_sampler *op;
1425 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1426 op->opcode = WINED3D_CS_OP_SET_SAMPLER;
1427 op->type = type;
1428 op->sampler_idx = sampler_idx;
1429 op->sampler = sampler;
1431 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1434 static void wined3d_cs_exec_set_shader(struct wined3d_cs *cs, const void *data)
1436 const struct wined3d_cs_set_shader *op = data;
1438 cs->state.shader[op->type] = op->shader;
1439 device_invalidate_state(cs->device, STATE_SHADER(op->type));
1440 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1441 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1442 else
1443 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1446 void wined3d_cs_emit_set_shader(struct wined3d_cs *cs, enum wined3d_shader_type type, struct wined3d_shader *shader)
1448 struct wined3d_cs_set_shader *op;
1450 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1451 op->opcode = WINED3D_CS_OP_SET_SHADER;
1452 op->type = type;
1453 op->shader = shader;
1455 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1458 static void wined3d_cs_exec_set_blend_state(struct wined3d_cs *cs, const void *data)
1460 const struct wined3d_cs_set_blend_state *op = data;
1462 cs->state.blend_state = op->state;
1463 device_invalidate_state(cs->device, STATE_BLEND);
1466 void wined3d_cs_emit_set_blend_state(struct wined3d_cs *cs, struct wined3d_blend_state *state)
1468 struct wined3d_cs_set_blend_state *op;
1470 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1471 op->opcode = WINED3D_CS_OP_SET_BLEND_STATE;
1472 op->state = state;
1474 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1477 static void wined3d_cs_exec_set_rasterizer_state(struct wined3d_cs *cs, const void *data)
1479 const struct wined3d_cs_set_rasterizer_state *op = data;
1481 cs->state.rasterizer_state = op->state;
1482 device_invalidate_state(cs->device, STATE_FRONTFACE);
1485 void wined3d_cs_emit_set_rasterizer_state(struct wined3d_cs *cs,
1486 struct wined3d_rasterizer_state *rasterizer_state)
1488 struct wined3d_cs_set_rasterizer_state *op;
1490 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1491 op->opcode = WINED3D_CS_OP_SET_RASTERIZER_STATE;
1492 op->state = rasterizer_state;
1494 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1497 static void wined3d_cs_exec_set_render_state(struct wined3d_cs *cs, const void *data)
1499 const struct wined3d_cs_set_render_state *op = data;
1501 cs->state.render_states[op->state] = op->value;
1502 device_invalidate_state(cs->device, STATE_RENDER(op->state));
1505 void wined3d_cs_emit_set_render_state(struct wined3d_cs *cs, enum wined3d_render_state state, DWORD value)
1507 struct wined3d_cs_set_render_state *op;
1509 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1510 op->opcode = WINED3D_CS_OP_SET_RENDER_STATE;
1511 op->state = state;
1512 op->value = value;
1514 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1517 static void wined3d_cs_exec_set_texture_state(struct wined3d_cs *cs, const void *data)
1519 const struct wined3d_cs_set_texture_state *op = data;
1521 cs->state.texture_states[op->stage][op->state] = op->value;
1522 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, op->state));
1525 void wined3d_cs_emit_set_texture_state(struct wined3d_cs *cs, UINT stage,
1526 enum wined3d_texture_stage_state state, DWORD value)
1528 struct wined3d_cs_set_texture_state *op;
1530 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1531 op->opcode = WINED3D_CS_OP_SET_TEXTURE_STATE;
1532 op->stage = stage;
1533 op->state = state;
1534 op->value = value;
1536 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1539 static void wined3d_cs_exec_set_sampler_state(struct wined3d_cs *cs, const void *data)
1541 const struct wined3d_cs_set_sampler_state *op = data;
1543 cs->state.sampler_states[op->sampler_idx][op->state] = op->value;
1544 device_invalidate_state(cs->device, STATE_SAMPLER(op->sampler_idx));
1547 void wined3d_cs_emit_set_sampler_state(struct wined3d_cs *cs, UINT sampler_idx,
1548 enum wined3d_sampler_state state, DWORD value)
1550 struct wined3d_cs_set_sampler_state *op;
1552 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1553 op->opcode = WINED3D_CS_OP_SET_SAMPLER_STATE;
1554 op->sampler_idx = sampler_idx;
1555 op->state = state;
1556 op->value = value;
1558 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1561 static void wined3d_cs_exec_set_transform(struct wined3d_cs *cs, const void *data)
1563 const struct wined3d_cs_set_transform *op = data;
1565 cs->state.transforms[op->state] = op->matrix;
1566 if (op->state < WINED3D_TS_WORLD_MATRIX(cs->device->adapter->d3d_info.limits.ffp_vertex_blend_matrices))
1567 device_invalidate_state(cs->device, STATE_TRANSFORM(op->state));
1570 void wined3d_cs_emit_set_transform(struct wined3d_cs *cs, enum wined3d_transform_state state,
1571 const struct wined3d_matrix *matrix)
1573 struct wined3d_cs_set_transform *op;
1575 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1576 op->opcode = WINED3D_CS_OP_SET_TRANSFORM;
1577 op->state = state;
1578 op->matrix = *matrix;
1580 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1583 static void wined3d_cs_exec_set_clip_plane(struct wined3d_cs *cs, const void *data)
1585 const struct wined3d_cs_set_clip_plane *op = data;
1587 cs->state.clip_planes[op->plane_idx] = op->plane;
1588 device_invalidate_state(cs->device, STATE_CLIPPLANE(op->plane_idx));
1591 void wined3d_cs_emit_set_clip_plane(struct wined3d_cs *cs, UINT plane_idx, const struct wined3d_vec4 *plane)
1593 struct wined3d_cs_set_clip_plane *op;
1595 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1596 op->opcode = WINED3D_CS_OP_SET_CLIP_PLANE;
1597 op->plane_idx = plane_idx;
1598 op->plane = *plane;
1600 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1603 static void wined3d_cs_exec_set_color_key(struct wined3d_cs *cs, const void *data)
1605 const struct wined3d_cs_set_color_key *op = data;
1606 struct wined3d_texture *texture = op->texture;
1608 if (op->set)
1610 switch (op->flags)
1612 case WINED3D_CKEY_DST_BLT:
1613 texture->async.dst_blt_color_key = op->color_key;
1614 texture->async.color_key_flags |= WINED3D_CKEY_DST_BLT;
1615 break;
1617 case WINED3D_CKEY_DST_OVERLAY:
1618 texture->async.dst_overlay_color_key = op->color_key;
1619 texture->async.color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
1620 break;
1622 case WINED3D_CKEY_SRC_BLT:
1623 if (texture == cs->state.textures[0])
1625 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1626 if (!(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1627 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1630 texture->async.src_blt_color_key = op->color_key;
1631 texture->async.color_key_flags |= WINED3D_CKEY_SRC_BLT;
1632 break;
1634 case WINED3D_CKEY_SRC_OVERLAY:
1635 texture->async.src_overlay_color_key = op->color_key;
1636 texture->async.color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
1637 break;
1640 else
1642 switch (op->flags)
1644 case WINED3D_CKEY_DST_BLT:
1645 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_BLT;
1646 break;
1648 case WINED3D_CKEY_DST_OVERLAY:
1649 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
1650 break;
1652 case WINED3D_CKEY_SRC_BLT:
1653 if (texture == cs->state.textures[0] && texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1654 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1656 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
1657 break;
1659 case WINED3D_CKEY_SRC_OVERLAY:
1660 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
1661 break;
1666 void wined3d_cs_emit_set_color_key(struct wined3d_cs *cs, struct wined3d_texture *texture,
1667 WORD flags, const struct wined3d_color_key *color_key)
1669 struct wined3d_cs_set_color_key *op;
1671 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1672 op->opcode = WINED3D_CS_OP_SET_COLOR_KEY;
1673 op->texture = texture;
1674 op->flags = flags;
1675 if (color_key)
1677 op->color_key = *color_key;
1678 op->set = 1;
1680 else
1681 op->set = 0;
1683 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1686 static void wined3d_cs_exec_set_material(struct wined3d_cs *cs, const void *data)
1688 const struct wined3d_cs_set_material *op = data;
1690 cs->state.material = op->material;
1691 device_invalidate_state(cs->device, STATE_MATERIAL);
1694 void wined3d_cs_emit_set_material(struct wined3d_cs *cs, const struct wined3d_material *material)
1696 struct wined3d_cs_set_material *op;
1698 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1699 op->opcode = WINED3D_CS_OP_SET_MATERIAL;
1700 op->material = *material;
1702 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1705 static void wined3d_cs_exec_set_light(struct wined3d_cs *cs, const void *data)
1707 const struct wined3d_cs_set_light *op = data;
1708 struct wined3d_light_info *light_info;
1709 unsigned int light_idx, hash_idx;
1711 light_idx = op->light.OriginalIndex;
1713 if (!(light_info = wined3d_state_get_light(&cs->state, light_idx)))
1715 TRACE("Adding new light.\n");
1716 if (!(light_info = heap_alloc_zero(sizeof(*light_info))))
1718 ERR("Failed to allocate light info.\n");
1719 return;
1722 hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1723 list_add_head(&cs->state.light_map[hash_idx], &light_info->entry);
1724 light_info->glIndex = -1;
1725 light_info->OriginalIndex = light_idx;
1728 if (light_info->glIndex != -1)
1730 if (light_info->OriginalParms.type != op->light.OriginalParms.type)
1731 device_invalidate_state(cs->device, STATE_LIGHT_TYPE);
1732 device_invalidate_state(cs->device, STATE_ACTIVELIGHT(light_info->glIndex));
1735 light_info->OriginalParms = op->light.OriginalParms;
1736 light_info->position = op->light.position;
1737 light_info->direction = op->light.direction;
1738 light_info->exponent = op->light.exponent;
1739 light_info->cutoff = op->light.cutoff;
1742 void wined3d_cs_emit_set_light(struct wined3d_cs *cs, const struct wined3d_light_info *light)
1744 struct wined3d_cs_set_light *op;
1746 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1747 op->opcode = WINED3D_CS_OP_SET_LIGHT;
1748 op->light = *light;
1750 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1753 static void wined3d_cs_exec_set_light_enable(struct wined3d_cs *cs, const void *data)
1755 const struct wined3d_cs_set_light_enable *op = data;
1756 struct wined3d_device *device = cs->device;
1757 struct wined3d_light_info *light_info;
1758 int prev_idx;
1760 if (!(light_info = wined3d_state_get_light(&cs->state, op->idx)))
1762 ERR("Light doesn't exist.\n");
1763 return;
1766 prev_idx = light_info->glIndex;
1767 wined3d_state_enable_light(&cs->state, &device->adapter->d3d_info, light_info, op->enable);
1768 if (light_info->glIndex != prev_idx)
1770 device_invalidate_state(device, STATE_LIGHT_TYPE);
1771 device_invalidate_state(device, STATE_ACTIVELIGHT(op->enable ? light_info->glIndex : prev_idx));
1775 void wined3d_cs_emit_set_light_enable(struct wined3d_cs *cs, unsigned int idx, BOOL enable)
1777 struct wined3d_cs_set_light_enable *op;
1779 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1780 op->opcode = WINED3D_CS_OP_SET_LIGHT_ENABLE;
1781 op->idx = idx;
1782 op->enable = enable;
1784 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1787 static const struct
1789 size_t offset;
1790 size_t size;
1791 DWORD mask;
1793 wined3d_cs_push_constant_info[] =
1795 /* WINED3D_PUSH_CONSTANTS_VS_F */
1796 {FIELD_OFFSET(struct wined3d_state, vs_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_VS_F},
1797 /* WINED3D_PUSH_CONSTANTS_PS_F */
1798 {FIELD_OFFSET(struct wined3d_state, ps_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_PS_F},
1799 /* WINED3D_PUSH_CONSTANTS_VS_I */
1800 {FIELD_OFFSET(struct wined3d_state, vs_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_VS_I},
1801 /* WINED3D_PUSH_CONSTANTS_PS_I */
1802 {FIELD_OFFSET(struct wined3d_state, ps_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_PS_I},
1803 /* WINED3D_PUSH_CONSTANTS_VS_B */
1804 {FIELD_OFFSET(struct wined3d_state, vs_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_VS_B},
1805 /* WINED3D_PUSH_CONSTANTS_PS_B */
1806 {FIELD_OFFSET(struct wined3d_state, ps_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_PS_B},
1809 static void wined3d_cs_st_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1810 unsigned int start_idx, unsigned int count, const void *constants)
1812 struct wined3d_device *device = cs->device;
1813 unsigned int context_count;
1814 unsigned int i;
1815 size_t offset;
1817 if (p == WINED3D_PUSH_CONSTANTS_VS_F)
1818 device->shader_backend->shader_update_float_vertex_constants(device, start_idx, count);
1819 else if (p == WINED3D_PUSH_CONSTANTS_PS_F)
1820 device->shader_backend->shader_update_float_pixel_constants(device, start_idx, count);
1822 offset = wined3d_cs_push_constant_info[p].offset + start_idx * wined3d_cs_push_constant_info[p].size;
1823 memcpy((BYTE *)&cs->state + offset, constants, count * wined3d_cs_push_constant_info[p].size);
1824 for (i = 0, context_count = device->context_count; i < context_count; ++i)
1826 device->contexts[i]->constant_update_mask |= wined3d_cs_push_constant_info[p].mask;
1830 static void wined3d_cs_exec_push_constants(struct wined3d_cs *cs, const void *data)
1832 const struct wined3d_cs_push_constants *op = data;
1834 wined3d_cs_st_push_constants(cs, op->type, op->start_idx, op->count, op->constants);
1837 static void wined3d_cs_mt_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1838 unsigned int start_idx, unsigned int count, const void *constants)
1840 struct wined3d_cs_push_constants *op;
1841 size_t size;
1843 size = count * wined3d_cs_push_constant_info[p].size;
1844 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_push_constants, constants[size]),
1845 WINED3D_CS_QUEUE_DEFAULT);
1846 op->opcode = WINED3D_CS_OP_PUSH_CONSTANTS;
1847 op->type = p;
1848 op->start_idx = start_idx;
1849 op->count = count;
1850 memcpy(op->constants, constants, size);
1852 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1855 static void wined3d_cs_exec_reset_state(struct wined3d_cs *cs, const void *data)
1857 struct wined3d_adapter *adapter = cs->device->adapter;
1859 state_cleanup(&cs->state);
1860 memset(&cs->state, 0, sizeof(cs->state));
1861 state_init(&cs->state, &cs->fb, &adapter->gl_info, &adapter->d3d_info,
1862 WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
1865 void wined3d_cs_emit_reset_state(struct wined3d_cs *cs)
1867 struct wined3d_cs_reset_state *op;
1869 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1870 op->opcode = WINED3D_CS_OP_RESET_STATE;
1872 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1875 static void wined3d_cs_exec_callback(struct wined3d_cs *cs, const void *data)
1877 const struct wined3d_cs_callback *op = data;
1879 op->callback(op->object);
1882 static void wined3d_cs_emit_callback(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1884 struct wined3d_cs_callback *op;
1886 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1887 op->opcode = WINED3D_CS_OP_CALLBACK;
1888 op->callback = callback;
1889 op->object = object;
1891 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1894 void wined3d_cs_destroy_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1896 wined3d_cs_emit_callback(cs, callback, object);
1899 void wined3d_cs_init_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1901 wined3d_cs_emit_callback(cs, callback, object);
1904 static void wined3d_cs_exec_query_issue(struct wined3d_cs *cs, const void *data)
1906 const struct wined3d_cs_query_issue *op = data;
1907 struct wined3d_query *query = op->query;
1908 BOOL poll;
1910 poll = query->query_ops->query_issue(query, op->flags);
1912 if (!cs->thread)
1913 return;
1915 if (poll && list_empty(&query->poll_list_entry))
1917 list_add_tail(&cs->query_poll_list, &query->poll_list_entry);
1918 return;
1921 /* This can happen if occlusion queries are restarted. This discards the
1922 * old result, since polling it could result in a GL error. */
1923 if ((op->flags & WINED3DISSUE_BEGIN) && !poll && !list_empty(&query->poll_list_entry))
1925 list_remove(&query->poll_list_entry);
1926 list_init(&query->poll_list_entry);
1927 InterlockedIncrement(&query->counter_retrieved);
1928 return;
1931 /* This can happen when an occlusion query is ended without being started,
1932 * in which case we don't want to poll, but still have to counter-balance
1933 * the increment of the main counter.
1935 * This can also happen if an event query is re-issued before the first
1936 * fence was reached. In this case the query is already in the list and
1937 * the poll function will check the new fence. We have to counter-balance
1938 * the discarded increment. */
1939 if (op->flags & WINED3DISSUE_END)
1940 InterlockedIncrement(&query->counter_retrieved);
1943 void wined3d_cs_emit_query_issue(struct wined3d_cs *cs, struct wined3d_query *query, DWORD flags)
1945 struct wined3d_cs_query_issue *op;
1947 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1948 op->opcode = WINED3D_CS_OP_QUERY_ISSUE;
1949 op->query = query;
1950 op->flags = flags;
1952 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1953 cs->queries_flushed = FALSE;
1956 static void wined3d_cs_exec_preload_resource(struct wined3d_cs *cs, const void *data)
1958 const struct wined3d_cs_preload_resource *op = data;
1959 struct wined3d_resource *resource = op->resource;
1961 resource->resource_ops->resource_preload(resource);
1962 wined3d_resource_release(resource);
1965 void wined3d_cs_emit_preload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
1967 struct wined3d_cs_preload_resource *op;
1969 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1970 op->opcode = WINED3D_CS_OP_PRELOAD_RESOURCE;
1971 op->resource = resource;
1973 wined3d_resource_acquire(resource);
1975 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1978 static void wined3d_cs_exec_unload_resource(struct wined3d_cs *cs, const void *data)
1980 const struct wined3d_cs_unload_resource *op = data;
1981 struct wined3d_resource *resource = op->resource;
1983 resource->resource_ops->resource_unload(resource);
1984 wined3d_resource_release(resource);
1987 void wined3d_cs_emit_unload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
1989 struct wined3d_cs_unload_resource *op;
1991 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1992 op->opcode = WINED3D_CS_OP_UNLOAD_RESOURCE;
1993 op->resource = resource;
1995 wined3d_resource_acquire(resource);
1997 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2000 static void wined3d_cs_exec_map(struct wined3d_cs *cs, const void *data)
2002 const struct wined3d_cs_map *op = data;
2003 struct wined3d_resource *resource = op->resource;
2005 *op->hr = resource->resource_ops->resource_sub_resource_map(resource,
2006 op->sub_resource_idx, op->map_desc, op->box, op->flags);
2009 HRESULT wined3d_cs_map(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx,
2010 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
2012 struct wined3d_cs_map *op;
2013 HRESULT hr;
2015 /* Mapping resources from the worker thread isn't an issue by itself, but
2016 * increasing the map count would be visible to applications. */
2017 wined3d_not_from_cs(cs);
2019 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2020 op->opcode = WINED3D_CS_OP_MAP;
2021 op->resource = resource;
2022 op->sub_resource_idx = sub_resource_idx;
2023 op->map_desc = map_desc;
2024 op->box = box;
2025 op->flags = flags;
2026 op->hr = &hr;
2028 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2029 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2031 return hr;
2034 static void wined3d_cs_exec_unmap(struct wined3d_cs *cs, const void *data)
2036 const struct wined3d_cs_unmap *op = data;
2037 struct wined3d_resource *resource = op->resource;
2039 *op->hr = resource->resource_ops->resource_sub_resource_unmap(resource, op->sub_resource_idx);
2042 HRESULT wined3d_cs_unmap(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx)
2044 struct wined3d_cs_unmap *op;
2045 HRESULT hr;
2047 wined3d_not_from_cs(cs);
2049 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2050 op->opcode = WINED3D_CS_OP_UNMAP;
2051 op->resource = resource;
2052 op->sub_resource_idx = sub_resource_idx;
2053 op->hr = &hr;
2055 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2056 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2058 return hr;
2061 static void wined3d_cs_exec_blt_sub_resource(struct wined3d_cs *cs, const void *data)
2063 const struct wined3d_cs_blt_sub_resource *op = data;
2065 if (op->dst_resource->type == WINED3D_RTYPE_BUFFER)
2067 wined3d_buffer_copy(buffer_from_resource(op->dst_resource), op->dst_box.left,
2068 buffer_from_resource(op->src_resource), op->src_box.left,
2069 op->src_box.right - op->src_box.left);
2071 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_2D)
2073 if (FAILED(texture2d_blt(texture_from_resource(op->dst_resource), op->dst_sub_resource_idx,
2074 &op->dst_box, texture_from_resource(op->src_resource), op->src_sub_resource_idx,
2075 &op->src_box, op->flags, &op->fx, op->filter)))
2076 FIXME("Blit failed.\n");
2078 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_3D)
2080 struct wined3d_texture *src_texture, *dst_texture;
2081 unsigned int level, update_w, update_h, update_d;
2082 unsigned int row_pitch, slice_pitch;
2083 struct wined3d_context *context;
2084 struct wined3d_bo_address addr;
2086 if (op->flags & ~WINED3D_BLT_RAW)
2088 FIXME("Flags %#x not implemented for %s resources.\n",
2089 op->flags, debug_d3dresourcetype(op->dst_resource->type));
2090 goto error;
2093 if (!(op->flags & WINED3D_BLT_RAW) && op->src_resource->format != op->dst_resource->format)
2095 FIXME("Format conversion not implemented for %s resources.\n",
2096 debug_d3dresourcetype(op->dst_resource->type));
2097 goto error;
2100 update_w = op->dst_box.right - op->dst_box.left;
2101 update_h = op->dst_box.bottom - op->dst_box.top;
2102 update_d = op->dst_box.back - op->dst_box.front;
2103 if (op->src_box.right - op->src_box.left != update_w
2104 || op->src_box.bottom - op->src_box.top != update_h
2105 || op->src_box.back - op->src_box.front != update_d)
2107 FIXME("Stretching not implemented for %s resources.\n",
2108 debug_d3dresourcetype(op->dst_resource->type));
2109 goto error;
2112 dst_texture = texture_from_resource(op->dst_resource);
2113 src_texture = texture_from_resource(op->src_resource);
2115 context = context_acquire(cs->device, NULL, 0);
2117 if (!wined3d_texture_load_location(src_texture, op->src_sub_resource_idx,
2118 context, src_texture->resource.map_binding))
2120 ERR("Failed to load source sub-resource into %s.\n",
2121 wined3d_debug_location(src_texture->resource.map_binding));
2122 context_release(context);
2123 goto error;
2126 level = op->dst_sub_resource_idx % dst_texture->level_count;
2127 if (update_w == wined3d_texture_get_level_width(dst_texture, level)
2128 && update_h == wined3d_texture_get_level_height(dst_texture, level)
2129 && update_d == wined3d_texture_get_level_depth(dst_texture, level))
2131 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
2133 else if (!wined3d_texture_load_location(dst_texture, op->dst_sub_resource_idx,
2134 context, WINED3D_LOCATION_TEXTURE_RGB))
2136 ERR("Failed to load destination sub-resource.\n");
2137 context_release(context);
2138 goto error;
2141 wined3d_texture_get_memory(src_texture, op->src_sub_resource_idx, &addr, src_texture->resource.map_binding);
2142 wined3d_texture_get_pitch(src_texture, op->src_sub_resource_idx % src_texture->level_count,
2143 &row_pitch, &slice_pitch);
2145 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
2146 wined3d_texture_upload_data(dst_texture, op->dst_sub_resource_idx, context,
2147 dst_texture->resource.format, &op->src_box, wined3d_const_bo_address(&addr),
2148 row_pitch, slice_pitch, op->dst_box.left, op->dst_box.top, op->dst_box.front, FALSE);
2149 wined3d_texture_validate_location(dst_texture, op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2150 wined3d_texture_invalidate_location(dst_texture, op->dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2152 context_release(context);
2154 else
2156 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(op->dst_resource->type));
2159 error:
2160 if (op->src_resource)
2161 wined3d_resource_release(op->src_resource);
2162 wined3d_resource_release(op->dst_resource);
2165 void wined3d_cs_emit_blt_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *dst_resource,
2166 unsigned int dst_sub_resource_idx, const struct wined3d_box *dst_box, struct wined3d_resource *src_resource,
2167 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, DWORD flags,
2168 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2170 struct wined3d_cs_blt_sub_resource *op;
2172 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2173 op->opcode = WINED3D_CS_OP_BLT_SUB_RESOURCE;
2174 op->dst_resource = dst_resource;
2175 op->dst_sub_resource_idx = dst_sub_resource_idx;
2176 op->dst_box = *dst_box;
2177 op->src_resource = src_resource;
2178 op->src_sub_resource_idx = src_sub_resource_idx;
2179 op->src_box = *src_box;
2180 op->flags = flags;
2181 if (fx)
2182 op->fx = *fx;
2183 else
2184 memset(&op->fx, 0, sizeof(op->fx));
2185 op->filter = filter;
2187 wined3d_resource_acquire(dst_resource);
2188 if (src_resource)
2189 wined3d_resource_acquire(src_resource);
2191 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2192 if (flags & WINED3D_BLT_SYNCHRONOUS)
2193 cs->ops->finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2196 static void wined3d_cs_exec_update_sub_resource(struct wined3d_cs *cs, const void *data)
2198 const struct wined3d_cs_update_sub_resource *op = data;
2199 struct wined3d_resource *resource = op->resource;
2200 const struct wined3d_box *box = &op->box;
2201 unsigned int width, height, depth, level;
2202 struct wined3d_const_bo_address addr;
2203 struct wined3d_context *context;
2204 struct wined3d_texture *texture;
2205 struct wined3d_box src_box;
2207 context = context_acquire(cs->device, NULL, 0);
2209 if (resource->type == WINED3D_RTYPE_BUFFER)
2211 struct wined3d_buffer *buffer = buffer_from_resource(resource);
2213 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
2215 ERR("Failed to load buffer location.\n");
2216 goto done;
2219 wined3d_buffer_upload_data(buffer, context, box, op->data.data);
2220 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
2221 goto done;
2224 texture = wined3d_texture_from_resource(resource);
2226 level = op->sub_resource_idx % texture->level_count;
2227 width = wined3d_texture_get_level_width(texture, level);
2228 height = wined3d_texture_get_level_height(texture, level);
2229 depth = wined3d_texture_get_level_depth(texture, level);
2231 addr.buffer_object = 0;
2232 addr.addr = op->data.data;
2234 /* Only load the sub-resource for partial updates. */
2235 if (!box->left && !box->top && !box->front
2236 && box->right == width && box->bottom == height && box->back == depth)
2237 wined3d_texture_prepare_texture(texture, context, FALSE);
2238 else
2239 wined3d_texture_load_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2240 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2242 wined3d_box_set(&src_box, 0, 0, box->right - box->left, box->bottom - box->top, 0, box->back - box->front);
2243 wined3d_texture_upload_data(texture, op->sub_resource_idx, context, texture->resource.format, &src_box,
2244 &addr, op->data.row_pitch, op->data.slice_pitch, box->left, box->top, box->front, FALSE);
2246 wined3d_texture_validate_location(texture, op->sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2247 wined3d_texture_invalidate_location(texture, op->sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2249 done:
2250 context_release(context);
2252 wined3d_resource_release(resource);
2255 void wined3d_cs_emit_update_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *resource,
2256 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
2257 unsigned int slice_pitch)
2259 struct wined3d_cs_update_sub_resource *op;
2261 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2262 op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2263 op->resource = resource;
2264 op->sub_resource_idx = sub_resource_idx;
2265 op->box = *box;
2266 op->data.row_pitch = row_pitch;
2267 op->data.slice_pitch = slice_pitch;
2268 op->data.data = data;
2270 wined3d_resource_acquire(resource);
2272 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2273 /* The data pointer may go away, so we need to wait until it is read.
2274 * Copying the data may be faster if it's small. */
2275 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2278 static void wined3d_cs_exec_add_dirty_texture_region(struct wined3d_cs *cs, const void *data)
2280 const struct wined3d_cs_add_dirty_texture_region *op = data;
2281 struct wined3d_texture *texture = op->texture;
2282 unsigned int sub_resource_idx, i;
2283 struct wined3d_context *context;
2285 context = context_acquire(cs->device, NULL, 0);
2286 sub_resource_idx = op->layer * texture->level_count;
2287 for (i = 0; i < texture->level_count; ++i, ++sub_resource_idx)
2289 if (wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
2290 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2291 else
2292 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
2294 context_release(context);
2296 wined3d_resource_release(&texture->resource);
2299 void wined3d_cs_emit_add_dirty_texture_region(struct wined3d_cs *cs,
2300 struct wined3d_texture *texture, unsigned int layer)
2302 struct wined3d_cs_add_dirty_texture_region *op;
2304 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2305 op->opcode = WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION;
2306 op->texture = texture;
2307 op->layer = layer;
2309 wined3d_resource_acquire(&texture->resource);
2311 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2314 static void wined3d_cs_exec_clear_unordered_access_view(struct wined3d_cs *cs, const void *data)
2316 const struct wined3d_cs_clear_unordered_access_view *op = data;
2317 struct wined3d_unordered_access_view *view = op->view;
2318 struct wined3d_context *context;
2320 context = context_acquire(cs->device, NULL, 0);
2321 wined3d_unordered_access_view_clear_uint(view, &op->clear_value, context);
2322 context_release(context);
2324 wined3d_resource_release(view->resource);
2327 void wined3d_cs_emit_clear_unordered_access_view_uint(struct wined3d_cs *cs,
2328 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
2330 struct wined3d_cs_clear_unordered_access_view *op;
2332 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2333 op->opcode = WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW;
2334 op->view = view;
2335 op->clear_value = *clear_value;
2337 wined3d_resource_acquire(view->resource);
2339 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2342 static void wined3d_cs_exec_copy_uav_counter(struct wined3d_cs *cs, const void *data)
2344 const struct wined3d_cs_copy_uav_counter *op = data;
2345 struct wined3d_unordered_access_view *view = op->view;
2346 struct wined3d_context *context;
2348 context = context_acquire(cs->device, NULL, 0);
2349 wined3d_unordered_access_view_copy_counter(view, op->buffer, op->offset, context);
2350 context_release(context);
2352 wined3d_resource_release(&op->buffer->resource);
2355 void wined3d_cs_emit_copy_uav_counter(struct wined3d_cs *cs, struct wined3d_buffer *dst_buffer,
2356 unsigned int offset, struct wined3d_unordered_access_view *uav)
2358 struct wined3d_cs_copy_uav_counter *op;
2360 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2361 op->opcode = WINED3D_CS_OP_COPY_UAV_COUNTER;
2362 op->buffer = dst_buffer;
2363 op->offset = offset;
2364 op->view = uav;
2366 wined3d_resource_acquire(&dst_buffer->resource);
2368 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2371 static void wined3d_cs_exec_generate_mipmaps(struct wined3d_cs *cs, const void *data)
2373 const struct wined3d_cs_generate_mipmaps *op = data;
2374 struct wined3d_shader_resource_view *view = op->view;
2376 shader_resource_view_generate_mipmaps(view);
2377 wined3d_resource_release(view->resource);
2380 void wined3d_cs_emit_generate_mipmaps(struct wined3d_cs *cs, struct wined3d_shader_resource_view *view)
2382 struct wined3d_cs_generate_mipmaps *op;
2384 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2385 op->opcode = WINED3D_CS_OP_GENERATE_MIPMAPS;
2386 op->view = view;
2388 wined3d_resource_acquire(view->resource);
2390 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2393 static void wined3d_cs_emit_stop(struct wined3d_cs *cs)
2395 struct wined3d_cs_stop *op;
2397 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2398 op->opcode = WINED3D_CS_OP_STOP;
2400 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2401 cs->ops->finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2404 static void (* const wined3d_cs_op_handlers[])(struct wined3d_cs *cs, const void *data) =
2406 /* WINED3D_CS_OP_NOP */ wined3d_cs_exec_nop,
2407 /* WINED3D_CS_OP_PRESENT */ wined3d_cs_exec_present,
2408 /* WINED3D_CS_OP_CLEAR */ wined3d_cs_exec_clear,
2409 /* WINED3D_CS_OP_DISPATCH */ wined3d_cs_exec_dispatch,
2410 /* WINED3D_CS_OP_DRAW */ wined3d_cs_exec_draw,
2411 /* WINED3D_CS_OP_FLUSH */ wined3d_cs_exec_flush,
2412 /* WINED3D_CS_OP_SET_PREDICATION */ wined3d_cs_exec_set_predication,
2413 /* WINED3D_CS_OP_SET_VIEWPORTS */ wined3d_cs_exec_set_viewports,
2414 /* WINED3D_CS_OP_SET_SCISSOR_RECT */ wined3d_cs_exec_set_scissor_rect,
2415 /* WINED3D_CS_OP_SET_RENDERTARGET_VIEW */ wined3d_cs_exec_set_rendertarget_view,
2416 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW */ wined3d_cs_exec_set_depth_stencil_view,
2417 /* WINED3D_CS_OP_SET_VERTEX_DECLARATION */ wined3d_cs_exec_set_vertex_declaration,
2418 /* WINED3D_CS_OP_SET_STREAM_SOURCE */ wined3d_cs_exec_set_stream_source,
2419 /* WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ */ wined3d_cs_exec_set_stream_source_freq,
2420 /* WINED3D_CS_OP_SET_STREAM_OUTPUT */ wined3d_cs_exec_set_stream_output,
2421 /* WINED3D_CS_OP_SET_INDEX_BUFFER */ wined3d_cs_exec_set_index_buffer,
2422 /* WINED3D_CS_OP_SET_CONSTANT_BUFFER */ wined3d_cs_exec_set_constant_buffer,
2423 /* WINED3D_CS_OP_SET_TEXTURE */ wined3d_cs_exec_set_texture,
2424 /* WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW */ wined3d_cs_exec_set_shader_resource_view,
2425 /* WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_set_unordered_access_view,
2426 /* WINED3D_CS_OP_SET_SAMPLER */ wined3d_cs_exec_set_sampler,
2427 /* WINED3D_CS_OP_SET_SHADER */ wined3d_cs_exec_set_shader,
2428 /* WINED3D_CS_OP_SET_BLEND_STATE */ wined3d_cs_exec_set_blend_state,
2429 /* WINED3D_CS_OP_SET_RASTERIZER_STATE */ wined3d_cs_exec_set_rasterizer_state,
2430 /* WINED3D_CS_OP_SET_RENDER_STATE */ wined3d_cs_exec_set_render_state,
2431 /* WINED3D_CS_OP_SET_TEXTURE_STATE */ wined3d_cs_exec_set_texture_state,
2432 /* WINED3D_CS_OP_SET_SAMPLER_STATE */ wined3d_cs_exec_set_sampler_state,
2433 /* WINED3D_CS_OP_SET_TRANSFORM */ wined3d_cs_exec_set_transform,
2434 /* WINED3D_CS_OP_SET_CLIP_PLANE */ wined3d_cs_exec_set_clip_plane,
2435 /* WINED3D_CS_OP_SET_COLOR_KEY */ wined3d_cs_exec_set_color_key,
2436 /* WINED3D_CS_OP_SET_MATERIAL */ wined3d_cs_exec_set_material,
2437 /* WINED3D_CS_OP_SET_LIGHT */ wined3d_cs_exec_set_light,
2438 /* WINED3D_CS_OP_SET_LIGHT_ENABLE */ wined3d_cs_exec_set_light_enable,
2439 /* WINED3D_CS_OP_PUSH_CONSTANTS */ wined3d_cs_exec_push_constants,
2440 /* WINED3D_CS_OP_RESET_STATE */ wined3d_cs_exec_reset_state,
2441 /* WINED3D_CS_OP_CALLBACK */ wined3d_cs_exec_callback,
2442 /* WINED3D_CS_OP_QUERY_ISSUE */ wined3d_cs_exec_query_issue,
2443 /* WINED3D_CS_OP_PRELOAD_RESOURCE */ wined3d_cs_exec_preload_resource,
2444 /* WINED3D_CS_OP_UNLOAD_RESOURCE */ wined3d_cs_exec_unload_resource,
2445 /* WINED3D_CS_OP_MAP */ wined3d_cs_exec_map,
2446 /* WINED3D_CS_OP_UNMAP */ wined3d_cs_exec_unmap,
2447 /* WINED3D_CS_OP_BLT_SUB_RESOURCE */ wined3d_cs_exec_blt_sub_resource,
2448 /* WINED3D_CS_OP_UPDATE_SUB_RESOURCE */ wined3d_cs_exec_update_sub_resource,
2449 /* WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION */ wined3d_cs_exec_add_dirty_texture_region,
2450 /* WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_clear_unordered_access_view,
2451 /* WINED3D_CS_OP_COPY_UAV_COUNTER */ wined3d_cs_exec_copy_uav_counter,
2452 /* WINED3D_CS_OP_GENERATE_MIPMAPS */ wined3d_cs_exec_generate_mipmaps,
2455 static void *wined3d_cs_st_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2457 if (size > (cs->data_size - cs->end))
2459 size_t new_size;
2460 void *new_data;
2462 new_size = max(size, cs->data_size * 2);
2463 if (!cs->end)
2464 new_data = heap_realloc(cs->data, new_size);
2465 else
2466 new_data = heap_alloc(new_size);
2467 if (!new_data)
2468 return NULL;
2470 cs->data_size = new_size;
2471 cs->start = cs->end = 0;
2472 cs->data = new_data;
2475 cs->end += size;
2477 return (BYTE *)cs->data + cs->start;
2480 static void wined3d_cs_st_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2482 enum wined3d_cs_op opcode;
2483 size_t start;
2484 BYTE *data;
2486 data = cs->data;
2487 start = cs->start;
2488 cs->start = cs->end;
2490 opcode = *(const enum wined3d_cs_op *)&data[start];
2491 if (opcode >= WINED3D_CS_OP_STOP)
2492 ERR("Invalid opcode %#x.\n", opcode);
2493 else
2494 wined3d_cs_op_handlers[opcode](cs, &data[start]);
2496 if (cs->data == data)
2497 cs->start = cs->end = start;
2498 else if (!start)
2499 heap_free(data);
2502 static void wined3d_cs_st_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2506 static const struct wined3d_cs_ops wined3d_cs_st_ops =
2508 wined3d_cs_st_require_space,
2509 wined3d_cs_st_submit,
2510 wined3d_cs_st_finish,
2511 wined3d_cs_st_push_constants,
2514 static BOOL wined3d_cs_queue_is_empty(const struct wined3d_cs *cs, const struct wined3d_cs_queue *queue)
2516 wined3d_from_cs(cs);
2517 return *(volatile LONG *)&queue->head == queue->tail;
2520 static void wined3d_cs_queue_submit(struct wined3d_cs_queue *queue, struct wined3d_cs *cs)
2522 struct wined3d_cs_packet *packet;
2523 size_t packet_size;
2525 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2526 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2527 InterlockedExchange(&queue->head, (queue->head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1));
2529 if (InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2530 SetEvent(cs->event);
2533 static void wined3d_cs_mt_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2535 if (cs->thread_id == GetCurrentThreadId())
2536 return wined3d_cs_st_submit(cs, queue_id);
2538 wined3d_cs_queue_submit(&cs->queue[queue_id], cs);
2541 static void *wined3d_cs_queue_require_space(struct wined3d_cs_queue *queue, size_t size, struct wined3d_cs *cs)
2543 size_t queue_size = ARRAY_SIZE(queue->data);
2544 size_t header_size, packet_size, remaining;
2545 struct wined3d_cs_packet *packet;
2547 header_size = FIELD_OFFSET(struct wined3d_cs_packet, data[0]);
2548 size = (size + header_size - 1) & ~(header_size - 1);
2549 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[size]);
2550 if (packet_size >= WINED3D_CS_QUEUE_SIZE)
2552 ERR("Packet size %lu >= queue size %u.\n",
2553 (unsigned long)packet_size, WINED3D_CS_QUEUE_SIZE);
2554 return NULL;
2557 remaining = queue_size - queue->head;
2558 if (remaining < packet_size)
2560 size_t nop_size = remaining - header_size;
2561 struct wined3d_cs_nop *nop;
2563 TRACE("Inserting a nop for %lu + %lu bytes.\n",
2564 (unsigned long)header_size, (unsigned long)nop_size);
2566 nop = wined3d_cs_queue_require_space(queue, nop_size, cs);
2567 if (nop_size)
2568 nop->opcode = WINED3D_CS_OP_NOP;
2570 wined3d_cs_queue_submit(queue, cs);
2571 assert(!queue->head);
2574 for (;;)
2576 LONG tail = *(volatile LONG *)&queue->tail;
2577 LONG head = queue->head;
2578 LONG new_pos;
2580 /* Empty. */
2581 if (head == tail)
2582 break;
2583 new_pos = (head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1);
2584 /* Head ahead of tail. We checked the remaining size above, so we only
2585 * need to make sure we don't make head equal to tail. */
2586 if (head > tail && (new_pos != tail))
2587 break;
2588 /* Tail ahead of head. Make sure the new head is before the tail as
2589 * well. Note that new_pos is 0 when it's at the end of the queue. */
2590 if (new_pos < tail && new_pos)
2591 break;
2593 TRACE("Waiting for free space. Head %u, tail %u, packet size %lu.\n",
2594 head, tail, (unsigned long)packet_size);
2597 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2598 packet->size = size;
2599 return packet->data;
2602 static void *wined3d_cs_mt_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2604 if (cs->thread_id == GetCurrentThreadId())
2605 return wined3d_cs_st_require_space(cs, size, queue_id);
2607 return wined3d_cs_queue_require_space(&cs->queue[queue_id], size, cs);
2610 static void wined3d_cs_mt_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2612 if (cs->thread_id == GetCurrentThreadId())
2613 return wined3d_cs_st_finish(cs, queue_id);
2615 while (cs->queue[queue_id].head != *(volatile LONG *)&cs->queue[queue_id].tail)
2616 wined3d_pause();
2619 static const struct wined3d_cs_ops wined3d_cs_mt_ops =
2621 wined3d_cs_mt_require_space,
2622 wined3d_cs_mt_submit,
2623 wined3d_cs_mt_finish,
2624 wined3d_cs_mt_push_constants,
2627 static void poll_queries(struct wined3d_cs *cs)
2629 struct wined3d_query *query, *cursor;
2631 LIST_FOR_EACH_ENTRY_SAFE(query, cursor, &cs->query_poll_list, struct wined3d_query, poll_list_entry)
2633 if (!query->query_ops->query_poll(query, 0))
2634 continue;
2636 list_remove(&query->poll_list_entry);
2637 list_init(&query->poll_list_entry);
2638 InterlockedIncrement(&query->counter_retrieved);
2642 static void wined3d_cs_wait_event(struct wined3d_cs *cs)
2644 InterlockedExchange(&cs->waiting_for_event, TRUE);
2646 /* The main thread might have enqueued a command and blocked on it after
2647 * the CS thread decided to enter wined3d_cs_wait_event(), but before
2648 * "waiting_for_event" was set.
2650 * Likewise, we can race with the main thread when resetting
2651 * "waiting_for_event", in which case we would need to call
2652 * WaitForSingleObject() because the main thread called SetEvent(). */
2653 if (!(wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_DEFAULT])
2654 && wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_MAP]))
2655 && InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2656 return;
2658 WaitForSingleObject(cs->event, INFINITE);
2661 static DWORD WINAPI wined3d_cs_run(void *ctx)
2663 struct wined3d_cs_packet *packet;
2664 struct wined3d_cs_queue *queue;
2665 unsigned int spin_count = 0;
2666 struct wined3d_cs *cs = ctx;
2667 enum wined3d_cs_op opcode;
2668 HMODULE wined3d_module;
2669 unsigned int poll = 0;
2670 LONG tail;
2672 TRACE("Started.\n");
2674 /* Copy the module handle to a local variable to avoid racing with the
2675 * thread freeing "cs" before the FreeLibraryAndExitThread() call. */
2676 wined3d_module = cs->wined3d_module;
2678 list_init(&cs->query_poll_list);
2679 cs->thread_id = GetCurrentThreadId();
2680 for (;;)
2682 if (++poll == WINED3D_CS_QUERY_POLL_INTERVAL)
2684 poll_queries(cs);
2685 poll = 0;
2688 queue = &cs->queue[WINED3D_CS_QUEUE_MAP];
2689 if (wined3d_cs_queue_is_empty(cs, queue))
2691 queue = &cs->queue[WINED3D_CS_QUEUE_DEFAULT];
2692 if (wined3d_cs_queue_is_empty(cs, queue))
2694 if (++spin_count >= WINED3D_CS_SPIN_COUNT && list_empty(&cs->query_poll_list))
2695 wined3d_cs_wait_event(cs);
2696 continue;
2699 spin_count = 0;
2701 tail = queue->tail;
2702 packet = (struct wined3d_cs_packet *)&queue->data[tail];
2703 if (packet->size)
2705 opcode = *(const enum wined3d_cs_op *)packet->data;
2707 if (opcode >= WINED3D_CS_OP_STOP)
2709 if (opcode > WINED3D_CS_OP_STOP)
2710 ERR("Invalid opcode %#x.\n", opcode);
2711 break;
2714 wined3d_cs_op_handlers[opcode](cs, packet->data);
2717 tail += FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2718 tail &= (WINED3D_CS_QUEUE_SIZE - 1);
2719 InterlockedExchange(&queue->tail, tail);
2722 cs->queue[WINED3D_CS_QUEUE_MAP].tail = cs->queue[WINED3D_CS_QUEUE_MAP].head;
2723 cs->queue[WINED3D_CS_QUEUE_DEFAULT].tail = cs->queue[WINED3D_CS_QUEUE_DEFAULT].head;
2724 TRACE("Stopped.\n");
2725 FreeLibraryAndExitThread(wined3d_module, 0);
2728 struct wined3d_cs *wined3d_cs_create(struct wined3d_device *device)
2730 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2731 struct wined3d_cs *cs;
2733 if (!(cs = heap_alloc_zero(sizeof(*cs))))
2734 return NULL;
2736 cs->ops = &wined3d_cs_st_ops;
2737 cs->device = device;
2739 state_init(&cs->state, &cs->fb, gl_info, &device->adapter->d3d_info,
2740 WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
2742 cs->data_size = WINED3D_INITIAL_CS_SIZE;
2743 if (!(cs->data = heap_alloc(cs->data_size)))
2744 goto fail;
2746 if (wined3d_settings.cs_multithreaded
2747 && !RtlIsCriticalSectionLockedByThread(NtCurrentTeb()->Peb->LoaderLock))
2749 cs->ops = &wined3d_cs_mt_ops;
2751 if (!(cs->event = CreateEventW(NULL, FALSE, FALSE, NULL)))
2753 ERR("Failed to create command stream event.\n");
2754 heap_free(cs->data);
2755 goto fail;
2758 if (!(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2759 (const WCHAR *)wined3d_cs_run, &cs->wined3d_module)))
2761 ERR("Failed to get wined3d module handle.\n");
2762 CloseHandle(cs->event);
2763 heap_free(cs->data);
2764 goto fail;
2767 if (!(cs->thread = CreateThread(NULL, 0, wined3d_cs_run, cs, 0, NULL)))
2769 ERR("Failed to create wined3d command stream thread.\n");
2770 FreeLibrary(cs->wined3d_module);
2771 CloseHandle(cs->event);
2772 heap_free(cs->data);
2773 goto fail;
2777 return cs;
2779 fail:
2780 state_cleanup(&cs->state);
2781 heap_free(cs);
2782 return NULL;
2785 void wined3d_cs_destroy(struct wined3d_cs *cs)
2787 if (cs->thread)
2789 wined3d_cs_emit_stop(cs);
2790 CloseHandle(cs->thread);
2791 if (!CloseHandle(cs->event))
2792 ERR("Closing event failed.\n");
2795 state_cleanup(&cs->state);
2796 heap_free(cs->data);
2797 heap_free(cs);