msvcrt: _Gettnames() should respect user overrides.
[wine.git] / dlls / wined3d / cs.c
blob78443d174a27fb14d63739161c7a366d136989e7
1 /*
2 * Copyright 2013 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
21 #include "wined3d_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
25 #define WINED3D_INITIAL_CS_SIZE 4096
27 enum wined3d_cs_op
29 WINED3D_CS_OP_NOP,
30 WINED3D_CS_OP_PRESENT,
31 WINED3D_CS_OP_CLEAR,
32 WINED3D_CS_OP_DISPATCH,
33 WINED3D_CS_OP_DRAW,
34 WINED3D_CS_OP_FLUSH,
35 WINED3D_CS_OP_SET_PREDICATION,
36 WINED3D_CS_OP_SET_VIEWPORT,
37 WINED3D_CS_OP_SET_SCISSOR_RECT,
38 WINED3D_CS_OP_SET_RENDERTARGET_VIEW,
39 WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW,
40 WINED3D_CS_OP_SET_VERTEX_DECLARATION,
41 WINED3D_CS_OP_SET_STREAM_SOURCE,
42 WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ,
43 WINED3D_CS_OP_SET_STREAM_OUTPUT,
44 WINED3D_CS_OP_SET_INDEX_BUFFER,
45 WINED3D_CS_OP_SET_CONSTANT_BUFFER,
46 WINED3D_CS_OP_SET_TEXTURE,
47 WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW,
48 WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW,
49 WINED3D_CS_OP_SET_SAMPLER,
50 WINED3D_CS_OP_SET_SHADER,
51 WINED3D_CS_OP_SET_RASTERIZER_STATE,
52 WINED3D_CS_OP_SET_RENDER_STATE,
53 WINED3D_CS_OP_SET_TEXTURE_STATE,
54 WINED3D_CS_OP_SET_SAMPLER_STATE,
55 WINED3D_CS_OP_SET_TRANSFORM,
56 WINED3D_CS_OP_SET_CLIP_PLANE,
57 WINED3D_CS_OP_SET_COLOR_KEY,
58 WINED3D_CS_OP_SET_MATERIAL,
59 WINED3D_CS_OP_SET_LIGHT,
60 WINED3D_CS_OP_SET_LIGHT_ENABLE,
61 WINED3D_CS_OP_PUSH_CONSTANTS,
62 WINED3D_CS_OP_RESET_STATE,
63 WINED3D_CS_OP_CALLBACK,
64 WINED3D_CS_OP_QUERY_ISSUE,
65 WINED3D_CS_OP_PRELOAD_RESOURCE,
66 WINED3D_CS_OP_UNLOAD_RESOURCE,
67 WINED3D_CS_OP_MAP,
68 WINED3D_CS_OP_UNMAP,
69 WINED3D_CS_OP_BLT_SUB_RESOURCE,
70 WINED3D_CS_OP_UPDATE_SUB_RESOURCE,
71 WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION,
72 WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW,
73 WINED3D_CS_OP_COPY_UAV_COUNTER,
74 WINED3D_CS_OP_GENERATE_MIPMAPS,
75 WINED3D_CS_OP_STOP,
78 struct wined3d_cs_packet
80 size_t size;
81 BYTE data[1];
84 struct wined3d_cs_nop
86 enum wined3d_cs_op opcode;
89 struct wined3d_cs_present
91 enum wined3d_cs_op opcode;
92 HWND dst_window_override;
93 struct wined3d_swapchain *swapchain;
94 RECT src_rect;
95 RECT dst_rect;
96 DWORD flags;
99 struct wined3d_cs_clear
101 enum wined3d_cs_op opcode;
102 DWORD flags;
103 unsigned int rt_count;
104 struct wined3d_fb_state *fb;
105 RECT draw_rect;
106 struct wined3d_color color;
107 float depth;
108 DWORD stencil;
109 unsigned int rect_count;
110 RECT rects[1];
113 struct wined3d_cs_dispatch
115 enum wined3d_cs_op opcode;
116 struct wined3d_dispatch_parameters parameters;
119 struct wined3d_cs_draw
121 enum wined3d_cs_op opcode;
122 GLenum primitive_type;
123 GLint patch_vertex_count;
124 struct wined3d_draw_parameters parameters;
127 struct wined3d_cs_flush
129 enum wined3d_cs_op opcode;
132 struct wined3d_cs_set_predication
134 enum wined3d_cs_op opcode;
135 struct wined3d_query *predicate;
136 BOOL value;
139 struct wined3d_cs_set_viewport
141 enum wined3d_cs_op opcode;
142 struct wined3d_viewport viewport;
145 struct wined3d_cs_set_scissor_rect
147 enum wined3d_cs_op opcode;
148 RECT rect;
151 struct wined3d_cs_set_rendertarget_view
153 enum wined3d_cs_op opcode;
154 unsigned int view_idx;
155 struct wined3d_rendertarget_view *view;
158 struct wined3d_cs_set_depth_stencil_view
160 enum wined3d_cs_op opcode;
161 struct wined3d_rendertarget_view *view;
164 struct wined3d_cs_set_vertex_declaration
166 enum wined3d_cs_op opcode;
167 struct wined3d_vertex_declaration *declaration;
170 struct wined3d_cs_set_stream_source
172 enum wined3d_cs_op opcode;
173 UINT stream_idx;
174 struct wined3d_buffer *buffer;
175 UINT offset;
176 UINT stride;
179 struct wined3d_cs_set_stream_source_freq
181 enum wined3d_cs_op opcode;
182 UINT stream_idx;
183 UINT frequency;
184 UINT flags;
187 struct wined3d_cs_set_stream_output
189 enum wined3d_cs_op opcode;
190 UINT stream_idx;
191 struct wined3d_buffer *buffer;
192 UINT offset;
195 struct wined3d_cs_set_index_buffer
197 enum wined3d_cs_op opcode;
198 struct wined3d_buffer *buffer;
199 enum wined3d_format_id format_id;
200 unsigned int offset;
203 struct wined3d_cs_set_constant_buffer
205 enum wined3d_cs_op opcode;
206 enum wined3d_shader_type type;
207 UINT cb_idx;
208 struct wined3d_buffer *buffer;
211 struct wined3d_cs_set_texture
213 enum wined3d_cs_op opcode;
214 UINT stage;
215 struct wined3d_texture *texture;
218 struct wined3d_cs_set_color_key
220 enum wined3d_cs_op opcode;
221 struct wined3d_texture *texture;
222 WORD flags;
223 WORD set;
224 struct wined3d_color_key color_key;
227 struct wined3d_cs_set_shader_resource_view
229 enum wined3d_cs_op opcode;
230 enum wined3d_shader_type type;
231 UINT view_idx;
232 struct wined3d_shader_resource_view *view;
235 struct wined3d_cs_set_unordered_access_view
237 enum wined3d_cs_op opcode;
238 enum wined3d_pipeline pipeline;
239 unsigned int view_idx;
240 struct wined3d_unordered_access_view *view;
241 unsigned int initial_count;
244 struct wined3d_cs_set_sampler
246 enum wined3d_cs_op opcode;
247 enum wined3d_shader_type type;
248 UINT sampler_idx;
249 struct wined3d_sampler *sampler;
252 struct wined3d_cs_set_shader
254 enum wined3d_cs_op opcode;
255 enum wined3d_shader_type type;
256 struct wined3d_shader *shader;
259 struct wined3d_cs_set_rasterizer_state
261 enum wined3d_cs_op opcode;
262 struct wined3d_rasterizer_state *state;
265 struct wined3d_cs_set_render_state
267 enum wined3d_cs_op opcode;
268 enum wined3d_render_state state;
269 DWORD value;
272 struct wined3d_cs_set_texture_state
274 enum wined3d_cs_op opcode;
275 UINT stage;
276 enum wined3d_texture_stage_state state;
277 DWORD value;
280 struct wined3d_cs_set_sampler_state
282 enum wined3d_cs_op opcode;
283 UINT sampler_idx;
284 enum wined3d_sampler_state state;
285 DWORD value;
288 struct wined3d_cs_set_transform
290 enum wined3d_cs_op opcode;
291 enum wined3d_transform_state state;
292 struct wined3d_matrix matrix;
295 struct wined3d_cs_set_clip_plane
297 enum wined3d_cs_op opcode;
298 UINT plane_idx;
299 struct wined3d_vec4 plane;
302 struct wined3d_cs_set_material
304 enum wined3d_cs_op opcode;
305 struct wined3d_material material;
308 struct wined3d_cs_set_light
310 enum wined3d_cs_op opcode;
311 struct wined3d_light_info light;
314 struct wined3d_cs_set_light_enable
316 enum wined3d_cs_op opcode;
317 unsigned int idx;
318 BOOL enable;
321 struct wined3d_cs_push_constants
323 enum wined3d_cs_op opcode;
324 enum wined3d_push_constants type;
325 unsigned int start_idx;
326 unsigned int count;
327 BYTE constants[1];
330 struct wined3d_cs_reset_state
332 enum wined3d_cs_op opcode;
335 struct wined3d_cs_callback
337 enum wined3d_cs_op opcode;
338 void (*callback)(void *object);
339 void *object;
342 struct wined3d_cs_query_issue
344 enum wined3d_cs_op opcode;
345 struct wined3d_query *query;
346 DWORD flags;
349 struct wined3d_cs_preload_resource
351 enum wined3d_cs_op opcode;
352 struct wined3d_resource *resource;
355 struct wined3d_cs_unload_resource
357 enum wined3d_cs_op opcode;
358 struct wined3d_resource *resource;
361 struct wined3d_cs_map
363 enum wined3d_cs_op opcode;
364 struct wined3d_resource *resource;
365 unsigned int sub_resource_idx;
366 struct wined3d_map_desc *map_desc;
367 const struct wined3d_box *box;
368 DWORD flags;
369 HRESULT *hr;
372 struct wined3d_cs_unmap
374 enum wined3d_cs_op opcode;
375 struct wined3d_resource *resource;
376 unsigned int sub_resource_idx;
377 HRESULT *hr;
380 struct wined3d_cs_blt_sub_resource
382 enum wined3d_cs_op opcode;
383 struct wined3d_resource *dst_resource;
384 unsigned int dst_sub_resource_idx;
385 struct wined3d_box dst_box;
386 struct wined3d_resource *src_resource;
387 unsigned int src_sub_resource_idx;
388 struct wined3d_box src_box;
389 DWORD flags;
390 struct wined3d_blt_fx fx;
391 enum wined3d_texture_filter_type filter;
394 struct wined3d_cs_update_sub_resource
396 enum wined3d_cs_op opcode;
397 struct wined3d_resource *resource;
398 unsigned int sub_resource_idx;
399 struct wined3d_box box;
400 struct wined3d_sub_resource_data data;
403 struct wined3d_cs_add_dirty_texture_region
405 enum wined3d_cs_op opcode;
406 struct wined3d_texture *texture;
407 unsigned int layer;
410 struct wined3d_cs_clear_unordered_access_view
412 enum wined3d_cs_op opcode;
413 struct wined3d_unordered_access_view *view;
414 struct wined3d_uvec4 clear_value;
417 struct wined3d_cs_copy_uav_counter
419 enum wined3d_cs_op opcode;
420 struct wined3d_buffer *buffer;
421 unsigned int offset;
422 struct wined3d_unordered_access_view *view;
425 struct wined3d_cs_generate_mipmaps
427 enum wined3d_cs_op opcode;
428 struct wined3d_shader_resource_view *view;
431 struct wined3d_cs_stop
433 enum wined3d_cs_op opcode;
436 static void wined3d_cs_exec_nop(struct wined3d_cs *cs, const void *data)
440 static void wined3d_cs_exec_present(struct wined3d_cs *cs, const void *data)
442 const struct wined3d_cs_present *op = data;
443 struct wined3d_swapchain *swapchain;
444 unsigned int i;
446 swapchain = op->swapchain;
447 wined3d_swapchain_set_window(swapchain, op->dst_window_override);
449 swapchain->swapchain_ops->swapchain_present(swapchain, &op->src_rect, &op->dst_rect, op->flags);
451 wined3d_resource_release(&swapchain->front_buffer->resource);
452 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
454 wined3d_resource_release(&swapchain->back_buffers[i]->resource);
457 InterlockedDecrement(&cs->pending_presents);
460 void wined3d_cs_emit_present(struct wined3d_cs *cs, struct wined3d_swapchain *swapchain,
461 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, DWORD flags)
463 struct wined3d_cs_present *op;
464 unsigned int i;
465 LONG pending;
467 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
468 op->opcode = WINED3D_CS_OP_PRESENT;
469 op->dst_window_override = dst_window_override;
470 op->swapchain = swapchain;
471 op->src_rect = *src_rect;
472 op->dst_rect = *dst_rect;
473 op->flags = flags;
475 pending = InterlockedIncrement(&cs->pending_presents);
477 wined3d_resource_acquire(&swapchain->front_buffer->resource);
478 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
480 wined3d_resource_acquire(&swapchain->back_buffers[i]->resource);
483 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
485 /* Limit input latency by limiting the number of presents that we can get
486 * ahead of the worker thread. We have a constant limit here, but
487 * IDXGIDevice1 allows tuning this. */
488 while (pending > 1)
490 wined3d_pause();
491 pending = InterlockedCompareExchange(&cs->pending_presents, 0, 0);
495 static void wined3d_cs_exec_clear(struct wined3d_cs *cs, const void *data)
497 const struct wined3d_state *state = &cs->state;
498 const struct wined3d_cs_clear *op = data;
499 struct wined3d_device *device;
500 unsigned int i;
501 RECT draw_rect;
503 device = cs->device;
504 wined3d_get_draw_rect(state, &draw_rect);
505 device->blitter->ops->blitter_clear(device->blitter, device, op->rt_count, op->fb,
506 op->rect_count, op->rects, &op->draw_rect, op->flags, &op->color, op->depth, op->stencil);
508 if (op->flags & WINED3DCLEAR_TARGET)
510 for (i = 0; i < op->rt_count; ++i)
512 if (op->fb->render_targets[i])
513 wined3d_resource_release(op->fb->render_targets[i]->resource);
516 if (op->flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
517 wined3d_resource_release(op->fb->depth_stencil->resource);
520 void wined3d_cs_emit_clear(struct wined3d_cs *cs, DWORD rect_count, const RECT *rects,
521 DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
523 unsigned int rt_count = cs->device->adapter->gl_info.limits.buffers;
524 const struct wined3d_state *state = &cs->device->state;
525 struct wined3d_cs_clear *op;
526 unsigned int i;
528 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_clear, rects[rect_count]),
529 WINED3D_CS_QUEUE_DEFAULT);
530 op->opcode = WINED3D_CS_OP_CLEAR;
531 op->flags = flags;
532 op->rt_count = rt_count;
533 op->fb = &cs->fb;
534 wined3d_get_draw_rect(state, &op->draw_rect);
535 op->color = *color;
536 op->depth = depth;
537 op->stencil = stencil;
538 op->rect_count = rect_count;
539 memcpy(op->rects, rects, sizeof(*rects) * rect_count);
541 if (flags & WINED3DCLEAR_TARGET)
543 for (i = 0; i < rt_count; ++i)
545 if (state->fb->render_targets[i])
546 wined3d_resource_acquire(state->fb->render_targets[i]->resource);
549 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
550 wined3d_resource_acquire(state->fb->depth_stencil->resource);
552 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
555 void wined3d_cs_emit_clear_rendertarget_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view,
556 const RECT *rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
558 struct wined3d_cs_clear *op;
559 struct
561 struct wined3d_rendertarget_view *rt;
562 struct wined3d_fb_state fb;
563 } *extra;
565 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_clear, rects[1]) + sizeof(*extra),
566 WINED3D_CS_QUEUE_DEFAULT);
567 extra = (void *)&op->rects[1];
568 extra->fb.render_targets = &extra->rt;
569 op->fb = &extra->fb;
571 op->opcode = WINED3D_CS_OP_CLEAR;
572 op->flags = flags;
573 if (flags & WINED3DCLEAR_TARGET)
575 op->rt_count = 1;
576 op->fb->render_targets[0] = view;
577 op->fb->depth_stencil = NULL;
578 op->color = *color;
580 else
582 op->rt_count = 0;
583 op->fb->render_targets[0] = NULL;
584 op->fb->depth_stencil = view;
585 op->depth = depth;
586 op->stencil = stencil;
588 SetRect(&op->draw_rect, 0, 0, view->width, view->height);
589 op->rect_count = 1;
590 op->rects[0] = *rect;
592 wined3d_resource_acquire(view->resource);
594 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
597 static void acquire_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
599 struct wined3d_shader_sampler_map_entry *entry;
600 struct wined3d_shader_resource_view *view;
601 struct wined3d_shader *shader;
602 unsigned int i, j;
604 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
606 if (!(shader_mask & (1u << i)))
607 continue;
609 if (!(shader = state->shader[i]))
610 continue;
612 for (j = 0; j < WINED3D_MAX_CBS; ++j)
614 if (state->cb[i][j])
615 wined3d_resource_acquire(&state->cb[i][j]->resource);
618 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
620 entry = &shader->reg_maps.sampler_map.entries[j];
622 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
623 continue;
625 wined3d_resource_acquire(view->resource);
630 static void release_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
632 struct wined3d_shader_sampler_map_entry *entry;
633 struct wined3d_shader_resource_view *view;
634 struct wined3d_shader *shader;
635 unsigned int i, j;
637 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
639 if (!(shader_mask & (1u << i)))
640 continue;
642 if (!(shader = state->shader[i]))
643 continue;
645 for (j = 0; j < WINED3D_MAX_CBS; ++j)
647 if (state->cb[i][j])
648 wined3d_resource_release(&state->cb[i][j]->resource);
651 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
653 entry = &shader->reg_maps.sampler_map.entries[j];
655 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
656 continue;
658 wined3d_resource_release(view->resource);
663 static void acquire_unordered_access_resources(const struct wined3d_shader *shader,
664 struct wined3d_unordered_access_view * const *views)
666 unsigned int i;
668 if (!shader)
669 return;
671 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
673 if (!shader->reg_maps.uav_resource_info[i].type)
674 continue;
676 if (!views[i])
677 continue;
679 wined3d_resource_acquire(views[i]->resource);
683 static void release_unordered_access_resources(const struct wined3d_shader *shader,
684 struct wined3d_unordered_access_view * const *views)
686 unsigned int i;
688 if (!shader)
689 return;
691 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
693 if (!shader->reg_maps.uav_resource_info[i].type)
694 continue;
696 if (!views[i])
697 continue;
699 wined3d_resource_release(views[i]->resource);
703 static void wined3d_cs_exec_dispatch(struct wined3d_cs *cs, const void *data)
705 const struct wined3d_cs_dispatch *op = data;
706 struct wined3d_state *state = &cs->state;
708 dispatch_compute(cs->device, state, &op->parameters);
710 if (op->parameters.indirect)
711 wined3d_resource_release(&op->parameters.u.indirect.buffer->resource);
713 release_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
714 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
715 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
718 static void acquire_compute_pipeline_resources(const struct wined3d_state *state)
720 acquire_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
721 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
722 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
725 void wined3d_cs_emit_dispatch(struct wined3d_cs *cs,
726 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
728 const struct wined3d_state *state = &cs->device->state;
729 struct wined3d_cs_dispatch *op;
731 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
732 op->opcode = WINED3D_CS_OP_DISPATCH;
733 op->parameters.indirect = FALSE;
734 op->parameters.u.direct.group_count_x = group_count_x;
735 op->parameters.u.direct.group_count_y = group_count_y;
736 op->parameters.u.direct.group_count_z = group_count_z;
738 acquire_compute_pipeline_resources(state);
740 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
743 void wined3d_cs_emit_dispatch_indirect(struct wined3d_cs *cs,
744 struct wined3d_buffer *buffer, unsigned int offset)
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 = TRUE;
752 op->parameters.u.indirect.buffer = buffer;
753 op->parameters.u.indirect.offset = offset;
755 acquire_compute_pipeline_resources(state);
756 wined3d_resource_acquire(&buffer->resource);
758 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
761 static void wined3d_cs_exec_draw(struct wined3d_cs *cs, const void *data)
763 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
764 struct wined3d_state *state = &cs->state;
765 const struct wined3d_cs_draw *op = data;
766 int load_base_vertex_idx;
767 unsigned int i;
769 /* ARB_draw_indirect always supports a base vertex offset. */
770 if (!op->parameters.indirect && !gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
771 load_base_vertex_idx = op->parameters.u.direct.base_vertex_idx;
772 else
773 load_base_vertex_idx = 0;
775 if (state->load_base_vertex_index != load_base_vertex_idx)
777 state->load_base_vertex_index = load_base_vertex_idx;
778 device_invalidate_state(cs->device, STATE_BASEVERTEXINDEX);
781 if (state->gl_primitive_type != op->primitive_type)
783 if (state->gl_primitive_type == GL_POINTS || op->primitive_type == GL_POINTS)
784 device_invalidate_state(cs->device, STATE_POINT_ENABLE);
785 state->gl_primitive_type = op->primitive_type;
787 state->gl_patch_vertices = op->patch_vertex_count;
789 draw_primitive(cs->device, state, &op->parameters);
791 if (op->parameters.indirect)
793 struct wined3d_buffer *buffer = op->parameters.u.indirect.buffer;
794 wined3d_resource_release(&buffer->resource);
797 if (op->parameters.indexed)
798 wined3d_resource_release(&state->index_buffer->resource);
799 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
801 if (state->streams[i].buffer)
802 wined3d_resource_release(&state->streams[i].buffer->resource);
804 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
806 if (state->stream_output[i].buffer)
807 wined3d_resource_release(&state->stream_output[i].buffer->resource);
809 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
811 if (state->textures[i])
812 wined3d_resource_release(&state->textures[i]->resource);
814 for (i = 0; i < gl_info->limits.buffers; ++i)
816 if (state->fb->render_targets[i])
817 wined3d_resource_release(state->fb->render_targets[i]->resource);
819 if (state->fb->depth_stencil)
820 wined3d_resource_release(state->fb->depth_stencil->resource);
821 release_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
822 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
823 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
826 static void acquire_graphics_pipeline_resources(const struct wined3d_state *state,
827 BOOL indexed, const struct wined3d_gl_info *gl_info)
829 unsigned int i;
831 if (indexed)
832 wined3d_resource_acquire(&state->index_buffer->resource);
833 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
835 if (state->streams[i].buffer)
836 wined3d_resource_acquire(&state->streams[i].buffer->resource);
838 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
840 if (state->stream_output[i].buffer)
841 wined3d_resource_acquire(&state->stream_output[i].buffer->resource);
843 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
845 if (state->textures[i])
846 wined3d_resource_acquire(&state->textures[i]->resource);
848 for (i = 0; i < gl_info->limits.buffers; ++i)
850 if (state->fb->render_targets[i])
851 wined3d_resource_acquire(state->fb->render_targets[i]->resource);
853 if (state->fb->depth_stencil)
854 wined3d_resource_acquire(state->fb->depth_stencil->resource);
855 acquire_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
856 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
857 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
860 void wined3d_cs_emit_draw(struct wined3d_cs *cs, GLenum primitive_type, unsigned int patch_vertex_count,
861 int base_vertex_idx, unsigned int start_idx, unsigned int index_count,
862 unsigned int start_instance, unsigned int instance_count, BOOL indexed)
864 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
865 const struct wined3d_state *state = &cs->device->state;
866 struct wined3d_cs_draw *op;
868 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
869 op->opcode = WINED3D_CS_OP_DRAW;
870 op->primitive_type = primitive_type;
871 op->patch_vertex_count = patch_vertex_count;
872 op->parameters.indirect = FALSE;
873 op->parameters.u.direct.base_vertex_idx = base_vertex_idx;
874 op->parameters.u.direct.start_idx = start_idx;
875 op->parameters.u.direct.index_count = index_count;
876 op->parameters.u.direct.start_instance = start_instance;
877 op->parameters.u.direct.instance_count = instance_count;
878 op->parameters.indexed = indexed;
880 acquire_graphics_pipeline_resources(state, indexed, gl_info);
882 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
885 void wined3d_cs_emit_draw_indirect(struct wined3d_cs *cs, GLenum primitive_type, unsigned int patch_vertex_count,
886 struct wined3d_buffer *buffer, unsigned int offset, BOOL indexed)
888 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
889 const struct wined3d_state *state = &cs->device->state;
890 struct wined3d_cs_draw *op;
892 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
893 op->opcode = WINED3D_CS_OP_DRAW;
894 op->primitive_type = primitive_type;
895 op->patch_vertex_count = patch_vertex_count;
896 op->parameters.indirect = TRUE;
897 op->parameters.u.indirect.buffer = buffer;
898 op->parameters.u.indirect.offset = offset;
899 op->parameters.indexed = indexed;
901 acquire_graphics_pipeline_resources(state, indexed, gl_info);
902 wined3d_resource_acquire(&buffer->resource);
904 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
907 static void wined3d_cs_exec_flush(struct wined3d_cs *cs, const void *data)
909 struct wined3d_context *context;
911 context = context_acquire(cs->device, NULL, 0);
912 if (context->valid)
913 context->gl_info->gl_ops.gl.p_glFlush();
914 context_release(context);
917 void wined3d_cs_emit_flush(struct wined3d_cs *cs)
919 struct wined3d_cs_flush *op;
921 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
922 op->opcode = WINED3D_CS_OP_FLUSH;
924 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
925 cs->queries_flushed = TRUE;
928 static void wined3d_cs_exec_set_predication(struct wined3d_cs *cs, const void *data)
930 const struct wined3d_cs_set_predication *op = data;
932 cs->state.predicate = op->predicate;
933 cs->state.predicate_value = op->value;
936 void wined3d_cs_emit_set_predication(struct wined3d_cs *cs, struct wined3d_query *predicate, BOOL value)
938 struct wined3d_cs_set_predication *op;
940 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
941 op->opcode = WINED3D_CS_OP_SET_PREDICATION;
942 op->predicate = predicate;
943 op->value = value;
945 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
948 static void wined3d_cs_exec_set_viewport(struct wined3d_cs *cs, const void *data)
950 const struct wined3d_cs_set_viewport *op = data;
952 cs->state.viewport = op->viewport;
953 device_invalidate_state(cs->device, STATE_VIEWPORT);
956 void wined3d_cs_emit_set_viewport(struct wined3d_cs *cs, const struct wined3d_viewport *viewport)
958 struct wined3d_cs_set_viewport *op;
960 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
961 op->opcode = WINED3D_CS_OP_SET_VIEWPORT;
962 op->viewport = *viewport;
964 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
967 static void wined3d_cs_exec_set_scissor_rect(struct wined3d_cs *cs, const void *data)
969 const struct wined3d_cs_set_scissor_rect *op = data;
971 cs->state.scissor_rect = op->rect;
972 device_invalidate_state(cs->device, STATE_SCISSORRECT);
975 void wined3d_cs_emit_set_scissor_rect(struct wined3d_cs *cs, const RECT *rect)
977 struct wined3d_cs_set_scissor_rect *op;
979 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
980 op->opcode = WINED3D_CS_OP_SET_SCISSOR_RECT;
981 op->rect = *rect;
983 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
986 static void wined3d_cs_exec_set_rendertarget_view(struct wined3d_cs *cs, const void *data)
988 const struct wined3d_cs_set_rendertarget_view *op = data;
990 cs->state.fb->render_targets[op->view_idx] = op->view;
991 device_invalidate_state(cs->device, STATE_FRAMEBUFFER);
994 void wined3d_cs_emit_set_rendertarget_view(struct wined3d_cs *cs, unsigned int view_idx,
995 struct wined3d_rendertarget_view *view)
997 struct wined3d_cs_set_rendertarget_view *op;
999 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1000 op->opcode = WINED3D_CS_OP_SET_RENDERTARGET_VIEW;
1001 op->view_idx = view_idx;
1002 op->view = view;
1004 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1007 static void wined3d_cs_exec_set_depth_stencil_view(struct wined3d_cs *cs, const void *data)
1009 const struct wined3d_cs_set_depth_stencil_view *op = data;
1010 struct wined3d_device *device = cs->device;
1011 struct wined3d_rendertarget_view *prev;
1013 if ((prev = cs->state.fb->depth_stencil))
1015 struct wined3d_surface *prev_surface = wined3d_rendertarget_view_get_surface(prev);
1017 if (prev_surface && (device->swapchains[0]->desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
1018 || prev_surface->container->flags & WINED3D_TEXTURE_DISCARD))
1020 wined3d_texture_validate_location(prev_surface->container,
1021 prev->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
1025 cs->fb.depth_stencil = op->view;
1027 if (!prev != !op->view)
1029 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
1030 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
1031 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
1032 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
1033 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
1035 else if (prev && prev->format->depth_bias_scale != op->view->format->depth_bias_scale)
1037 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
1040 device_invalidate_state(device, STATE_FRAMEBUFFER);
1043 void wined3d_cs_emit_set_depth_stencil_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view)
1045 struct wined3d_cs_set_depth_stencil_view *op;
1047 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1048 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW;
1049 op->view = view;
1051 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1054 static void wined3d_cs_exec_set_vertex_declaration(struct wined3d_cs *cs, const void *data)
1056 const struct wined3d_cs_set_vertex_declaration *op = data;
1058 cs->state.vertex_declaration = op->declaration;
1059 device_invalidate_state(cs->device, STATE_VDECL);
1062 void wined3d_cs_emit_set_vertex_declaration(struct wined3d_cs *cs, struct wined3d_vertex_declaration *declaration)
1064 struct wined3d_cs_set_vertex_declaration *op;
1066 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1067 op->opcode = WINED3D_CS_OP_SET_VERTEX_DECLARATION;
1068 op->declaration = declaration;
1070 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1073 static void wined3d_cs_exec_set_stream_source(struct wined3d_cs *cs, const void *data)
1075 const struct wined3d_cs_set_stream_source *op = data;
1076 struct wined3d_stream_state *stream;
1077 struct wined3d_buffer *prev;
1079 stream = &cs->state.streams[op->stream_idx];
1080 prev = stream->buffer;
1081 stream->buffer = op->buffer;
1082 stream->offset = op->offset;
1083 stream->stride = op->stride;
1085 if (op->buffer)
1086 InterlockedIncrement(&op->buffer->resource.bind_count);
1087 if (prev)
1088 InterlockedDecrement(&prev->resource.bind_count);
1090 device_invalidate_state(cs->device, STATE_STREAMSRC);
1093 void wined3d_cs_emit_set_stream_source(struct wined3d_cs *cs, UINT stream_idx,
1094 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1096 struct wined3d_cs_set_stream_source *op;
1098 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1099 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE;
1100 op->stream_idx = stream_idx;
1101 op->buffer = buffer;
1102 op->offset = offset;
1103 op->stride = stride;
1105 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1108 static void wined3d_cs_exec_set_stream_source_freq(struct wined3d_cs *cs, const void *data)
1110 const struct wined3d_cs_set_stream_source_freq *op = data;
1111 struct wined3d_stream_state *stream;
1113 stream = &cs->state.streams[op->stream_idx];
1114 stream->frequency = op->frequency;
1115 stream->flags = op->flags;
1117 device_invalidate_state(cs->device, STATE_STREAMSRC);
1120 void wined3d_cs_emit_set_stream_source_freq(struct wined3d_cs *cs, UINT stream_idx, UINT frequency, UINT flags)
1122 struct wined3d_cs_set_stream_source_freq *op;
1124 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1125 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ;
1126 op->stream_idx = stream_idx;
1127 op->frequency = frequency;
1128 op->flags = flags;
1130 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1133 static void wined3d_cs_exec_set_stream_output(struct wined3d_cs *cs, const void *data)
1135 const struct wined3d_cs_set_stream_output *op = data;
1136 struct wined3d_stream_output *stream;
1137 struct wined3d_buffer *prev;
1139 stream = &cs->state.stream_output[op->stream_idx];
1140 prev = stream->buffer;
1141 stream->buffer = op->buffer;
1142 stream->offset = op->offset;
1144 if (op->buffer)
1145 InterlockedIncrement(&op->buffer->resource.bind_count);
1146 if (prev)
1147 InterlockedDecrement(&prev->resource.bind_count);
1149 device_invalidate_state(cs->device, STATE_STREAM_OUTPUT);
1152 void wined3d_cs_emit_set_stream_output(struct wined3d_cs *cs, UINT stream_idx,
1153 struct wined3d_buffer *buffer, UINT offset)
1155 struct wined3d_cs_set_stream_output *op;
1157 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1158 op->opcode = WINED3D_CS_OP_SET_STREAM_OUTPUT;
1159 op->stream_idx = stream_idx;
1160 op->buffer = buffer;
1161 op->offset = offset;
1163 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1166 static void wined3d_cs_exec_set_index_buffer(struct wined3d_cs *cs, const void *data)
1168 const struct wined3d_cs_set_index_buffer *op = data;
1169 struct wined3d_buffer *prev;
1171 prev = cs->state.index_buffer;
1172 cs->state.index_buffer = op->buffer;
1173 cs->state.index_format = op->format_id;
1174 cs->state.index_offset = op->offset;
1176 if (op->buffer)
1177 InterlockedIncrement(&op->buffer->resource.bind_count);
1178 if (prev)
1179 InterlockedDecrement(&prev->resource.bind_count);
1181 device_invalidate_state(cs->device, STATE_INDEXBUFFER);
1184 void wined3d_cs_emit_set_index_buffer(struct wined3d_cs *cs, struct wined3d_buffer *buffer,
1185 enum wined3d_format_id format_id, unsigned int offset)
1187 struct wined3d_cs_set_index_buffer *op;
1189 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1190 op->opcode = WINED3D_CS_OP_SET_INDEX_BUFFER;
1191 op->buffer = buffer;
1192 op->format_id = format_id;
1193 op->offset = offset;
1195 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1198 static void wined3d_cs_exec_set_constant_buffer(struct wined3d_cs *cs, const void *data)
1200 const struct wined3d_cs_set_constant_buffer *op = data;
1201 struct wined3d_buffer *prev;
1203 prev = cs->state.cb[op->type][op->cb_idx];
1204 cs->state.cb[op->type][op->cb_idx] = op->buffer;
1206 if (op->buffer)
1207 InterlockedIncrement(&op->buffer->resource.bind_count);
1208 if (prev)
1209 InterlockedDecrement(&prev->resource.bind_count);
1211 device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(op->type));
1214 void wined3d_cs_emit_set_constant_buffer(struct wined3d_cs *cs, enum wined3d_shader_type type,
1215 UINT cb_idx, struct wined3d_buffer *buffer)
1217 struct wined3d_cs_set_constant_buffer *op;
1219 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1220 op->opcode = WINED3D_CS_OP_SET_CONSTANT_BUFFER;
1221 op->type = type;
1222 op->cb_idx = cb_idx;
1223 op->buffer = buffer;
1225 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1228 static void wined3d_cs_exec_set_texture(struct wined3d_cs *cs, const void *data)
1230 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
1231 const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
1232 const struct wined3d_cs_set_texture *op = data;
1233 struct wined3d_texture *prev;
1234 BOOL old_use_color_key = FALSE, new_use_color_key = FALSE;
1236 prev = cs->state.textures[op->stage];
1237 cs->state.textures[op->stage] = op->texture;
1239 if (op->texture)
1241 const struct wined3d_format *new_format = op->texture->resource.format;
1242 const struct wined3d_format *old_format = prev ? prev->resource.format : NULL;
1243 unsigned int old_fmt_flags = prev ? prev->resource.format_flags : 0;
1244 unsigned int new_fmt_flags = op->texture->resource.format_flags;
1246 if (InterlockedIncrement(&op->texture->resource.bind_count) == 1)
1247 op->texture->sampler = op->stage;
1249 if (!prev || op->texture->target != prev->target
1250 || (!is_same_fixup(new_format->color_fixup, old_format->color_fixup)
1251 && !(can_use_texture_swizzle(gl_info, new_format) && can_use_texture_swizzle(gl_info, old_format)))
1252 || (new_fmt_flags & WINED3DFMT_FLAG_SHADOW) != (old_fmt_flags & WINED3DFMT_FLAG_SHADOW))
1253 device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1255 if (!prev && op->stage < d3d_info->limits.ffp_blend_stages)
1257 /* The source arguments for color and alpha ops have different
1258 * meanings when a NULL texture is bound, so the COLOR_OP and
1259 * ALPHA_OP have to be dirtified. */
1260 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1261 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1264 if (!op->stage && op->texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1265 new_use_color_key = TRUE;
1268 if (prev)
1270 if (InterlockedDecrement(&prev->resource.bind_count) && prev->sampler == op->stage)
1272 unsigned int i;
1274 /* Search for other stages the texture is bound to. Shouldn't
1275 * happen if applications bind textures to a single stage only. */
1276 TRACE("Searching for other stages the texture is bound to.\n");
1277 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
1279 if (cs->state.textures[i] == prev)
1281 TRACE("Texture is also bound to stage %u.\n", i);
1282 prev->sampler = i;
1283 break;
1288 if (!op->texture && op->stage < d3d_info->limits.ffp_blend_stages)
1290 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1291 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1294 if (!op->stage && prev->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1295 old_use_color_key = TRUE;
1298 device_invalidate_state(cs->device, STATE_SAMPLER(op->stage));
1300 if (new_use_color_key != old_use_color_key)
1301 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1303 if (new_use_color_key)
1304 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1307 void wined3d_cs_emit_set_texture(struct wined3d_cs *cs, UINT stage, struct wined3d_texture *texture)
1309 struct wined3d_cs_set_texture *op;
1311 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1312 op->opcode = WINED3D_CS_OP_SET_TEXTURE;
1313 op->stage = stage;
1314 op->texture = texture;
1316 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1319 static void wined3d_cs_exec_set_shader_resource_view(struct wined3d_cs *cs, const void *data)
1321 const struct wined3d_cs_set_shader_resource_view *op = data;
1322 struct wined3d_shader_resource_view *prev;
1324 prev = cs->state.shader_resource_view[op->type][op->view_idx];
1325 cs->state.shader_resource_view[op->type][op->view_idx] = op->view;
1327 if (op->view)
1328 InterlockedIncrement(&op->view->resource->bind_count);
1329 if (prev)
1330 InterlockedDecrement(&prev->resource->bind_count);
1332 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1333 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1334 else
1335 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1338 void wined3d_cs_emit_set_shader_resource_view(struct wined3d_cs *cs, enum wined3d_shader_type type,
1339 UINT view_idx, struct wined3d_shader_resource_view *view)
1341 struct wined3d_cs_set_shader_resource_view *op;
1343 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1344 op->opcode = WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW;
1345 op->type = type;
1346 op->view_idx = view_idx;
1347 op->view = view;
1349 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1352 static void wined3d_cs_exec_set_unordered_access_view(struct wined3d_cs *cs, const void *data)
1354 const struct wined3d_cs_set_unordered_access_view *op = data;
1355 struct wined3d_unordered_access_view *prev;
1357 prev = cs->state.unordered_access_view[op->pipeline][op->view_idx];
1358 cs->state.unordered_access_view[op->pipeline][op->view_idx] = op->view;
1360 if (op->view)
1361 InterlockedIncrement(&op->view->resource->bind_count);
1362 if (prev)
1363 InterlockedDecrement(&prev->resource->bind_count);
1365 if (op->view && op->initial_count != ~0u)
1366 wined3d_unordered_access_view_set_counter(op->view, op->initial_count);
1368 device_invalidate_state(cs->device, STATE_UNORDERED_ACCESS_VIEW_BINDING(op->pipeline));
1371 void wined3d_cs_emit_set_unordered_access_view(struct wined3d_cs *cs, enum wined3d_pipeline pipeline,
1372 unsigned int view_idx, struct wined3d_unordered_access_view *view, unsigned int initial_count)
1374 struct wined3d_cs_set_unordered_access_view *op;
1376 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1377 op->opcode = WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW;
1378 op->pipeline = pipeline;
1379 op->view_idx = view_idx;
1380 op->view = view;
1381 op->initial_count = initial_count;
1383 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1386 static void wined3d_cs_exec_set_sampler(struct wined3d_cs *cs, const void *data)
1388 const struct wined3d_cs_set_sampler *op = data;
1390 cs->state.sampler[op->type][op->sampler_idx] = op->sampler;
1391 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1392 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1393 else
1394 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1397 void wined3d_cs_emit_set_sampler(struct wined3d_cs *cs, enum wined3d_shader_type type,
1398 UINT sampler_idx, struct wined3d_sampler *sampler)
1400 struct wined3d_cs_set_sampler *op;
1402 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1403 op->opcode = WINED3D_CS_OP_SET_SAMPLER;
1404 op->type = type;
1405 op->sampler_idx = sampler_idx;
1406 op->sampler = sampler;
1408 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1411 static void wined3d_cs_exec_set_shader(struct wined3d_cs *cs, const void *data)
1413 const struct wined3d_cs_set_shader *op = data;
1415 cs->state.shader[op->type] = op->shader;
1416 device_invalidate_state(cs->device, STATE_SHADER(op->type));
1417 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1418 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1419 else
1420 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1423 void wined3d_cs_emit_set_shader(struct wined3d_cs *cs, enum wined3d_shader_type type, struct wined3d_shader *shader)
1425 struct wined3d_cs_set_shader *op;
1427 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1428 op->opcode = WINED3D_CS_OP_SET_SHADER;
1429 op->type = type;
1430 op->shader = shader;
1432 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1435 static void wined3d_cs_exec_set_rasterizer_state(struct wined3d_cs *cs, const void *data)
1437 const struct wined3d_cs_set_rasterizer_state *op = data;
1439 cs->state.rasterizer_state = op->state;
1440 device_invalidate_state(cs->device, STATE_FRONTFACE);
1443 void wined3d_cs_emit_set_rasterizer_state(struct wined3d_cs *cs,
1444 struct wined3d_rasterizer_state *rasterizer_state)
1446 struct wined3d_cs_set_rasterizer_state *op;
1448 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1449 op->opcode = WINED3D_CS_OP_SET_RASTERIZER_STATE;
1450 op->state = rasterizer_state;
1452 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1455 static void wined3d_cs_exec_set_render_state(struct wined3d_cs *cs, const void *data)
1457 const struct wined3d_cs_set_render_state *op = data;
1459 cs->state.render_states[op->state] = op->value;
1460 device_invalidate_state(cs->device, STATE_RENDER(op->state));
1463 void wined3d_cs_emit_set_render_state(struct wined3d_cs *cs, enum wined3d_render_state state, DWORD value)
1465 struct wined3d_cs_set_render_state *op;
1467 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1468 op->opcode = WINED3D_CS_OP_SET_RENDER_STATE;
1469 op->state = state;
1470 op->value = value;
1472 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1475 static void wined3d_cs_exec_set_texture_state(struct wined3d_cs *cs, const void *data)
1477 const struct wined3d_cs_set_texture_state *op = data;
1479 cs->state.texture_states[op->stage][op->state] = op->value;
1480 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, op->state));
1483 void wined3d_cs_emit_set_texture_state(struct wined3d_cs *cs, UINT stage,
1484 enum wined3d_texture_stage_state state, DWORD value)
1486 struct wined3d_cs_set_texture_state *op;
1488 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1489 op->opcode = WINED3D_CS_OP_SET_TEXTURE_STATE;
1490 op->stage = stage;
1491 op->state = state;
1492 op->value = value;
1494 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1497 static void wined3d_cs_exec_set_sampler_state(struct wined3d_cs *cs, const void *data)
1499 const struct wined3d_cs_set_sampler_state *op = data;
1501 cs->state.sampler_states[op->sampler_idx][op->state] = op->value;
1502 device_invalidate_state(cs->device, STATE_SAMPLER(op->sampler_idx));
1505 void wined3d_cs_emit_set_sampler_state(struct wined3d_cs *cs, UINT sampler_idx,
1506 enum wined3d_sampler_state state, DWORD value)
1508 struct wined3d_cs_set_sampler_state *op;
1510 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1511 op->opcode = WINED3D_CS_OP_SET_SAMPLER_STATE;
1512 op->sampler_idx = sampler_idx;
1513 op->state = state;
1514 op->value = value;
1516 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1519 static void wined3d_cs_exec_set_transform(struct wined3d_cs *cs, const void *data)
1521 const struct wined3d_cs_set_transform *op = data;
1523 cs->state.transforms[op->state] = op->matrix;
1524 if (op->state < WINED3D_TS_WORLD_MATRIX(cs->device->adapter->d3d_info.limits.ffp_vertex_blend_matrices))
1525 device_invalidate_state(cs->device, STATE_TRANSFORM(op->state));
1528 void wined3d_cs_emit_set_transform(struct wined3d_cs *cs, enum wined3d_transform_state state,
1529 const struct wined3d_matrix *matrix)
1531 struct wined3d_cs_set_transform *op;
1533 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1534 op->opcode = WINED3D_CS_OP_SET_TRANSFORM;
1535 op->state = state;
1536 op->matrix = *matrix;
1538 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1541 static void wined3d_cs_exec_set_clip_plane(struct wined3d_cs *cs, const void *data)
1543 const struct wined3d_cs_set_clip_plane *op = data;
1545 cs->state.clip_planes[op->plane_idx] = op->plane;
1546 device_invalidate_state(cs->device, STATE_CLIPPLANE(op->plane_idx));
1549 void wined3d_cs_emit_set_clip_plane(struct wined3d_cs *cs, UINT plane_idx, const struct wined3d_vec4 *plane)
1551 struct wined3d_cs_set_clip_plane *op;
1553 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1554 op->opcode = WINED3D_CS_OP_SET_CLIP_PLANE;
1555 op->plane_idx = plane_idx;
1556 op->plane = *plane;
1558 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1561 static void wined3d_cs_exec_set_color_key(struct wined3d_cs *cs, const void *data)
1563 const struct wined3d_cs_set_color_key *op = data;
1564 struct wined3d_texture *texture = op->texture;
1566 if (op->set)
1568 switch (op->flags)
1570 case WINED3D_CKEY_DST_BLT:
1571 texture->async.dst_blt_color_key = op->color_key;
1572 texture->async.color_key_flags |= WINED3D_CKEY_DST_BLT;
1573 break;
1575 case WINED3D_CKEY_DST_OVERLAY:
1576 texture->async.dst_overlay_color_key = op->color_key;
1577 texture->async.color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
1578 break;
1580 case WINED3D_CKEY_SRC_BLT:
1581 if (texture == cs->state.textures[0])
1583 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1584 if (!(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1585 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1588 texture->async.src_blt_color_key = op->color_key;
1589 texture->async.color_key_flags |= WINED3D_CKEY_SRC_BLT;
1590 break;
1592 case WINED3D_CKEY_SRC_OVERLAY:
1593 texture->async.src_overlay_color_key = op->color_key;
1594 texture->async.color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
1595 break;
1598 else
1600 switch (op->flags)
1602 case WINED3D_CKEY_DST_BLT:
1603 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_BLT;
1604 break;
1606 case WINED3D_CKEY_DST_OVERLAY:
1607 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
1608 break;
1610 case WINED3D_CKEY_SRC_BLT:
1611 if (texture == cs->state.textures[0] && texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1612 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1614 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
1615 break;
1617 case WINED3D_CKEY_SRC_OVERLAY:
1618 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
1619 break;
1624 void wined3d_cs_emit_set_color_key(struct wined3d_cs *cs, struct wined3d_texture *texture,
1625 WORD flags, const struct wined3d_color_key *color_key)
1627 struct wined3d_cs_set_color_key *op;
1629 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1630 op->opcode = WINED3D_CS_OP_SET_COLOR_KEY;
1631 op->texture = texture;
1632 op->flags = flags;
1633 if (color_key)
1635 op->color_key = *color_key;
1636 op->set = 1;
1638 else
1639 op->set = 0;
1641 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1644 static void wined3d_cs_exec_set_material(struct wined3d_cs *cs, const void *data)
1646 const struct wined3d_cs_set_material *op = data;
1648 cs->state.material = op->material;
1649 device_invalidate_state(cs->device, STATE_MATERIAL);
1652 void wined3d_cs_emit_set_material(struct wined3d_cs *cs, const struct wined3d_material *material)
1654 struct wined3d_cs_set_material *op;
1656 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1657 op->opcode = WINED3D_CS_OP_SET_MATERIAL;
1658 op->material = *material;
1660 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1663 static void wined3d_cs_exec_set_light(struct wined3d_cs *cs, const void *data)
1665 const struct wined3d_cs_set_light *op = data;
1666 struct wined3d_light_info *light_info;
1667 unsigned int light_idx, hash_idx;
1669 light_idx = op->light.OriginalIndex;
1671 if (!(light_info = wined3d_state_get_light(&cs->state, light_idx)))
1673 TRACE("Adding new light.\n");
1674 if (!(light_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*light_info))))
1676 ERR("Failed to allocate light info.\n");
1677 return;
1680 hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1681 list_add_head(&cs->state.light_map[hash_idx], &light_info->entry);
1682 light_info->glIndex = -1;
1683 light_info->OriginalIndex = light_idx;
1686 if (light_info->glIndex != -1)
1688 if (light_info->OriginalParms.type != op->light.OriginalParms.type)
1689 device_invalidate_state(cs->device, STATE_LIGHT_TYPE);
1690 device_invalidate_state(cs->device, STATE_ACTIVELIGHT(light_info->glIndex));
1693 light_info->OriginalParms = op->light.OriginalParms;
1694 light_info->position = op->light.position;
1695 light_info->direction = op->light.direction;
1696 light_info->exponent = op->light.exponent;
1697 light_info->cutoff = op->light.cutoff;
1700 void wined3d_cs_emit_set_light(struct wined3d_cs *cs, const struct wined3d_light_info *light)
1702 struct wined3d_cs_set_light *op;
1704 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1705 op->opcode = WINED3D_CS_OP_SET_LIGHT;
1706 op->light = *light;
1708 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1711 static void wined3d_cs_exec_set_light_enable(struct wined3d_cs *cs, const void *data)
1713 const struct wined3d_cs_set_light_enable *op = data;
1714 struct wined3d_device *device = cs->device;
1715 struct wined3d_light_info *light_info;
1716 int prev_idx;
1718 if (!(light_info = wined3d_state_get_light(&cs->state, op->idx)))
1720 ERR("Light doesn't exist.\n");
1721 return;
1724 prev_idx = light_info->glIndex;
1725 wined3d_state_enable_light(&cs->state, &device->adapter->d3d_info, light_info, op->enable);
1726 if (light_info->glIndex != prev_idx)
1728 device_invalidate_state(device, STATE_LIGHT_TYPE);
1729 device_invalidate_state(device, STATE_ACTIVELIGHT(op->enable ? light_info->glIndex : prev_idx));
1733 void wined3d_cs_emit_set_light_enable(struct wined3d_cs *cs, unsigned int idx, BOOL enable)
1735 struct wined3d_cs_set_light_enable *op;
1737 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1738 op->opcode = WINED3D_CS_OP_SET_LIGHT_ENABLE;
1739 op->idx = idx;
1740 op->enable = enable;
1742 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1745 static const struct
1747 size_t offset;
1748 size_t size;
1749 DWORD mask;
1751 wined3d_cs_push_constant_info[] =
1753 /* WINED3D_PUSH_CONSTANTS_VS_F */
1754 {FIELD_OFFSET(struct wined3d_state, vs_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_VS_F},
1755 /* WINED3D_PUSH_CONSTANTS_PS_F */
1756 {FIELD_OFFSET(struct wined3d_state, ps_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_PS_F},
1757 /* WINED3D_PUSH_CONSTANTS_VS_I */
1758 {FIELD_OFFSET(struct wined3d_state, vs_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_VS_I},
1759 /* WINED3D_PUSH_CONSTANTS_PS_I */
1760 {FIELD_OFFSET(struct wined3d_state, ps_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_PS_I},
1761 /* WINED3D_PUSH_CONSTANTS_VS_B */
1762 {FIELD_OFFSET(struct wined3d_state, vs_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_VS_B},
1763 /* WINED3D_PUSH_CONSTANTS_PS_B */
1764 {FIELD_OFFSET(struct wined3d_state, ps_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_PS_B},
1767 static void wined3d_cs_st_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1768 unsigned int start_idx, unsigned int count, const void *constants)
1770 struct wined3d_device *device = cs->device;
1771 unsigned int context_count;
1772 unsigned int i;
1773 size_t offset;
1775 if (p == WINED3D_PUSH_CONSTANTS_VS_F)
1776 device->shader_backend->shader_update_float_vertex_constants(device, start_idx, count);
1777 else if (p == WINED3D_PUSH_CONSTANTS_PS_F)
1778 device->shader_backend->shader_update_float_pixel_constants(device, start_idx, count);
1780 offset = wined3d_cs_push_constant_info[p].offset + start_idx * wined3d_cs_push_constant_info[p].size;
1781 memcpy((BYTE *)&cs->state + offset, constants, count * wined3d_cs_push_constant_info[p].size);
1782 for (i = 0, context_count = device->context_count; i < context_count; ++i)
1784 device->contexts[i]->constant_update_mask |= wined3d_cs_push_constant_info[p].mask;
1788 static void wined3d_cs_exec_push_constants(struct wined3d_cs *cs, const void *data)
1790 const struct wined3d_cs_push_constants *op = data;
1792 wined3d_cs_st_push_constants(cs, op->type, op->start_idx, op->count, op->constants);
1795 static void wined3d_cs_mt_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1796 unsigned int start_idx, unsigned int count, const void *constants)
1798 struct wined3d_cs_push_constants *op;
1799 size_t size;
1801 size = count * wined3d_cs_push_constant_info[p].size;
1802 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_push_constants, constants[size]),
1803 WINED3D_CS_QUEUE_DEFAULT);
1804 op->opcode = WINED3D_CS_OP_PUSH_CONSTANTS;
1805 op->type = p;
1806 op->start_idx = start_idx;
1807 op->count = count;
1808 memcpy(op->constants, constants, size);
1810 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1813 static void wined3d_cs_exec_reset_state(struct wined3d_cs *cs, const void *data)
1815 struct wined3d_adapter *adapter = cs->device->adapter;
1817 state_cleanup(&cs->state);
1818 memset(&cs->state, 0, sizeof(cs->state));
1819 state_init(&cs->state, &cs->fb, &adapter->gl_info, &adapter->d3d_info,
1820 WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
1823 void wined3d_cs_emit_reset_state(struct wined3d_cs *cs)
1825 struct wined3d_cs_reset_state *op;
1827 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1828 op->opcode = WINED3D_CS_OP_RESET_STATE;
1830 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1833 static void wined3d_cs_exec_callback(struct wined3d_cs *cs, const void *data)
1835 const struct wined3d_cs_callback *op = data;
1837 op->callback(op->object);
1840 static void wined3d_cs_emit_callback(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1842 struct wined3d_cs_callback *op;
1844 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1845 op->opcode = WINED3D_CS_OP_CALLBACK;
1846 op->callback = callback;
1847 op->object = object;
1849 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1852 void wined3d_cs_destroy_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1854 wined3d_cs_emit_callback(cs, callback, object);
1857 void wined3d_cs_init_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1859 wined3d_cs_emit_callback(cs, callback, object);
1862 static void wined3d_cs_exec_query_issue(struct wined3d_cs *cs, const void *data)
1864 const struct wined3d_cs_query_issue *op = data;
1865 struct wined3d_query *query = op->query;
1866 BOOL poll;
1868 poll = query->query_ops->query_issue(query, op->flags);
1870 if (!cs->thread)
1871 return;
1873 if (poll && list_empty(&query->poll_list_entry))
1875 list_add_tail(&cs->query_poll_list, &query->poll_list_entry);
1876 return;
1879 /* This can happen if occlusion queries are restarted. This discards the
1880 * old result, since polling it could result in a GL error. */
1881 if ((op->flags & WINED3DISSUE_BEGIN) && !poll && !list_empty(&query->poll_list_entry))
1883 list_remove(&query->poll_list_entry);
1884 list_init(&query->poll_list_entry);
1885 InterlockedIncrement(&query->counter_retrieved);
1886 return;
1889 /* This can happen when an occlusion query is ended without being started,
1890 * in which case we don't want to poll, but still have to counter-balance
1891 * the increment of the main counter.
1893 * This can also happen if an event query is re-issued before the first
1894 * fence was reached. In this case the query is already in the list and
1895 * the poll function will check the new fence. We have to counter-balance
1896 * the discarded increment. */
1897 if (op->flags & WINED3DISSUE_END)
1898 InterlockedIncrement(&query->counter_retrieved);
1901 void wined3d_cs_emit_query_issue(struct wined3d_cs *cs, struct wined3d_query *query, DWORD flags)
1903 struct wined3d_cs_query_issue *op;
1905 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1906 op->opcode = WINED3D_CS_OP_QUERY_ISSUE;
1907 op->query = query;
1908 op->flags = flags;
1910 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1911 cs->queries_flushed = FALSE;
1914 static void wined3d_cs_exec_preload_resource(struct wined3d_cs *cs, const void *data)
1916 const struct wined3d_cs_preload_resource *op = data;
1917 struct wined3d_resource *resource = op->resource;
1919 resource->resource_ops->resource_preload(resource);
1920 wined3d_resource_release(resource);
1923 void wined3d_cs_emit_preload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
1925 struct wined3d_cs_preload_resource *op;
1927 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1928 op->opcode = WINED3D_CS_OP_PRELOAD_RESOURCE;
1929 op->resource = resource;
1931 wined3d_resource_acquire(resource);
1933 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1936 static void wined3d_cs_exec_unload_resource(struct wined3d_cs *cs, const void *data)
1938 const struct wined3d_cs_unload_resource *op = data;
1939 struct wined3d_resource *resource = op->resource;
1941 resource->resource_ops->resource_unload(resource);
1942 wined3d_resource_release(resource);
1945 void wined3d_cs_emit_unload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
1947 struct wined3d_cs_unload_resource *op;
1949 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1950 op->opcode = WINED3D_CS_OP_UNLOAD_RESOURCE;
1951 op->resource = resource;
1953 wined3d_resource_acquire(resource);
1955 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1958 static void wined3d_cs_exec_map(struct wined3d_cs *cs, const void *data)
1960 const struct wined3d_cs_map *op = data;
1961 struct wined3d_resource *resource = op->resource;
1963 *op->hr = resource->resource_ops->resource_sub_resource_map(resource,
1964 op->sub_resource_idx, op->map_desc, op->box, op->flags);
1967 HRESULT wined3d_cs_map(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx,
1968 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
1970 struct wined3d_cs_map *op;
1971 HRESULT hr;
1973 /* Mapping resources from the worker thread isn't an issue by itself, but
1974 * increasing the map count would be visible to applications. */
1975 wined3d_not_from_cs(cs);
1977 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
1978 op->opcode = WINED3D_CS_OP_MAP;
1979 op->resource = resource;
1980 op->sub_resource_idx = sub_resource_idx;
1981 op->map_desc = map_desc;
1982 op->box = box;
1983 op->flags = flags;
1984 op->hr = &hr;
1986 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
1987 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
1989 return hr;
1992 static void wined3d_cs_exec_unmap(struct wined3d_cs *cs, const void *data)
1994 const struct wined3d_cs_unmap *op = data;
1995 struct wined3d_resource *resource = op->resource;
1997 *op->hr = resource->resource_ops->resource_sub_resource_unmap(resource, op->sub_resource_idx);
2000 HRESULT wined3d_cs_unmap(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx)
2002 struct wined3d_cs_unmap *op;
2003 HRESULT hr;
2005 wined3d_not_from_cs(cs);
2007 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2008 op->opcode = WINED3D_CS_OP_UNMAP;
2009 op->resource = resource;
2010 op->sub_resource_idx = sub_resource_idx;
2011 op->hr = &hr;
2013 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2014 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2016 return hr;
2019 static void wined3d_cs_exec_blt_sub_resource(struct wined3d_cs *cs, const void *data)
2021 const struct wined3d_cs_blt_sub_resource *op = data;
2023 if (op->dst_resource->type == WINED3D_RTYPE_BUFFER)
2025 wined3d_buffer_copy(buffer_from_resource(op->dst_resource), op->dst_box.left,
2026 buffer_from_resource(op->src_resource), op->src_box.left,
2027 op->src_box.right - op->src_box.left);
2029 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_2D)
2031 struct wined3d_surface *dst_surface, *src_surface;
2032 struct wined3d_texture *dst_texture, *src_texture;
2033 RECT dst_rect, src_rect;
2035 dst_texture = texture_from_resource(op->dst_resource);
2036 src_texture = texture_from_resource(op->src_resource);
2037 dst_surface = dst_texture->sub_resources[op->dst_sub_resource_idx].u.surface;
2038 src_surface = src_texture->sub_resources[op->src_sub_resource_idx].u.surface;
2039 SetRect(&dst_rect, op->dst_box.left, op->dst_box.top, op->dst_box.right, op->dst_box.bottom);
2040 SetRect(&src_rect, op->src_box.left, op->src_box.top, op->src_box.right, op->src_box.bottom);
2042 if (FAILED(wined3d_surface_blt(dst_surface, &dst_rect, src_surface,
2043 &src_rect, op->flags, &op->fx, op->filter)))
2044 FIXME("Blit failed.\n");
2046 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_3D)
2048 struct wined3d_texture *src_texture, *dst_texture;
2049 unsigned int level, update_w, update_h, update_d;
2050 unsigned int row_pitch, slice_pitch;
2051 struct wined3d_context *context;
2052 struct wined3d_bo_address addr;
2054 if (op->flags & ~WINED3D_BLT_RAW)
2056 FIXME("Flags %#x not implemented for %s resources.\n",
2057 op->flags, debug_d3dresourcetype(op->dst_resource->type));
2058 goto error;
2061 if (!(op->flags & WINED3D_BLT_RAW) && op->src_resource->format != op->dst_resource->format)
2063 FIXME("Format conversion not implemented for %s resources.\n",
2064 debug_d3dresourcetype(op->dst_resource->type));
2065 goto error;
2068 update_w = op->dst_box.right - op->dst_box.left;
2069 update_h = op->dst_box.bottom - op->dst_box.top;
2070 update_d = op->dst_box.back - op->dst_box.front;
2071 if (op->src_box.right - op->src_box.left != update_w
2072 || op->src_box.bottom - op->src_box.top != update_h
2073 || op->src_box.back - op->src_box.front != update_d)
2075 FIXME("Stretching not implemented for %s resources.\n",
2076 debug_d3dresourcetype(op->dst_resource->type));
2077 goto error;
2080 if (op->src_box.left || op->src_box.top || op->src_box.front)
2082 FIXME("Source box %s not supported for %s resources.\n",
2083 debug_box(&op->src_box), debug_d3dresourcetype(op->dst_resource->type));
2084 goto error;
2087 dst_texture = texture_from_resource(op->dst_resource);
2088 src_texture = texture_from_resource(op->src_resource);
2090 context = context_acquire(cs->device, NULL, 0);
2092 if (!wined3d_texture_load_location(src_texture, op->src_sub_resource_idx,
2093 context, src_texture->resource.map_binding))
2095 ERR("Failed to load source sub-resource into %s.\n",
2096 wined3d_debug_location(src_texture->resource.map_binding));
2097 context_release(context);
2098 goto error;
2101 level = op->dst_sub_resource_idx % dst_texture->level_count;
2102 if (update_w == wined3d_texture_get_level_width(dst_texture, level)
2103 && update_h == wined3d_texture_get_level_height(dst_texture, level)
2104 && update_d == wined3d_texture_get_level_depth(dst_texture, level))
2106 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
2108 else if (!wined3d_texture_load_location(dst_texture, op->dst_sub_resource_idx,
2109 context, WINED3D_LOCATION_TEXTURE_RGB))
2111 ERR("Failed to load destination sub-resource.\n");
2112 context_release(context);
2113 goto error;
2116 wined3d_texture_get_memory(src_texture, op->src_sub_resource_idx, &addr, src_texture->resource.map_binding);
2117 wined3d_texture_get_pitch(src_texture, op->src_sub_resource_idx % src_texture->level_count,
2118 &row_pitch, &slice_pitch);
2120 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
2121 wined3d_texture_upload_data(dst_texture, op->dst_sub_resource_idx, context, &op->dst_box,
2122 wined3d_const_bo_address(&addr), row_pitch, slice_pitch);
2123 wined3d_texture_validate_location(dst_texture, op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2124 wined3d_texture_invalidate_location(dst_texture, op->dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2126 context_release(context);
2128 else
2130 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(op->dst_resource->type));
2133 error:
2134 if (op->src_resource)
2135 wined3d_resource_release(op->src_resource);
2136 wined3d_resource_release(op->dst_resource);
2139 void wined3d_cs_emit_blt_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *dst_resource,
2140 unsigned int dst_sub_resource_idx, const struct wined3d_box *dst_box, struct wined3d_resource *src_resource,
2141 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, DWORD flags,
2142 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2144 struct wined3d_cs_blt_sub_resource *op;
2146 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2147 op->opcode = WINED3D_CS_OP_BLT_SUB_RESOURCE;
2148 op->dst_resource = dst_resource;
2149 op->dst_sub_resource_idx = dst_sub_resource_idx;
2150 op->dst_box = *dst_box;
2151 op->src_resource = src_resource;
2152 op->src_sub_resource_idx = src_sub_resource_idx;
2153 op->src_box = *src_box;
2154 op->flags = flags;
2155 if (fx)
2156 op->fx = *fx;
2157 else
2158 memset(&op->fx, 0, sizeof(op->fx));
2159 op->filter = filter;
2161 wined3d_resource_acquire(dst_resource);
2162 if (src_resource)
2163 wined3d_resource_acquire(src_resource);
2165 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2166 if (flags & WINED3D_BLT_SYNCHRONOUS)
2167 cs->ops->finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2170 static void wined3d_cs_exec_update_sub_resource(struct wined3d_cs *cs, const void *data)
2172 const struct wined3d_cs_update_sub_resource *op = data;
2173 const struct wined3d_box *box = &op->box;
2174 unsigned int width, height, depth, level;
2175 struct wined3d_const_bo_address addr;
2176 struct wined3d_context *context;
2177 struct wined3d_texture *texture;
2179 if (op->resource->type == WINED3D_RTYPE_BUFFER)
2181 struct wined3d_buffer *buffer = buffer_from_resource(op->resource);
2183 context = context_acquire(op->resource->device, NULL, 0);
2184 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
2186 ERR("Failed to load buffer location.\n");
2187 context_release(context);
2188 goto done;
2191 wined3d_buffer_upload_data(buffer, context, box, op->data.data);
2192 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
2193 context_release(context);
2194 goto done;
2197 texture = wined3d_texture_from_resource(op->resource);
2199 level = op->sub_resource_idx % texture->level_count;
2200 width = wined3d_texture_get_level_width(texture, level);
2201 height = wined3d_texture_get_level_height(texture, level);
2202 depth = wined3d_texture_get_level_depth(texture, level);
2204 addr.buffer_object = 0;
2205 addr.addr = op->data.data;
2207 context = context_acquire(op->resource->device, NULL, 0);
2209 /* Only load the sub-resource for partial updates. */
2210 if (!box->left && !box->top && !box->front
2211 && box->right == width && box->bottom == height && box->back == depth)
2212 wined3d_texture_prepare_texture(texture, context, FALSE);
2213 else
2214 wined3d_texture_load_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2215 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2217 wined3d_texture_upload_data(texture, op->sub_resource_idx, context,
2218 box, &addr, op->data.row_pitch, op->data.slice_pitch);
2220 context_release(context);
2222 wined3d_texture_validate_location(texture, op->sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2223 wined3d_texture_invalidate_location(texture, op->sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2225 done:
2226 wined3d_resource_release(op->resource);
2229 void wined3d_cs_emit_update_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *resource,
2230 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
2231 unsigned int slice_pitch)
2233 struct wined3d_cs_update_sub_resource *op;
2235 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2236 op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2237 op->resource = resource;
2238 op->sub_resource_idx = sub_resource_idx;
2239 op->box = *box;
2240 op->data.row_pitch = row_pitch;
2241 op->data.slice_pitch = slice_pitch;
2242 op->data.data = data;
2244 wined3d_resource_acquire(resource);
2246 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2247 /* The data pointer may go away, so we need to wait until it is read.
2248 * Copying the data may be faster if it's small. */
2249 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2252 static void wined3d_cs_exec_add_dirty_texture_region(struct wined3d_cs *cs, const void *data)
2254 const struct wined3d_cs_add_dirty_texture_region *op = data;
2255 struct wined3d_texture *texture = op->texture;
2256 unsigned int sub_resource_idx, i;
2257 struct wined3d_context *context;
2259 context = context_acquire(cs->device, NULL, 0);
2260 sub_resource_idx = op->layer * texture->level_count;
2261 for (i = 0; i < texture->level_count; ++i, ++sub_resource_idx)
2263 if (wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
2264 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2265 else
2266 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
2268 context_release(context);
2270 wined3d_resource_release(&texture->resource);
2273 void wined3d_cs_emit_add_dirty_texture_region(struct wined3d_cs *cs,
2274 struct wined3d_texture *texture, unsigned int layer)
2276 struct wined3d_cs_add_dirty_texture_region *op;
2278 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2279 op->opcode = WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION;
2280 op->texture = texture;
2281 op->layer = layer;
2283 wined3d_resource_acquire(&texture->resource);
2285 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2288 static void wined3d_cs_exec_clear_unordered_access_view(struct wined3d_cs *cs, const void *data)
2290 const struct wined3d_cs_clear_unordered_access_view *op = data;
2291 struct wined3d_unordered_access_view *view = op->view;
2292 struct wined3d_context *context;
2294 context = context_acquire(cs->device, NULL, 0);
2295 wined3d_unordered_access_view_clear_uint(view, &op->clear_value, context);
2296 context_release(context);
2298 wined3d_resource_release(view->resource);
2301 void wined3d_cs_emit_clear_unordered_access_view_uint(struct wined3d_cs *cs,
2302 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
2304 struct wined3d_cs_clear_unordered_access_view *op;
2306 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2307 op->opcode = WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW;
2308 op->view = view;
2309 op->clear_value = *clear_value;
2311 wined3d_resource_acquire(view->resource);
2313 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2316 static void wined3d_cs_exec_copy_uav_counter(struct wined3d_cs *cs, const void *data)
2318 const struct wined3d_cs_copy_uav_counter *op = data;
2319 struct wined3d_unordered_access_view *view = op->view;
2320 struct wined3d_context *context;
2322 context = context_acquire(cs->device, NULL, 0);
2323 wined3d_unordered_access_view_copy_counter(view, op->buffer, op->offset, context);
2324 context_release(context);
2326 wined3d_resource_release(&op->buffer->resource);
2329 void wined3d_cs_emit_copy_uav_counter(struct wined3d_cs *cs, struct wined3d_buffer *dst_buffer,
2330 unsigned int offset, struct wined3d_unordered_access_view *uav)
2332 struct wined3d_cs_copy_uav_counter *op;
2334 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2335 op->opcode = WINED3D_CS_OP_COPY_UAV_COUNTER;
2336 op->buffer = dst_buffer;
2337 op->offset = offset;
2338 op->view = uav;
2340 wined3d_resource_acquire(&dst_buffer->resource);
2342 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2345 static void wined3d_cs_exec_generate_mipmaps(struct wined3d_cs *cs, const void *data)
2347 const struct wined3d_cs_generate_mipmaps *op = data;
2348 struct wined3d_shader_resource_view *view = op->view;
2350 shader_resource_view_generate_mipmaps(view);
2351 wined3d_resource_release(view->resource);
2354 void wined3d_cs_emit_generate_mipmaps(struct wined3d_cs *cs, struct wined3d_shader_resource_view *view)
2356 struct wined3d_cs_generate_mipmaps *op;
2358 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2359 op->opcode = WINED3D_CS_OP_GENERATE_MIPMAPS;
2360 op->view = view;
2362 wined3d_resource_acquire(view->resource);
2364 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2367 static void wined3d_cs_emit_stop(struct wined3d_cs *cs)
2369 struct wined3d_cs_stop *op;
2371 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2372 op->opcode = WINED3D_CS_OP_STOP;
2374 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2375 cs->ops->finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2378 static void (* const wined3d_cs_op_handlers[])(struct wined3d_cs *cs, const void *data) =
2380 /* WINED3D_CS_OP_NOP */ wined3d_cs_exec_nop,
2381 /* WINED3D_CS_OP_PRESENT */ wined3d_cs_exec_present,
2382 /* WINED3D_CS_OP_CLEAR */ wined3d_cs_exec_clear,
2383 /* WINED3D_CS_OP_DISPATCH */ wined3d_cs_exec_dispatch,
2384 /* WINED3D_CS_OP_DRAW */ wined3d_cs_exec_draw,
2385 /* WINED3D_CS_OP_FLUSH */ wined3d_cs_exec_flush,
2386 /* WINED3D_CS_OP_SET_PREDICATION */ wined3d_cs_exec_set_predication,
2387 /* WINED3D_CS_OP_SET_VIEWPORT */ wined3d_cs_exec_set_viewport,
2388 /* WINED3D_CS_OP_SET_SCISSOR_RECT */ wined3d_cs_exec_set_scissor_rect,
2389 /* WINED3D_CS_OP_SET_RENDERTARGET_VIEW */ wined3d_cs_exec_set_rendertarget_view,
2390 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW */ wined3d_cs_exec_set_depth_stencil_view,
2391 /* WINED3D_CS_OP_SET_VERTEX_DECLARATION */ wined3d_cs_exec_set_vertex_declaration,
2392 /* WINED3D_CS_OP_SET_STREAM_SOURCE */ wined3d_cs_exec_set_stream_source,
2393 /* WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ */ wined3d_cs_exec_set_stream_source_freq,
2394 /* WINED3D_CS_OP_SET_STREAM_OUTPUT */ wined3d_cs_exec_set_stream_output,
2395 /* WINED3D_CS_OP_SET_INDEX_BUFFER */ wined3d_cs_exec_set_index_buffer,
2396 /* WINED3D_CS_OP_SET_CONSTANT_BUFFER */ wined3d_cs_exec_set_constant_buffer,
2397 /* WINED3D_CS_OP_SET_TEXTURE */ wined3d_cs_exec_set_texture,
2398 /* WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW */ wined3d_cs_exec_set_shader_resource_view,
2399 /* WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_set_unordered_access_view,
2400 /* WINED3D_CS_OP_SET_SAMPLER */ wined3d_cs_exec_set_sampler,
2401 /* WINED3D_CS_OP_SET_SHADER */ wined3d_cs_exec_set_shader,
2402 /* WINED3D_CS_OP_SET_RASTERIZER_STATE */ wined3d_cs_exec_set_rasterizer_state,
2403 /* WINED3D_CS_OP_SET_RENDER_STATE */ wined3d_cs_exec_set_render_state,
2404 /* WINED3D_CS_OP_SET_TEXTURE_STATE */ wined3d_cs_exec_set_texture_state,
2405 /* WINED3D_CS_OP_SET_SAMPLER_STATE */ wined3d_cs_exec_set_sampler_state,
2406 /* WINED3D_CS_OP_SET_TRANSFORM */ wined3d_cs_exec_set_transform,
2407 /* WINED3D_CS_OP_SET_CLIP_PLANE */ wined3d_cs_exec_set_clip_plane,
2408 /* WINED3D_CS_OP_SET_COLOR_KEY */ wined3d_cs_exec_set_color_key,
2409 /* WINED3D_CS_OP_SET_MATERIAL */ wined3d_cs_exec_set_material,
2410 /* WINED3D_CS_OP_SET_LIGHT */ wined3d_cs_exec_set_light,
2411 /* WINED3D_CS_OP_SET_LIGHT_ENABLE */ wined3d_cs_exec_set_light_enable,
2412 /* WINED3D_CS_OP_PUSH_CONSTANTS */ wined3d_cs_exec_push_constants,
2413 /* WINED3D_CS_OP_RESET_STATE */ wined3d_cs_exec_reset_state,
2414 /* WINED3D_CS_OP_CALLBACK */ wined3d_cs_exec_callback,
2415 /* WINED3D_CS_OP_QUERY_ISSUE */ wined3d_cs_exec_query_issue,
2416 /* WINED3D_CS_OP_PRELOAD_RESOURCE */ wined3d_cs_exec_preload_resource,
2417 /* WINED3D_CS_OP_UNLOAD_RESOURCE */ wined3d_cs_exec_unload_resource,
2418 /* WINED3D_CS_OP_MAP */ wined3d_cs_exec_map,
2419 /* WINED3D_CS_OP_UNMAP */ wined3d_cs_exec_unmap,
2420 /* WINED3D_CS_OP_BLT_SUB_RESOURCE */ wined3d_cs_exec_blt_sub_resource,
2421 /* WINED3D_CS_OP_UPDATE_SUB_RESOURCE */ wined3d_cs_exec_update_sub_resource,
2422 /* WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION */ wined3d_cs_exec_add_dirty_texture_region,
2423 /* WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_clear_unordered_access_view,
2424 /* WINED3D_CS_OP_COPY_UAV_COUNTER */ wined3d_cs_exec_copy_uav_counter,
2425 /* WINED3D_CS_OP_GENERATE_MIPMAPS */ wined3d_cs_exec_generate_mipmaps,
2428 static void *wined3d_cs_st_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2430 if (size > (cs->data_size - cs->end))
2432 size_t new_size;
2433 void *new_data;
2435 new_size = max(size, cs->data_size * 2);
2436 if (!cs->end)
2437 new_data = HeapReAlloc(GetProcessHeap(), 0, cs->data, new_size);
2438 else
2439 new_data = HeapAlloc(GetProcessHeap(), 0, new_size);
2440 if (!new_data)
2441 return NULL;
2443 cs->data_size = new_size;
2444 cs->start = cs->end = 0;
2445 cs->data = new_data;
2448 cs->end += size;
2450 return (BYTE *)cs->data + cs->start;
2453 static void wined3d_cs_st_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2455 enum wined3d_cs_op opcode;
2456 size_t start;
2457 BYTE *data;
2459 data = cs->data;
2460 start = cs->start;
2461 cs->start = cs->end;
2463 opcode = *(const enum wined3d_cs_op *)&data[start];
2464 if (opcode >= WINED3D_CS_OP_STOP)
2465 ERR("Invalid opcode %#x.\n", opcode);
2466 else
2467 wined3d_cs_op_handlers[opcode](cs, &data[start]);
2469 if (cs->data == data)
2470 cs->start = cs->end = start;
2471 else if (!start)
2472 HeapFree(GetProcessHeap(), 0, data);
2475 static void wined3d_cs_st_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2479 static const struct wined3d_cs_ops wined3d_cs_st_ops =
2481 wined3d_cs_st_require_space,
2482 wined3d_cs_st_submit,
2483 wined3d_cs_st_finish,
2484 wined3d_cs_st_push_constants,
2487 static BOOL wined3d_cs_queue_is_empty(const struct wined3d_cs *cs, const struct wined3d_cs_queue *queue)
2489 wined3d_from_cs(cs);
2490 return *(volatile LONG *)&queue->head == queue->tail;
2493 static void wined3d_cs_queue_submit(struct wined3d_cs_queue *queue, struct wined3d_cs *cs)
2495 struct wined3d_cs_packet *packet;
2496 size_t packet_size;
2498 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2499 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2500 InterlockedExchange(&queue->head, (queue->head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1));
2502 if (InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2503 SetEvent(cs->event);
2506 static void wined3d_cs_mt_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2508 if (cs->thread_id == GetCurrentThreadId())
2509 return wined3d_cs_st_submit(cs, queue_id);
2511 wined3d_cs_queue_submit(&cs->queue[queue_id], cs);
2514 static void *wined3d_cs_queue_require_space(struct wined3d_cs_queue *queue, size_t size, struct wined3d_cs *cs)
2516 size_t queue_size = ARRAY_SIZE(queue->data);
2517 size_t header_size, packet_size, remaining;
2518 struct wined3d_cs_packet *packet;
2520 header_size = FIELD_OFFSET(struct wined3d_cs_packet, data[0]);
2521 size = (size + header_size - 1) & ~(header_size - 1);
2522 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[size]);
2523 if (packet_size >= WINED3D_CS_QUEUE_SIZE)
2525 ERR("Packet size %lu >= queue size %u.\n",
2526 (unsigned long)packet_size, WINED3D_CS_QUEUE_SIZE);
2527 return NULL;
2530 remaining = queue_size - queue->head;
2531 if (remaining < packet_size)
2533 size_t nop_size = remaining - header_size;
2534 struct wined3d_cs_nop *nop;
2536 TRACE("Inserting a nop for %lu + %lu bytes.\n",
2537 (unsigned long)header_size, (unsigned long)nop_size);
2539 nop = wined3d_cs_queue_require_space(queue, nop_size, cs);
2540 if (nop_size)
2541 nop->opcode = WINED3D_CS_OP_NOP;
2543 wined3d_cs_queue_submit(queue, cs);
2544 assert(!queue->head);
2547 for (;;)
2549 LONG tail = *(volatile LONG *)&queue->tail;
2550 LONG head = queue->head;
2551 LONG new_pos;
2553 /* Empty. */
2554 if (head == tail)
2555 break;
2556 new_pos = (head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1);
2557 /* Head ahead of tail. We checked the remaining size above, so we only
2558 * need to make sure we don't make head equal to tail. */
2559 if (head > tail && (new_pos != tail))
2560 break;
2561 /* Tail ahead of head. Make sure the new head is before the tail as
2562 * well. Note that new_pos is 0 when it's at the end of the queue. */
2563 if (new_pos < tail && new_pos)
2564 break;
2566 TRACE("Waiting for free space. Head %u, tail %u, packet size %lu.\n",
2567 head, tail, (unsigned long)packet_size);
2570 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2571 packet->size = size;
2572 return packet->data;
2575 static void *wined3d_cs_mt_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2577 if (cs->thread_id == GetCurrentThreadId())
2578 return wined3d_cs_st_require_space(cs, size, queue_id);
2580 return wined3d_cs_queue_require_space(&cs->queue[queue_id], size, cs);
2583 static void wined3d_cs_mt_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2585 if (cs->thread_id == GetCurrentThreadId())
2586 return wined3d_cs_st_finish(cs, queue_id);
2588 while (cs->queue[queue_id].head != *(volatile LONG *)&cs->queue[queue_id].tail)
2589 wined3d_pause();
2592 static const struct wined3d_cs_ops wined3d_cs_mt_ops =
2594 wined3d_cs_mt_require_space,
2595 wined3d_cs_mt_submit,
2596 wined3d_cs_mt_finish,
2597 wined3d_cs_mt_push_constants,
2600 static void poll_queries(struct wined3d_cs *cs)
2602 struct wined3d_query *query, *cursor;
2604 LIST_FOR_EACH_ENTRY_SAFE(query, cursor, &cs->query_poll_list, struct wined3d_query, poll_list_entry)
2606 if (!query->query_ops->query_poll(query, 0))
2607 continue;
2609 list_remove(&query->poll_list_entry);
2610 list_init(&query->poll_list_entry);
2611 InterlockedIncrement(&query->counter_retrieved);
2615 static void wined3d_cs_wait_event(struct wined3d_cs *cs)
2617 InterlockedExchange(&cs->waiting_for_event, TRUE);
2619 /* The main thread might have enqueued a command and blocked on it after
2620 * the CS thread decided to enter wined3d_cs_wait_event(), but before
2621 * "waiting_for_event" was set.
2623 * Likewise, we can race with the main thread when resetting
2624 * "waiting_for_event", in which case we would need to call
2625 * WaitForSingleObject() because the main thread called SetEvent(). */
2626 if (!(wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_DEFAULT])
2627 && wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_MAP]))
2628 && InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2629 return;
2631 WaitForSingleObject(cs->event, INFINITE);
2634 static DWORD WINAPI wined3d_cs_run(void *ctx)
2636 struct wined3d_cs_packet *packet;
2637 struct wined3d_cs_queue *queue;
2638 unsigned int spin_count = 0;
2639 struct wined3d_cs *cs = ctx;
2640 enum wined3d_cs_op opcode;
2641 HMODULE wined3d_module;
2642 unsigned int poll = 0;
2643 LONG tail;
2645 TRACE("Started.\n");
2647 /* Copy the module handle to a local variable to avoid racing with the
2648 * thread freeing "cs" before the FreeLibraryAndExitThread() call. */
2649 wined3d_module = cs->wined3d_module;
2651 list_init(&cs->query_poll_list);
2652 cs->thread_id = GetCurrentThreadId();
2653 for (;;)
2655 if (++poll == WINED3D_CS_QUERY_POLL_INTERVAL)
2657 poll_queries(cs);
2658 poll = 0;
2661 queue = &cs->queue[WINED3D_CS_QUEUE_MAP];
2662 if (wined3d_cs_queue_is_empty(cs, queue))
2664 queue = &cs->queue[WINED3D_CS_QUEUE_DEFAULT];
2665 if (wined3d_cs_queue_is_empty(cs, queue))
2667 if (++spin_count >= WINED3D_CS_SPIN_COUNT && list_empty(&cs->query_poll_list))
2668 wined3d_cs_wait_event(cs);
2669 continue;
2672 spin_count = 0;
2674 tail = queue->tail;
2675 packet = (struct wined3d_cs_packet *)&queue->data[tail];
2676 if (packet->size)
2678 opcode = *(const enum wined3d_cs_op *)packet->data;
2680 if (opcode >= WINED3D_CS_OP_STOP)
2682 if (opcode > WINED3D_CS_OP_STOP)
2683 ERR("Invalid opcode %#x.\n", opcode);
2684 break;
2687 wined3d_cs_op_handlers[opcode](cs, packet->data);
2690 tail += FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2691 tail &= (WINED3D_CS_QUEUE_SIZE - 1);
2692 InterlockedExchange(&queue->tail, tail);
2695 cs->queue[WINED3D_CS_QUEUE_MAP].tail = cs->queue[WINED3D_CS_QUEUE_MAP].head;
2696 cs->queue[WINED3D_CS_QUEUE_DEFAULT].tail = cs->queue[WINED3D_CS_QUEUE_DEFAULT].head;
2697 TRACE("Stopped.\n");
2698 FreeLibraryAndExitThread(wined3d_module, 0);
2701 struct wined3d_cs *wined3d_cs_create(struct wined3d_device *device)
2703 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2704 struct wined3d_cs *cs;
2706 if (!(cs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cs))))
2707 return NULL;
2709 cs->ops = &wined3d_cs_st_ops;
2710 cs->device = device;
2712 if (!(cs->fb.render_targets = wined3d_calloc(gl_info->limits.buffers, sizeof(*cs->fb.render_targets))))
2714 HeapFree(GetProcessHeap(), 0, cs);
2715 return NULL;
2718 state_init(&cs->state, &cs->fb, gl_info, &device->adapter->d3d_info,
2719 WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
2721 cs->data_size = WINED3D_INITIAL_CS_SIZE;
2722 if (!(cs->data = HeapAlloc(GetProcessHeap(), 0, cs->data_size)))
2723 goto fail;
2725 if (wined3d_settings.cs_multithreaded
2726 && !RtlIsCriticalSectionLockedByThread(NtCurrentTeb()->Peb->LoaderLock))
2728 cs->ops = &wined3d_cs_mt_ops;
2730 if (!(cs->event = CreateEventW(NULL, FALSE, FALSE, NULL)))
2732 ERR("Failed to create command stream event.\n");
2733 HeapFree(GetProcessHeap(), 0, cs->data);
2734 goto fail;
2737 if (!(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2738 (const WCHAR *)wined3d_cs_run, &cs->wined3d_module)))
2740 ERR("Failed to get wined3d module handle.\n");
2741 CloseHandle(cs->event);
2742 HeapFree(GetProcessHeap(), 0, cs->data);
2743 goto fail;
2746 if (!(cs->thread = CreateThread(NULL, 0, wined3d_cs_run, cs, 0, NULL)))
2748 ERR("Failed to create wined3d command stream thread.\n");
2749 FreeLibrary(cs->wined3d_module);
2750 CloseHandle(cs->event);
2751 HeapFree(GetProcessHeap(), 0, cs->data);
2752 goto fail;
2756 return cs;
2758 fail:
2759 state_cleanup(&cs->state);
2760 HeapFree(GetProcessHeap(), 0, cs->fb.render_targets);
2761 HeapFree(GetProcessHeap(), 0, cs);
2762 return NULL;
2765 void wined3d_cs_destroy(struct wined3d_cs *cs)
2767 if (cs->thread)
2769 wined3d_cs_emit_stop(cs);
2770 CloseHandle(cs->thread);
2771 if (!CloseHandle(cs->event))
2772 ERR("Closing event failed.\n");
2775 state_cleanup(&cs->state);
2776 HeapFree(GetProcessHeap(), 0, cs->fb.render_targets);
2777 HeapFree(GetProcessHeap(), 0, cs->data);
2778 HeapFree(GetProcessHeap(), 0, cs);