oleaut32: Use VariantInit() rather than open coding it.
[wine.git] / dlls / wined3d / cs.c
blob43352eb7cb528809e0ff120a947b8d3418000f2a
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_STOP,
75 struct wined3d_cs_packet
77 size_t size;
78 BYTE data[1];
81 struct wined3d_cs_nop
83 enum wined3d_cs_op opcode;
86 struct wined3d_cs_present
88 enum wined3d_cs_op opcode;
89 HWND dst_window_override;
90 struct wined3d_swapchain *swapchain;
91 RECT src_rect;
92 RECT dst_rect;
93 DWORD flags;
96 struct wined3d_cs_clear
98 enum wined3d_cs_op opcode;
99 DWORD flags;
100 unsigned int rt_count;
101 struct wined3d_fb_state *fb;
102 RECT draw_rect;
103 struct wined3d_color color;
104 float depth;
105 DWORD stencil;
106 unsigned int rect_count;
107 RECT rects[1];
110 struct wined3d_cs_dispatch
112 enum wined3d_cs_op opcode;
113 unsigned int group_count_x;
114 unsigned int group_count_y;
115 unsigned int group_count_z;
118 struct wined3d_cs_draw
120 enum wined3d_cs_op opcode;
121 GLenum primitive_type;
122 int base_vertex_idx;
123 unsigned int start_idx;
124 unsigned int index_count;
125 unsigned int start_instance;
126 unsigned int instance_count;
127 BOOL indexed;
130 struct wined3d_cs_flush
132 enum wined3d_cs_op opcode;
135 struct wined3d_cs_set_predication
137 enum wined3d_cs_op opcode;
138 struct wined3d_query *predicate;
139 BOOL value;
142 struct wined3d_cs_set_viewport
144 enum wined3d_cs_op opcode;
145 struct wined3d_viewport viewport;
148 struct wined3d_cs_set_scissor_rect
150 enum wined3d_cs_op opcode;
151 RECT rect;
154 struct wined3d_cs_set_rendertarget_view
156 enum wined3d_cs_op opcode;
157 unsigned int view_idx;
158 struct wined3d_rendertarget_view *view;
161 struct wined3d_cs_set_depth_stencil_view
163 enum wined3d_cs_op opcode;
164 struct wined3d_rendertarget_view *view;
167 struct wined3d_cs_set_vertex_declaration
169 enum wined3d_cs_op opcode;
170 struct wined3d_vertex_declaration *declaration;
173 struct wined3d_cs_set_stream_source
175 enum wined3d_cs_op opcode;
176 UINT stream_idx;
177 struct wined3d_buffer *buffer;
178 UINT offset;
179 UINT stride;
182 struct wined3d_cs_set_stream_source_freq
184 enum wined3d_cs_op opcode;
185 UINT stream_idx;
186 UINT frequency;
187 UINT flags;
190 struct wined3d_cs_set_stream_output
192 enum wined3d_cs_op opcode;
193 UINT stream_idx;
194 struct wined3d_buffer *buffer;
195 UINT offset;
198 struct wined3d_cs_set_index_buffer
200 enum wined3d_cs_op opcode;
201 struct wined3d_buffer *buffer;
202 enum wined3d_format_id format_id;
203 unsigned int offset;
206 struct wined3d_cs_set_constant_buffer
208 enum wined3d_cs_op opcode;
209 enum wined3d_shader_type type;
210 UINT cb_idx;
211 struct wined3d_buffer *buffer;
214 struct wined3d_cs_set_texture
216 enum wined3d_cs_op opcode;
217 UINT stage;
218 struct wined3d_texture *texture;
221 struct wined3d_cs_set_color_key
223 enum wined3d_cs_op opcode;
224 struct wined3d_texture *texture;
225 WORD flags;
226 WORD set;
227 struct wined3d_color_key color_key;
230 struct wined3d_cs_set_shader_resource_view
232 enum wined3d_cs_op opcode;
233 enum wined3d_shader_type type;
234 UINT view_idx;
235 struct wined3d_shader_resource_view *view;
238 struct wined3d_cs_set_unordered_access_view
240 enum wined3d_cs_op opcode;
241 enum wined3d_pipeline pipeline;
242 unsigned int view_idx;
243 struct wined3d_unordered_access_view *view;
246 struct wined3d_cs_set_sampler
248 enum wined3d_cs_op opcode;
249 enum wined3d_shader_type type;
250 UINT sampler_idx;
251 struct wined3d_sampler *sampler;
254 struct wined3d_cs_set_shader
256 enum wined3d_cs_op opcode;
257 enum wined3d_shader_type type;
258 struct wined3d_shader *shader;
261 struct wined3d_cs_set_rasterizer_state
263 enum wined3d_cs_op opcode;
264 struct wined3d_rasterizer_state *state;
267 struct wined3d_cs_set_render_state
269 enum wined3d_cs_op opcode;
270 enum wined3d_render_state state;
271 DWORD value;
274 struct wined3d_cs_set_texture_state
276 enum wined3d_cs_op opcode;
277 UINT stage;
278 enum wined3d_texture_stage_state state;
279 DWORD value;
282 struct wined3d_cs_set_sampler_state
284 enum wined3d_cs_op opcode;
285 UINT sampler_idx;
286 enum wined3d_sampler_state state;
287 DWORD value;
290 struct wined3d_cs_set_transform
292 enum wined3d_cs_op opcode;
293 enum wined3d_transform_state state;
294 struct wined3d_matrix matrix;
297 struct wined3d_cs_set_clip_plane
299 enum wined3d_cs_op opcode;
300 UINT plane_idx;
301 struct wined3d_vec4 plane;
304 struct wined3d_cs_set_material
306 enum wined3d_cs_op opcode;
307 struct wined3d_material material;
310 struct wined3d_cs_set_light
312 enum wined3d_cs_op opcode;
313 struct wined3d_light_info light;
316 struct wined3d_cs_set_light_enable
318 enum wined3d_cs_op opcode;
319 unsigned int idx;
320 BOOL enable;
323 struct wined3d_cs_push_constants
325 enum wined3d_cs_op opcode;
326 enum wined3d_push_constants type;
327 unsigned int start_idx;
328 unsigned int count;
329 BYTE constants[1];
332 struct wined3d_cs_reset_state
334 enum wined3d_cs_op opcode;
337 struct wined3d_cs_callback
339 enum wined3d_cs_op opcode;
340 void (*callback)(void *object);
341 void *object;
344 struct wined3d_cs_query_issue
346 enum wined3d_cs_op opcode;
347 struct wined3d_query *query;
348 DWORD flags;
351 struct wined3d_cs_preload_resource
353 enum wined3d_cs_op opcode;
354 struct wined3d_resource *resource;
357 struct wined3d_cs_unload_resource
359 enum wined3d_cs_op opcode;
360 struct wined3d_resource *resource;
363 struct wined3d_cs_map
365 enum wined3d_cs_op opcode;
366 struct wined3d_resource *resource;
367 unsigned int sub_resource_idx;
368 struct wined3d_map_desc *map_desc;
369 const struct wined3d_box *box;
370 DWORD flags;
371 HRESULT *hr;
374 struct wined3d_cs_unmap
376 enum wined3d_cs_op opcode;
377 struct wined3d_resource *resource;
378 unsigned int sub_resource_idx;
379 HRESULT *hr;
382 struct wined3d_cs_blt_sub_resource
384 enum wined3d_cs_op opcode;
385 struct wined3d_resource *dst_resource;
386 unsigned int dst_sub_resource_idx;
387 struct wined3d_box dst_box;
388 struct wined3d_resource *src_resource;
389 unsigned int src_sub_resource_idx;
390 struct wined3d_box src_box;
391 DWORD flags;
392 struct wined3d_blt_fx fx;
393 enum wined3d_texture_filter_type filter;
396 struct wined3d_cs_update_sub_resource
398 enum wined3d_cs_op opcode;
399 struct wined3d_resource *resource;
400 unsigned int sub_resource_idx;
401 struct wined3d_box box;
402 struct wined3d_sub_resource_data data;
405 struct wined3d_cs_add_dirty_texture_region
407 enum wined3d_cs_op opcode;
408 struct wined3d_texture *texture;
409 unsigned int layer;
412 struct wined3d_cs_stop
414 enum wined3d_cs_op opcode;
417 static void wined3d_cs_exec_nop(struct wined3d_cs *cs, const void *data)
421 static void wined3d_cs_exec_present(struct wined3d_cs *cs, const void *data)
423 const struct wined3d_cs_present *op = data;
424 struct wined3d_swapchain *swapchain;
425 unsigned int i;
427 swapchain = op->swapchain;
428 wined3d_swapchain_set_window(swapchain, op->dst_window_override);
430 swapchain->swapchain_ops->swapchain_present(swapchain, &op->src_rect, &op->dst_rect, op->flags);
432 wined3d_resource_release(&swapchain->front_buffer->resource);
433 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
435 wined3d_resource_release(&swapchain->back_buffers[i]->resource);
439 void wined3d_cs_emit_present(struct wined3d_cs *cs, struct wined3d_swapchain *swapchain,
440 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, DWORD flags)
442 struct wined3d_cs_present *op;
443 unsigned int i;
445 op = cs->ops->require_space(cs, sizeof(*op));
446 op->opcode = WINED3D_CS_OP_PRESENT;
447 op->dst_window_override = dst_window_override;
448 op->swapchain = swapchain;
449 op->src_rect = *src_rect;
450 op->dst_rect = *dst_rect;
451 op->flags = flags;
453 wined3d_resource_acquire(&swapchain->front_buffer->resource);
454 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
456 wined3d_resource_acquire(&swapchain->back_buffers[i]->resource);
459 cs->ops->submit(cs);
462 static void wined3d_cs_exec_clear(struct wined3d_cs *cs, const void *data)
464 const struct wined3d_state *state = &cs->state;
465 const struct wined3d_cs_clear *op = data;
466 struct wined3d_device *device;
467 unsigned int i;
468 RECT draw_rect;
470 device = cs->device;
471 wined3d_get_draw_rect(state, &draw_rect);
472 device->blitter->ops->blitter_clear(device->blitter, device, op->rt_count, op->fb,
473 op->rect_count, op->rects, &op->draw_rect, op->flags, &op->color, op->depth, op->stencil);
475 if (op->flags & WINED3DCLEAR_TARGET)
477 for (i = 0; i < op->rt_count; ++i)
479 if (op->fb->render_targets[i])
480 wined3d_resource_release(op->fb->render_targets[i]->resource);
483 if (op->flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
484 wined3d_resource_release(op->fb->depth_stencil->resource);
487 void wined3d_cs_emit_clear(struct wined3d_cs *cs, DWORD rect_count, const RECT *rects,
488 DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
490 unsigned int rt_count = cs->device->adapter->gl_info.limits.buffers;
491 const struct wined3d_state *state = &cs->device->state;
492 struct wined3d_cs_clear *op;
493 unsigned int i;
495 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_clear, rects[rect_count]));
496 op->opcode = WINED3D_CS_OP_CLEAR;
497 op->flags = flags;
498 op->rt_count = rt_count;
499 op->fb = &cs->fb;
500 wined3d_get_draw_rect(state, &op->draw_rect);
501 op->color = *color;
502 op->depth = depth;
503 op->stencil = stencil;
504 op->rect_count = rect_count;
505 memcpy(op->rects, rects, sizeof(*rects) * rect_count);
507 if (flags & WINED3DCLEAR_TARGET)
509 for (i = 0; i < rt_count; ++i)
511 if (state->fb->render_targets[i])
512 wined3d_resource_acquire(state->fb->render_targets[i]->resource);
515 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
516 wined3d_resource_acquire(state->fb->depth_stencil->resource);
518 cs->ops->submit(cs);
521 void wined3d_cs_emit_clear_rendertarget_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view,
522 const RECT *rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
524 struct wined3d_cs_clear *op;
525 struct
527 struct wined3d_rendertarget_view *rt;
528 struct wined3d_fb_state fb;
529 } *extra;
531 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_clear, rects[1]) + sizeof(*extra));
532 extra = (void *)&op->rects[1];
533 extra->fb.render_targets = &extra->rt;
534 op->fb = &extra->fb;
536 op->opcode = WINED3D_CS_OP_CLEAR;
537 op->flags = flags;
538 if (flags & WINED3DCLEAR_TARGET)
540 op->rt_count = 1;
541 op->fb->render_targets[0] = view;
542 op->fb->depth_stencil = NULL;
543 op->color = *color;
545 else
547 op->rt_count = 0;
548 op->fb->render_targets[0] = NULL;
549 op->fb->depth_stencil = view;
550 op->depth = depth;
551 op->stencil = stencil;
553 SetRect(&op->draw_rect, 0, 0, view->width, view->height);
554 op->rect_count = 1;
555 op->rects[0] = *rect;
557 wined3d_resource_acquire(view->resource);
559 cs->ops->submit(cs);
562 static void acquire_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
564 struct wined3d_shader_sampler_map_entry *entry;
565 struct wined3d_shader_resource_view *view;
566 struct wined3d_shader *shader;
567 unsigned int i, j;
569 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
571 if (!(shader_mask & (1u << i)))
572 continue;
574 if (!(shader = state->shader[i]))
575 continue;
577 for (j = 0; j < WINED3D_MAX_CBS; ++j)
579 if (state->cb[i][j])
580 wined3d_resource_acquire(&state->cb[i][j]->resource);
583 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
585 entry = &shader->reg_maps.sampler_map.entries[j];
587 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
588 continue;
590 wined3d_resource_acquire(view->resource);
595 static void release_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
597 struct wined3d_shader_sampler_map_entry *entry;
598 struct wined3d_shader_resource_view *view;
599 struct wined3d_shader *shader;
600 unsigned int i, j;
602 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
604 if (!(shader_mask & (1u << i)))
605 continue;
607 if (!(shader = state->shader[i]))
608 continue;
610 for (j = 0; j < WINED3D_MAX_CBS; ++j)
612 if (state->cb[i][j])
613 wined3d_resource_release(&state->cb[i][j]->resource);
616 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
618 entry = &shader->reg_maps.sampler_map.entries[j];
620 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
621 continue;
623 wined3d_resource_release(view->resource);
628 static void acquire_unordered_access_resources(const struct wined3d_shader *shader,
629 struct wined3d_unordered_access_view * const *views)
631 unsigned int i;
633 if (!shader)
634 return;
636 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
638 if (!shader->reg_maps.uav_resource_info[i].type)
639 continue;
641 if (!views[i])
642 continue;
644 wined3d_resource_acquire(views[i]->resource);
648 static void release_unordered_access_resources(const struct wined3d_shader *shader,
649 struct wined3d_unordered_access_view * const *views)
651 unsigned int i;
653 if (!shader)
654 return;
656 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
658 if (!shader->reg_maps.uav_resource_info[i].type)
659 continue;
661 if (!views[i])
662 continue;
664 wined3d_resource_release(views[i]->resource);
668 static void wined3d_cs_exec_dispatch(struct wined3d_cs *cs, const void *data)
670 const struct wined3d_cs_dispatch *op = data;
671 struct wined3d_state *state = &cs->state;
673 dispatch_compute(cs->device, state,
674 op->group_count_x, op->group_count_y, op->group_count_z);
676 release_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
677 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
678 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
681 void wined3d_cs_emit_dispatch(struct wined3d_cs *cs,
682 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
684 const struct wined3d_state *state = &cs->device->state;
685 struct wined3d_cs_dispatch *op;
687 op = cs->ops->require_space(cs, sizeof(*op));
688 op->opcode = WINED3D_CS_OP_DISPATCH;
689 op->group_count_x = group_count_x;
690 op->group_count_y = group_count_y;
691 op->group_count_z = group_count_z;
693 acquire_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
694 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
695 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
697 cs->ops->submit(cs);
700 static void wined3d_cs_exec_draw(struct wined3d_cs *cs, const void *data)
702 struct wined3d_state *state = &cs->state;
703 const struct wined3d_cs_draw *op = data;
704 unsigned int i;
706 if (!cs->device->adapter->gl_info.supported[ARB_DRAW_ELEMENTS_BASE_VERTEX]
707 && state->load_base_vertex_index != op->base_vertex_idx)
709 state->load_base_vertex_index = op->base_vertex_idx;
710 device_invalidate_state(cs->device, STATE_BASEVERTEXINDEX);
713 if (state->gl_primitive_type != op->primitive_type)
715 if (state->gl_primitive_type == GL_POINTS || op->primitive_type == GL_POINTS)
716 device_invalidate_state(cs->device, STATE_POINT_ENABLE);
717 state->gl_primitive_type = op->primitive_type;
720 draw_primitive(cs->device, state, op->base_vertex_idx, op->start_idx,
721 op->index_count, op->start_instance, op->instance_count, op->indexed);
723 if (op->indexed)
724 wined3d_resource_release(&state->index_buffer->resource);
725 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
727 if (state->streams[i].buffer)
728 wined3d_resource_release(&state->streams[i].buffer->resource);
730 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
732 if (state->stream_output[i].buffer)
733 wined3d_resource_release(&state->stream_output[i].buffer->resource);
735 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
737 if (state->textures[i])
738 wined3d_resource_release(&state->textures[i]->resource);
740 for (i = 0; i < cs->device->adapter->gl_info.limits.buffers; ++i)
742 if (state->fb->render_targets[i])
743 wined3d_resource_release(state->fb->render_targets[i]->resource);
745 if (state->fb->depth_stencil)
746 wined3d_resource_release(state->fb->depth_stencil->resource);
747 release_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
748 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
749 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
752 void wined3d_cs_emit_draw(struct wined3d_cs *cs, GLenum primitive_type, int base_vertex_idx, unsigned int start_idx,
753 unsigned int index_count, unsigned int start_instance, unsigned int instance_count, BOOL indexed)
755 const struct wined3d_state *state = &cs->device->state;
756 struct wined3d_cs_draw *op;
757 unsigned int i;
759 op = cs->ops->require_space(cs, sizeof(*op));
760 op->opcode = WINED3D_CS_OP_DRAW;
761 op->primitive_type = primitive_type;
762 op->base_vertex_idx = base_vertex_idx;
763 op->start_idx = start_idx;
764 op->index_count = index_count;
765 op->start_instance = start_instance;
766 op->instance_count = instance_count;
767 op->indexed = indexed;
769 if (indexed)
770 wined3d_resource_acquire(&state->index_buffer->resource);
771 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
773 if (state->streams[i].buffer)
774 wined3d_resource_acquire(&state->streams[i].buffer->resource);
776 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
778 if (state->stream_output[i].buffer)
779 wined3d_resource_acquire(&state->stream_output[i].buffer->resource);
781 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
783 if (state->textures[i])
784 wined3d_resource_acquire(&state->textures[i]->resource);
786 for (i = 0; i < cs->device->adapter->gl_info.limits.buffers; ++i)
788 if (state->fb->render_targets[i])
789 wined3d_resource_acquire(state->fb->render_targets[i]->resource);
791 if (state->fb->depth_stencil)
792 wined3d_resource_acquire(state->fb->depth_stencil->resource);
793 acquire_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
794 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
795 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
797 cs->ops->submit(cs);
800 static void wined3d_cs_exec_flush(struct wined3d_cs *cs, const void *data)
802 struct wined3d_context *context;
804 context = context_acquire(cs->device, NULL, 0);
805 context->gl_info->gl_ops.gl.p_glFlush();
806 context_release(context);
809 void wined3d_cs_emit_flush(struct wined3d_cs *cs)
811 struct wined3d_cs_flush *op;
813 op = cs->ops->require_space(cs, sizeof(*op));
814 op->opcode = WINED3D_CS_OP_FLUSH;
816 cs->ops->submit(cs);
819 static void wined3d_cs_exec_set_predication(struct wined3d_cs *cs, const void *data)
821 const struct wined3d_cs_set_predication *op = data;
823 cs->state.predicate = op->predicate;
824 cs->state.predicate_value = op->value;
827 void wined3d_cs_emit_set_predication(struct wined3d_cs *cs, struct wined3d_query *predicate, BOOL value)
829 struct wined3d_cs_set_predication *op;
831 op = cs->ops->require_space(cs, sizeof(*op));
832 op->opcode = WINED3D_CS_OP_SET_PREDICATION;
833 op->predicate = predicate;
834 op->value = value;
836 cs->ops->submit(cs);
839 static void wined3d_cs_exec_set_viewport(struct wined3d_cs *cs, const void *data)
841 const struct wined3d_cs_set_viewport *op = data;
843 cs->state.viewport = op->viewport;
844 device_invalidate_state(cs->device, STATE_VIEWPORT);
847 void wined3d_cs_emit_set_viewport(struct wined3d_cs *cs, const struct wined3d_viewport *viewport)
849 struct wined3d_cs_set_viewport *op;
851 op = cs->ops->require_space(cs, sizeof(*op));
852 op->opcode = WINED3D_CS_OP_SET_VIEWPORT;
853 op->viewport = *viewport;
855 cs->ops->submit(cs);
858 static void wined3d_cs_exec_set_scissor_rect(struct wined3d_cs *cs, const void *data)
860 const struct wined3d_cs_set_scissor_rect *op = data;
862 cs->state.scissor_rect = op->rect;
863 device_invalidate_state(cs->device, STATE_SCISSORRECT);
866 void wined3d_cs_emit_set_scissor_rect(struct wined3d_cs *cs, const RECT *rect)
868 struct wined3d_cs_set_scissor_rect *op;
870 op = cs->ops->require_space(cs, sizeof(*op));
871 op->opcode = WINED3D_CS_OP_SET_SCISSOR_RECT;
872 op->rect = *rect;
874 cs->ops->submit(cs);
877 static void wined3d_cs_exec_set_rendertarget_view(struct wined3d_cs *cs, const void *data)
879 const struct wined3d_cs_set_rendertarget_view *op = data;
881 cs->state.fb->render_targets[op->view_idx] = op->view;
882 device_invalidate_state(cs->device, STATE_FRAMEBUFFER);
885 void wined3d_cs_emit_set_rendertarget_view(struct wined3d_cs *cs, unsigned int view_idx,
886 struct wined3d_rendertarget_view *view)
888 struct wined3d_cs_set_rendertarget_view *op;
890 op = cs->ops->require_space(cs, sizeof(*op));
891 op->opcode = WINED3D_CS_OP_SET_RENDERTARGET_VIEW;
892 op->view_idx = view_idx;
893 op->view = view;
895 cs->ops->submit(cs);
898 static void wined3d_cs_exec_set_depth_stencil_view(struct wined3d_cs *cs, const void *data)
900 const struct wined3d_cs_set_depth_stencil_view *op = data;
901 struct wined3d_device *device = cs->device;
902 struct wined3d_rendertarget_view *prev;
904 if ((prev = cs->state.fb->depth_stencil))
906 struct wined3d_surface *prev_surface = wined3d_rendertarget_view_get_surface(prev);
908 if (prev_surface && (device->swapchains[0]->desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
909 || prev_surface->container->flags & WINED3D_TEXTURE_DISCARD))
911 wined3d_texture_validate_location(prev_surface->container,
912 prev->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
916 cs->fb.depth_stencil = op->view;
918 if (!prev != !op->view)
920 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
921 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
922 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
923 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
924 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
926 else if (prev && (prev->format_flags & WINED3DFMT_FLAG_FLOAT)
927 != (op->view->format_flags & WINED3DFMT_FLAG_FLOAT))
929 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
932 device_invalidate_state(device, STATE_FRAMEBUFFER);
935 void wined3d_cs_emit_set_depth_stencil_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view)
937 struct wined3d_cs_set_depth_stencil_view *op;
939 op = cs->ops->require_space(cs, sizeof(*op));
940 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW;
941 op->view = view;
943 cs->ops->submit(cs);
946 static void wined3d_cs_exec_set_vertex_declaration(struct wined3d_cs *cs, const void *data)
948 const struct wined3d_cs_set_vertex_declaration *op = data;
950 cs->state.vertex_declaration = op->declaration;
951 device_invalidate_state(cs->device, STATE_VDECL);
954 void wined3d_cs_emit_set_vertex_declaration(struct wined3d_cs *cs, struct wined3d_vertex_declaration *declaration)
956 struct wined3d_cs_set_vertex_declaration *op;
958 op = cs->ops->require_space(cs, sizeof(*op));
959 op->opcode = WINED3D_CS_OP_SET_VERTEX_DECLARATION;
960 op->declaration = declaration;
962 cs->ops->submit(cs);
965 static void wined3d_cs_exec_set_stream_source(struct wined3d_cs *cs, const void *data)
967 const struct wined3d_cs_set_stream_source *op = data;
968 struct wined3d_stream_state *stream;
969 struct wined3d_buffer *prev;
971 stream = &cs->state.streams[op->stream_idx];
972 prev = stream->buffer;
973 stream->buffer = op->buffer;
974 stream->offset = op->offset;
975 stream->stride = op->stride;
977 if (op->buffer)
978 InterlockedIncrement(&op->buffer->resource.bind_count);
979 if (prev)
980 InterlockedDecrement(&prev->resource.bind_count);
982 device_invalidate_state(cs->device, STATE_STREAMSRC);
985 void wined3d_cs_emit_set_stream_source(struct wined3d_cs *cs, UINT stream_idx,
986 struct wined3d_buffer *buffer, UINT offset, UINT stride)
988 struct wined3d_cs_set_stream_source *op;
990 op = cs->ops->require_space(cs, sizeof(*op));
991 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE;
992 op->stream_idx = stream_idx;
993 op->buffer = buffer;
994 op->offset = offset;
995 op->stride = stride;
997 cs->ops->submit(cs);
1000 static void wined3d_cs_exec_set_stream_source_freq(struct wined3d_cs *cs, const void *data)
1002 const struct wined3d_cs_set_stream_source_freq *op = data;
1003 struct wined3d_stream_state *stream;
1005 stream = &cs->state.streams[op->stream_idx];
1006 stream->frequency = op->frequency;
1007 stream->flags = op->flags;
1009 device_invalidate_state(cs->device, STATE_STREAMSRC);
1012 void wined3d_cs_emit_set_stream_source_freq(struct wined3d_cs *cs, UINT stream_idx, UINT frequency, UINT flags)
1014 struct wined3d_cs_set_stream_source_freq *op;
1016 op = cs->ops->require_space(cs, sizeof(*op));
1017 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ;
1018 op->stream_idx = stream_idx;
1019 op->frequency = frequency;
1020 op->flags = flags;
1022 cs->ops->submit(cs);
1025 static void wined3d_cs_exec_set_stream_output(struct wined3d_cs *cs, const void *data)
1027 const struct wined3d_cs_set_stream_output *op = data;
1028 struct wined3d_stream_output *stream;
1029 struct wined3d_buffer *prev;
1031 stream = &cs->state.stream_output[op->stream_idx];
1032 prev = stream->buffer;
1033 stream->buffer = op->buffer;
1034 stream->offset = op->offset;
1036 if (op->buffer)
1037 InterlockedIncrement(&op->buffer->resource.bind_count);
1038 if (prev)
1039 InterlockedDecrement(&prev->resource.bind_count);
1041 device_invalidate_state(cs->device, STATE_STREAM_OUTPUT);
1044 void wined3d_cs_emit_set_stream_output(struct wined3d_cs *cs, UINT stream_idx,
1045 struct wined3d_buffer *buffer, UINT offset)
1047 struct wined3d_cs_set_stream_output *op;
1049 op = cs->ops->require_space(cs, sizeof(*op));
1050 op->opcode = WINED3D_CS_OP_SET_STREAM_OUTPUT;
1051 op->stream_idx = stream_idx;
1052 op->buffer = buffer;
1053 op->offset = offset;
1055 cs->ops->submit(cs);
1058 static void wined3d_cs_exec_set_index_buffer(struct wined3d_cs *cs, const void *data)
1060 const struct wined3d_cs_set_index_buffer *op = data;
1061 struct wined3d_buffer *prev;
1063 prev = cs->state.index_buffer;
1064 cs->state.index_buffer = op->buffer;
1065 cs->state.index_format = op->format_id;
1066 cs->state.index_offset = op->offset;
1068 if (op->buffer)
1069 InterlockedIncrement(&op->buffer->resource.bind_count);
1070 if (prev)
1071 InterlockedDecrement(&prev->resource.bind_count);
1073 device_invalidate_state(cs->device, STATE_INDEXBUFFER);
1076 void wined3d_cs_emit_set_index_buffer(struct wined3d_cs *cs, struct wined3d_buffer *buffer,
1077 enum wined3d_format_id format_id, unsigned int offset)
1079 struct wined3d_cs_set_index_buffer *op;
1081 op = cs->ops->require_space(cs, sizeof(*op));
1082 op->opcode = WINED3D_CS_OP_SET_INDEX_BUFFER;
1083 op->buffer = buffer;
1084 op->format_id = format_id;
1085 op->offset = offset;
1087 cs->ops->submit(cs);
1090 static void wined3d_cs_exec_set_constant_buffer(struct wined3d_cs *cs, const void *data)
1092 const struct wined3d_cs_set_constant_buffer *op = data;
1093 struct wined3d_buffer *prev;
1095 prev = cs->state.cb[op->type][op->cb_idx];
1096 cs->state.cb[op->type][op->cb_idx] = op->buffer;
1098 if (op->buffer)
1099 InterlockedIncrement(&op->buffer->resource.bind_count);
1100 if (prev)
1101 InterlockedDecrement(&prev->resource.bind_count);
1103 device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(op->type));
1106 void wined3d_cs_emit_set_constant_buffer(struct wined3d_cs *cs, enum wined3d_shader_type type,
1107 UINT cb_idx, struct wined3d_buffer *buffer)
1109 struct wined3d_cs_set_constant_buffer *op;
1111 op = cs->ops->require_space(cs, sizeof(*op));
1112 op->opcode = WINED3D_CS_OP_SET_CONSTANT_BUFFER;
1113 op->type = type;
1114 op->cb_idx = cb_idx;
1115 op->buffer = buffer;
1117 cs->ops->submit(cs);
1120 static void wined3d_cs_exec_set_texture(struct wined3d_cs *cs, const void *data)
1122 const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
1123 const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
1124 const struct wined3d_cs_set_texture *op = data;
1125 struct wined3d_texture *prev;
1126 BOOL old_use_color_key = FALSE, new_use_color_key = FALSE;
1128 prev = cs->state.textures[op->stage];
1129 cs->state.textures[op->stage] = op->texture;
1131 if (op->texture)
1133 const struct wined3d_format *new_format = op->texture->resource.format;
1134 const struct wined3d_format *old_format = prev ? prev->resource.format : NULL;
1135 unsigned int old_fmt_flags = prev ? prev->resource.format_flags : 0;
1136 unsigned int new_fmt_flags = op->texture->resource.format_flags;
1138 if (InterlockedIncrement(&op->texture->resource.bind_count) == 1)
1139 op->texture->sampler = op->stage;
1141 if (!prev || op->texture->target != prev->target
1142 || (!is_same_fixup(new_format->color_fixup, old_format->color_fixup)
1143 && !(can_use_texture_swizzle(gl_info, new_format) && can_use_texture_swizzle(gl_info, old_format)))
1144 || (new_fmt_flags & WINED3DFMT_FLAG_SHADOW) != (old_fmt_flags & WINED3DFMT_FLAG_SHADOW))
1145 device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1147 if (!prev && op->stage < d3d_info->limits.ffp_blend_stages)
1149 /* The source arguments for color and alpha ops have different
1150 * meanings when a NULL texture is bound, so the COLOR_OP and
1151 * ALPHA_OP have to be dirtified. */
1152 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1153 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1156 if (!op->stage && op->texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1157 new_use_color_key = TRUE;
1160 if (prev)
1162 if (InterlockedDecrement(&prev->resource.bind_count) && prev->sampler == op->stage)
1164 unsigned int i;
1166 /* Search for other stages the texture is bound to. Shouldn't
1167 * happen if applications bind textures to a single stage only. */
1168 TRACE("Searching for other stages the texture is bound to.\n");
1169 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
1171 if (cs->state.textures[i] == prev)
1173 TRACE("Texture is also bound to stage %u.\n", i);
1174 prev->sampler = i;
1175 break;
1180 if (!op->texture && op->stage < d3d_info->limits.ffp_blend_stages)
1182 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1183 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1186 if (!op->stage && prev->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1187 old_use_color_key = TRUE;
1190 device_invalidate_state(cs->device, STATE_SAMPLER(op->stage));
1192 if (new_use_color_key != old_use_color_key)
1193 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1195 if (new_use_color_key)
1196 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1199 void wined3d_cs_emit_set_texture(struct wined3d_cs *cs, UINT stage, struct wined3d_texture *texture)
1201 struct wined3d_cs_set_texture *op;
1203 op = cs->ops->require_space(cs, sizeof(*op));
1204 op->opcode = WINED3D_CS_OP_SET_TEXTURE;
1205 op->stage = stage;
1206 op->texture = texture;
1208 cs->ops->submit(cs);
1211 static void wined3d_cs_exec_set_shader_resource_view(struct wined3d_cs *cs, const void *data)
1213 const struct wined3d_cs_set_shader_resource_view *op = data;
1214 struct wined3d_shader_resource_view *prev;
1216 prev = cs->state.shader_resource_view[op->type][op->view_idx];
1217 cs->state.shader_resource_view[op->type][op->view_idx] = op->view;
1219 if (op->view)
1220 InterlockedIncrement(&op->view->resource->bind_count);
1221 if (prev)
1222 InterlockedDecrement(&prev->resource->bind_count);
1224 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1225 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1226 else
1227 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1230 void wined3d_cs_emit_set_shader_resource_view(struct wined3d_cs *cs, enum wined3d_shader_type type,
1231 UINT view_idx, struct wined3d_shader_resource_view *view)
1233 struct wined3d_cs_set_shader_resource_view *op;
1235 op = cs->ops->require_space(cs, sizeof(*op));
1236 op->opcode = WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW;
1237 op->type = type;
1238 op->view_idx = view_idx;
1239 op->view = view;
1241 cs->ops->submit(cs);
1244 static void wined3d_cs_exec_set_unordered_access_view(struct wined3d_cs *cs, const void *data)
1246 const struct wined3d_cs_set_unordered_access_view *op = data;
1247 struct wined3d_unordered_access_view *prev;
1249 prev = cs->state.unordered_access_view[op->pipeline][op->view_idx];
1250 cs->state.unordered_access_view[op->pipeline][op->view_idx] = op->view;
1252 if (op->view)
1253 InterlockedIncrement(&op->view->resource->bind_count);
1254 if (prev)
1255 InterlockedDecrement(&prev->resource->bind_count);
1257 device_invalidate_state(cs->device, STATE_UNORDERED_ACCESS_VIEW_BINDING(op->pipeline));
1260 void wined3d_cs_emit_set_unordered_access_view(struct wined3d_cs *cs, enum wined3d_pipeline pipeline,
1261 unsigned int view_idx, struct wined3d_unordered_access_view *view)
1263 struct wined3d_cs_set_unordered_access_view *op;
1265 op = cs->ops->require_space(cs, sizeof(*op));
1266 op->opcode = WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW;
1267 op->pipeline = pipeline;
1268 op->view_idx = view_idx;
1269 op->view = view;
1271 cs->ops->submit(cs);
1274 static void wined3d_cs_exec_set_sampler(struct wined3d_cs *cs, const void *data)
1276 const struct wined3d_cs_set_sampler *op = data;
1278 cs->state.sampler[op->type][op->sampler_idx] = op->sampler;
1279 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1280 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1281 else
1282 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1285 void wined3d_cs_emit_set_sampler(struct wined3d_cs *cs, enum wined3d_shader_type type,
1286 UINT sampler_idx, struct wined3d_sampler *sampler)
1288 struct wined3d_cs_set_sampler *op;
1290 op = cs->ops->require_space(cs, sizeof(*op));
1291 op->opcode = WINED3D_CS_OP_SET_SAMPLER;
1292 op->type = type;
1293 op->sampler_idx = sampler_idx;
1294 op->sampler = sampler;
1296 cs->ops->submit(cs);
1299 static void wined3d_cs_exec_set_shader(struct wined3d_cs *cs, const void *data)
1301 const struct wined3d_cs_set_shader *op = data;
1303 cs->state.shader[op->type] = op->shader;
1304 device_invalidate_state(cs->device, STATE_SHADER(op->type));
1305 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1306 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1307 else
1308 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1311 void wined3d_cs_emit_set_shader(struct wined3d_cs *cs, enum wined3d_shader_type type, struct wined3d_shader *shader)
1313 struct wined3d_cs_set_shader *op;
1315 op = cs->ops->require_space(cs, sizeof(*op));
1316 op->opcode = WINED3D_CS_OP_SET_SHADER;
1317 op->type = type;
1318 op->shader = shader;
1320 cs->ops->submit(cs);
1323 static void wined3d_cs_exec_set_rasterizer_state(struct wined3d_cs *cs, const void *data)
1325 const struct wined3d_cs_set_rasterizer_state *op = data;
1327 cs->state.rasterizer_state = op->state;
1328 device_invalidate_state(cs->device, STATE_FRONTFACE);
1331 void wined3d_cs_emit_set_rasterizer_state(struct wined3d_cs *cs,
1332 struct wined3d_rasterizer_state *rasterizer_state)
1334 struct wined3d_cs_set_rasterizer_state *op;
1336 op = cs->ops->require_space(cs, sizeof(*op));
1337 op->opcode = WINED3D_CS_OP_SET_RASTERIZER_STATE;
1338 op->state = rasterizer_state;
1340 cs->ops->submit(cs);
1343 static void wined3d_cs_exec_set_render_state(struct wined3d_cs *cs, const void *data)
1345 const struct wined3d_cs_set_render_state *op = data;
1347 cs->state.render_states[op->state] = op->value;
1348 device_invalidate_state(cs->device, STATE_RENDER(op->state));
1351 void wined3d_cs_emit_set_render_state(struct wined3d_cs *cs, enum wined3d_render_state state, DWORD value)
1353 struct wined3d_cs_set_render_state *op;
1355 op = cs->ops->require_space(cs, sizeof(*op));
1356 op->opcode = WINED3D_CS_OP_SET_RENDER_STATE;
1357 op->state = state;
1358 op->value = value;
1360 cs->ops->submit(cs);
1363 static void wined3d_cs_exec_set_texture_state(struct wined3d_cs *cs, const void *data)
1365 const struct wined3d_cs_set_texture_state *op = data;
1367 cs->state.texture_states[op->stage][op->state] = op->value;
1368 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, op->state));
1371 void wined3d_cs_emit_set_texture_state(struct wined3d_cs *cs, UINT stage,
1372 enum wined3d_texture_stage_state state, DWORD value)
1374 struct wined3d_cs_set_texture_state *op;
1376 op = cs->ops->require_space(cs, sizeof(*op));
1377 op->opcode = WINED3D_CS_OP_SET_TEXTURE_STATE;
1378 op->stage = stage;
1379 op->state = state;
1380 op->value = value;
1382 cs->ops->submit(cs);
1385 static void wined3d_cs_exec_set_sampler_state(struct wined3d_cs *cs, const void *data)
1387 const struct wined3d_cs_set_sampler_state *op = data;
1389 cs->state.sampler_states[op->sampler_idx][op->state] = op->value;
1390 device_invalidate_state(cs->device, STATE_SAMPLER(op->sampler_idx));
1393 void wined3d_cs_emit_set_sampler_state(struct wined3d_cs *cs, UINT sampler_idx,
1394 enum wined3d_sampler_state state, DWORD value)
1396 struct wined3d_cs_set_sampler_state *op;
1398 op = cs->ops->require_space(cs, sizeof(*op));
1399 op->opcode = WINED3D_CS_OP_SET_SAMPLER_STATE;
1400 op->sampler_idx = sampler_idx;
1401 op->state = state;
1402 op->value = value;
1404 cs->ops->submit(cs);
1407 static void wined3d_cs_exec_set_transform(struct wined3d_cs *cs, const void *data)
1409 const struct wined3d_cs_set_transform *op = data;
1411 cs->state.transforms[op->state] = op->matrix;
1412 if (op->state < WINED3D_TS_WORLD_MATRIX(cs->device->adapter->d3d_info.limits.ffp_vertex_blend_matrices))
1413 device_invalidate_state(cs->device, STATE_TRANSFORM(op->state));
1416 void wined3d_cs_emit_set_transform(struct wined3d_cs *cs, enum wined3d_transform_state state,
1417 const struct wined3d_matrix *matrix)
1419 struct wined3d_cs_set_transform *op;
1421 op = cs->ops->require_space(cs, sizeof(*op));
1422 op->opcode = WINED3D_CS_OP_SET_TRANSFORM;
1423 op->state = state;
1424 op->matrix = *matrix;
1426 cs->ops->submit(cs);
1429 static void wined3d_cs_exec_set_clip_plane(struct wined3d_cs *cs, const void *data)
1431 const struct wined3d_cs_set_clip_plane *op = data;
1433 cs->state.clip_planes[op->plane_idx] = op->plane;
1434 device_invalidate_state(cs->device, STATE_CLIPPLANE(op->plane_idx));
1437 void wined3d_cs_emit_set_clip_plane(struct wined3d_cs *cs, UINT plane_idx, const struct wined3d_vec4 *plane)
1439 struct wined3d_cs_set_clip_plane *op;
1441 op = cs->ops->require_space(cs, sizeof(*op));
1442 op->opcode = WINED3D_CS_OP_SET_CLIP_PLANE;
1443 op->plane_idx = plane_idx;
1444 op->plane = *plane;
1446 cs->ops->submit(cs);
1449 static void wined3d_cs_exec_set_color_key(struct wined3d_cs *cs, const void *data)
1451 const struct wined3d_cs_set_color_key *op = data;
1452 struct wined3d_texture *texture = op->texture;
1454 if (op->set)
1456 switch (op->flags)
1458 case WINED3D_CKEY_DST_BLT:
1459 texture->async.dst_blt_color_key = op->color_key;
1460 texture->async.color_key_flags |= WINED3D_CKEY_DST_BLT;
1461 break;
1463 case WINED3D_CKEY_DST_OVERLAY:
1464 texture->async.dst_overlay_color_key = op->color_key;
1465 texture->async.color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
1466 break;
1468 case WINED3D_CKEY_SRC_BLT:
1469 if (texture == cs->state.textures[0])
1471 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1472 if (!(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1473 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1476 texture->async.src_blt_color_key = op->color_key;
1477 texture->async.color_key_flags |= WINED3D_CKEY_SRC_BLT;
1478 break;
1480 case WINED3D_CKEY_SRC_OVERLAY:
1481 texture->async.src_overlay_color_key = op->color_key;
1482 texture->async.color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
1483 break;
1486 else
1488 switch (op->flags)
1490 case WINED3D_CKEY_DST_BLT:
1491 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_BLT;
1492 break;
1494 case WINED3D_CKEY_DST_OVERLAY:
1495 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
1496 break;
1498 case WINED3D_CKEY_SRC_BLT:
1499 if (texture == cs->state.textures[0] && texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1500 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1502 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
1503 break;
1505 case WINED3D_CKEY_SRC_OVERLAY:
1506 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
1507 break;
1512 void wined3d_cs_emit_set_color_key(struct wined3d_cs *cs, struct wined3d_texture *texture,
1513 WORD flags, const struct wined3d_color_key *color_key)
1515 struct wined3d_cs_set_color_key *op;
1517 op = cs->ops->require_space(cs, sizeof(*op));
1518 op->opcode = WINED3D_CS_OP_SET_COLOR_KEY;
1519 op->texture = texture;
1520 op->flags = flags;
1521 if (color_key)
1523 op->color_key = *color_key;
1524 op->set = 1;
1526 else
1527 op->set = 0;
1529 cs->ops->submit(cs);
1532 static void wined3d_cs_exec_set_material(struct wined3d_cs *cs, const void *data)
1534 const struct wined3d_cs_set_material *op = data;
1536 cs->state.material = op->material;
1537 device_invalidate_state(cs->device, STATE_MATERIAL);
1540 void wined3d_cs_emit_set_material(struct wined3d_cs *cs, const struct wined3d_material *material)
1542 struct wined3d_cs_set_material *op;
1544 op = cs->ops->require_space(cs, sizeof(*op));
1545 op->opcode = WINED3D_CS_OP_SET_MATERIAL;
1546 op->material = *material;
1548 cs->ops->submit(cs);
1551 static void wined3d_cs_exec_set_light(struct wined3d_cs *cs, const void *data)
1553 const struct wined3d_cs_set_light *op = data;
1554 struct wined3d_light_info *light_info;
1555 unsigned int light_idx, hash_idx;
1557 light_idx = op->light.OriginalIndex;
1559 if (!(light_info = wined3d_state_get_light(&cs->state, light_idx)))
1561 TRACE("Adding new light.\n");
1562 if (!(light_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*light_info))))
1564 ERR("Failed to allocate light info.\n");
1565 return;
1568 hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1569 list_add_head(&cs->state.light_map[hash_idx], &light_info->entry);
1570 light_info->glIndex = -1;
1571 light_info->OriginalIndex = light_idx;
1574 if (light_info->glIndex != -1)
1576 if (light_info->OriginalParms.type != op->light.OriginalParms.type)
1577 device_invalidate_state(cs->device, STATE_LIGHT_TYPE);
1578 device_invalidate_state(cs->device, STATE_ACTIVELIGHT(light_info->glIndex));
1581 light_info->OriginalParms = op->light.OriginalParms;
1582 light_info->position = op->light.position;
1583 light_info->direction = op->light.direction;
1584 light_info->exponent = op->light.exponent;
1585 light_info->cutoff = op->light.cutoff;
1588 void wined3d_cs_emit_set_light(struct wined3d_cs *cs, const struct wined3d_light_info *light)
1590 struct wined3d_cs_set_light *op;
1592 op = cs->ops->require_space(cs, sizeof(*op));
1593 op->opcode = WINED3D_CS_OP_SET_LIGHT;
1594 op->light = *light;
1596 cs->ops->submit(cs);
1599 static void wined3d_cs_exec_set_light_enable(struct wined3d_cs *cs, const void *data)
1601 const struct wined3d_cs_set_light_enable *op = data;
1602 struct wined3d_device *device = cs->device;
1603 struct wined3d_light_info *light_info;
1604 int prev_idx;
1606 if (!(light_info = wined3d_state_get_light(&cs->state, op->idx)))
1608 ERR("Light doesn't exist.\n");
1609 return;
1612 prev_idx = light_info->glIndex;
1613 wined3d_state_enable_light(&cs->state, &device->adapter->d3d_info, light_info, op->enable);
1614 if (light_info->glIndex != prev_idx)
1616 device_invalidate_state(device, STATE_LIGHT_TYPE);
1617 device_invalidate_state(device, STATE_ACTIVELIGHT(op->enable ? light_info->glIndex : prev_idx));
1621 void wined3d_cs_emit_set_light_enable(struct wined3d_cs *cs, unsigned int idx, BOOL enable)
1623 struct wined3d_cs_set_light_enable *op;
1625 op = cs->ops->require_space(cs, sizeof(*op));
1626 op->opcode = WINED3D_CS_OP_SET_LIGHT_ENABLE;
1627 op->idx = idx;
1628 op->enable = enable;
1630 cs->ops->submit(cs);
1633 static const struct
1635 size_t offset;
1636 size_t size;
1637 DWORD mask;
1639 wined3d_cs_push_constant_info[] =
1641 /* WINED3D_PUSH_CONSTANTS_VS_F */
1642 {FIELD_OFFSET(struct wined3d_state, vs_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_VS_F},
1643 /* WINED3D_PUSH_CONSTANTS_PS_F */
1644 {FIELD_OFFSET(struct wined3d_state, ps_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_PS_F},
1645 /* WINED3D_PUSH_CONSTANTS_VS_I */
1646 {FIELD_OFFSET(struct wined3d_state, vs_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_VS_I},
1647 /* WINED3D_PUSH_CONSTANTS_PS_I */
1648 {FIELD_OFFSET(struct wined3d_state, ps_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_PS_I},
1649 /* WINED3D_PUSH_CONSTANTS_VS_B */
1650 {FIELD_OFFSET(struct wined3d_state, vs_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_VS_B},
1651 /* WINED3D_PUSH_CONSTANTS_PS_B */
1652 {FIELD_OFFSET(struct wined3d_state, ps_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_PS_B},
1655 static void wined3d_cs_st_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1656 unsigned int start_idx, unsigned int count, const void *constants)
1658 struct wined3d_device *device = cs->device;
1659 unsigned int context_count;
1660 unsigned int i;
1661 size_t offset;
1663 if (p == WINED3D_PUSH_CONSTANTS_VS_F)
1664 device->shader_backend->shader_update_float_vertex_constants(device, start_idx, count);
1665 else if (p == WINED3D_PUSH_CONSTANTS_PS_F)
1666 device->shader_backend->shader_update_float_pixel_constants(device, start_idx, count);
1668 offset = wined3d_cs_push_constant_info[p].offset + start_idx * wined3d_cs_push_constant_info[p].size;
1669 memcpy((BYTE *)&cs->state + offset, constants, count * wined3d_cs_push_constant_info[p].size);
1670 for (i = 0, context_count = device->context_count; i < context_count; ++i)
1672 device->contexts[i]->constant_update_mask |= wined3d_cs_push_constant_info[p].mask;
1676 static void wined3d_cs_exec_push_constants(struct wined3d_cs *cs, const void *data)
1678 const struct wined3d_cs_push_constants *op = data;
1680 wined3d_cs_st_push_constants(cs, op->type, op->start_idx, op->count, op->constants);
1683 static void wined3d_cs_mt_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1684 unsigned int start_idx, unsigned int count, const void *constants)
1686 struct wined3d_cs_push_constants *op;
1687 size_t size;
1689 size = count * wined3d_cs_push_constant_info[p].size;
1690 op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_push_constants, constants[size]));
1691 op->opcode = WINED3D_CS_OP_PUSH_CONSTANTS;
1692 op->type = p;
1693 op->start_idx = start_idx;
1694 op->count = count;
1695 memcpy(op->constants, constants, size);
1697 cs->ops->submit(cs);
1700 static void wined3d_cs_exec_reset_state(struct wined3d_cs *cs, const void *data)
1702 struct wined3d_adapter *adapter = cs->device->adapter;
1704 state_cleanup(&cs->state);
1705 memset(&cs->state, 0, sizeof(cs->state));
1706 state_init(&cs->state, &cs->fb, &adapter->gl_info, &adapter->d3d_info,
1707 WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
1710 void wined3d_cs_emit_reset_state(struct wined3d_cs *cs)
1712 struct wined3d_cs_reset_state *op;
1714 op = cs->ops->require_space(cs, sizeof(*op));
1715 op->opcode = WINED3D_CS_OP_RESET_STATE;
1717 cs->ops->submit(cs);
1720 static void wined3d_cs_exec_callback(struct wined3d_cs *cs, const void *data)
1722 const struct wined3d_cs_callback *op = data;
1724 op->callback(op->object);
1727 static void wined3d_cs_emit_callback(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1729 struct wined3d_cs_callback *op;
1731 op = cs->ops->require_space(cs, sizeof(*op));
1732 op->opcode = WINED3D_CS_OP_CALLBACK;
1733 op->callback = callback;
1734 op->object = object;
1736 cs->ops->submit(cs);
1739 void wined3d_cs_destroy_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1741 wined3d_cs_emit_callback(cs, callback, object);
1744 void wined3d_cs_init_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1746 wined3d_cs_emit_callback(cs, callback, object);
1749 static void wined3d_cs_exec_query_issue(struct wined3d_cs *cs, const void *data)
1751 const struct wined3d_cs_query_issue *op = data;
1752 struct wined3d_query *query = op->query;
1753 BOOL poll;
1755 poll = query->query_ops->query_issue(query, op->flags);
1757 if (!cs->thread)
1758 return;
1760 if (poll && list_empty(&query->poll_list_entry))
1762 list_add_tail(&cs->query_poll_list, &query->poll_list_entry);
1763 return;
1766 /* This can happen if occlusion queries are restarted. This discards the
1767 * old result, since polling it could result in a GL error. */
1768 if ((op->flags & WINED3DISSUE_BEGIN) && !poll && !list_empty(&query->poll_list_entry))
1770 list_remove(&query->poll_list_entry);
1771 list_init(&query->poll_list_entry);
1772 InterlockedIncrement(&query->counter_retrieved);
1773 return;
1776 /* This can happen when an occlusion query is ended without being started,
1777 * in which case we don't want to poll, but still have to counter-balance
1778 * the increment of the main counter.
1780 * This can also happen if an event query is re-issued before the first
1781 * fence was reached. In this case the query is already in the list and
1782 * the poll function will check the new fence. We have to counter-balance
1783 * the discarded increment. */
1784 if (op->flags & WINED3DISSUE_END)
1785 InterlockedIncrement(&query->counter_retrieved);
1788 void wined3d_cs_emit_query_issue(struct wined3d_cs *cs, struct wined3d_query *query, DWORD flags)
1790 struct wined3d_cs_query_issue *op;
1792 op = cs->ops->require_space(cs, sizeof(*op));
1793 op->opcode = WINED3D_CS_OP_QUERY_ISSUE;
1794 op->query = query;
1795 op->flags = flags;
1797 cs->ops->submit(cs);
1800 static void wined3d_cs_exec_preload_resource(struct wined3d_cs *cs, const void *data)
1802 const struct wined3d_cs_preload_resource *op = data;
1803 struct wined3d_resource *resource = op->resource;
1805 resource->resource_ops->resource_preload(resource);
1806 wined3d_resource_release(resource);
1809 void wined3d_cs_emit_preload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
1811 struct wined3d_cs_preload_resource *op;
1813 op = cs->ops->require_space(cs, sizeof(*op));
1814 op->opcode = WINED3D_CS_OP_PRELOAD_RESOURCE;
1815 op->resource = resource;
1817 wined3d_resource_acquire(resource);
1819 cs->ops->submit(cs);
1822 static void wined3d_cs_exec_unload_resource(struct wined3d_cs *cs, const void *data)
1824 const struct wined3d_cs_unload_resource *op = data;
1825 struct wined3d_resource *resource = op->resource;
1827 resource->resource_ops->resource_unload(resource);
1828 wined3d_resource_release(resource);
1831 void wined3d_cs_emit_unload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
1833 struct wined3d_cs_unload_resource *op;
1835 op = cs->ops->require_space(cs, sizeof(*op));
1836 op->opcode = WINED3D_CS_OP_UNLOAD_RESOURCE;
1837 op->resource = resource;
1839 wined3d_resource_acquire(resource);
1841 cs->ops->submit(cs);
1844 static void wined3d_cs_exec_map(struct wined3d_cs *cs, const void *data)
1846 const struct wined3d_cs_map *op = data;
1847 struct wined3d_resource *resource = op->resource;
1849 *op->hr = resource->resource_ops->resource_sub_resource_map(resource,
1850 op->sub_resource_idx, op->map_desc, op->box, op->flags);
1853 HRESULT wined3d_cs_map(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx,
1854 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
1856 struct wined3d_cs_map *op;
1857 HRESULT hr;
1859 op = cs->ops->require_space(cs, sizeof(*op));
1860 op->opcode = WINED3D_CS_OP_MAP;
1861 op->resource = resource;
1862 op->sub_resource_idx = sub_resource_idx;
1863 op->map_desc = map_desc;
1864 op->box = box;
1865 op->flags = flags;
1866 op->hr = &hr;
1868 cs->ops->submit(cs);
1870 return hr;
1873 static void wined3d_cs_exec_unmap(struct wined3d_cs *cs, const void *data)
1875 const struct wined3d_cs_unmap *op = data;
1876 struct wined3d_resource *resource = op->resource;
1878 *op->hr = resource->resource_ops->resource_sub_resource_unmap(resource, op->sub_resource_idx);
1881 HRESULT wined3d_cs_unmap(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx)
1883 struct wined3d_cs_unmap *op;
1884 HRESULT hr;
1886 op = cs->ops->require_space(cs, sizeof(*op));
1887 op->opcode = WINED3D_CS_OP_UNMAP;
1888 op->resource = resource;
1889 op->sub_resource_idx = sub_resource_idx;
1890 op->hr = &hr;
1892 cs->ops->submit(cs);
1894 return hr;
1897 static void wined3d_cs_exec_blt_sub_resource(struct wined3d_cs *cs, const void *data)
1899 const struct wined3d_cs_blt_sub_resource *op = data;
1901 if (op->dst_resource->type == WINED3D_RTYPE_BUFFER)
1903 if (FAILED(wined3d_buffer_copy(buffer_from_resource(op->dst_resource), op->dst_box.left,
1904 buffer_from_resource(op->src_resource), op->src_box.left,
1905 op->src_box.right - op->src_box.left)))
1906 ERR("Failed to copy buffer.\n");
1908 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_2D)
1910 struct wined3d_surface *dst_surface, *src_surface;
1911 struct wined3d_texture *dst_texture, *src_texture;
1912 RECT dst_rect, src_rect;
1914 dst_texture = texture_from_resource(op->dst_resource);
1915 src_texture = texture_from_resource(op->src_resource);
1916 dst_surface = dst_texture->sub_resources[op->dst_sub_resource_idx].u.surface;
1917 src_surface = src_texture->sub_resources[op->src_sub_resource_idx].u.surface;
1918 SetRect(&dst_rect, op->dst_box.left, op->dst_box.top, op->dst_box.right, op->dst_box.bottom);
1919 SetRect(&src_rect, op->src_box.left, op->src_box.top, op->src_box.right, op->src_box.bottom);
1921 if (FAILED(wined3d_surface_blt(dst_surface, &dst_rect, src_surface,
1922 &src_rect, op->flags, &op->fx, op->filter)))
1923 FIXME("Blit failed.\n");
1925 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_3D)
1927 struct wined3d_texture *src_texture, *dst_texture;
1928 unsigned int level, update_w, update_h, update_d;
1929 unsigned int row_pitch, slice_pitch;
1930 struct wined3d_context *context;
1931 struct wined3d_bo_address addr;
1933 if (op->flags)
1935 FIXME("Flags %#x not implemented for %s resources.\n",
1936 op->flags, debug_d3dresourcetype(op->dst_resource->type));
1937 return;
1940 if (op->src_resource->format != op->dst_resource->format)
1942 FIXME("Format conversion not implemented for %s resources.\n",
1943 debug_d3dresourcetype(op->dst_resource->type));
1944 return;
1947 update_w = op->dst_box.right - op->dst_box.left;
1948 update_h = op->dst_box.bottom - op->dst_box.top;
1949 update_d = op->dst_box.back - op->dst_box.front;
1950 if (op->src_box.right - op->src_box.left != update_w
1951 || op->src_box.bottom - op->src_box.top != update_h
1952 || op->src_box.back - op->src_box.front != update_d)
1954 FIXME("Stretching not implemented for %s resources.\n",
1955 debug_d3dresourcetype(op->dst_resource->type));
1956 return;
1959 if (op->src_box.left || op->src_box.top || op->src_box.front)
1961 FIXME("Source box %s not supported for %s resources.\n",
1962 debug_box(&op->src_box), debug_d3dresourcetype(op->dst_resource->type));
1963 return;
1966 dst_texture = texture_from_resource(op->dst_resource);
1967 src_texture = texture_from_resource(op->src_resource);
1969 context = context_acquire(cs->device, NULL, 0);
1971 if (!wined3d_texture_load_location(src_texture, op->src_sub_resource_idx,
1972 context, src_texture->resource.map_binding))
1974 ERR("Failed to load source sub-resource into %s.\n",
1975 wined3d_debug_location(src_texture->resource.map_binding));
1976 context_release(context);
1977 return;
1980 level = op->dst_sub_resource_idx % dst_texture->level_count;
1981 if (update_w == wined3d_texture_get_level_width(dst_texture, level)
1982 && update_h == wined3d_texture_get_level_height(dst_texture, level)
1983 && update_d == wined3d_texture_get_level_depth(dst_texture, level))
1985 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
1987 else if (!wined3d_texture_load_location(dst_texture, op->dst_sub_resource_idx,
1988 context, WINED3D_LOCATION_TEXTURE_RGB))
1990 ERR("Failed to load destination sub-resource.\n");
1991 context_release(context);
1992 return;
1995 wined3d_texture_get_memory(src_texture, op->src_sub_resource_idx, &addr, src_texture->resource.map_binding);
1996 wined3d_texture_get_pitch(src_texture, op->src_sub_resource_idx % src_texture->level_count,
1997 &row_pitch, &slice_pitch);
1999 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
2000 wined3d_texture_upload_data(dst_texture, op->dst_sub_resource_idx, context, &op->dst_box,
2001 wined3d_const_bo_address(&addr), row_pitch, slice_pitch);
2002 wined3d_texture_validate_location(dst_texture, op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2003 wined3d_texture_invalidate_location(dst_texture, op->dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2005 context_release(context);
2007 else
2009 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(op->dst_resource->type));
2012 if (op->src_resource)
2013 wined3d_resource_release(op->src_resource);
2014 wined3d_resource_release(op->dst_resource);
2017 void wined3d_cs_emit_blt_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *dst_resource,
2018 unsigned int dst_sub_resource_idx, const struct wined3d_box *dst_box, struct wined3d_resource *src_resource,
2019 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, DWORD flags,
2020 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2022 struct wined3d_cs_blt_sub_resource *op;
2024 op = cs->ops->require_space(cs, sizeof(*op));
2025 op->opcode = WINED3D_CS_OP_BLT_SUB_RESOURCE;
2026 op->dst_resource = dst_resource;
2027 op->dst_sub_resource_idx = dst_sub_resource_idx;
2028 op->dst_box = *dst_box;
2029 op->src_resource = src_resource;
2030 op->src_sub_resource_idx = src_sub_resource_idx;
2031 op->src_box = *src_box;
2032 op->flags = flags;
2033 if (fx)
2034 op->fx = *fx;
2035 op->filter = filter;
2037 wined3d_resource_acquire(dst_resource);
2038 if (src_resource)
2039 wined3d_resource_acquire(src_resource);
2041 cs->ops->submit(cs);
2044 static void wined3d_cs_exec_update_sub_resource(struct wined3d_cs *cs, const void *data)
2046 const struct wined3d_cs_update_sub_resource *op = data;
2047 const struct wined3d_box *box = &op->box;
2048 unsigned int width, height, depth, level;
2049 struct wined3d_const_bo_address addr;
2050 struct wined3d_context *context;
2051 struct wined3d_texture *texture;
2053 if (op->resource->type == WINED3D_RTYPE_BUFFER)
2055 struct wined3d_buffer *buffer = buffer_from_resource(op->resource);
2057 context = context_acquire(op->resource->device, NULL, 0);
2058 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
2060 ERR("Failed to load buffer location.\n");
2061 context_release(context);
2062 goto done;
2065 wined3d_buffer_upload_data(buffer, context, box, op->data.data);
2066 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
2067 context_release(context);
2068 goto done;
2071 texture = wined3d_texture_from_resource(op->resource);
2073 level = op->sub_resource_idx % texture->level_count;
2074 width = wined3d_texture_get_level_width(texture, level);
2075 height = wined3d_texture_get_level_height(texture, level);
2076 depth = wined3d_texture_get_level_depth(texture, level);
2078 addr.buffer_object = 0;
2079 addr.addr = op->data.data;
2081 context = context_acquire(op->resource->device, NULL, 0);
2083 /* Only load the sub-resource for partial updates. */
2084 if (!box->left && !box->top && !box->front
2085 && box->right == width && box->bottom == height && box->back == depth)
2086 wined3d_texture_prepare_texture(texture, context, FALSE);
2087 else
2088 wined3d_texture_load_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2089 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2091 wined3d_texture_upload_data(texture, op->sub_resource_idx, context,
2092 box, &addr, op->data.row_pitch, op->data.slice_pitch);
2094 context_release(context);
2096 wined3d_texture_validate_location(texture, op->sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2097 wined3d_texture_invalidate_location(texture, op->sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2099 done:
2100 wined3d_resource_release(op->resource);
2103 void wined3d_cs_emit_update_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *resource,
2104 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
2105 unsigned int slice_pitch)
2107 struct wined3d_cs_update_sub_resource *op;
2109 op = cs->ops->require_space(cs, sizeof(*op));
2110 op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2111 op->resource = resource;
2112 op->sub_resource_idx = sub_resource_idx;
2113 op->box = *box;
2114 op->data.row_pitch = row_pitch;
2115 op->data.slice_pitch = slice_pitch;
2116 op->data.data = data;
2118 wined3d_resource_acquire(resource);
2120 cs->ops->submit(cs);
2123 static void wined3d_cs_exec_add_dirty_texture_region(struct wined3d_cs *cs, const void *data)
2125 const struct wined3d_cs_add_dirty_texture_region *op = data;
2126 struct wined3d_texture *texture = op->texture;
2127 unsigned int sub_resource_idx, i;
2128 struct wined3d_context *context;
2130 context = context_acquire(cs->device, NULL, 0);
2131 sub_resource_idx = op->layer * texture->level_count;
2132 for (i = 0; i < texture->level_count; ++i, ++sub_resource_idx)
2134 if (wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
2135 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2136 else
2137 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
2139 context_release(context);
2141 wined3d_resource_release(&texture->resource);
2144 void wined3d_cs_emit_add_dirty_texture_region(struct wined3d_cs *cs,
2145 struct wined3d_texture *texture, unsigned int layer)
2147 struct wined3d_cs_add_dirty_texture_region *op;
2149 op = cs->ops->require_space(cs, sizeof(*op));
2150 op->opcode = WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION;
2151 op->texture = texture;
2152 op->layer = layer;
2154 wined3d_resource_acquire(&texture->resource);
2156 cs->ops->submit(cs);
2159 static void wined3d_cs_emit_stop(struct wined3d_cs *cs)
2161 struct wined3d_cs_stop *op;
2163 op = cs->ops->require_space(cs, sizeof(*op));
2164 op->opcode = WINED3D_CS_OP_STOP;
2166 cs->ops->submit(cs);
2169 static void (* const wined3d_cs_op_handlers[])(struct wined3d_cs *cs, const void *data) =
2171 /* WINED3D_CS_OP_NOP */ wined3d_cs_exec_nop,
2172 /* WINED3D_CS_OP_PRESENT */ wined3d_cs_exec_present,
2173 /* WINED3D_CS_OP_CLEAR */ wined3d_cs_exec_clear,
2174 /* WINED3D_CS_OP_DISPATCH */ wined3d_cs_exec_dispatch,
2175 /* WINED3D_CS_OP_DRAW */ wined3d_cs_exec_draw,
2176 /* WINED3D_CS_OP_FLUSH */ wined3d_cs_exec_flush,
2177 /* WINED3D_CS_OP_SET_PREDICATION */ wined3d_cs_exec_set_predication,
2178 /* WINED3D_CS_OP_SET_VIEWPORT */ wined3d_cs_exec_set_viewport,
2179 /* WINED3D_CS_OP_SET_SCISSOR_RECT */ wined3d_cs_exec_set_scissor_rect,
2180 /* WINED3D_CS_OP_SET_RENDERTARGET_VIEW */ wined3d_cs_exec_set_rendertarget_view,
2181 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW */ wined3d_cs_exec_set_depth_stencil_view,
2182 /* WINED3D_CS_OP_SET_VERTEX_DECLARATION */ wined3d_cs_exec_set_vertex_declaration,
2183 /* WINED3D_CS_OP_SET_STREAM_SOURCE */ wined3d_cs_exec_set_stream_source,
2184 /* WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ */ wined3d_cs_exec_set_stream_source_freq,
2185 /* WINED3D_CS_OP_SET_STREAM_OUTPUT */ wined3d_cs_exec_set_stream_output,
2186 /* WINED3D_CS_OP_SET_INDEX_BUFFER */ wined3d_cs_exec_set_index_buffer,
2187 /* WINED3D_CS_OP_SET_CONSTANT_BUFFER */ wined3d_cs_exec_set_constant_buffer,
2188 /* WINED3D_CS_OP_SET_TEXTURE */ wined3d_cs_exec_set_texture,
2189 /* WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW */ wined3d_cs_exec_set_shader_resource_view,
2190 /* WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_set_unordered_access_view,
2191 /* WINED3D_CS_OP_SET_SAMPLER */ wined3d_cs_exec_set_sampler,
2192 /* WINED3D_CS_OP_SET_SHADER */ wined3d_cs_exec_set_shader,
2193 /* WINED3D_CS_OP_SET_RASTERIZER_STATE */ wined3d_cs_exec_set_rasterizer_state,
2194 /* WINED3D_CS_OP_SET_RENDER_STATE */ wined3d_cs_exec_set_render_state,
2195 /* WINED3D_CS_OP_SET_TEXTURE_STATE */ wined3d_cs_exec_set_texture_state,
2196 /* WINED3D_CS_OP_SET_SAMPLER_STATE */ wined3d_cs_exec_set_sampler_state,
2197 /* WINED3D_CS_OP_SET_TRANSFORM */ wined3d_cs_exec_set_transform,
2198 /* WINED3D_CS_OP_SET_CLIP_PLANE */ wined3d_cs_exec_set_clip_plane,
2199 /* WINED3D_CS_OP_SET_COLOR_KEY */ wined3d_cs_exec_set_color_key,
2200 /* WINED3D_CS_OP_SET_MATERIAL */ wined3d_cs_exec_set_material,
2201 /* WINED3D_CS_OP_SET_LIGHT */ wined3d_cs_exec_set_light,
2202 /* WINED3D_CS_OP_SET_LIGHT_ENABLE */ wined3d_cs_exec_set_light_enable,
2203 /* WINED3D_CS_OP_PUSH_CONSTANTS */ wined3d_cs_exec_push_constants,
2204 /* WINED3D_CS_OP_RESET_STATE */ wined3d_cs_exec_reset_state,
2205 /* WINED3D_CS_OP_CALLBACK */ wined3d_cs_exec_callback,
2206 /* WINED3D_CS_OP_QUERY_ISSUE */ wined3d_cs_exec_query_issue,
2207 /* WINED3D_CS_OP_PRELOAD_RESOURCE */ wined3d_cs_exec_preload_resource,
2208 /* WINED3D_CS_OP_UNLOAD_RESOURCE */ wined3d_cs_exec_unload_resource,
2209 /* WINED3D_CS_OP_MAP */ wined3d_cs_exec_map,
2210 /* WINED3D_CS_OP_UNMAP */ wined3d_cs_exec_unmap,
2211 /* WINED3D_CS_OP_BLT_SUB_RESOURCE */ wined3d_cs_exec_blt_sub_resource,
2212 /* WINED3D_CS_OP_UPDATE_SUB_RESOURCE */ wined3d_cs_exec_update_sub_resource,
2213 /* WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION */ wined3d_cs_exec_add_dirty_texture_region,
2216 static void *wined3d_cs_st_require_space(struct wined3d_cs *cs, size_t size)
2218 if (size > (cs->data_size - cs->end))
2220 size_t new_size;
2221 void *new_data;
2223 new_size = max(size, cs->data_size * 2);
2224 if (!cs->end)
2225 new_data = HeapReAlloc(GetProcessHeap(), 0, cs->data, new_size);
2226 else
2227 new_data = HeapAlloc(GetProcessHeap(), 0, new_size);
2228 if (!new_data)
2229 return NULL;
2231 cs->data_size = new_size;
2232 cs->start = cs->end = 0;
2233 cs->data = new_data;
2236 cs->end += size;
2238 return (BYTE *)cs->data + cs->start;
2241 static void wined3d_cs_st_submit(struct wined3d_cs *cs)
2243 enum wined3d_cs_op opcode;
2244 size_t start;
2245 BYTE *data;
2247 data = cs->data;
2248 start = cs->start;
2249 cs->start = cs->end;
2251 opcode = *(const enum wined3d_cs_op *)&data[start];
2252 if (opcode >= WINED3D_CS_OP_STOP)
2253 ERR("Invalid opcode %#x.\n", opcode);
2254 else
2255 wined3d_cs_op_handlers[opcode](cs, &data[start]);
2257 if (cs->data == data)
2258 cs->start = cs->end = start;
2259 else if (!start)
2260 HeapFree(GetProcessHeap(), 0, data);
2263 static const struct wined3d_cs_ops wined3d_cs_st_ops =
2265 wined3d_cs_st_require_space,
2266 wined3d_cs_st_submit,
2267 wined3d_cs_st_push_constants,
2270 static BOOL wined3d_cs_queue_is_empty(const struct wined3d_cs_queue *queue)
2272 return *(volatile LONG *)&queue->head == queue->tail;
2275 static void wined3d_cs_mt_submit(struct wined3d_cs *cs)
2277 struct wined3d_cs_queue *queue = &cs->queue;
2278 struct wined3d_cs_packet *packet;
2279 size_t packet_size;
2281 if (cs->thread_id == GetCurrentThreadId())
2282 return wined3d_cs_st_submit(cs);
2284 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2285 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2286 InterlockedExchange(&queue->head, (queue->head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1));
2288 if (InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2289 SetEvent(cs->event);
2291 while (!wined3d_cs_queue_is_empty(queue))
2292 wined3d_pause();
2295 static void *wined3d_cs_mt_require_space(struct wined3d_cs *cs, size_t size)
2297 struct wined3d_cs_queue *queue = &cs->queue;
2298 size_t queue_size = ARRAY_SIZE(queue->data);
2299 size_t header_size, packet_size, remaining;
2300 struct wined3d_cs_packet *packet;
2302 if (cs->thread_id == GetCurrentThreadId())
2303 return wined3d_cs_st_require_space(cs, size);
2305 header_size = FIELD_OFFSET(struct wined3d_cs_packet, data[0]);
2306 size = (size + header_size - 1) & ~(header_size - 1);
2307 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[size]);
2308 if (packet_size >= WINED3D_CS_QUEUE_SIZE)
2310 ERR("Packet size %lu >= queue size %u.\n",
2311 (unsigned long)packet_size, WINED3D_CS_QUEUE_SIZE);
2312 return NULL;
2315 remaining = queue_size - queue->head;
2316 if (remaining < packet_size)
2318 size_t nop_size = remaining - header_size;
2319 struct wined3d_cs_nop *nop;
2321 TRACE("Inserting a nop for %lu + %lu bytes.\n",
2322 (unsigned long)header_size, (unsigned long)nop_size);
2324 nop = wined3d_cs_mt_require_space(cs, nop_size);
2325 if (nop_size)
2326 nop->opcode = WINED3D_CS_OP_NOP;
2328 wined3d_cs_mt_submit(cs);
2329 assert(!queue->head);
2332 for (;;)
2334 LONG tail = *(volatile LONG *)&queue->tail;
2335 LONG head = queue->head;
2336 LONG new_pos;
2338 /* Empty. */
2339 if (head == tail)
2340 break;
2341 new_pos = (head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1);
2342 /* Head ahead of tail. We checked the remaining size above, so we only
2343 * need to make sure we don't make head equal to tail. */
2344 if (head > tail && (new_pos != tail))
2345 break;
2346 /* Tail ahead of head. Make sure the new head is before the tail as
2347 * well. Note that new_pos is 0 when it's at the end of the queue. */
2348 if (new_pos < tail && new_pos)
2349 break;
2351 TRACE("Waiting for free space. Head %u, tail %u, packet size %lu.\n",
2352 head, tail, (unsigned long)packet_size);
2355 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2356 packet->size = size;
2357 return packet->data;
2360 static const struct wined3d_cs_ops wined3d_cs_mt_ops =
2362 wined3d_cs_mt_require_space,
2363 wined3d_cs_mt_submit,
2364 wined3d_cs_mt_push_constants,
2367 static void poll_queries(struct wined3d_cs *cs)
2369 struct wined3d_query *query, *cursor;
2371 LIST_FOR_EACH_ENTRY_SAFE(query, cursor, &cs->query_poll_list, struct wined3d_query, poll_list_entry)
2373 if (!query->query_ops->query_poll(query, 0))
2374 continue;
2376 list_remove(&query->poll_list_entry);
2377 list_init(&query->poll_list_entry);
2378 InterlockedIncrement(&query->counter_retrieved);
2382 static void wined3d_cs_wait_event(struct wined3d_cs *cs)
2384 InterlockedExchange(&cs->waiting_for_event, TRUE);
2386 /* The main thread might have enqueued a command and blocked on it after
2387 * the CS thread decided to enter wined3d_cs_wait_event(), but before
2388 * "waiting_for_event" was set.
2390 * Likewise, we can race with the main thread when resetting
2391 * "waiting_for_event", in which case we would need to call
2392 * WaitForSingleObject() because the main thread called SetEvent(). */
2393 if (!wined3d_cs_queue_is_empty(&cs->queue)
2394 && InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2395 return;
2397 WaitForSingleObject(cs->event, INFINITE);
2400 static DWORD WINAPI wined3d_cs_run(void *ctx)
2402 struct wined3d_cs_packet *packet;
2403 struct wined3d_cs_queue *queue;
2404 unsigned int spin_count = 0;
2405 struct wined3d_cs *cs = ctx;
2406 enum wined3d_cs_op opcode;
2407 unsigned int poll = 0;
2408 LONG tail;
2410 TRACE("Started.\n");
2412 queue = &cs->queue;
2413 list_init(&cs->query_poll_list);
2414 cs->thread_id = GetCurrentThreadId();
2415 for (;;)
2417 if (++poll == WINED3D_CS_QUERY_POLL_INTERVAL)
2419 poll_queries(cs);
2420 poll = 0;
2423 if (wined3d_cs_queue_is_empty(queue))
2425 if (++spin_count >= WINED3D_CS_SPIN_COUNT && list_empty(&cs->query_poll_list))
2426 wined3d_cs_wait_event(cs);
2427 continue;
2429 spin_count = 0;
2431 tail = queue->tail;
2432 packet = (struct wined3d_cs_packet *)&queue->data[tail];
2433 if (packet->size)
2435 opcode = *(const enum wined3d_cs_op *)packet->data;
2437 if (opcode >= WINED3D_CS_OP_STOP)
2439 if (opcode > WINED3D_CS_OP_STOP)
2440 ERR("Invalid opcode %#x.\n", opcode);
2441 break;
2444 wined3d_cs_op_handlers[opcode](cs, packet->data);
2447 tail += FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2448 tail &= (WINED3D_CS_QUEUE_SIZE - 1);
2449 InterlockedExchange(&queue->tail, tail);
2452 queue->tail = queue->head = 0;
2453 TRACE("Stopped.\n");
2454 return 0;
2457 struct wined3d_cs *wined3d_cs_create(struct wined3d_device *device)
2459 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2460 struct wined3d_cs *cs;
2462 if (!(cs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cs))))
2463 return NULL;
2465 cs->ops = &wined3d_cs_st_ops;
2466 cs->device = device;
2468 if (!(cs->fb.render_targets = wined3d_calloc(gl_info->limits.buffers, sizeof(*cs->fb.render_targets))))
2470 HeapFree(GetProcessHeap(), 0, cs);
2471 return NULL;
2474 state_init(&cs->state, &cs->fb, gl_info, &device->adapter->d3d_info,
2475 WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
2477 cs->data_size = WINED3D_INITIAL_CS_SIZE;
2478 if (!(cs->data = HeapAlloc(GetProcessHeap(), 0, cs->data_size)))
2479 goto fail;
2481 if (wined3d_settings.cs_multithreaded
2482 && !RtlIsCriticalSectionLockedByThread(NtCurrentTeb()->Peb->LoaderLock))
2484 cs->ops = &wined3d_cs_mt_ops;
2486 if (!(cs->event = CreateEventW(NULL, FALSE, FALSE, NULL)))
2488 ERR("Failed to create command stream event.\n");
2489 HeapFree(GetProcessHeap(), 0, cs->data);
2490 goto fail;
2493 if (!(cs->thread = CreateThread(NULL, 0, wined3d_cs_run, cs, 0, NULL)))
2495 ERR("Failed to create wined3d command stream thread.\n");
2496 CloseHandle(cs->event);
2497 HeapFree(GetProcessHeap(), 0, cs->data);
2498 goto fail;
2502 return cs;
2504 fail:
2505 state_cleanup(&cs->state);
2506 HeapFree(GetProcessHeap(), 0, cs->fb.render_targets);
2507 HeapFree(GetProcessHeap(), 0, cs);
2508 return NULL;
2511 void wined3d_cs_destroy(struct wined3d_cs *cs)
2513 state_cleanup(&cs->state);
2514 HeapFree(GetProcessHeap(), 0, cs->fb.render_targets);
2515 HeapFree(GetProcessHeap(), 0, cs->data);
2517 if (cs->thread)
2519 wined3d_cs_emit_stop(cs);
2520 CloseHandle(cs->thread);
2521 if (!CloseHandle(cs->event))
2522 ERR("Closing event failed.\n");
2525 HeapFree(GetProcessHeap(), 0, cs);