kernel32: Don't clear WINEDEBUG in the debugger process if +winedbg is set.
[wine.git] / dlls / wined3d / cs.c
blob3f1ca8ce65854ee066b2c2a5d828981fc860af91
1 /*
2 * Copyright 2013 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
21 #include "wined3d_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
25 #define WINED3D_INITIAL_CS_SIZE 4096
27 enum wined3d_cs_op
29 WINED3D_CS_OP_NOP,
30 WINED3D_CS_OP_PRESENT,
31 WINED3D_CS_OP_CLEAR,
32 WINED3D_CS_OP_DISPATCH,
33 WINED3D_CS_OP_DRAW,
34 WINED3D_CS_OP_FLUSH,
35 WINED3D_CS_OP_SET_PREDICATION,
36 WINED3D_CS_OP_SET_VIEWPORTS,
37 WINED3D_CS_OP_SET_SCISSOR_RECTS,
38 WINED3D_CS_OP_SET_RENDERTARGET_VIEW,
39 WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW,
40 WINED3D_CS_OP_SET_VERTEX_DECLARATION,
41 WINED3D_CS_OP_SET_STREAM_SOURCE,
42 WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ,
43 WINED3D_CS_OP_SET_STREAM_OUTPUT,
44 WINED3D_CS_OP_SET_INDEX_BUFFER,
45 WINED3D_CS_OP_SET_CONSTANT_BUFFER,
46 WINED3D_CS_OP_SET_TEXTURE,
47 WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW,
48 WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW,
49 WINED3D_CS_OP_SET_SAMPLER,
50 WINED3D_CS_OP_SET_SHADER,
51 WINED3D_CS_OP_SET_BLEND_STATE,
52 WINED3D_CS_OP_SET_RASTERIZER_STATE,
53 WINED3D_CS_OP_SET_RENDER_STATE,
54 WINED3D_CS_OP_SET_TEXTURE_STATE,
55 WINED3D_CS_OP_SET_SAMPLER_STATE,
56 WINED3D_CS_OP_SET_TRANSFORM,
57 WINED3D_CS_OP_SET_CLIP_PLANE,
58 WINED3D_CS_OP_SET_COLOR_KEY,
59 WINED3D_CS_OP_SET_MATERIAL,
60 WINED3D_CS_OP_SET_LIGHT,
61 WINED3D_CS_OP_SET_LIGHT_ENABLE,
62 WINED3D_CS_OP_PUSH_CONSTANTS,
63 WINED3D_CS_OP_RESET_STATE,
64 WINED3D_CS_OP_CALLBACK,
65 WINED3D_CS_OP_QUERY_ISSUE,
66 WINED3D_CS_OP_PRELOAD_RESOURCE,
67 WINED3D_CS_OP_UNLOAD_RESOURCE,
68 WINED3D_CS_OP_MAP,
69 WINED3D_CS_OP_UNMAP,
70 WINED3D_CS_OP_BLT_SUB_RESOURCE,
71 WINED3D_CS_OP_UPDATE_SUB_RESOURCE,
72 WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION,
73 WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW,
74 WINED3D_CS_OP_COPY_UAV_COUNTER,
75 WINED3D_CS_OP_GENERATE_MIPMAPS,
76 WINED3D_CS_OP_STOP,
79 struct wined3d_cs_packet
81 size_t size;
82 BYTE data[1];
85 struct wined3d_cs_nop
87 enum wined3d_cs_op opcode;
90 struct wined3d_cs_present
92 enum wined3d_cs_op opcode;
93 HWND dst_window_override;
94 struct wined3d_swapchain *swapchain;
95 RECT src_rect;
96 RECT dst_rect;
97 unsigned int swap_interval;
98 DWORD flags;
101 struct wined3d_cs_clear
103 enum wined3d_cs_op opcode;
104 DWORD flags;
105 unsigned int rt_count;
106 struct wined3d_fb_state *fb;
107 RECT draw_rect;
108 struct wined3d_color color;
109 float depth;
110 DWORD stencil;
111 unsigned int rect_count;
112 RECT rects[1];
115 struct wined3d_cs_dispatch
117 enum wined3d_cs_op opcode;
118 struct wined3d_dispatch_parameters parameters;
121 struct wined3d_cs_draw
123 enum wined3d_cs_op opcode;
124 GLenum primitive_type;
125 GLint patch_vertex_count;
126 struct wined3d_draw_parameters parameters;
129 struct wined3d_cs_flush
131 enum wined3d_cs_op opcode;
134 struct wined3d_cs_set_predication
136 enum wined3d_cs_op opcode;
137 struct wined3d_query *predicate;
138 BOOL value;
141 struct wined3d_cs_set_viewports
143 enum wined3d_cs_op opcode;
144 unsigned int viewport_count;
145 struct wined3d_viewport viewports[1];
148 struct wined3d_cs_set_scissor_rects
150 enum wined3d_cs_op opcode;
151 unsigned int rect_count;
152 RECT rects[1];
155 struct wined3d_cs_set_rendertarget_view
157 enum wined3d_cs_op opcode;
158 unsigned int view_idx;
159 struct wined3d_rendertarget_view *view;
162 struct wined3d_cs_set_depth_stencil_view
164 enum wined3d_cs_op opcode;
165 struct wined3d_rendertarget_view *view;
168 struct wined3d_cs_set_vertex_declaration
170 enum wined3d_cs_op opcode;
171 struct wined3d_vertex_declaration *declaration;
174 struct wined3d_cs_set_stream_source
176 enum wined3d_cs_op opcode;
177 UINT stream_idx;
178 struct wined3d_buffer *buffer;
179 UINT offset;
180 UINT stride;
183 struct wined3d_cs_set_stream_source_freq
185 enum wined3d_cs_op opcode;
186 UINT stream_idx;
187 UINT frequency;
188 UINT flags;
191 struct wined3d_cs_set_stream_output
193 enum wined3d_cs_op opcode;
194 UINT stream_idx;
195 struct wined3d_buffer *buffer;
196 UINT offset;
199 struct wined3d_cs_set_index_buffer
201 enum wined3d_cs_op opcode;
202 struct wined3d_buffer *buffer;
203 enum wined3d_format_id format_id;
204 unsigned int offset;
207 struct wined3d_cs_set_constant_buffer
209 enum wined3d_cs_op opcode;
210 enum wined3d_shader_type type;
211 UINT cb_idx;
212 struct wined3d_buffer *buffer;
215 struct wined3d_cs_set_texture
217 enum wined3d_cs_op opcode;
218 UINT stage;
219 struct wined3d_texture *texture;
222 struct wined3d_cs_set_color_key
224 enum wined3d_cs_op opcode;
225 struct wined3d_texture *texture;
226 WORD flags;
227 WORD set;
228 struct wined3d_color_key color_key;
231 struct wined3d_cs_set_shader_resource_view
233 enum wined3d_cs_op opcode;
234 enum wined3d_shader_type type;
235 UINT view_idx;
236 struct wined3d_shader_resource_view *view;
239 struct wined3d_cs_set_unordered_access_view
241 enum wined3d_cs_op opcode;
242 enum wined3d_pipeline pipeline;
243 unsigned int view_idx;
244 struct wined3d_unordered_access_view *view;
245 unsigned int initial_count;
248 struct wined3d_cs_set_sampler
250 enum wined3d_cs_op opcode;
251 enum wined3d_shader_type type;
252 UINT sampler_idx;
253 struct wined3d_sampler *sampler;
256 struct wined3d_cs_set_shader
258 enum wined3d_cs_op opcode;
259 enum wined3d_shader_type type;
260 struct wined3d_shader *shader;
263 struct wined3d_cs_set_blend_state
265 enum wined3d_cs_op opcode;
266 struct wined3d_blend_state *state;
269 struct wined3d_cs_set_rasterizer_state
271 enum wined3d_cs_op opcode;
272 struct wined3d_rasterizer_state *state;
275 struct wined3d_cs_set_render_state
277 enum wined3d_cs_op opcode;
278 enum wined3d_render_state state;
279 DWORD value;
282 struct wined3d_cs_set_texture_state
284 enum wined3d_cs_op opcode;
285 UINT stage;
286 enum wined3d_texture_stage_state state;
287 DWORD value;
290 struct wined3d_cs_set_sampler_state
292 enum wined3d_cs_op opcode;
293 UINT sampler_idx;
294 enum wined3d_sampler_state state;
295 DWORD value;
298 struct wined3d_cs_set_transform
300 enum wined3d_cs_op opcode;
301 enum wined3d_transform_state state;
302 struct wined3d_matrix matrix;
305 struct wined3d_cs_set_clip_plane
307 enum wined3d_cs_op opcode;
308 UINT plane_idx;
309 struct wined3d_vec4 plane;
312 struct wined3d_cs_set_material
314 enum wined3d_cs_op opcode;
315 struct wined3d_material material;
318 struct wined3d_cs_set_light
320 enum wined3d_cs_op opcode;
321 struct wined3d_light_info light;
324 struct wined3d_cs_set_light_enable
326 enum wined3d_cs_op opcode;
327 unsigned int idx;
328 BOOL enable;
331 struct wined3d_cs_push_constants
333 enum wined3d_cs_op opcode;
334 enum wined3d_push_constants type;
335 unsigned int start_idx;
336 unsigned int count;
337 BYTE constants[1];
340 struct wined3d_cs_reset_state
342 enum wined3d_cs_op opcode;
345 struct wined3d_cs_callback
347 enum wined3d_cs_op opcode;
348 void (*callback)(void *object);
349 void *object;
352 struct wined3d_cs_query_issue
354 enum wined3d_cs_op opcode;
355 struct wined3d_query *query;
356 DWORD flags;
359 struct wined3d_cs_preload_resource
361 enum wined3d_cs_op opcode;
362 struct wined3d_resource *resource;
365 struct wined3d_cs_unload_resource
367 enum wined3d_cs_op opcode;
368 struct wined3d_resource *resource;
371 struct wined3d_cs_map
373 enum wined3d_cs_op opcode;
374 struct wined3d_resource *resource;
375 unsigned int sub_resource_idx;
376 struct wined3d_map_desc *map_desc;
377 const struct wined3d_box *box;
378 DWORD flags;
379 HRESULT *hr;
382 struct wined3d_cs_unmap
384 enum wined3d_cs_op opcode;
385 struct wined3d_resource *resource;
386 unsigned int sub_resource_idx;
387 HRESULT *hr;
390 struct wined3d_cs_blt_sub_resource
392 enum wined3d_cs_op opcode;
393 struct wined3d_resource *dst_resource;
394 unsigned int dst_sub_resource_idx;
395 struct wined3d_box dst_box;
396 struct wined3d_resource *src_resource;
397 unsigned int src_sub_resource_idx;
398 struct wined3d_box src_box;
399 DWORD flags;
400 struct wined3d_blt_fx fx;
401 enum wined3d_texture_filter_type filter;
404 struct wined3d_cs_update_sub_resource
406 enum wined3d_cs_op opcode;
407 struct wined3d_resource *resource;
408 unsigned int sub_resource_idx;
409 struct wined3d_box box;
410 struct wined3d_sub_resource_data data;
413 struct wined3d_cs_add_dirty_texture_region
415 enum wined3d_cs_op opcode;
416 struct wined3d_texture *texture;
417 unsigned int layer;
420 struct wined3d_cs_clear_unordered_access_view
422 enum wined3d_cs_op opcode;
423 struct wined3d_unordered_access_view *view;
424 struct wined3d_uvec4 clear_value;
427 struct wined3d_cs_copy_uav_counter
429 enum wined3d_cs_op opcode;
430 struct wined3d_buffer *buffer;
431 unsigned int offset;
432 struct wined3d_unordered_access_view *view;
435 struct wined3d_cs_generate_mipmaps
437 enum wined3d_cs_op opcode;
438 struct wined3d_shader_resource_view *view;
441 struct wined3d_cs_stop
443 enum wined3d_cs_op opcode;
446 static void wined3d_cs_exec_nop(struct wined3d_cs *cs, const void *data)
450 static void wined3d_cs_exec_present(struct wined3d_cs *cs, const void *data)
452 const struct wined3d_cs_present *op = data;
453 struct wined3d_swapchain *swapchain;
454 unsigned int i;
456 swapchain = op->swapchain;
457 wined3d_swapchain_set_window(swapchain, op->dst_window_override);
458 wined3d_swapchain_set_swap_interval(swapchain, op->swap_interval);
460 swapchain->swapchain_ops->swapchain_present(swapchain, &op->src_rect, &op->dst_rect, op->flags);
462 wined3d_resource_release(&swapchain->front_buffer->resource);
463 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
465 wined3d_resource_release(&swapchain->back_buffers[i]->resource);
468 InterlockedDecrement(&cs->pending_presents);
471 void wined3d_cs_emit_present(struct wined3d_cs *cs, struct wined3d_swapchain *swapchain,
472 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
473 unsigned int swap_interval, DWORD flags)
475 struct wined3d_cs_present *op;
476 unsigned int i;
477 LONG pending;
479 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
480 op->opcode = WINED3D_CS_OP_PRESENT;
481 op->dst_window_override = dst_window_override;
482 op->swapchain = swapchain;
483 op->src_rect = *src_rect;
484 op->dst_rect = *dst_rect;
485 op->swap_interval = swap_interval;
486 op->flags = flags;
488 pending = InterlockedIncrement(&cs->pending_presents);
490 wined3d_resource_acquire(&swapchain->front_buffer->resource);
491 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
493 wined3d_resource_acquire(&swapchain->back_buffers[i]->resource);
496 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
498 /* Limit input latency by limiting the number of presents that we can get
499 * ahead of the worker thread. */
500 while (pending >= swapchain->max_frame_latency)
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 if (flags & WINED3DCLEAR_TARGET)
545 op->rt_count = rt_count;
546 else
547 op->rt_count = 0;
548 op->fb = &cs->fb;
549 SetRect(&op->draw_rect, vp->x, vp->y, vp->x + vp->width, vp->y + vp->height);
550 if (state->render_states[WINED3D_RS_SCISSORTESTENABLE])
551 IntersectRect(&op->draw_rect, &op->draw_rect, &state->scissor_rects[0]);
552 op->color = *color;
553 op->depth = depth;
554 op->stencil = stencil;
555 op->rect_count = rect_count;
556 memcpy(op->rects, rects, sizeof(*rects) * rect_count);
558 if (flags & WINED3DCLEAR_TARGET)
560 for (i = 0; i < rt_count; ++i)
562 if ((view = state->fb->render_targets[i]))
564 SetRect(&view_rect, 0, 0, view->width, view->height);
565 IntersectRect(&op->draw_rect, &op->draw_rect, &view_rect);
566 wined3d_resource_acquire(view->resource);
570 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
572 view = state->fb->depth_stencil;
573 SetRect(&view_rect, 0, 0, view->width, view->height);
574 IntersectRect(&op->draw_rect, &op->draw_rect, &view_rect);
575 wined3d_resource_acquire(view->resource);
578 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
581 void wined3d_cs_emit_clear_rendertarget_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view,
582 const RECT *rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
584 struct wined3d_cs_clear *op;
585 size_t size;
587 size = FIELD_OFFSET(struct wined3d_cs_clear, rects[1]) + sizeof(struct wined3d_fb_state);
588 op = cs->ops->require_space(cs, size, WINED3D_CS_QUEUE_DEFAULT);
589 op->fb = (void *)&op->rects[1];
591 op->opcode = WINED3D_CS_OP_CLEAR;
592 op->flags = flags;
593 if (flags & WINED3DCLEAR_TARGET)
595 op->rt_count = 1;
596 op->fb->render_targets[0] = view;
597 op->fb->depth_stencil = NULL;
598 op->color = *color;
600 else
602 op->rt_count = 0;
603 op->fb->render_targets[0] = NULL;
604 op->fb->depth_stencil = view;
605 op->depth = depth;
606 op->stencil = stencil;
608 SetRect(&op->draw_rect, 0, 0, view->width, view->height);
609 op->rect_count = 1;
610 op->rects[0] = *rect;
612 wined3d_resource_acquire(view->resource);
614 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
617 static void acquire_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
619 struct wined3d_shader_sampler_map_entry *entry;
620 struct wined3d_shader_resource_view *view;
621 struct wined3d_shader *shader;
622 unsigned int i, j;
624 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
626 if (!(shader_mask & (1u << i)))
627 continue;
629 if (!(shader = state->shader[i]))
630 continue;
632 for (j = 0; j < WINED3D_MAX_CBS; ++j)
634 if (state->cb[i][j])
635 wined3d_resource_acquire(&state->cb[i][j]->resource);
638 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
640 entry = &shader->reg_maps.sampler_map.entries[j];
642 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
643 continue;
645 wined3d_resource_acquire(view->resource);
650 static void release_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
652 struct wined3d_shader_sampler_map_entry *entry;
653 struct wined3d_shader_resource_view *view;
654 struct wined3d_shader *shader;
655 unsigned int i, j;
657 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
659 if (!(shader_mask & (1u << i)))
660 continue;
662 if (!(shader = state->shader[i]))
663 continue;
665 for (j = 0; j < WINED3D_MAX_CBS; ++j)
667 if (state->cb[i][j])
668 wined3d_resource_release(&state->cb[i][j]->resource);
671 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
673 entry = &shader->reg_maps.sampler_map.entries[j];
675 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
676 continue;
678 wined3d_resource_release(view->resource);
683 static void acquire_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_acquire(views[i]->resource);
703 static void release_unordered_access_resources(const struct wined3d_shader *shader,
704 struct wined3d_unordered_access_view * const *views)
706 unsigned int i;
708 if (!shader)
709 return;
711 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
713 if (!shader->reg_maps.uav_resource_info[i].type)
714 continue;
716 if (!views[i])
717 continue;
719 wined3d_resource_release(views[i]->resource);
723 static void wined3d_cs_exec_dispatch(struct wined3d_cs *cs, const void *data)
725 const struct wined3d_cs_dispatch *op = data;
726 struct wined3d_state *state = &cs->state;
728 dispatch_compute(cs->device, state, &op->parameters);
730 if (op->parameters.indirect)
731 wined3d_resource_release(&op->parameters.u.indirect.buffer->resource);
733 release_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
734 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
735 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
738 static void acquire_compute_pipeline_resources(const struct wined3d_state *state)
740 acquire_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
741 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
742 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
745 void wined3d_cs_emit_dispatch(struct wined3d_cs *cs,
746 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
748 const struct wined3d_state *state = &cs->device->state;
749 struct wined3d_cs_dispatch *op;
751 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
752 op->opcode = WINED3D_CS_OP_DISPATCH;
753 op->parameters.indirect = FALSE;
754 op->parameters.u.direct.group_count_x = group_count_x;
755 op->parameters.u.direct.group_count_y = group_count_y;
756 op->parameters.u.direct.group_count_z = group_count_z;
758 acquire_compute_pipeline_resources(state);
760 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
763 void wined3d_cs_emit_dispatch_indirect(struct wined3d_cs *cs,
764 struct wined3d_buffer *buffer, unsigned int offset)
766 const struct wined3d_state *state = &cs->device->state;
767 struct wined3d_cs_dispatch *op;
769 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
770 op->opcode = WINED3D_CS_OP_DISPATCH;
771 op->parameters.indirect = TRUE;
772 op->parameters.u.indirect.buffer = buffer;
773 op->parameters.u.indirect.offset = offset;
775 acquire_compute_pipeline_resources(state);
776 wined3d_resource_acquire(&buffer->resource);
778 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
781 static void wined3d_cs_exec_draw(struct wined3d_cs *cs, const void *data)
783 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
784 const struct wined3d_shader *geometry_shader;
785 struct wined3d_state *state = &cs->state;
786 const struct wined3d_cs_draw *op = data;
787 int load_base_vertex_idx;
788 unsigned int i;
790 /* ARB_draw_indirect always supports a base vertex offset. */
791 if (!op->parameters.indirect && !gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
792 load_base_vertex_idx = op->parameters.u.direct.base_vertex_idx;
793 else
794 load_base_vertex_idx = 0;
796 if (state->load_base_vertex_index != load_base_vertex_idx)
798 state->load_base_vertex_index = load_base_vertex_idx;
799 device_invalidate_state(cs->device, STATE_BASEVERTEXINDEX);
802 if (state->gl_primitive_type != op->primitive_type)
804 if ((geometry_shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]) && !geometry_shader->function)
805 device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY));
806 if (state->gl_primitive_type == GL_POINTS || op->primitive_type == GL_POINTS)
807 device_invalidate_state(cs->device, STATE_POINT_ENABLE);
808 state->gl_primitive_type = op->primitive_type;
810 state->gl_patch_vertices = op->patch_vertex_count;
812 draw_primitive(cs->device, state, &op->parameters);
814 if (op->parameters.indirect)
816 struct wined3d_buffer *buffer = op->parameters.u.indirect.buffer;
817 wined3d_resource_release(&buffer->resource);
820 if (op->parameters.indexed)
821 wined3d_resource_release(&state->index_buffer->resource);
822 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
824 if (state->streams[i].buffer)
825 wined3d_resource_release(&state->streams[i].buffer->resource);
827 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
829 if (state->stream_output[i].buffer)
830 wined3d_resource_release(&state->stream_output[i].buffer->resource);
832 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
834 if (state->textures[i])
835 wined3d_resource_release(&state->textures[i]->resource);
837 for (i = 0; i < gl_info->limits.buffers; ++i)
839 if (state->fb->render_targets[i])
840 wined3d_resource_release(state->fb->render_targets[i]->resource);
842 if (state->fb->depth_stencil)
843 wined3d_resource_release(state->fb->depth_stencil->resource);
844 release_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
845 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
846 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
849 static void acquire_graphics_pipeline_resources(const struct wined3d_state *state,
850 BOOL indexed, const struct wined3d_gl_info *gl_info)
852 unsigned int i;
854 if (indexed)
855 wined3d_resource_acquire(&state->index_buffer->resource);
856 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
858 if (state->streams[i].buffer)
859 wined3d_resource_acquire(&state->streams[i].buffer->resource);
861 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
863 if (state->stream_output[i].buffer)
864 wined3d_resource_acquire(&state->stream_output[i].buffer->resource);
866 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
868 if (state->textures[i])
869 wined3d_resource_acquire(&state->textures[i]->resource);
871 for (i = 0; i < gl_info->limits.buffers; ++i)
873 if (state->fb->render_targets[i])
874 wined3d_resource_acquire(state->fb->render_targets[i]->resource);
876 if (state->fb->depth_stencil)
877 wined3d_resource_acquire(state->fb->depth_stencil->resource);
878 acquire_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
879 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
880 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
883 void wined3d_cs_emit_draw(struct wined3d_cs *cs, GLenum primitive_type, unsigned int patch_vertex_count,
884 int base_vertex_idx, unsigned int start_idx, unsigned int index_count,
885 unsigned int start_instance, unsigned int instance_count, BOOL indexed)
887 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
888 const struct wined3d_state *state = &cs->device->state;
889 struct wined3d_cs_draw *op;
891 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
892 op->opcode = WINED3D_CS_OP_DRAW;
893 op->primitive_type = primitive_type;
894 op->patch_vertex_count = patch_vertex_count;
895 op->parameters.indirect = FALSE;
896 op->parameters.u.direct.base_vertex_idx = base_vertex_idx;
897 op->parameters.u.direct.start_idx = start_idx;
898 op->parameters.u.direct.index_count = index_count;
899 op->parameters.u.direct.start_instance = start_instance;
900 op->parameters.u.direct.instance_count = instance_count;
901 op->parameters.indexed = indexed;
903 acquire_graphics_pipeline_resources(state, indexed, gl_info);
905 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
908 void wined3d_cs_emit_draw_indirect(struct wined3d_cs *cs, GLenum primitive_type, unsigned int patch_vertex_count,
909 struct wined3d_buffer *buffer, unsigned int offset, BOOL indexed)
911 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
912 const struct wined3d_state *state = &cs->device->state;
913 struct wined3d_cs_draw *op;
915 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
916 op->opcode = WINED3D_CS_OP_DRAW;
917 op->primitive_type = primitive_type;
918 op->patch_vertex_count = patch_vertex_count;
919 op->parameters.indirect = TRUE;
920 op->parameters.u.indirect.buffer = buffer;
921 op->parameters.u.indirect.offset = offset;
922 op->parameters.indexed = indexed;
924 acquire_graphics_pipeline_resources(state, indexed, gl_info);
925 wined3d_resource_acquire(&buffer->resource);
927 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
930 static void wined3d_cs_exec_flush(struct wined3d_cs *cs, const void *data)
932 struct wined3d_context *context;
934 context = context_acquire(cs->device, NULL, 0);
935 if (context->valid)
936 context->gl_info->gl_ops.gl.p_glFlush();
937 context_release(context);
940 void wined3d_cs_emit_flush(struct wined3d_cs *cs)
942 struct wined3d_cs_flush *op;
944 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
945 op->opcode = WINED3D_CS_OP_FLUSH;
947 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
948 cs->queries_flushed = TRUE;
951 static void wined3d_cs_exec_set_predication(struct wined3d_cs *cs, const void *data)
953 const struct wined3d_cs_set_predication *op = data;
955 cs->state.predicate = op->predicate;
956 cs->state.predicate_value = op->value;
959 void wined3d_cs_emit_set_predication(struct wined3d_cs *cs, struct wined3d_query *predicate, BOOL value)
961 struct wined3d_cs_set_predication *op;
963 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
964 op->opcode = WINED3D_CS_OP_SET_PREDICATION;
965 op->predicate = predicate;
966 op->value = value;
968 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
971 static void wined3d_cs_exec_set_viewports(struct wined3d_cs *cs, const void *data)
973 const struct wined3d_cs_set_viewports *op = data;
975 if (op->viewport_count)
976 memcpy(cs->state.viewports, op->viewports, op->viewport_count * sizeof(*op->viewports));
977 else
978 memset(cs->state.viewports, 0, sizeof(*cs->state.viewports));
979 cs->state.viewport_count = op->viewport_count;
980 device_invalidate_state(cs->device, STATE_VIEWPORT);
983 void wined3d_cs_emit_set_viewports(struct wined3d_cs *cs, unsigned int viewport_count,
984 const struct wined3d_viewport *viewports)
986 struct wined3d_cs_set_viewports *op;
988 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_set_viewports, viewports[viewport_count]),
989 WINED3D_CS_QUEUE_DEFAULT);
990 op->opcode = WINED3D_CS_OP_SET_VIEWPORTS;
991 memcpy(op->viewports, viewports, viewport_count * sizeof(*viewports));
992 op->viewport_count = viewport_count;
994 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
997 static void wined3d_cs_exec_set_scissor_rects(struct wined3d_cs *cs, const void *data)
999 const struct wined3d_cs_set_scissor_rects *op = data;
1001 if (op->rect_count)
1002 memcpy(cs->state.scissor_rects, op->rects, op->rect_count * sizeof(*op->rects));
1003 else
1004 SetRectEmpty(cs->state.scissor_rects);
1005 cs->state.scissor_rect_count = op->rect_count;
1006 device_invalidate_state(cs->device, STATE_SCISSORRECT);
1009 void wined3d_cs_emit_set_scissor_rects(struct wined3d_cs *cs, unsigned int rect_count, const RECT *rects)
1011 struct wined3d_cs_set_scissor_rects *op;
1013 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_set_scissor_rects, rects[rect_count]),
1014 WINED3D_CS_QUEUE_DEFAULT);
1015 op->opcode = WINED3D_CS_OP_SET_SCISSOR_RECTS;
1016 memcpy(op->rects, rects, rect_count * sizeof(*rects));
1017 op->rect_count = rect_count;
1019 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1022 static void wined3d_cs_exec_set_rendertarget_view(struct wined3d_cs *cs, const void *data)
1024 const struct wined3d_cs_set_rendertarget_view *op = data;
1026 cs->fb.render_targets[op->view_idx] = op->view;
1027 device_invalidate_state(cs->device, STATE_FRAMEBUFFER);
1030 void wined3d_cs_emit_set_rendertarget_view(struct wined3d_cs *cs, unsigned int view_idx,
1031 struct wined3d_rendertarget_view *view)
1033 struct wined3d_cs_set_rendertarget_view *op;
1035 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1036 op->opcode = WINED3D_CS_OP_SET_RENDERTARGET_VIEW;
1037 op->view_idx = view_idx;
1038 op->view = view;
1040 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1043 static void wined3d_cs_exec_set_depth_stencil_view(struct wined3d_cs *cs, const void *data)
1045 const struct wined3d_cs_set_depth_stencil_view *op = data;
1046 struct wined3d_device *device = cs->device;
1047 struct wined3d_rendertarget_view *prev;
1049 if ((prev = cs->state.fb->depth_stencil) && prev->resource->type != WINED3D_RTYPE_BUFFER)
1051 struct wined3d_texture *prev_texture = texture_from_resource(prev->resource);
1053 if (device->swapchains[0]->desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
1054 || prev_texture->flags & WINED3D_TEXTURE_DISCARD)
1055 wined3d_texture_validate_location(prev_texture,
1056 prev->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
1059 cs->fb.depth_stencil = op->view;
1061 if (!prev != !op->view)
1063 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
1064 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
1065 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
1066 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
1067 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
1069 else if (prev && prev->format->depth_bias_scale != op->view->format->depth_bias_scale)
1071 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
1074 device_invalidate_state(device, STATE_FRAMEBUFFER);
1077 void wined3d_cs_emit_set_depth_stencil_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view)
1079 struct wined3d_cs_set_depth_stencil_view *op;
1081 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1082 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW;
1083 op->view = view;
1085 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1088 static void wined3d_cs_exec_set_vertex_declaration(struct wined3d_cs *cs, const void *data)
1090 const struct wined3d_cs_set_vertex_declaration *op = data;
1092 cs->state.vertex_declaration = op->declaration;
1093 device_invalidate_state(cs->device, STATE_VDECL);
1096 void wined3d_cs_emit_set_vertex_declaration(struct wined3d_cs *cs, struct wined3d_vertex_declaration *declaration)
1098 struct wined3d_cs_set_vertex_declaration *op;
1100 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1101 op->opcode = WINED3D_CS_OP_SET_VERTEX_DECLARATION;
1102 op->declaration = declaration;
1104 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1107 static void wined3d_cs_exec_set_stream_source(struct wined3d_cs *cs, const void *data)
1109 const struct wined3d_cs_set_stream_source *op = data;
1110 struct wined3d_stream_state *stream;
1111 struct wined3d_buffer *prev;
1113 stream = &cs->state.streams[op->stream_idx];
1114 prev = stream->buffer;
1115 stream->buffer = op->buffer;
1116 stream->offset = op->offset;
1117 stream->stride = op->stride;
1119 if (op->buffer)
1120 InterlockedIncrement(&op->buffer->resource.bind_count);
1121 if (prev)
1122 InterlockedDecrement(&prev->resource.bind_count);
1124 device_invalidate_state(cs->device, STATE_STREAMSRC);
1127 void wined3d_cs_emit_set_stream_source(struct wined3d_cs *cs, UINT stream_idx,
1128 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1130 struct wined3d_cs_set_stream_source *op;
1132 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1133 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE;
1134 op->stream_idx = stream_idx;
1135 op->buffer = buffer;
1136 op->offset = offset;
1137 op->stride = stride;
1139 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1142 static void wined3d_cs_exec_set_stream_source_freq(struct wined3d_cs *cs, const void *data)
1144 const struct wined3d_cs_set_stream_source_freq *op = data;
1145 struct wined3d_stream_state *stream;
1147 stream = &cs->state.streams[op->stream_idx];
1148 stream->frequency = op->frequency;
1149 stream->flags = op->flags;
1151 device_invalidate_state(cs->device, STATE_STREAMSRC);
1154 void wined3d_cs_emit_set_stream_source_freq(struct wined3d_cs *cs, UINT stream_idx, UINT frequency, UINT flags)
1156 struct wined3d_cs_set_stream_source_freq *op;
1158 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1159 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ;
1160 op->stream_idx = stream_idx;
1161 op->frequency = frequency;
1162 op->flags = flags;
1164 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1167 static void wined3d_cs_exec_set_stream_output(struct wined3d_cs *cs, const void *data)
1169 const struct wined3d_cs_set_stream_output *op = data;
1170 struct wined3d_stream_output *stream;
1171 struct wined3d_buffer *prev;
1173 stream = &cs->state.stream_output[op->stream_idx];
1174 prev = stream->buffer;
1175 stream->buffer = op->buffer;
1176 stream->offset = op->offset;
1178 if (op->buffer)
1179 InterlockedIncrement(&op->buffer->resource.bind_count);
1180 if (prev)
1181 InterlockedDecrement(&prev->resource.bind_count);
1183 device_invalidate_state(cs->device, STATE_STREAM_OUTPUT);
1186 void wined3d_cs_emit_set_stream_output(struct wined3d_cs *cs, UINT stream_idx,
1187 struct wined3d_buffer *buffer, UINT offset)
1189 struct wined3d_cs_set_stream_output *op;
1191 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1192 op->opcode = WINED3D_CS_OP_SET_STREAM_OUTPUT;
1193 op->stream_idx = stream_idx;
1194 op->buffer = buffer;
1195 op->offset = offset;
1197 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1200 static void wined3d_cs_exec_set_index_buffer(struct wined3d_cs *cs, const void *data)
1202 const struct wined3d_cs_set_index_buffer *op = data;
1203 struct wined3d_buffer *prev;
1205 prev = cs->state.index_buffer;
1206 cs->state.index_buffer = op->buffer;
1207 cs->state.index_format = op->format_id;
1208 cs->state.index_offset = op->offset;
1210 if (op->buffer)
1211 InterlockedIncrement(&op->buffer->resource.bind_count);
1212 if (prev)
1213 InterlockedDecrement(&prev->resource.bind_count);
1215 device_invalidate_state(cs->device, STATE_INDEXBUFFER);
1218 void wined3d_cs_emit_set_index_buffer(struct wined3d_cs *cs, struct wined3d_buffer *buffer,
1219 enum wined3d_format_id format_id, unsigned int offset)
1221 struct wined3d_cs_set_index_buffer *op;
1223 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1224 op->opcode = WINED3D_CS_OP_SET_INDEX_BUFFER;
1225 op->buffer = buffer;
1226 op->format_id = format_id;
1227 op->offset = offset;
1229 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1232 static void wined3d_cs_exec_set_constant_buffer(struct wined3d_cs *cs, const void *data)
1234 const struct wined3d_cs_set_constant_buffer *op = data;
1235 struct wined3d_buffer *prev;
1237 prev = cs->state.cb[op->type][op->cb_idx];
1238 cs->state.cb[op->type][op->cb_idx] = op->buffer;
1240 if (op->buffer)
1241 InterlockedIncrement(&op->buffer->resource.bind_count);
1242 if (prev)
1243 InterlockedDecrement(&prev->resource.bind_count);
1245 device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(op->type));
1248 void wined3d_cs_emit_set_constant_buffer(struct wined3d_cs *cs, enum wined3d_shader_type type,
1249 UINT cb_idx, struct wined3d_buffer *buffer)
1251 struct wined3d_cs_set_constant_buffer *op;
1253 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1254 op->opcode = WINED3D_CS_OP_SET_CONSTANT_BUFFER;
1255 op->type = type;
1256 op->cb_idx = cb_idx;
1257 op->buffer = buffer;
1259 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1262 static void wined3d_cs_exec_set_texture(struct wined3d_cs *cs, const void *data)
1264 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
1265 const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
1266 const struct wined3d_cs_set_texture *op = data;
1267 struct wined3d_texture *prev;
1268 BOOL old_use_color_key = FALSE, new_use_color_key = FALSE;
1270 prev = cs->state.textures[op->stage];
1271 cs->state.textures[op->stage] = op->texture;
1273 if (op->texture)
1275 const struct wined3d_format *new_format = op->texture->resource.format;
1276 const struct wined3d_format *old_format = prev ? prev->resource.format : NULL;
1277 unsigned int old_fmt_flags = prev ? prev->resource.format_flags : 0;
1278 unsigned int new_fmt_flags = op->texture->resource.format_flags;
1280 if (InterlockedIncrement(&op->texture->resource.bind_count) == 1)
1281 op->texture->sampler = op->stage;
1283 if (!prev || op->texture->target != prev->target
1284 || (!is_same_fixup(new_format->color_fixup, old_format->color_fixup)
1285 && !(can_use_texture_swizzle(gl_info, new_format) && can_use_texture_swizzle(gl_info, old_format)))
1286 || (new_fmt_flags & WINED3DFMT_FLAG_SHADOW) != (old_fmt_flags & WINED3DFMT_FLAG_SHADOW))
1287 device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1289 if (!prev && op->stage < d3d_info->limits.ffp_blend_stages)
1291 /* The source arguments for color and alpha ops have different
1292 * meanings when a NULL texture is bound, so the COLOR_OP and
1293 * ALPHA_OP have to be dirtified. */
1294 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1295 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1298 if (!op->stage && op->texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1299 new_use_color_key = TRUE;
1302 if (prev)
1304 if (InterlockedDecrement(&prev->resource.bind_count) && prev->sampler == op->stage)
1306 unsigned int i;
1308 /* Search for other stages the texture is bound to. Shouldn't
1309 * happen if applications bind textures to a single stage only. */
1310 TRACE("Searching for other stages the texture is bound to.\n");
1311 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
1313 if (cs->state.textures[i] == prev)
1315 TRACE("Texture is also bound to stage %u.\n", i);
1316 prev->sampler = i;
1317 break;
1322 if (!op->texture && op->stage < d3d_info->limits.ffp_blend_stages)
1324 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1325 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1328 if (!op->stage && prev->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1329 old_use_color_key = TRUE;
1332 device_invalidate_state(cs->device, STATE_SAMPLER(op->stage));
1334 if (new_use_color_key != old_use_color_key)
1335 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1337 if (new_use_color_key)
1338 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1341 void wined3d_cs_emit_set_texture(struct wined3d_cs *cs, UINT stage, struct wined3d_texture *texture)
1343 struct wined3d_cs_set_texture *op;
1345 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1346 op->opcode = WINED3D_CS_OP_SET_TEXTURE;
1347 op->stage = stage;
1348 op->texture = texture;
1350 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1353 static void wined3d_cs_exec_set_shader_resource_view(struct wined3d_cs *cs, const void *data)
1355 const struct wined3d_cs_set_shader_resource_view *op = data;
1356 struct wined3d_shader_resource_view *prev;
1358 prev = cs->state.shader_resource_view[op->type][op->view_idx];
1359 cs->state.shader_resource_view[op->type][op->view_idx] = op->view;
1361 if (op->view)
1362 InterlockedIncrement(&op->view->resource->bind_count);
1363 if (prev)
1364 InterlockedDecrement(&prev->resource->bind_count);
1366 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1367 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1368 else
1369 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1372 void wined3d_cs_emit_set_shader_resource_view(struct wined3d_cs *cs, enum wined3d_shader_type type,
1373 UINT view_idx, struct wined3d_shader_resource_view *view)
1375 struct wined3d_cs_set_shader_resource_view *op;
1377 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1378 op->opcode = WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW;
1379 op->type = type;
1380 op->view_idx = view_idx;
1381 op->view = view;
1383 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1386 static void wined3d_cs_exec_set_unordered_access_view(struct wined3d_cs *cs, const void *data)
1388 const struct wined3d_cs_set_unordered_access_view *op = data;
1389 struct wined3d_unordered_access_view *prev;
1391 prev = cs->state.unordered_access_view[op->pipeline][op->view_idx];
1392 cs->state.unordered_access_view[op->pipeline][op->view_idx] = op->view;
1394 if (op->view)
1395 InterlockedIncrement(&op->view->resource->bind_count);
1396 if (prev)
1397 InterlockedDecrement(&prev->resource->bind_count);
1399 if (op->view && op->initial_count != ~0u)
1400 wined3d_unordered_access_view_set_counter(op->view, op->initial_count);
1402 device_invalidate_state(cs->device, STATE_UNORDERED_ACCESS_VIEW_BINDING(op->pipeline));
1405 void wined3d_cs_emit_set_unordered_access_view(struct wined3d_cs *cs, enum wined3d_pipeline pipeline,
1406 unsigned int view_idx, struct wined3d_unordered_access_view *view, unsigned int initial_count)
1408 struct wined3d_cs_set_unordered_access_view *op;
1410 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1411 op->opcode = WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW;
1412 op->pipeline = pipeline;
1413 op->view_idx = view_idx;
1414 op->view = view;
1415 op->initial_count = initial_count;
1417 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1420 static void wined3d_cs_exec_set_sampler(struct wined3d_cs *cs, const void *data)
1422 const struct wined3d_cs_set_sampler *op = data;
1424 cs->state.sampler[op->type][op->sampler_idx] = op->sampler;
1425 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1426 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1427 else
1428 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1431 void wined3d_cs_emit_set_sampler(struct wined3d_cs *cs, enum wined3d_shader_type type,
1432 UINT sampler_idx, struct wined3d_sampler *sampler)
1434 struct wined3d_cs_set_sampler *op;
1436 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1437 op->opcode = WINED3D_CS_OP_SET_SAMPLER;
1438 op->type = type;
1439 op->sampler_idx = sampler_idx;
1440 op->sampler = sampler;
1442 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1445 static void wined3d_cs_exec_set_shader(struct wined3d_cs *cs, const void *data)
1447 const struct wined3d_cs_set_shader *op = data;
1449 cs->state.shader[op->type] = op->shader;
1450 device_invalidate_state(cs->device, STATE_SHADER(op->type));
1451 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1452 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1453 else
1454 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1457 void wined3d_cs_emit_set_shader(struct wined3d_cs *cs, enum wined3d_shader_type type, struct wined3d_shader *shader)
1459 struct wined3d_cs_set_shader *op;
1461 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1462 op->opcode = WINED3D_CS_OP_SET_SHADER;
1463 op->type = type;
1464 op->shader = shader;
1466 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1469 static void wined3d_cs_exec_set_blend_state(struct wined3d_cs *cs, const void *data)
1471 const struct wined3d_cs_set_blend_state *op = data;
1473 cs->state.blend_state = op->state;
1474 device_invalidate_state(cs->device, STATE_BLEND);
1477 void wined3d_cs_emit_set_blend_state(struct wined3d_cs *cs, struct wined3d_blend_state *state)
1479 struct wined3d_cs_set_blend_state *op;
1481 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1482 op->opcode = WINED3D_CS_OP_SET_BLEND_STATE;
1483 op->state = state;
1485 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1488 static void wined3d_cs_exec_set_rasterizer_state(struct wined3d_cs *cs, const void *data)
1490 const struct wined3d_cs_set_rasterizer_state *op = data;
1492 cs->state.rasterizer_state = op->state;
1493 device_invalidate_state(cs->device, STATE_RASTERIZER);
1496 void wined3d_cs_emit_set_rasterizer_state(struct wined3d_cs *cs,
1497 struct wined3d_rasterizer_state *rasterizer_state)
1499 struct wined3d_cs_set_rasterizer_state *op;
1501 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1502 op->opcode = WINED3D_CS_OP_SET_RASTERIZER_STATE;
1503 op->state = rasterizer_state;
1505 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1508 static void wined3d_cs_exec_set_render_state(struct wined3d_cs *cs, const void *data)
1510 const struct wined3d_cs_set_render_state *op = data;
1512 cs->state.render_states[op->state] = op->value;
1513 device_invalidate_state(cs->device, STATE_RENDER(op->state));
1516 void wined3d_cs_emit_set_render_state(struct wined3d_cs *cs, enum wined3d_render_state state, DWORD value)
1518 struct wined3d_cs_set_render_state *op;
1520 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1521 op->opcode = WINED3D_CS_OP_SET_RENDER_STATE;
1522 op->state = state;
1523 op->value = value;
1525 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1528 static void wined3d_cs_exec_set_texture_state(struct wined3d_cs *cs, const void *data)
1530 const struct wined3d_cs_set_texture_state *op = data;
1532 cs->state.texture_states[op->stage][op->state] = op->value;
1533 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, op->state));
1536 void wined3d_cs_emit_set_texture_state(struct wined3d_cs *cs, UINT stage,
1537 enum wined3d_texture_stage_state state, DWORD value)
1539 struct wined3d_cs_set_texture_state *op;
1541 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1542 op->opcode = WINED3D_CS_OP_SET_TEXTURE_STATE;
1543 op->stage = stage;
1544 op->state = state;
1545 op->value = value;
1547 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1550 static void wined3d_cs_exec_set_sampler_state(struct wined3d_cs *cs, const void *data)
1552 const struct wined3d_cs_set_sampler_state *op = data;
1554 cs->state.sampler_states[op->sampler_idx][op->state] = op->value;
1555 device_invalidate_state(cs->device, STATE_SAMPLER(op->sampler_idx));
1558 void wined3d_cs_emit_set_sampler_state(struct wined3d_cs *cs, UINT sampler_idx,
1559 enum wined3d_sampler_state state, DWORD value)
1561 struct wined3d_cs_set_sampler_state *op;
1563 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1564 op->opcode = WINED3D_CS_OP_SET_SAMPLER_STATE;
1565 op->sampler_idx = sampler_idx;
1566 op->state = state;
1567 op->value = value;
1569 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1572 static void wined3d_cs_exec_set_transform(struct wined3d_cs *cs, const void *data)
1574 const struct wined3d_cs_set_transform *op = data;
1576 cs->state.transforms[op->state] = op->matrix;
1577 if (op->state < WINED3D_TS_WORLD_MATRIX(cs->device->adapter->d3d_info.limits.ffp_vertex_blend_matrices))
1578 device_invalidate_state(cs->device, STATE_TRANSFORM(op->state));
1581 void wined3d_cs_emit_set_transform(struct wined3d_cs *cs, enum wined3d_transform_state state,
1582 const struct wined3d_matrix *matrix)
1584 struct wined3d_cs_set_transform *op;
1586 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1587 op->opcode = WINED3D_CS_OP_SET_TRANSFORM;
1588 op->state = state;
1589 op->matrix = *matrix;
1591 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1594 static void wined3d_cs_exec_set_clip_plane(struct wined3d_cs *cs, const void *data)
1596 const struct wined3d_cs_set_clip_plane *op = data;
1598 cs->state.clip_planes[op->plane_idx] = op->plane;
1599 device_invalidate_state(cs->device, STATE_CLIPPLANE(op->plane_idx));
1602 void wined3d_cs_emit_set_clip_plane(struct wined3d_cs *cs, UINT plane_idx, const struct wined3d_vec4 *plane)
1604 struct wined3d_cs_set_clip_plane *op;
1606 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1607 op->opcode = WINED3D_CS_OP_SET_CLIP_PLANE;
1608 op->plane_idx = plane_idx;
1609 op->plane = *plane;
1611 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1614 static void wined3d_cs_exec_set_color_key(struct wined3d_cs *cs, const void *data)
1616 const struct wined3d_cs_set_color_key *op = data;
1617 struct wined3d_texture *texture = op->texture;
1619 if (op->set)
1621 switch (op->flags)
1623 case WINED3D_CKEY_DST_BLT:
1624 texture->async.dst_blt_color_key = op->color_key;
1625 texture->async.color_key_flags |= WINED3D_CKEY_DST_BLT;
1626 break;
1628 case WINED3D_CKEY_DST_OVERLAY:
1629 texture->async.dst_overlay_color_key = op->color_key;
1630 texture->async.color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
1631 break;
1633 case WINED3D_CKEY_SRC_BLT:
1634 if (texture == cs->state.textures[0])
1636 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1637 if (!(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1638 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1641 texture->async.src_blt_color_key = op->color_key;
1642 texture->async.color_key_flags |= WINED3D_CKEY_SRC_BLT;
1643 break;
1645 case WINED3D_CKEY_SRC_OVERLAY:
1646 texture->async.src_overlay_color_key = op->color_key;
1647 texture->async.color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
1648 break;
1651 else
1653 switch (op->flags)
1655 case WINED3D_CKEY_DST_BLT:
1656 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_BLT;
1657 break;
1659 case WINED3D_CKEY_DST_OVERLAY:
1660 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
1661 break;
1663 case WINED3D_CKEY_SRC_BLT:
1664 if (texture == cs->state.textures[0] && texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1665 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1667 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
1668 break;
1670 case WINED3D_CKEY_SRC_OVERLAY:
1671 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
1672 break;
1677 void wined3d_cs_emit_set_color_key(struct wined3d_cs *cs, struct wined3d_texture *texture,
1678 WORD flags, const struct wined3d_color_key *color_key)
1680 struct wined3d_cs_set_color_key *op;
1682 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1683 op->opcode = WINED3D_CS_OP_SET_COLOR_KEY;
1684 op->texture = texture;
1685 op->flags = flags;
1686 if (color_key)
1688 op->color_key = *color_key;
1689 op->set = 1;
1691 else
1692 op->set = 0;
1694 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1697 static void wined3d_cs_exec_set_material(struct wined3d_cs *cs, const void *data)
1699 const struct wined3d_cs_set_material *op = data;
1701 cs->state.material = op->material;
1702 device_invalidate_state(cs->device, STATE_MATERIAL);
1705 void wined3d_cs_emit_set_material(struct wined3d_cs *cs, const struct wined3d_material *material)
1707 struct wined3d_cs_set_material *op;
1709 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1710 op->opcode = WINED3D_CS_OP_SET_MATERIAL;
1711 op->material = *material;
1713 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1716 static void wined3d_cs_exec_set_light(struct wined3d_cs *cs, const void *data)
1718 const struct wined3d_cs_set_light *op = data;
1719 struct wined3d_light_info *light_info;
1720 unsigned int light_idx, hash_idx;
1722 light_idx = op->light.OriginalIndex;
1724 if (!(light_info = wined3d_state_get_light(&cs->state, light_idx)))
1726 TRACE("Adding new light.\n");
1727 if (!(light_info = heap_alloc_zero(sizeof(*light_info))))
1729 ERR("Failed to allocate light info.\n");
1730 return;
1733 hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1734 list_add_head(&cs->state.light_map[hash_idx], &light_info->entry);
1735 light_info->glIndex = -1;
1736 light_info->OriginalIndex = light_idx;
1739 if (light_info->glIndex != -1)
1741 if (light_info->OriginalParms.type != op->light.OriginalParms.type)
1742 device_invalidate_state(cs->device, STATE_LIGHT_TYPE);
1743 device_invalidate_state(cs->device, STATE_ACTIVELIGHT(light_info->glIndex));
1746 light_info->OriginalParms = op->light.OriginalParms;
1747 light_info->position = op->light.position;
1748 light_info->direction = op->light.direction;
1749 light_info->exponent = op->light.exponent;
1750 light_info->cutoff = op->light.cutoff;
1753 void wined3d_cs_emit_set_light(struct wined3d_cs *cs, const struct wined3d_light_info *light)
1755 struct wined3d_cs_set_light *op;
1757 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1758 op->opcode = WINED3D_CS_OP_SET_LIGHT;
1759 op->light = *light;
1761 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1764 static void wined3d_cs_exec_set_light_enable(struct wined3d_cs *cs, const void *data)
1766 const struct wined3d_cs_set_light_enable *op = data;
1767 struct wined3d_device *device = cs->device;
1768 struct wined3d_light_info *light_info;
1769 int prev_idx;
1771 if (!(light_info = wined3d_state_get_light(&cs->state, op->idx)))
1773 ERR("Light doesn't exist.\n");
1774 return;
1777 prev_idx = light_info->glIndex;
1778 wined3d_state_enable_light(&cs->state, &device->adapter->d3d_info, light_info, op->enable);
1779 if (light_info->glIndex != prev_idx)
1781 device_invalidate_state(device, STATE_LIGHT_TYPE);
1782 device_invalidate_state(device, STATE_ACTIVELIGHT(op->enable ? light_info->glIndex : prev_idx));
1786 void wined3d_cs_emit_set_light_enable(struct wined3d_cs *cs, unsigned int idx, BOOL enable)
1788 struct wined3d_cs_set_light_enable *op;
1790 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1791 op->opcode = WINED3D_CS_OP_SET_LIGHT_ENABLE;
1792 op->idx = idx;
1793 op->enable = enable;
1795 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1798 static const struct
1800 size_t offset;
1801 size_t size;
1802 DWORD mask;
1804 wined3d_cs_push_constant_info[] =
1806 /* WINED3D_PUSH_CONSTANTS_VS_F */
1807 {FIELD_OFFSET(struct wined3d_state, vs_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_VS_F},
1808 /* WINED3D_PUSH_CONSTANTS_PS_F */
1809 {FIELD_OFFSET(struct wined3d_state, ps_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_PS_F},
1810 /* WINED3D_PUSH_CONSTANTS_VS_I */
1811 {FIELD_OFFSET(struct wined3d_state, vs_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_VS_I},
1812 /* WINED3D_PUSH_CONSTANTS_PS_I */
1813 {FIELD_OFFSET(struct wined3d_state, ps_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_PS_I},
1814 /* WINED3D_PUSH_CONSTANTS_VS_B */
1815 {FIELD_OFFSET(struct wined3d_state, vs_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_VS_B},
1816 /* WINED3D_PUSH_CONSTANTS_PS_B */
1817 {FIELD_OFFSET(struct wined3d_state, ps_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_PS_B},
1820 static void wined3d_cs_st_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1821 unsigned int start_idx, unsigned int count, const void *constants)
1823 struct wined3d_device *device = cs->device;
1824 unsigned int context_count;
1825 unsigned int i;
1826 size_t offset;
1828 if (p == WINED3D_PUSH_CONSTANTS_VS_F)
1829 device->shader_backend->shader_update_float_vertex_constants(device, start_idx, count);
1830 else if (p == WINED3D_PUSH_CONSTANTS_PS_F)
1831 device->shader_backend->shader_update_float_pixel_constants(device, start_idx, count);
1833 offset = wined3d_cs_push_constant_info[p].offset + start_idx * wined3d_cs_push_constant_info[p].size;
1834 memcpy((BYTE *)&cs->state + offset, constants, count * wined3d_cs_push_constant_info[p].size);
1835 for (i = 0, context_count = device->context_count; i < context_count; ++i)
1837 device->contexts[i]->constant_update_mask |= wined3d_cs_push_constant_info[p].mask;
1841 static void wined3d_cs_exec_push_constants(struct wined3d_cs *cs, const void *data)
1843 const struct wined3d_cs_push_constants *op = data;
1845 wined3d_cs_st_push_constants(cs, op->type, op->start_idx, op->count, op->constants);
1848 static void wined3d_cs_mt_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1849 unsigned int start_idx, unsigned int count, const void *constants)
1851 struct wined3d_cs_push_constants *op;
1852 size_t size;
1854 size = count * wined3d_cs_push_constant_info[p].size;
1855 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_push_constants, constants[size]),
1856 WINED3D_CS_QUEUE_DEFAULT);
1857 op->opcode = WINED3D_CS_OP_PUSH_CONSTANTS;
1858 op->type = p;
1859 op->start_idx = start_idx;
1860 op->count = count;
1861 memcpy(op->constants, constants, size);
1863 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1866 static void wined3d_cs_exec_reset_state(struct wined3d_cs *cs, const void *data)
1868 struct wined3d_adapter *adapter = cs->device->adapter;
1870 state_cleanup(&cs->state);
1871 memset(&cs->state, 0, sizeof(cs->state));
1872 state_init(&cs->state, &cs->fb, &adapter->gl_info, &adapter->d3d_info,
1873 WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
1876 void wined3d_cs_emit_reset_state(struct wined3d_cs *cs)
1878 struct wined3d_cs_reset_state *op;
1880 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1881 op->opcode = WINED3D_CS_OP_RESET_STATE;
1883 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1886 static void wined3d_cs_exec_callback(struct wined3d_cs *cs, const void *data)
1888 const struct wined3d_cs_callback *op = data;
1890 op->callback(op->object);
1893 static void wined3d_cs_emit_callback(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1895 struct wined3d_cs_callback *op;
1897 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1898 op->opcode = WINED3D_CS_OP_CALLBACK;
1899 op->callback = callback;
1900 op->object = object;
1902 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1905 void wined3d_cs_destroy_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1907 wined3d_cs_emit_callback(cs, callback, object);
1910 void wined3d_cs_init_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1912 wined3d_cs_emit_callback(cs, callback, object);
1915 static void wined3d_cs_exec_query_issue(struct wined3d_cs *cs, const void *data)
1917 const struct wined3d_cs_query_issue *op = data;
1918 struct wined3d_query *query = op->query;
1919 BOOL poll;
1921 poll = query->query_ops->query_issue(query, op->flags);
1923 if (!cs->thread)
1924 return;
1926 if (poll && list_empty(&query->poll_list_entry))
1928 list_add_tail(&cs->query_poll_list, &query->poll_list_entry);
1929 return;
1932 /* This can happen if occlusion queries are restarted. This discards the
1933 * old result, since polling it could result in a GL error. */
1934 if ((op->flags & WINED3DISSUE_BEGIN) && !poll && !list_empty(&query->poll_list_entry))
1936 list_remove(&query->poll_list_entry);
1937 list_init(&query->poll_list_entry);
1938 InterlockedIncrement(&query->counter_retrieved);
1939 return;
1942 /* This can happen when an occlusion query is ended without being started,
1943 * in which case we don't want to poll, but still have to counter-balance
1944 * the increment of the main counter.
1946 * This can also happen if an event query is re-issued before the first
1947 * fence was reached. In this case the query is already in the list and
1948 * the poll function will check the new fence. We have to counter-balance
1949 * the discarded increment. */
1950 if (op->flags & WINED3DISSUE_END)
1951 InterlockedIncrement(&query->counter_retrieved);
1954 void wined3d_cs_emit_query_issue(struct wined3d_cs *cs, struct wined3d_query *query, DWORD flags)
1956 struct wined3d_cs_query_issue *op;
1958 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1959 op->opcode = WINED3D_CS_OP_QUERY_ISSUE;
1960 op->query = query;
1961 op->flags = flags;
1963 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1964 cs->queries_flushed = FALSE;
1967 static void wined3d_cs_exec_preload_resource(struct wined3d_cs *cs, const void *data)
1969 const struct wined3d_cs_preload_resource *op = data;
1970 struct wined3d_resource *resource = op->resource;
1972 resource->resource_ops->resource_preload(resource);
1973 wined3d_resource_release(resource);
1976 void wined3d_cs_emit_preload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
1978 struct wined3d_cs_preload_resource *op;
1980 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1981 op->opcode = WINED3D_CS_OP_PRELOAD_RESOURCE;
1982 op->resource = resource;
1984 wined3d_resource_acquire(resource);
1986 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1989 static void wined3d_cs_exec_unload_resource(struct wined3d_cs *cs, const void *data)
1991 const struct wined3d_cs_unload_resource *op = data;
1992 struct wined3d_resource *resource = op->resource;
1994 resource->resource_ops->resource_unload(resource);
1995 wined3d_resource_release(resource);
1998 void wined3d_cs_emit_unload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
2000 struct wined3d_cs_unload_resource *op;
2002 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2003 op->opcode = WINED3D_CS_OP_UNLOAD_RESOURCE;
2004 op->resource = resource;
2006 wined3d_resource_acquire(resource);
2008 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2011 static void wined3d_cs_exec_map(struct wined3d_cs *cs, const void *data)
2013 const struct wined3d_cs_map *op = data;
2014 struct wined3d_resource *resource = op->resource;
2016 *op->hr = resource->resource_ops->resource_sub_resource_map(resource,
2017 op->sub_resource_idx, op->map_desc, op->box, op->flags);
2020 HRESULT wined3d_cs_map(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx,
2021 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
2023 struct wined3d_cs_map *op;
2024 HRESULT hr;
2026 /* Mapping resources from the worker thread isn't an issue by itself, but
2027 * increasing the map count would be visible to applications. */
2028 wined3d_not_from_cs(cs);
2030 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2031 op->opcode = WINED3D_CS_OP_MAP;
2032 op->resource = resource;
2033 op->sub_resource_idx = sub_resource_idx;
2034 op->map_desc = map_desc;
2035 op->box = box;
2036 op->flags = flags;
2037 op->hr = &hr;
2039 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2040 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2042 return hr;
2045 static void wined3d_cs_exec_unmap(struct wined3d_cs *cs, const void *data)
2047 const struct wined3d_cs_unmap *op = data;
2048 struct wined3d_resource *resource = op->resource;
2050 *op->hr = resource->resource_ops->resource_sub_resource_unmap(resource, op->sub_resource_idx);
2053 HRESULT wined3d_cs_unmap(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx)
2055 struct wined3d_cs_unmap *op;
2056 HRESULT hr;
2058 wined3d_not_from_cs(cs);
2060 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2061 op->opcode = WINED3D_CS_OP_UNMAP;
2062 op->resource = resource;
2063 op->sub_resource_idx = sub_resource_idx;
2064 op->hr = &hr;
2066 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2067 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2069 return hr;
2072 static void wined3d_cs_exec_blt_sub_resource(struct wined3d_cs *cs, const void *data)
2074 const struct wined3d_cs_blt_sub_resource *op = data;
2076 if (op->dst_resource->type == WINED3D_RTYPE_BUFFER)
2078 wined3d_buffer_copy(buffer_from_resource(op->dst_resource), op->dst_box.left,
2079 buffer_from_resource(op->src_resource), op->src_box.left,
2080 op->src_box.right - op->src_box.left);
2082 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_3D)
2084 struct wined3d_texture *src_texture, *dst_texture;
2085 unsigned int level, update_w, update_h, update_d;
2086 unsigned int row_pitch, slice_pitch;
2087 struct wined3d_context *context;
2088 struct wined3d_bo_address addr;
2090 if (op->flags & ~WINED3D_BLT_RAW)
2092 FIXME("Flags %#x not implemented for %s resources.\n",
2093 op->flags, debug_d3dresourcetype(op->dst_resource->type));
2094 goto error;
2097 if (!(op->flags & WINED3D_BLT_RAW) && op->src_resource->format != op->dst_resource->format)
2099 FIXME("Format conversion not implemented for %s resources.\n",
2100 debug_d3dresourcetype(op->dst_resource->type));
2101 goto error;
2104 update_w = op->dst_box.right - op->dst_box.left;
2105 update_h = op->dst_box.bottom - op->dst_box.top;
2106 update_d = op->dst_box.back - op->dst_box.front;
2107 if (op->src_box.right - op->src_box.left != update_w
2108 || op->src_box.bottom - op->src_box.top != update_h
2109 || op->src_box.back - op->src_box.front != update_d)
2111 FIXME("Stretching not implemented for %s resources.\n",
2112 debug_d3dresourcetype(op->dst_resource->type));
2113 goto error;
2116 dst_texture = texture_from_resource(op->dst_resource);
2117 src_texture = texture_from_resource(op->src_resource);
2119 context = context_acquire(cs->device, NULL, 0);
2121 if (!wined3d_texture_load_location(src_texture, op->src_sub_resource_idx,
2122 context, src_texture->resource.map_binding))
2124 ERR("Failed to load source sub-resource into %s.\n",
2125 wined3d_debug_location(src_texture->resource.map_binding));
2126 context_release(context);
2127 goto error;
2130 level = op->dst_sub_resource_idx % dst_texture->level_count;
2131 if (update_w == wined3d_texture_get_level_width(dst_texture, level)
2132 && update_h == wined3d_texture_get_level_height(dst_texture, level)
2133 && update_d == wined3d_texture_get_level_depth(dst_texture, level))
2135 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
2137 else if (!wined3d_texture_load_location(dst_texture, op->dst_sub_resource_idx,
2138 context, WINED3D_LOCATION_TEXTURE_RGB))
2140 ERR("Failed to load destination sub-resource.\n");
2141 context_release(context);
2142 goto error;
2145 wined3d_texture_get_memory(src_texture, op->src_sub_resource_idx, &addr, src_texture->resource.map_binding);
2146 wined3d_texture_get_pitch(src_texture, op->src_sub_resource_idx % src_texture->level_count,
2147 &row_pitch, &slice_pitch);
2149 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
2150 wined3d_texture_upload_data(dst_texture, op->dst_sub_resource_idx, context,
2151 dst_texture->resource.format, &op->src_box, wined3d_const_bo_address(&addr),
2152 row_pitch, slice_pitch, op->dst_box.left, op->dst_box.top, op->dst_box.front, FALSE);
2153 wined3d_texture_validate_location(dst_texture, op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2154 wined3d_texture_invalidate_location(dst_texture, op->dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2156 context_release(context);
2158 else
2160 if (FAILED(texture2d_blt(texture_from_resource(op->dst_resource), op->dst_sub_resource_idx,
2161 &op->dst_box, texture_from_resource(op->src_resource), op->src_sub_resource_idx,
2162 &op->src_box, op->flags, &op->fx, op->filter)))
2163 FIXME("Blit failed.\n");
2166 error:
2167 if (op->src_resource)
2168 wined3d_resource_release(op->src_resource);
2169 wined3d_resource_release(op->dst_resource);
2172 void wined3d_cs_emit_blt_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *dst_resource,
2173 unsigned int dst_sub_resource_idx, const struct wined3d_box *dst_box, struct wined3d_resource *src_resource,
2174 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, DWORD flags,
2175 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2177 struct wined3d_cs_blt_sub_resource *op;
2179 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2180 op->opcode = WINED3D_CS_OP_BLT_SUB_RESOURCE;
2181 op->dst_resource = dst_resource;
2182 op->dst_sub_resource_idx = dst_sub_resource_idx;
2183 op->dst_box = *dst_box;
2184 op->src_resource = src_resource;
2185 op->src_sub_resource_idx = src_sub_resource_idx;
2186 op->src_box = *src_box;
2187 op->flags = flags;
2188 if (fx)
2189 op->fx = *fx;
2190 else
2191 memset(&op->fx, 0, sizeof(op->fx));
2192 op->filter = filter;
2194 wined3d_resource_acquire(dst_resource);
2195 if (src_resource)
2196 wined3d_resource_acquire(src_resource);
2198 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2199 if (flags & WINED3D_BLT_SYNCHRONOUS)
2200 cs->ops->finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2203 static void wined3d_cs_exec_update_sub_resource(struct wined3d_cs *cs, const void *data)
2205 const struct wined3d_cs_update_sub_resource *op = data;
2206 struct wined3d_resource *resource = op->resource;
2207 const struct wined3d_box *box = &op->box;
2208 unsigned int width, height, depth, level;
2209 struct wined3d_const_bo_address addr;
2210 struct wined3d_context *context;
2211 struct wined3d_texture *texture;
2212 struct wined3d_box src_box;
2214 context = context_acquire(cs->device, NULL, 0);
2216 if (resource->type == WINED3D_RTYPE_BUFFER)
2218 struct wined3d_buffer *buffer = buffer_from_resource(resource);
2220 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
2222 ERR("Failed to load buffer location.\n");
2223 goto done;
2226 wined3d_buffer_upload_data(buffer, context, box, op->data.data);
2227 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
2228 goto done;
2231 texture = wined3d_texture_from_resource(resource);
2233 level = op->sub_resource_idx % texture->level_count;
2234 width = wined3d_texture_get_level_width(texture, level);
2235 height = wined3d_texture_get_level_height(texture, level);
2236 depth = wined3d_texture_get_level_depth(texture, level);
2238 addr.buffer_object = 0;
2239 addr.addr = op->data.data;
2241 /* Only load the sub-resource for partial updates. */
2242 if (!box->left && !box->top && !box->front
2243 && box->right == width && box->bottom == height && box->back == depth)
2244 wined3d_texture_prepare_texture(texture, context, FALSE);
2245 else
2246 wined3d_texture_load_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2247 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2249 wined3d_box_set(&src_box, 0, 0, box->right - box->left, box->bottom - box->top, 0, box->back - box->front);
2250 wined3d_texture_upload_data(texture, op->sub_resource_idx, context, texture->resource.format, &src_box,
2251 &addr, op->data.row_pitch, op->data.slice_pitch, box->left, box->top, box->front, FALSE);
2253 wined3d_texture_validate_location(texture, op->sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2254 wined3d_texture_invalidate_location(texture, op->sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2256 done:
2257 context_release(context);
2259 wined3d_resource_release(resource);
2262 void wined3d_cs_emit_update_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *resource,
2263 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
2264 unsigned int slice_pitch)
2266 struct wined3d_cs_update_sub_resource *op;
2268 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2269 op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2270 op->resource = resource;
2271 op->sub_resource_idx = sub_resource_idx;
2272 op->box = *box;
2273 op->data.row_pitch = row_pitch;
2274 op->data.slice_pitch = slice_pitch;
2275 op->data.data = data;
2277 wined3d_resource_acquire(resource);
2279 cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2280 /* The data pointer may go away, so we need to wait until it is read.
2281 * Copying the data may be faster if it's small. */
2282 cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2285 static void wined3d_cs_exec_add_dirty_texture_region(struct wined3d_cs *cs, const void *data)
2287 const struct wined3d_cs_add_dirty_texture_region *op = data;
2288 struct wined3d_texture *texture = op->texture;
2289 unsigned int sub_resource_idx, i;
2290 struct wined3d_context *context;
2292 context = context_acquire(cs->device, NULL, 0);
2293 sub_resource_idx = op->layer * texture->level_count;
2294 for (i = 0; i < texture->level_count; ++i, ++sub_resource_idx)
2296 if (wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
2297 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2298 else
2299 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
2301 context_release(context);
2303 wined3d_resource_release(&texture->resource);
2306 void wined3d_cs_emit_add_dirty_texture_region(struct wined3d_cs *cs,
2307 struct wined3d_texture *texture, unsigned int layer)
2309 struct wined3d_cs_add_dirty_texture_region *op;
2311 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2312 op->opcode = WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION;
2313 op->texture = texture;
2314 op->layer = layer;
2316 wined3d_resource_acquire(&texture->resource);
2318 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2321 static void wined3d_cs_exec_clear_unordered_access_view(struct wined3d_cs *cs, const void *data)
2323 const struct wined3d_cs_clear_unordered_access_view *op = data;
2324 struct wined3d_unordered_access_view *view = op->view;
2325 struct wined3d_context *context;
2327 context = context_acquire(cs->device, NULL, 0);
2328 wined3d_unordered_access_view_clear_uint(view, &op->clear_value, context);
2329 context_release(context);
2331 wined3d_resource_release(view->resource);
2334 void wined3d_cs_emit_clear_unordered_access_view_uint(struct wined3d_cs *cs,
2335 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
2337 struct wined3d_cs_clear_unordered_access_view *op;
2339 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2340 op->opcode = WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW;
2341 op->view = view;
2342 op->clear_value = *clear_value;
2344 wined3d_resource_acquire(view->resource);
2346 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2349 static void wined3d_cs_exec_copy_uav_counter(struct wined3d_cs *cs, const void *data)
2351 const struct wined3d_cs_copy_uav_counter *op = data;
2352 struct wined3d_unordered_access_view *view = op->view;
2353 struct wined3d_context *context;
2355 context = context_acquire(cs->device, NULL, 0);
2356 wined3d_unordered_access_view_copy_counter(view, op->buffer, op->offset, context);
2357 context_release(context);
2359 wined3d_resource_release(&op->buffer->resource);
2362 void wined3d_cs_emit_copy_uav_counter(struct wined3d_cs *cs, struct wined3d_buffer *dst_buffer,
2363 unsigned int offset, struct wined3d_unordered_access_view *uav)
2365 struct wined3d_cs_copy_uav_counter *op;
2367 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2368 op->opcode = WINED3D_CS_OP_COPY_UAV_COUNTER;
2369 op->buffer = dst_buffer;
2370 op->offset = offset;
2371 op->view = uav;
2373 wined3d_resource_acquire(&dst_buffer->resource);
2375 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2378 static void wined3d_cs_exec_generate_mipmaps(struct wined3d_cs *cs, const void *data)
2380 const struct wined3d_cs_generate_mipmaps *op = data;
2381 struct wined3d_shader_resource_view *view = op->view;
2383 shader_resource_view_generate_mipmaps(view);
2384 wined3d_resource_release(view->resource);
2387 void wined3d_cs_emit_generate_mipmaps(struct wined3d_cs *cs, struct wined3d_shader_resource_view *view)
2389 struct wined3d_cs_generate_mipmaps *op;
2391 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2392 op->opcode = WINED3D_CS_OP_GENERATE_MIPMAPS;
2393 op->view = view;
2395 wined3d_resource_acquire(view->resource);
2397 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2400 static void wined3d_cs_emit_stop(struct wined3d_cs *cs)
2402 struct wined3d_cs_stop *op;
2404 op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2405 op->opcode = WINED3D_CS_OP_STOP;
2407 cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2408 cs->ops->finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2411 static void (* const wined3d_cs_op_handlers[])(struct wined3d_cs *cs, const void *data) =
2413 /* WINED3D_CS_OP_NOP */ wined3d_cs_exec_nop,
2414 /* WINED3D_CS_OP_PRESENT */ wined3d_cs_exec_present,
2415 /* WINED3D_CS_OP_CLEAR */ wined3d_cs_exec_clear,
2416 /* WINED3D_CS_OP_DISPATCH */ wined3d_cs_exec_dispatch,
2417 /* WINED3D_CS_OP_DRAW */ wined3d_cs_exec_draw,
2418 /* WINED3D_CS_OP_FLUSH */ wined3d_cs_exec_flush,
2419 /* WINED3D_CS_OP_SET_PREDICATION */ wined3d_cs_exec_set_predication,
2420 /* WINED3D_CS_OP_SET_VIEWPORTS */ wined3d_cs_exec_set_viewports,
2421 /* WINED3D_CS_OP_SET_SCISSOR_RECTS */ wined3d_cs_exec_set_scissor_rects,
2422 /* WINED3D_CS_OP_SET_RENDERTARGET_VIEW */ wined3d_cs_exec_set_rendertarget_view,
2423 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW */ wined3d_cs_exec_set_depth_stencil_view,
2424 /* WINED3D_CS_OP_SET_VERTEX_DECLARATION */ wined3d_cs_exec_set_vertex_declaration,
2425 /* WINED3D_CS_OP_SET_STREAM_SOURCE */ wined3d_cs_exec_set_stream_source,
2426 /* WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ */ wined3d_cs_exec_set_stream_source_freq,
2427 /* WINED3D_CS_OP_SET_STREAM_OUTPUT */ wined3d_cs_exec_set_stream_output,
2428 /* WINED3D_CS_OP_SET_INDEX_BUFFER */ wined3d_cs_exec_set_index_buffer,
2429 /* WINED3D_CS_OP_SET_CONSTANT_BUFFER */ wined3d_cs_exec_set_constant_buffer,
2430 /* WINED3D_CS_OP_SET_TEXTURE */ wined3d_cs_exec_set_texture,
2431 /* WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW */ wined3d_cs_exec_set_shader_resource_view,
2432 /* WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_set_unordered_access_view,
2433 /* WINED3D_CS_OP_SET_SAMPLER */ wined3d_cs_exec_set_sampler,
2434 /* WINED3D_CS_OP_SET_SHADER */ wined3d_cs_exec_set_shader,
2435 /* WINED3D_CS_OP_SET_BLEND_STATE */ wined3d_cs_exec_set_blend_state,
2436 /* WINED3D_CS_OP_SET_RASTERIZER_STATE */ wined3d_cs_exec_set_rasterizer_state,
2437 /* WINED3D_CS_OP_SET_RENDER_STATE */ wined3d_cs_exec_set_render_state,
2438 /* WINED3D_CS_OP_SET_TEXTURE_STATE */ wined3d_cs_exec_set_texture_state,
2439 /* WINED3D_CS_OP_SET_SAMPLER_STATE */ wined3d_cs_exec_set_sampler_state,
2440 /* WINED3D_CS_OP_SET_TRANSFORM */ wined3d_cs_exec_set_transform,
2441 /* WINED3D_CS_OP_SET_CLIP_PLANE */ wined3d_cs_exec_set_clip_plane,
2442 /* WINED3D_CS_OP_SET_COLOR_KEY */ wined3d_cs_exec_set_color_key,
2443 /* WINED3D_CS_OP_SET_MATERIAL */ wined3d_cs_exec_set_material,
2444 /* WINED3D_CS_OP_SET_LIGHT */ wined3d_cs_exec_set_light,
2445 /* WINED3D_CS_OP_SET_LIGHT_ENABLE */ wined3d_cs_exec_set_light_enable,
2446 /* WINED3D_CS_OP_PUSH_CONSTANTS */ wined3d_cs_exec_push_constants,
2447 /* WINED3D_CS_OP_RESET_STATE */ wined3d_cs_exec_reset_state,
2448 /* WINED3D_CS_OP_CALLBACK */ wined3d_cs_exec_callback,
2449 /* WINED3D_CS_OP_QUERY_ISSUE */ wined3d_cs_exec_query_issue,
2450 /* WINED3D_CS_OP_PRELOAD_RESOURCE */ wined3d_cs_exec_preload_resource,
2451 /* WINED3D_CS_OP_UNLOAD_RESOURCE */ wined3d_cs_exec_unload_resource,
2452 /* WINED3D_CS_OP_MAP */ wined3d_cs_exec_map,
2453 /* WINED3D_CS_OP_UNMAP */ wined3d_cs_exec_unmap,
2454 /* WINED3D_CS_OP_BLT_SUB_RESOURCE */ wined3d_cs_exec_blt_sub_resource,
2455 /* WINED3D_CS_OP_UPDATE_SUB_RESOURCE */ wined3d_cs_exec_update_sub_resource,
2456 /* WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION */ wined3d_cs_exec_add_dirty_texture_region,
2457 /* WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_clear_unordered_access_view,
2458 /* WINED3D_CS_OP_COPY_UAV_COUNTER */ wined3d_cs_exec_copy_uav_counter,
2459 /* WINED3D_CS_OP_GENERATE_MIPMAPS */ wined3d_cs_exec_generate_mipmaps,
2462 static void *wined3d_cs_st_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2464 if (size > (cs->data_size - cs->end))
2466 size_t new_size;
2467 void *new_data;
2469 new_size = max(size, cs->data_size * 2);
2470 if (!cs->end)
2471 new_data = heap_realloc(cs->data, new_size);
2472 else
2473 new_data = heap_alloc(new_size);
2474 if (!new_data)
2475 return NULL;
2477 cs->data_size = new_size;
2478 cs->start = cs->end = 0;
2479 cs->data = new_data;
2482 cs->end += size;
2484 return (BYTE *)cs->data + cs->start;
2487 static void wined3d_cs_st_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2489 enum wined3d_cs_op opcode;
2490 size_t start;
2491 BYTE *data;
2493 data = cs->data;
2494 start = cs->start;
2495 cs->start = cs->end;
2497 opcode = *(const enum wined3d_cs_op *)&data[start];
2498 if (opcode >= WINED3D_CS_OP_STOP)
2499 ERR("Invalid opcode %#x.\n", opcode);
2500 else
2501 wined3d_cs_op_handlers[opcode](cs, &data[start]);
2503 if (cs->data == data)
2504 cs->start = cs->end = start;
2505 else if (!start)
2506 heap_free(data);
2509 static void wined3d_cs_st_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2513 static const struct wined3d_cs_ops wined3d_cs_st_ops =
2515 wined3d_cs_st_require_space,
2516 wined3d_cs_st_submit,
2517 wined3d_cs_st_finish,
2518 wined3d_cs_st_push_constants,
2521 static BOOL wined3d_cs_queue_is_empty(const struct wined3d_cs *cs, const struct wined3d_cs_queue *queue)
2523 wined3d_from_cs(cs);
2524 return *(volatile LONG *)&queue->head == queue->tail;
2527 static void wined3d_cs_queue_submit(struct wined3d_cs_queue *queue, struct wined3d_cs *cs)
2529 struct wined3d_cs_packet *packet;
2530 size_t packet_size;
2532 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2533 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2534 InterlockedExchange(&queue->head, (queue->head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1));
2536 if (InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2537 SetEvent(cs->event);
2540 static void wined3d_cs_mt_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2542 if (cs->thread_id == GetCurrentThreadId())
2543 return wined3d_cs_st_submit(cs, queue_id);
2545 wined3d_cs_queue_submit(&cs->queue[queue_id], cs);
2548 static void *wined3d_cs_queue_require_space(struct wined3d_cs_queue *queue, size_t size, struct wined3d_cs *cs)
2550 size_t queue_size = ARRAY_SIZE(queue->data);
2551 size_t header_size, packet_size, remaining;
2552 struct wined3d_cs_packet *packet;
2554 header_size = FIELD_OFFSET(struct wined3d_cs_packet, data[0]);
2555 size = (size + header_size - 1) & ~(header_size - 1);
2556 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[size]);
2557 if (packet_size >= WINED3D_CS_QUEUE_SIZE)
2559 ERR("Packet size %lu >= queue size %u.\n",
2560 (unsigned long)packet_size, WINED3D_CS_QUEUE_SIZE);
2561 return NULL;
2564 remaining = queue_size - queue->head;
2565 if (remaining < packet_size)
2567 size_t nop_size = remaining - header_size;
2568 struct wined3d_cs_nop *nop;
2570 TRACE("Inserting a nop for %lu + %lu bytes.\n",
2571 (unsigned long)header_size, (unsigned long)nop_size);
2573 nop = wined3d_cs_queue_require_space(queue, nop_size, cs);
2574 if (nop_size)
2575 nop->opcode = WINED3D_CS_OP_NOP;
2577 wined3d_cs_queue_submit(queue, cs);
2578 assert(!queue->head);
2581 for (;;)
2583 LONG tail = *(volatile LONG *)&queue->tail;
2584 LONG head = queue->head;
2585 LONG new_pos;
2587 /* Empty. */
2588 if (head == tail)
2589 break;
2590 new_pos = (head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1);
2591 /* Head ahead of tail. We checked the remaining size above, so we only
2592 * need to make sure we don't make head equal to tail. */
2593 if (head > tail && (new_pos != tail))
2594 break;
2595 /* Tail ahead of head. Make sure the new head is before the tail as
2596 * well. Note that new_pos is 0 when it's at the end of the queue. */
2597 if (new_pos < tail && new_pos)
2598 break;
2600 TRACE("Waiting for free space. Head %u, tail %u, packet size %lu.\n",
2601 head, tail, (unsigned long)packet_size);
2604 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2605 packet->size = size;
2606 return packet->data;
2609 static void *wined3d_cs_mt_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2611 if (cs->thread_id == GetCurrentThreadId())
2612 return wined3d_cs_st_require_space(cs, size, queue_id);
2614 return wined3d_cs_queue_require_space(&cs->queue[queue_id], size, cs);
2617 static void wined3d_cs_mt_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2619 if (cs->thread_id == GetCurrentThreadId())
2620 return wined3d_cs_st_finish(cs, queue_id);
2622 while (cs->queue[queue_id].head != *(volatile LONG *)&cs->queue[queue_id].tail)
2623 wined3d_pause();
2626 static const struct wined3d_cs_ops wined3d_cs_mt_ops =
2628 wined3d_cs_mt_require_space,
2629 wined3d_cs_mt_submit,
2630 wined3d_cs_mt_finish,
2631 wined3d_cs_mt_push_constants,
2634 static void poll_queries(struct wined3d_cs *cs)
2636 struct wined3d_query *query, *cursor;
2638 LIST_FOR_EACH_ENTRY_SAFE(query, cursor, &cs->query_poll_list, struct wined3d_query, poll_list_entry)
2640 if (!query->query_ops->query_poll(query, 0))
2641 continue;
2643 list_remove(&query->poll_list_entry);
2644 list_init(&query->poll_list_entry);
2645 InterlockedIncrement(&query->counter_retrieved);
2649 static void wined3d_cs_wait_event(struct wined3d_cs *cs)
2651 InterlockedExchange(&cs->waiting_for_event, TRUE);
2653 /* The main thread might have enqueued a command and blocked on it after
2654 * the CS thread decided to enter wined3d_cs_wait_event(), but before
2655 * "waiting_for_event" was set.
2657 * Likewise, we can race with the main thread when resetting
2658 * "waiting_for_event", in which case we would need to call
2659 * WaitForSingleObject() because the main thread called SetEvent(). */
2660 if (!(wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_DEFAULT])
2661 && wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_MAP]))
2662 && InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2663 return;
2665 WaitForSingleObject(cs->event, INFINITE);
2668 static DWORD WINAPI wined3d_cs_run(void *ctx)
2670 struct wined3d_cs_packet *packet;
2671 struct wined3d_cs_queue *queue;
2672 unsigned int spin_count = 0;
2673 struct wined3d_cs *cs = ctx;
2674 enum wined3d_cs_op opcode;
2675 HMODULE wined3d_module;
2676 unsigned int poll = 0;
2677 LONG tail;
2679 TRACE("Started.\n");
2681 /* Copy the module handle to a local variable to avoid racing with the
2682 * thread freeing "cs" before the FreeLibraryAndExitThread() call. */
2683 wined3d_module = cs->wined3d_module;
2685 list_init(&cs->query_poll_list);
2686 cs->thread_id = GetCurrentThreadId();
2687 for (;;)
2689 if (++poll == WINED3D_CS_QUERY_POLL_INTERVAL)
2691 poll_queries(cs);
2692 poll = 0;
2695 queue = &cs->queue[WINED3D_CS_QUEUE_MAP];
2696 if (wined3d_cs_queue_is_empty(cs, queue))
2698 queue = &cs->queue[WINED3D_CS_QUEUE_DEFAULT];
2699 if (wined3d_cs_queue_is_empty(cs, queue))
2701 if (++spin_count >= WINED3D_CS_SPIN_COUNT && list_empty(&cs->query_poll_list))
2702 wined3d_cs_wait_event(cs);
2703 continue;
2706 spin_count = 0;
2708 tail = queue->tail;
2709 packet = (struct wined3d_cs_packet *)&queue->data[tail];
2710 if (packet->size)
2712 opcode = *(const enum wined3d_cs_op *)packet->data;
2714 if (opcode >= WINED3D_CS_OP_STOP)
2716 if (opcode > WINED3D_CS_OP_STOP)
2717 ERR("Invalid opcode %#x.\n", opcode);
2718 break;
2721 wined3d_cs_op_handlers[opcode](cs, packet->data);
2724 tail += FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2725 tail &= (WINED3D_CS_QUEUE_SIZE - 1);
2726 InterlockedExchange(&queue->tail, tail);
2729 cs->queue[WINED3D_CS_QUEUE_MAP].tail = cs->queue[WINED3D_CS_QUEUE_MAP].head;
2730 cs->queue[WINED3D_CS_QUEUE_DEFAULT].tail = cs->queue[WINED3D_CS_QUEUE_DEFAULT].head;
2731 TRACE("Stopped.\n");
2732 FreeLibraryAndExitThread(wined3d_module, 0);
2735 struct wined3d_cs *wined3d_cs_create(struct wined3d_device *device)
2737 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2738 struct wined3d_cs *cs;
2740 if (!(cs = heap_alloc_zero(sizeof(*cs))))
2741 return NULL;
2743 cs->ops = &wined3d_cs_st_ops;
2744 cs->device = device;
2746 state_init(&cs->state, &cs->fb, gl_info, &device->adapter->d3d_info,
2747 WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
2749 cs->data_size = WINED3D_INITIAL_CS_SIZE;
2750 if (!(cs->data = heap_alloc(cs->data_size)))
2751 goto fail;
2753 if (wined3d_settings.cs_multithreaded
2754 && !RtlIsCriticalSectionLockedByThread(NtCurrentTeb()->Peb->LoaderLock))
2756 cs->ops = &wined3d_cs_mt_ops;
2758 if (!(cs->event = CreateEventW(NULL, FALSE, FALSE, NULL)))
2760 ERR("Failed to create command stream event.\n");
2761 heap_free(cs->data);
2762 goto fail;
2765 if (!(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2766 (const WCHAR *)wined3d_cs_run, &cs->wined3d_module)))
2768 ERR("Failed to get wined3d module handle.\n");
2769 CloseHandle(cs->event);
2770 heap_free(cs->data);
2771 goto fail;
2774 if (!(cs->thread = CreateThread(NULL, 0, wined3d_cs_run, cs, 0, NULL)))
2776 ERR("Failed to create wined3d command stream thread.\n");
2777 FreeLibrary(cs->wined3d_module);
2778 CloseHandle(cs->event);
2779 heap_free(cs->data);
2780 goto fail;
2784 return cs;
2786 fail:
2787 state_cleanup(&cs->state);
2788 heap_free(cs);
2789 return NULL;
2792 void wined3d_cs_destroy(struct wined3d_cs *cs)
2794 if (cs->thread)
2796 wined3d_cs_emit_stop(cs);
2797 CloseHandle(cs->thread);
2798 if (!CloseHandle(cs->event))
2799 ERR("Closing event failed.\n");
2802 state_cleanup(&cs->state);
2803 heap_free(cs->data);
2804 heap_free(cs);