wined3d: Print a message when forcing CS serialization.
[wine.git] / dlls / wined3d / cs.c
blobce0417c9f7142baef96dc996cb3e986ef4e3dd2c
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);
24 WINE_DECLARE_DEBUG_CHANNEL(d3d_sync);
25 WINE_DECLARE_DEBUG_CHANNEL(fps);
27 #define WINED3D_INITIAL_CS_SIZE 4096
29 struct wined3d_deferred_upload
31 struct wined3d_resource *resource;
32 unsigned int sub_resource_idx;
33 uint8_t *sysmem;
34 struct wined3d_box box;
37 struct wined3d_command_list
39 LONG refcount;
41 struct wined3d_device *device;
43 SIZE_T data_size;
44 void *data;
46 SIZE_T resource_count;
47 struct wined3d_resource **resources;
49 SIZE_T upload_count;
50 struct wined3d_deferred_upload *uploads;
52 /* List of command lists queued for execution on this command list. We might
53 * be the only thing holding a pointer to another command list, so we need
54 * to hold a reference here (and in wined3d_deferred_context) as well. */
55 SIZE_T command_list_count;
56 struct wined3d_command_list **command_lists;
59 static void wined3d_command_list_destroy_object(void *object)
61 struct wined3d_command_list *list = object;
62 SIZE_T i;
64 TRACE("list %p.\n", list);
66 for (i = 0; i < list->upload_count; ++i)
67 heap_free(list->uploads[i].sysmem);
69 heap_free(list->resources);
70 heap_free(list->data);
71 heap_free(list);
74 ULONG CDECL wined3d_command_list_incref(struct wined3d_command_list *list)
76 ULONG refcount = InterlockedIncrement(&list->refcount);
78 TRACE("%p increasing refcount to %u.\n", list, refcount);
80 return refcount;
83 ULONG CDECL wined3d_command_list_decref(struct wined3d_command_list *list)
85 ULONG refcount = InterlockedDecrement(&list->refcount);
86 struct wined3d_device *device = list->device;
88 TRACE("%p decreasing refcount to %u.\n", list, refcount);
90 if (!refcount)
92 SIZE_T i;
94 for (i = 0; i < list->command_list_count; ++i)
95 wined3d_command_list_decref(list->command_lists[i]);
96 for (i = 0; i < list->resource_count; ++i)
97 wined3d_resource_decref(list->resources[i]);
99 wined3d_cs_destroy_object(device->cs, wined3d_command_list_destroy_object, list);
102 return refcount;
105 enum wined3d_cs_op
107 WINED3D_CS_OP_NOP,
108 WINED3D_CS_OP_PRESENT,
109 WINED3D_CS_OP_CLEAR,
110 WINED3D_CS_OP_DISPATCH,
111 WINED3D_CS_OP_DRAW,
112 WINED3D_CS_OP_FLUSH,
113 WINED3D_CS_OP_SET_PREDICATION,
114 WINED3D_CS_OP_SET_VIEWPORTS,
115 WINED3D_CS_OP_SET_SCISSOR_RECTS,
116 WINED3D_CS_OP_SET_RENDERTARGET_VIEW,
117 WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW,
118 WINED3D_CS_OP_SET_VERTEX_DECLARATION,
119 WINED3D_CS_OP_SET_STREAM_SOURCE,
120 WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ,
121 WINED3D_CS_OP_SET_STREAM_OUTPUT,
122 WINED3D_CS_OP_SET_INDEX_BUFFER,
123 WINED3D_CS_OP_SET_CONSTANT_BUFFER,
124 WINED3D_CS_OP_SET_TEXTURE,
125 WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW,
126 WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW,
127 WINED3D_CS_OP_SET_SAMPLER,
128 WINED3D_CS_OP_SET_SHADER,
129 WINED3D_CS_OP_SET_BLEND_STATE,
130 WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE,
131 WINED3D_CS_OP_SET_RASTERIZER_STATE,
132 WINED3D_CS_OP_SET_RENDER_STATE,
133 WINED3D_CS_OP_SET_TEXTURE_STATE,
134 WINED3D_CS_OP_SET_SAMPLER_STATE,
135 WINED3D_CS_OP_SET_TRANSFORM,
136 WINED3D_CS_OP_SET_CLIP_PLANE,
137 WINED3D_CS_OP_SET_COLOR_KEY,
138 WINED3D_CS_OP_SET_MATERIAL,
139 WINED3D_CS_OP_SET_LIGHT,
140 WINED3D_CS_OP_SET_LIGHT_ENABLE,
141 WINED3D_CS_OP_SET_FEATURE_LEVEL,
142 WINED3D_CS_OP_PUSH_CONSTANTS,
143 WINED3D_CS_OP_RESET_STATE,
144 WINED3D_CS_OP_CALLBACK,
145 WINED3D_CS_OP_QUERY_ISSUE,
146 WINED3D_CS_OP_PRELOAD_RESOURCE,
147 WINED3D_CS_OP_UNLOAD_RESOURCE,
148 WINED3D_CS_OP_MAP,
149 WINED3D_CS_OP_UNMAP,
150 WINED3D_CS_OP_BLT_SUB_RESOURCE,
151 WINED3D_CS_OP_UPDATE_SUB_RESOURCE,
152 WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION,
153 WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW,
154 WINED3D_CS_OP_COPY_UAV_COUNTER,
155 WINED3D_CS_OP_GENERATE_MIPMAPS,
156 WINED3D_CS_OP_EXECUTE_COMMAND_LIST,
157 WINED3D_CS_OP_STOP,
160 struct wined3d_cs_packet
162 size_t size;
163 BYTE data[1];
166 struct wined3d_cs_nop
168 enum wined3d_cs_op opcode;
171 struct wined3d_cs_present
173 enum wined3d_cs_op opcode;
174 HWND dst_window_override;
175 struct wined3d_swapchain *swapchain;
176 RECT src_rect;
177 RECT dst_rect;
178 unsigned int swap_interval;
179 DWORD flags;
182 struct wined3d_cs_clear
184 enum wined3d_cs_op opcode;
185 DWORD flags;
186 unsigned int rt_count;
187 struct wined3d_fb_state fb;
188 RECT draw_rect;
189 struct wined3d_color color;
190 float depth;
191 DWORD stencil;
192 unsigned int rect_count;
193 RECT rects[1];
196 struct wined3d_cs_dispatch
198 enum wined3d_cs_op opcode;
199 struct wined3d_dispatch_parameters parameters;
202 struct wined3d_cs_draw
204 enum wined3d_cs_op opcode;
205 enum wined3d_primitive_type primitive_type;
206 GLint patch_vertex_count;
207 struct wined3d_draw_parameters parameters;
210 struct wined3d_cs_flush
212 enum wined3d_cs_op opcode;
215 struct wined3d_cs_set_predication
217 enum wined3d_cs_op opcode;
218 struct wined3d_query *predicate;
219 BOOL value;
222 struct wined3d_cs_set_viewports
224 enum wined3d_cs_op opcode;
225 unsigned int viewport_count;
226 struct wined3d_viewport viewports[1];
229 struct wined3d_cs_set_scissor_rects
231 enum wined3d_cs_op opcode;
232 unsigned int rect_count;
233 RECT rects[1];
236 struct wined3d_cs_set_rendertarget_view
238 enum wined3d_cs_op opcode;
239 unsigned int view_idx;
240 struct wined3d_rendertarget_view *view;
243 struct wined3d_cs_set_depth_stencil_view
245 enum wined3d_cs_op opcode;
246 struct wined3d_rendertarget_view *view;
249 struct wined3d_cs_set_vertex_declaration
251 enum wined3d_cs_op opcode;
252 struct wined3d_vertex_declaration *declaration;
255 struct wined3d_cs_set_stream_source
257 enum wined3d_cs_op opcode;
258 UINT stream_idx;
259 struct wined3d_buffer *buffer;
260 UINT offset;
261 UINT stride;
264 struct wined3d_cs_set_stream_source_freq
266 enum wined3d_cs_op opcode;
267 UINT stream_idx;
268 UINT frequency;
269 UINT flags;
272 struct wined3d_cs_set_stream_output
274 enum wined3d_cs_op opcode;
275 UINT stream_idx;
276 struct wined3d_buffer *buffer;
277 UINT offset;
280 struct wined3d_cs_set_index_buffer
282 enum wined3d_cs_op opcode;
283 struct wined3d_buffer *buffer;
284 enum wined3d_format_id format_id;
285 unsigned int offset;
288 struct wined3d_cs_set_constant_buffer
290 enum wined3d_cs_op opcode;
291 enum wined3d_shader_type type;
292 UINT cb_idx;
293 struct wined3d_buffer *buffer;
296 struct wined3d_cs_set_texture
298 enum wined3d_cs_op opcode;
299 UINT stage;
300 struct wined3d_texture *texture;
303 struct wined3d_cs_set_color_key
305 enum wined3d_cs_op opcode;
306 struct wined3d_texture *texture;
307 WORD flags;
308 WORD set;
309 struct wined3d_color_key color_key;
312 struct wined3d_cs_set_shader_resource_view
314 enum wined3d_cs_op opcode;
315 enum wined3d_shader_type type;
316 UINT view_idx;
317 struct wined3d_shader_resource_view *view;
320 struct wined3d_cs_set_unordered_access_view
322 enum wined3d_cs_op opcode;
323 enum wined3d_pipeline pipeline;
324 unsigned int view_idx;
325 struct wined3d_unordered_access_view *view;
326 unsigned int initial_count;
329 struct wined3d_cs_set_sampler
331 enum wined3d_cs_op opcode;
332 enum wined3d_shader_type type;
333 UINT sampler_idx;
334 struct wined3d_sampler *sampler;
337 struct wined3d_cs_set_shader
339 enum wined3d_cs_op opcode;
340 enum wined3d_shader_type type;
341 struct wined3d_shader *shader;
344 struct wined3d_cs_set_blend_state
346 enum wined3d_cs_op opcode;
347 struct wined3d_blend_state *state;
348 struct wined3d_color factor;
349 unsigned int sample_mask;
352 struct wined3d_cs_set_depth_stencil_state
354 enum wined3d_cs_op opcode;
355 struct wined3d_depth_stencil_state *state;
356 unsigned int stencil_ref;
359 struct wined3d_cs_set_rasterizer_state
361 enum wined3d_cs_op opcode;
362 struct wined3d_rasterizer_state *state;
365 struct wined3d_cs_set_render_state
367 enum wined3d_cs_op opcode;
368 enum wined3d_render_state state;
369 DWORD value;
372 struct wined3d_cs_set_texture_state
374 enum wined3d_cs_op opcode;
375 UINT stage;
376 enum wined3d_texture_stage_state state;
377 DWORD value;
380 struct wined3d_cs_set_sampler_state
382 enum wined3d_cs_op opcode;
383 UINT sampler_idx;
384 enum wined3d_sampler_state state;
385 DWORD value;
388 struct wined3d_cs_set_transform
390 enum wined3d_cs_op opcode;
391 enum wined3d_transform_state state;
392 struct wined3d_matrix matrix;
395 struct wined3d_cs_set_clip_plane
397 enum wined3d_cs_op opcode;
398 UINT plane_idx;
399 struct wined3d_vec4 plane;
402 struct wined3d_cs_set_material
404 enum wined3d_cs_op opcode;
405 struct wined3d_material material;
408 struct wined3d_cs_set_light
410 enum wined3d_cs_op opcode;
411 struct wined3d_light_info light;
414 struct wined3d_cs_set_light_enable
416 enum wined3d_cs_op opcode;
417 unsigned int idx;
418 BOOL enable;
421 struct wined3d_cs_set_feature_level
423 enum wined3d_cs_op opcode;
424 enum wined3d_feature_level level;
427 struct wined3d_cs_push_constants
429 enum wined3d_cs_op opcode;
430 enum wined3d_push_constants type;
431 unsigned int start_idx;
432 unsigned int count;
433 BYTE constants[1];
436 struct wined3d_cs_reset_state
438 enum wined3d_cs_op opcode;
439 bool invalidate;
442 struct wined3d_cs_callback
444 enum wined3d_cs_op opcode;
445 void (*callback)(void *object);
446 void *object;
449 struct wined3d_cs_query_issue
451 enum wined3d_cs_op opcode;
452 struct wined3d_query *query;
453 DWORD flags;
456 struct wined3d_cs_preload_resource
458 enum wined3d_cs_op opcode;
459 struct wined3d_resource *resource;
462 struct wined3d_cs_unload_resource
464 enum wined3d_cs_op opcode;
465 struct wined3d_resource *resource;
468 struct wined3d_cs_map
470 enum wined3d_cs_op opcode;
471 struct wined3d_resource *resource;
472 unsigned int sub_resource_idx;
473 void **map_ptr;
474 const struct wined3d_box *box;
475 DWORD flags;
476 HRESULT *hr;
479 struct wined3d_cs_unmap
481 enum wined3d_cs_op opcode;
482 struct wined3d_resource *resource;
483 unsigned int sub_resource_idx;
484 HRESULT *hr;
487 struct wined3d_cs_blt_sub_resource
489 enum wined3d_cs_op opcode;
490 struct wined3d_resource *dst_resource;
491 unsigned int dst_sub_resource_idx;
492 struct wined3d_box dst_box;
493 struct wined3d_resource *src_resource;
494 unsigned int src_sub_resource_idx;
495 struct wined3d_box src_box;
496 DWORD flags;
497 struct wined3d_blt_fx fx;
498 enum wined3d_texture_filter_type filter;
501 struct wined3d_cs_update_sub_resource
503 enum wined3d_cs_op opcode;
504 struct wined3d_resource *resource;
505 unsigned int sub_resource_idx;
506 struct wined3d_box box;
507 struct wined3d_const_bo_address addr;
508 unsigned int row_pitch, slice_pitch;
511 struct wined3d_cs_add_dirty_texture_region
513 enum wined3d_cs_op opcode;
514 struct wined3d_texture *texture;
515 unsigned int layer;
518 struct wined3d_cs_clear_unordered_access_view
520 enum wined3d_cs_op opcode;
521 struct wined3d_unordered_access_view *view;
522 struct wined3d_uvec4 clear_value;
523 bool fp;
526 struct wined3d_cs_copy_uav_counter
528 enum wined3d_cs_op opcode;
529 struct wined3d_buffer *buffer;
530 unsigned int offset;
531 struct wined3d_unordered_access_view *view;
534 struct wined3d_cs_generate_mipmaps
536 enum wined3d_cs_op opcode;
537 struct wined3d_shader_resource_view *view;
540 struct wined3d_cs_execute_command_list
542 enum wined3d_cs_op opcode;
543 struct wined3d_command_list *list;
546 struct wined3d_cs_stop
548 enum wined3d_cs_op opcode;
551 static inline void *wined3d_device_context_require_space(struct wined3d_device_context *context,
552 size_t size, enum wined3d_cs_queue_id queue_id)
554 return context->ops->require_space(context, size, queue_id);
557 static inline void wined3d_device_context_submit(struct wined3d_device_context *context,
558 enum wined3d_cs_queue_id queue_id)
560 context->ops->submit(context, queue_id);
563 static inline void wined3d_device_context_finish(struct wined3d_device_context *context,
564 enum wined3d_cs_queue_id queue_id)
566 context->ops->finish(context, queue_id);
569 static inline void wined3d_device_context_acquire_resource(struct wined3d_device_context *context,
570 struct wined3d_resource *resource)
572 context->ops->acquire_resource(context, resource);
575 static struct wined3d_cs *wined3d_cs_from_context(struct wined3d_device_context *context)
577 return CONTAINING_RECORD(context, struct wined3d_cs, c);
580 static const char *debug_cs_op(enum wined3d_cs_op op)
582 switch (op)
584 #define WINED3D_TO_STR(type) case type: return #type
585 WINED3D_TO_STR(WINED3D_CS_OP_NOP);
586 WINED3D_TO_STR(WINED3D_CS_OP_PRESENT);
587 WINED3D_TO_STR(WINED3D_CS_OP_CLEAR);
588 WINED3D_TO_STR(WINED3D_CS_OP_DISPATCH);
589 WINED3D_TO_STR(WINED3D_CS_OP_DRAW);
590 WINED3D_TO_STR(WINED3D_CS_OP_FLUSH);
591 WINED3D_TO_STR(WINED3D_CS_OP_SET_PREDICATION);
592 WINED3D_TO_STR(WINED3D_CS_OP_SET_VIEWPORTS);
593 WINED3D_TO_STR(WINED3D_CS_OP_SET_SCISSOR_RECTS);
594 WINED3D_TO_STR(WINED3D_CS_OP_SET_RENDERTARGET_VIEW);
595 WINED3D_TO_STR(WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW);
596 WINED3D_TO_STR(WINED3D_CS_OP_SET_VERTEX_DECLARATION);
597 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_SOURCE);
598 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ);
599 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_OUTPUT);
600 WINED3D_TO_STR(WINED3D_CS_OP_SET_INDEX_BUFFER);
601 WINED3D_TO_STR(WINED3D_CS_OP_SET_CONSTANT_BUFFER);
602 WINED3D_TO_STR(WINED3D_CS_OP_SET_TEXTURE);
603 WINED3D_TO_STR(WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW);
604 WINED3D_TO_STR(WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW);
605 WINED3D_TO_STR(WINED3D_CS_OP_SET_SAMPLER);
606 WINED3D_TO_STR(WINED3D_CS_OP_SET_SHADER);
607 WINED3D_TO_STR(WINED3D_CS_OP_SET_BLEND_STATE);
608 WINED3D_TO_STR(WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE);
609 WINED3D_TO_STR(WINED3D_CS_OP_SET_RASTERIZER_STATE);
610 WINED3D_TO_STR(WINED3D_CS_OP_SET_RENDER_STATE);
611 WINED3D_TO_STR(WINED3D_CS_OP_SET_TEXTURE_STATE);
612 WINED3D_TO_STR(WINED3D_CS_OP_SET_SAMPLER_STATE);
613 WINED3D_TO_STR(WINED3D_CS_OP_SET_TRANSFORM);
614 WINED3D_TO_STR(WINED3D_CS_OP_SET_CLIP_PLANE);
615 WINED3D_TO_STR(WINED3D_CS_OP_SET_COLOR_KEY);
616 WINED3D_TO_STR(WINED3D_CS_OP_SET_MATERIAL);
617 WINED3D_TO_STR(WINED3D_CS_OP_SET_LIGHT);
618 WINED3D_TO_STR(WINED3D_CS_OP_SET_LIGHT_ENABLE);
619 WINED3D_TO_STR(WINED3D_CS_OP_SET_FEATURE_LEVEL);
620 WINED3D_TO_STR(WINED3D_CS_OP_PUSH_CONSTANTS);
621 WINED3D_TO_STR(WINED3D_CS_OP_RESET_STATE);
622 WINED3D_TO_STR(WINED3D_CS_OP_CALLBACK);
623 WINED3D_TO_STR(WINED3D_CS_OP_QUERY_ISSUE);
624 WINED3D_TO_STR(WINED3D_CS_OP_PRELOAD_RESOURCE);
625 WINED3D_TO_STR(WINED3D_CS_OP_UNLOAD_RESOURCE);
626 WINED3D_TO_STR(WINED3D_CS_OP_MAP);
627 WINED3D_TO_STR(WINED3D_CS_OP_UNMAP);
628 WINED3D_TO_STR(WINED3D_CS_OP_BLT_SUB_RESOURCE);
629 WINED3D_TO_STR(WINED3D_CS_OP_UPDATE_SUB_RESOURCE);
630 WINED3D_TO_STR(WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION);
631 WINED3D_TO_STR(WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW);
632 WINED3D_TO_STR(WINED3D_CS_OP_COPY_UAV_COUNTER);
633 WINED3D_TO_STR(WINED3D_CS_OP_GENERATE_MIPMAPS);
634 WINED3D_TO_STR(WINED3D_CS_OP_EXECUTE_COMMAND_LIST);
635 WINED3D_TO_STR(WINED3D_CS_OP_STOP);
636 #undef WINED3D_TO_STR
638 return wine_dbg_sprintf("UNKNOWN_OP(%#x)", op);
641 static void wined3d_cs_exec_nop(struct wined3d_cs *cs, const void *data)
645 static void wined3d_cs_exec_present(struct wined3d_cs *cs, const void *data)
647 struct wined3d_texture *logo_texture, *cursor_texture, *back_buffer;
648 struct wined3d_rendertarget_view *dsv = cs->state.fb.depth_stencil;
649 const struct wined3d_cs_present *op = data;
650 const struct wined3d_swapchain_desc *desc;
651 struct wined3d_swapchain *swapchain;
652 unsigned int i;
654 swapchain = op->swapchain;
655 desc = &swapchain->state.desc;
656 back_buffer = swapchain->back_buffers[0];
657 wined3d_swapchain_set_window(swapchain, op->dst_window_override);
659 if ((logo_texture = swapchain->device->logo_texture))
661 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
663 /* Blit the logo into the upper left corner of the back-buffer. */
664 wined3d_device_context_blt(&cs->c, back_buffer, 0, &rect, logo_texture, 0,
665 &rect, WINED3D_BLT_SRC_CKEY, NULL, WINED3D_TEXF_POINT);
668 if ((cursor_texture = swapchain->device->cursor_texture)
669 && swapchain->device->bCursorVisible && !swapchain->device->hardwareCursor)
671 RECT dst_rect =
673 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
674 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
675 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
676 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
678 RECT src_rect =
680 0, 0, cursor_texture->resource.width, cursor_texture->resource.height
682 const RECT clip_rect = {0, 0, back_buffer->resource.width, back_buffer->resource.height};
684 TRACE("Rendering the software cursor.\n");
686 if (desc->windowed)
687 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
688 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
689 wined3d_device_context_blt(&cs->c, back_buffer, 0, &dst_rect, cursor_texture, 0,
690 &src_rect, WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
693 swapchain->swapchain_ops->swapchain_present(swapchain, &op->src_rect, &op->dst_rect, op->swap_interval, op->flags);
695 /* Discard buffers if the swap effect allows it. */
696 back_buffer = swapchain->back_buffers[desc->backbuffer_count - 1];
697 if (desc->swap_effect == WINED3D_SWAP_EFFECT_DISCARD || desc->swap_effect == WINED3D_SWAP_EFFECT_FLIP_DISCARD)
698 wined3d_texture_validate_location(back_buffer, 0, WINED3D_LOCATION_DISCARDED);
700 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
702 struct wined3d_texture *ds = texture_from_resource(dsv->resource);
704 if ((desc->flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL || ds->flags & WINED3D_TEXTURE_DISCARD))
705 wined3d_rendertarget_view_validate_location(dsv, WINED3D_LOCATION_DISCARDED);
708 if (TRACE_ON(fps))
710 DWORD time = GetTickCount();
711 ++swapchain->frames;
713 /* every 1.5 seconds */
714 if (time - swapchain->prev_time > 1500)
716 TRACE_(fps)("%p @ approx %.2ffps\n",
717 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
718 swapchain->prev_time = time;
719 swapchain->frames = 0;
723 wined3d_resource_release(&swapchain->front_buffer->resource);
724 for (i = 0; i < desc->backbuffer_count; ++i)
726 wined3d_resource_release(&swapchain->back_buffers[i]->resource);
729 InterlockedDecrement(&cs->pending_presents);
732 void wined3d_cs_emit_present(struct wined3d_cs *cs, struct wined3d_swapchain *swapchain,
733 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
734 unsigned int swap_interval, DWORD flags)
736 struct wined3d_cs_present *op;
737 unsigned int i;
738 LONG pending;
740 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
741 op->opcode = WINED3D_CS_OP_PRESENT;
742 op->dst_window_override = dst_window_override;
743 op->swapchain = swapchain;
744 op->src_rect = *src_rect;
745 op->dst_rect = *dst_rect;
746 op->swap_interval = swap_interval;
747 op->flags = flags;
749 pending = InterlockedIncrement(&cs->pending_presents);
751 wined3d_resource_acquire(&swapchain->front_buffer->resource);
752 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
754 wined3d_resource_acquire(&swapchain->back_buffers[i]->resource);
757 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
759 /* Limit input latency by limiting the number of presents that we can get
760 * ahead of the worker thread. */
761 while (pending >= swapchain->max_frame_latency)
763 YieldProcessor();
764 pending = InterlockedCompareExchange(&cs->pending_presents, 0, 0);
768 static void wined3d_cs_exec_clear(struct wined3d_cs *cs, const void *data)
770 struct wined3d_device *device = cs->c.device;
771 const struct wined3d_cs_clear *op = data;
772 unsigned int i;
774 device->blitter->ops->blitter_clear(device->blitter, device, op->rt_count, &op->fb,
775 op->rect_count, op->rects, &op->draw_rect, op->flags, &op->color, op->depth, op->stencil);
777 if (op->flags & WINED3DCLEAR_TARGET)
779 for (i = 0; i < op->rt_count; ++i)
781 if (op->fb.render_targets[i])
782 wined3d_resource_release(op->fb.render_targets[i]->resource);
785 if (op->flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
786 wined3d_resource_release(op->fb.depth_stencil->resource);
789 void wined3d_cs_emit_clear(struct wined3d_cs *cs, DWORD rect_count, const RECT *rects,
790 DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
792 const struct wined3d_state *state = cs->c.state;
793 const struct wined3d_viewport *vp = &state->viewports[0];
794 struct wined3d_rendertarget_view *view;
795 struct wined3d_cs_clear *op;
796 unsigned int rt_count, i;
798 rt_count = flags & WINED3DCLEAR_TARGET ? cs->c.device->adapter->d3d_info.limits.max_rt_count : 0;
800 op = wined3d_device_context_require_space(&cs->c, FIELD_OFFSET(struct wined3d_cs_clear, rects[rect_count]),
801 WINED3D_CS_QUEUE_DEFAULT);
802 op->opcode = WINED3D_CS_OP_CLEAR;
803 op->flags = flags & (WINED3DCLEAR_TARGET | WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
804 op->rt_count = rt_count;
805 op->fb = state->fb;
806 SetRect(&op->draw_rect, vp->x, vp->y, vp->x + vp->width, vp->y + vp->height);
807 if (state->rasterizer_state && state->rasterizer_state->desc.scissor)
808 IntersectRect(&op->draw_rect, &op->draw_rect, &state->scissor_rects[0]);
809 op->color = *color;
810 op->depth = depth;
811 op->stencil = stencil;
812 op->rect_count = rect_count;
813 memcpy(op->rects, rects, sizeof(*rects) * rect_count);
815 for (i = 0; i < rt_count; ++i)
817 if ((view = state->fb.render_targets[i]))
818 wined3d_resource_acquire(view->resource);
820 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
822 view = state->fb.depth_stencil;
823 wined3d_resource_acquire(view->resource);
826 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
829 void wined3d_device_context_emit_clear_rendertarget_view(struct wined3d_device_context *context,
830 struct wined3d_rendertarget_view *view, const RECT *rect, unsigned int flags,
831 const struct wined3d_color *color, float depth, unsigned int stencil)
833 struct wined3d_cs_clear *op;
834 size_t size;
836 size = FIELD_OFFSET(struct wined3d_cs_clear, rects[1]);
837 op = wined3d_device_context_require_space(context, size, WINED3D_CS_QUEUE_DEFAULT);
839 op->opcode = WINED3D_CS_OP_CLEAR;
840 op->flags = flags & (WINED3DCLEAR_TARGET | WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
841 if (flags & WINED3DCLEAR_TARGET)
843 op->rt_count = 1;
844 op->fb.render_targets[0] = view;
845 op->fb.depth_stencil = NULL;
846 op->color = *color;
848 else
850 op->rt_count = 0;
851 op->fb.render_targets[0] = NULL;
852 op->fb.depth_stencil = view;
853 op->depth = depth;
854 op->stencil = stencil;
856 SetRect(&op->draw_rect, 0, 0, view->width, view->height);
857 op->rect_count = 1;
858 op->rects[0] = *rect;
860 wined3d_device_context_acquire_resource(context, view->resource);
862 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
863 if (flags & WINED3DCLEAR_SYNCHRONOUS)
864 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_DEFAULT);
867 static void acquire_shader_resources(struct wined3d_device_context *context, unsigned int shader_mask)
869 const struct wined3d_state *state = context->state;
870 struct wined3d_shader_sampler_map_entry *entry;
871 struct wined3d_shader_resource_view *view;
872 struct wined3d_shader *shader;
873 unsigned int i, j;
875 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
877 if (!(shader_mask & (1u << i)))
878 continue;
880 if (!(shader = state->shader[i]))
881 continue;
883 for (j = 0; j < WINED3D_MAX_CBS; ++j)
885 if (state->cb[i][j])
886 wined3d_device_context_acquire_resource(context, &state->cb[i][j]->resource);
889 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
891 entry = &shader->reg_maps.sampler_map.entries[j];
893 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
894 continue;
896 wined3d_device_context_acquire_resource(context, view->resource);
901 static void release_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
903 struct wined3d_shader_sampler_map_entry *entry;
904 struct wined3d_shader_resource_view *view;
905 struct wined3d_shader *shader;
906 unsigned int i, j;
908 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
910 if (!(shader_mask & (1u << i)))
911 continue;
913 if (!(shader = state->shader[i]))
914 continue;
916 for (j = 0; j < WINED3D_MAX_CBS; ++j)
918 if (state->cb[i][j])
919 wined3d_resource_release(&state->cb[i][j]->resource);
922 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
924 entry = &shader->reg_maps.sampler_map.entries[j];
926 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
927 continue;
929 wined3d_resource_release(view->resource);
934 static void acquire_unordered_access_resources(struct wined3d_device_context *context,
935 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
937 unsigned int i;
939 if (!shader)
940 return;
942 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
944 if (!shader->reg_maps.uav_resource_info[i].type)
945 continue;
947 if (!views[i])
948 continue;
950 wined3d_device_context_acquire_resource(context, views[i]->resource);
954 static void release_unordered_access_resources(const struct wined3d_shader *shader,
955 struct wined3d_unordered_access_view * const *views)
957 unsigned int i;
959 if (!shader)
960 return;
962 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
964 if (!shader->reg_maps.uav_resource_info[i].type)
965 continue;
967 if (!views[i])
968 continue;
970 wined3d_resource_release(views[i]->resource);
974 static void wined3d_cs_exec_dispatch(struct wined3d_cs *cs, const void *data)
976 const struct wined3d_cs_dispatch *op = data;
977 struct wined3d_state *state = &cs->state;
979 if (!state->shader[WINED3D_SHADER_TYPE_COMPUTE])
980 WARN("No compute shader bound, skipping dispatch.\n");
981 else
982 cs->c.device->adapter->adapter_ops->adapter_dispatch_compute(cs->c.device, state, &op->parameters);
984 if (op->parameters.indirect)
985 wined3d_resource_release(&op->parameters.u.indirect.buffer->resource);
987 release_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
988 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
989 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
992 static void acquire_compute_pipeline_resources(struct wined3d_device_context *context)
994 const struct wined3d_state *state = context->state;
996 acquire_shader_resources(context, 1u << WINED3D_SHADER_TYPE_COMPUTE);
997 acquire_unordered_access_resources(context, state->shader[WINED3D_SHADER_TYPE_COMPUTE],
998 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
1001 void CDECL wined3d_device_context_dispatch(struct wined3d_device_context *context,
1002 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
1004 struct wined3d_cs_dispatch *op;
1006 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1007 op->opcode = WINED3D_CS_OP_DISPATCH;
1008 op->parameters.indirect = FALSE;
1009 op->parameters.u.direct.group_count_x = group_count_x;
1010 op->parameters.u.direct.group_count_y = group_count_y;
1011 op->parameters.u.direct.group_count_z = group_count_z;
1013 acquire_compute_pipeline_resources(context);
1015 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1018 void CDECL wined3d_device_context_dispatch_indirect(struct wined3d_device_context *context,
1019 struct wined3d_buffer *buffer, unsigned int offset)
1021 struct wined3d_cs_dispatch *op;
1023 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1024 op->opcode = WINED3D_CS_OP_DISPATCH;
1025 op->parameters.indirect = TRUE;
1026 op->parameters.u.indirect.buffer = buffer;
1027 op->parameters.u.indirect.offset = offset;
1029 acquire_compute_pipeline_resources(context);
1030 wined3d_device_context_acquire_resource(context, &buffer->resource);
1032 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1035 static void wined3d_cs_exec_draw(struct wined3d_cs *cs, const void *data)
1037 const struct wined3d_d3d_info *d3d_info = &cs->c.device->adapter->d3d_info;
1038 const struct wined3d_shader *geometry_shader;
1039 struct wined3d_device *device = cs->c.device;
1040 int base_vertex_idx, load_base_vertex_idx;
1041 struct wined3d_state *state = &cs->state;
1042 const struct wined3d_cs_draw *op = data;
1043 unsigned int i;
1045 base_vertex_idx = 0;
1046 if (!op->parameters.indirect)
1048 const struct wined3d_direct_draw_parameters *direct = &op->parameters.u.direct;
1050 if (op->parameters.indexed && d3d_info->draw_base_vertex_offset)
1051 base_vertex_idx = direct->base_vertex_idx;
1052 else if (!op->parameters.indexed)
1053 base_vertex_idx = direct->start_idx;
1056 /* ARB_draw_indirect always supports a base vertex offset. */
1057 if (!op->parameters.indirect && !d3d_info->draw_base_vertex_offset)
1058 load_base_vertex_idx = op->parameters.u.direct.base_vertex_idx;
1059 else
1060 load_base_vertex_idx = 0;
1062 if (state->base_vertex_index != base_vertex_idx)
1064 state->base_vertex_index = base_vertex_idx;
1065 for (i = 0; i < device->context_count; ++i)
1066 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_BASE_VERTEX_ID;
1069 if (state->load_base_vertex_index != load_base_vertex_idx)
1071 state->load_base_vertex_index = load_base_vertex_idx;
1072 device_invalidate_state(cs->c.device, STATE_BASEVERTEXINDEX);
1075 if (state->primitive_type != op->primitive_type)
1077 if ((geometry_shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]) && !geometry_shader->function)
1078 device_invalidate_state(cs->c.device, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY));
1079 if (state->primitive_type == WINED3D_PT_POINTLIST || op->primitive_type == WINED3D_PT_POINTLIST)
1080 device_invalidate_state(cs->c.device, STATE_POINT_ENABLE);
1081 state->primitive_type = op->primitive_type;
1083 state->patch_vertex_count = op->patch_vertex_count;
1085 cs->c.device->adapter->adapter_ops->adapter_draw_primitive(cs->c.device, state, &op->parameters);
1087 if (op->parameters.indirect)
1089 struct wined3d_buffer *buffer = op->parameters.u.indirect.buffer;
1090 wined3d_resource_release(&buffer->resource);
1093 if (op->parameters.indexed)
1094 wined3d_resource_release(&state->index_buffer->resource);
1095 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
1097 if (state->streams[i].buffer)
1098 wined3d_resource_release(&state->streams[i].buffer->resource);
1100 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
1102 if (state->stream_output[i].buffer)
1103 wined3d_resource_release(&state->stream_output[i].buffer->resource);
1105 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
1107 if (state->textures[i])
1108 wined3d_resource_release(&state->textures[i]->resource);
1110 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
1112 if (state->fb.render_targets[i])
1113 wined3d_resource_release(state->fb.render_targets[i]->resource);
1115 if (state->fb.depth_stencil)
1116 wined3d_resource_release(state->fb.depth_stencil->resource);
1117 release_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
1118 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
1119 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
1122 static void acquire_graphics_pipeline_resources(struct wined3d_device_context *context,
1123 BOOL indexed, const struct wined3d_d3d_info *d3d_info)
1125 const struct wined3d_state *state = context->state;
1126 unsigned int i;
1128 if (indexed)
1129 wined3d_device_context_acquire_resource(context, &state->index_buffer->resource);
1130 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
1132 if (state->streams[i].buffer)
1133 wined3d_device_context_acquire_resource(context, &state->streams[i].buffer->resource);
1135 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
1137 if (state->stream_output[i].buffer)
1138 wined3d_device_context_acquire_resource(context, &state->stream_output[i].buffer->resource);
1140 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
1142 if (state->textures[i])
1143 wined3d_device_context_acquire_resource(context, &state->textures[i]->resource);
1145 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
1147 if (state->fb.render_targets[i])
1148 wined3d_device_context_acquire_resource(context, state->fb.render_targets[i]->resource);
1150 if (state->fb.depth_stencil)
1151 wined3d_device_context_acquire_resource(context, state->fb.depth_stencil->resource);
1152 acquire_shader_resources(context, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
1153 acquire_unordered_access_resources(context, state->shader[WINED3D_SHADER_TYPE_PIXEL],
1154 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
1157 void wined3d_device_context_emit_draw(struct wined3d_device_context *context,
1158 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count, int base_vertex_idx,
1159 unsigned int start_idx, unsigned int index_count, unsigned int start_instance, unsigned int instance_count,
1160 bool indexed)
1162 const struct wined3d_d3d_info *d3d_info = &context->device->adapter->d3d_info;
1163 struct wined3d_cs_draw *op;
1165 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1166 op->opcode = WINED3D_CS_OP_DRAW;
1167 op->primitive_type = primitive_type;
1168 op->patch_vertex_count = patch_vertex_count;
1169 op->parameters.indirect = FALSE;
1170 op->parameters.u.direct.base_vertex_idx = base_vertex_idx;
1171 op->parameters.u.direct.start_idx = start_idx;
1172 op->parameters.u.direct.index_count = index_count;
1173 op->parameters.u.direct.start_instance = start_instance;
1174 op->parameters.u.direct.instance_count = instance_count;
1175 op->parameters.indexed = indexed;
1177 acquire_graphics_pipeline_resources(context, indexed, d3d_info);
1179 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1182 void CDECL wined3d_device_context_draw_indirect(struct wined3d_device_context *context,
1183 struct wined3d_buffer *buffer, unsigned int offset, bool indexed)
1185 const struct wined3d_d3d_info *d3d_info = &context->device->adapter->d3d_info;
1186 const struct wined3d_state *state = context->state;
1187 struct wined3d_cs_draw *op;
1189 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1190 op->opcode = WINED3D_CS_OP_DRAW;
1191 op->primitive_type = state->primitive_type;
1192 op->patch_vertex_count = state->patch_vertex_count;
1193 op->parameters.indirect = TRUE;
1194 op->parameters.u.indirect.buffer = buffer;
1195 op->parameters.u.indirect.offset = offset;
1196 op->parameters.indexed = indexed;
1198 acquire_graphics_pipeline_resources(context, indexed, d3d_info);
1199 wined3d_device_context_acquire_resource(context, &buffer->resource);
1201 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1204 static void wined3d_cs_exec_flush(struct wined3d_cs *cs, const void *data)
1206 struct wined3d_context *context;
1208 context = context_acquire(cs->c.device, NULL, 0);
1209 cs->c.device->adapter->adapter_ops->adapter_flush_context(context);
1210 context_release(context);
1213 static void wined3d_cs_flush(struct wined3d_device_context *context)
1215 struct wined3d_cs *cs = wined3d_cs_from_context(context);
1216 struct wined3d_cs_flush *op;
1218 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1219 op->opcode = WINED3D_CS_OP_FLUSH;
1221 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1222 cs->queries_flushed = TRUE;
1225 static void wined3d_cs_exec_set_predication(struct wined3d_cs *cs, const void *data)
1227 const struct wined3d_cs_set_predication *op = data;
1229 cs->state.predicate = op->predicate;
1230 cs->state.predicate_value = op->value;
1233 void wined3d_device_context_emit_set_predication(struct wined3d_device_context *context,
1234 struct wined3d_query *predicate, BOOL value)
1236 struct wined3d_cs_set_predication *op;
1238 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1239 op->opcode = WINED3D_CS_OP_SET_PREDICATION;
1240 op->predicate = predicate;
1241 op->value = value;
1243 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1246 static void wined3d_cs_exec_set_viewports(struct wined3d_cs *cs, const void *data)
1248 const struct wined3d_cs_set_viewports *op = data;
1250 if (op->viewport_count)
1251 memcpy(cs->state.viewports, op->viewports, op->viewport_count * sizeof(*op->viewports));
1252 else
1253 memset(cs->state.viewports, 0, sizeof(*cs->state.viewports));
1254 cs->state.viewport_count = op->viewport_count;
1255 device_invalidate_state(cs->c.device, STATE_VIEWPORT);
1258 void wined3d_device_context_emit_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
1259 const struct wined3d_viewport *viewports)
1261 struct wined3d_cs_set_viewports *op;
1263 op = wined3d_device_context_require_space(context,
1264 FIELD_OFFSET(struct wined3d_cs_set_viewports,viewports[viewport_count]), WINED3D_CS_QUEUE_DEFAULT);
1265 op->opcode = WINED3D_CS_OP_SET_VIEWPORTS;
1266 memcpy(op->viewports, viewports, viewport_count * sizeof(*viewports));
1267 op->viewport_count = viewport_count;
1269 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1272 static void wined3d_cs_exec_set_scissor_rects(struct wined3d_cs *cs, const void *data)
1274 const struct wined3d_cs_set_scissor_rects *op = data;
1276 if (op->rect_count)
1277 memcpy(cs->state.scissor_rects, op->rects, op->rect_count * sizeof(*op->rects));
1278 else
1279 SetRectEmpty(cs->state.scissor_rects);
1280 cs->state.scissor_rect_count = op->rect_count;
1281 device_invalidate_state(cs->c.device, STATE_SCISSORRECT);
1284 void wined3d_device_context_emit_set_scissor_rects(struct wined3d_device_context *context,
1285 unsigned int rect_count, const RECT *rects)
1287 struct wined3d_cs_set_scissor_rects *op;
1289 op = wined3d_device_context_require_space(context, FIELD_OFFSET(struct wined3d_cs_set_scissor_rects, rects[rect_count]),
1290 WINED3D_CS_QUEUE_DEFAULT);
1291 op->opcode = WINED3D_CS_OP_SET_SCISSOR_RECTS;
1292 memcpy(op->rects, rects, rect_count * sizeof(*rects));
1293 op->rect_count = rect_count;
1295 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1298 static void wined3d_cs_exec_set_rendertarget_view(struct wined3d_cs *cs, const void *data)
1300 const struct wined3d_cs_set_rendertarget_view *op = data;
1301 bool prev_alpha_swizzle, curr_alpha_swizzle;
1302 struct wined3d_rendertarget_view *prev;
1303 bool prev_srgb_write, curr_srgb_write;
1304 struct wined3d_device *device;
1306 device = cs->c.device;
1307 prev = cs->state.fb.render_targets[op->view_idx];
1308 cs->state.fb.render_targets[op->view_idx] = op->view;
1309 device_invalidate_state(device, STATE_FRAMEBUFFER);
1311 prev_alpha_swizzle = prev && prev->format->id == WINED3DFMT_A8_UNORM;
1312 curr_alpha_swizzle = op->view && op->view->format->id == WINED3DFMT_A8_UNORM;
1313 if (prev_alpha_swizzle != curr_alpha_swizzle)
1314 device_invalidate_state(device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1316 if (!(device->adapter->d3d_info.wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1317 || cs->state.render_states[WINED3D_RS_SRGBWRITEENABLE])
1319 prev_srgb_write = prev && prev->format_flags & WINED3DFMT_FLAG_SRGB_WRITE;
1320 curr_srgb_write = op->view && op->view->format_flags & WINED3DFMT_FLAG_SRGB_WRITE;
1321 if (prev_srgb_write != curr_srgb_write)
1322 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
1326 void wined3d_device_context_emit_set_rendertarget_view(struct wined3d_device_context *context, unsigned int view_idx,
1327 struct wined3d_rendertarget_view *view)
1329 struct wined3d_cs_set_rendertarget_view *op;
1331 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1332 op->opcode = WINED3D_CS_OP_SET_RENDERTARGET_VIEW;
1333 op->view_idx = view_idx;
1334 op->view = view;
1336 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1339 static void wined3d_cs_exec_set_depth_stencil_view(struct wined3d_cs *cs, const void *data)
1341 const struct wined3d_cs_set_depth_stencil_view *op = data;
1342 struct wined3d_device *device = cs->c.device;
1343 struct wined3d_rendertarget_view *prev;
1345 if ((prev = cs->state.fb.depth_stencil) && prev->resource->type != WINED3D_RTYPE_BUFFER)
1347 struct wined3d_texture *prev_texture = texture_from_resource(prev->resource);
1349 if (device->swapchains[0]->state.desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
1350 || prev_texture->flags & WINED3D_TEXTURE_DISCARD)
1351 wined3d_texture_validate_location(prev_texture,
1352 prev->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
1355 cs->state.fb.depth_stencil = op->view;
1357 if (!prev != !op->view)
1359 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
1360 device_invalidate_state(device, STATE_DEPTH_STENCIL);
1361 device_invalidate_state(device, STATE_STENCIL_REF);
1362 device_invalidate_state(device, STATE_RASTERIZER);
1364 else if (prev)
1366 if (prev->format->depth_bias_scale != op->view->format->depth_bias_scale)
1367 device_invalidate_state(device, STATE_RASTERIZER);
1368 if (prev->format->stencil_size != op->view->format->stencil_size)
1369 device_invalidate_state(device, STATE_STENCIL_REF);
1372 device_invalidate_state(device, STATE_FRAMEBUFFER);
1375 void wined3d_device_context_emit_set_depth_stencil_view(struct wined3d_device_context *context,
1376 struct wined3d_rendertarget_view *view)
1378 struct wined3d_cs_set_depth_stencil_view *op;
1380 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1381 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW;
1382 op->view = view;
1384 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1387 static void wined3d_cs_exec_set_vertex_declaration(struct wined3d_cs *cs, const void *data)
1389 const struct wined3d_cs_set_vertex_declaration *op = data;
1391 cs->state.vertex_declaration = op->declaration;
1392 device_invalidate_state(cs->c.device, STATE_VDECL);
1395 void wined3d_device_context_emit_set_vertex_declaration(struct wined3d_device_context *context,
1396 struct wined3d_vertex_declaration *declaration)
1398 struct wined3d_cs_set_vertex_declaration *op;
1400 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1401 op->opcode = WINED3D_CS_OP_SET_VERTEX_DECLARATION;
1402 op->declaration = declaration;
1404 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1407 static void wined3d_cs_exec_set_stream_source(struct wined3d_cs *cs, const void *data)
1409 const struct wined3d_cs_set_stream_source *op = data;
1410 struct wined3d_stream_state *stream;
1411 struct wined3d_buffer *prev;
1413 stream = &cs->state.streams[op->stream_idx];
1414 prev = stream->buffer;
1415 stream->buffer = op->buffer;
1416 stream->offset = op->offset;
1417 stream->stride = op->stride;
1419 if (op->buffer)
1420 InterlockedIncrement(&op->buffer->resource.bind_count);
1421 if (prev)
1422 InterlockedDecrement(&prev->resource.bind_count);
1424 device_invalidate_state(cs->c.device, STATE_STREAMSRC);
1427 void wined3d_device_context_emit_set_stream_source(struct wined3d_device_context *context, unsigned int stream_idx,
1428 struct wined3d_buffer *buffer, unsigned int offset, unsigned int stride)
1430 struct wined3d_cs_set_stream_source *op;
1432 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1433 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE;
1434 op->stream_idx = stream_idx;
1435 op->buffer = buffer;
1436 op->offset = offset;
1437 op->stride = stride;
1439 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1442 static void wined3d_cs_exec_set_stream_source_freq(struct wined3d_cs *cs, const void *data)
1444 const struct wined3d_cs_set_stream_source_freq *op = data;
1445 struct wined3d_stream_state *stream;
1447 stream = &cs->state.streams[op->stream_idx];
1448 stream->frequency = op->frequency;
1449 stream->flags = op->flags;
1451 device_invalidate_state(cs->c.device, STATE_STREAMSRC);
1454 void wined3d_cs_emit_set_stream_source_freq(struct wined3d_cs *cs, UINT stream_idx, UINT frequency, UINT flags)
1456 struct wined3d_cs_set_stream_source_freq *op;
1458 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1459 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ;
1460 op->stream_idx = stream_idx;
1461 op->frequency = frequency;
1462 op->flags = flags;
1464 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1467 static void wined3d_cs_exec_set_stream_output(struct wined3d_cs *cs, const void *data)
1469 const struct wined3d_cs_set_stream_output *op = data;
1470 struct wined3d_stream_output *stream;
1471 struct wined3d_buffer *prev;
1473 stream = &cs->state.stream_output[op->stream_idx];
1474 prev = stream->buffer;
1475 stream->buffer = op->buffer;
1476 stream->offset = op->offset;
1478 if (op->buffer)
1479 InterlockedIncrement(&op->buffer->resource.bind_count);
1480 if (prev)
1481 InterlockedDecrement(&prev->resource.bind_count);
1483 device_invalidate_state(cs->c.device, STATE_STREAM_OUTPUT);
1486 void wined3d_device_context_emit_set_stream_output(struct wined3d_device_context *context, unsigned int stream_idx,
1487 struct wined3d_buffer *buffer, unsigned int offset)
1489 struct wined3d_cs_set_stream_output *op;
1491 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1492 op->opcode = WINED3D_CS_OP_SET_STREAM_OUTPUT;
1493 op->stream_idx = stream_idx;
1494 op->buffer = buffer;
1495 op->offset = offset;
1497 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1500 static void wined3d_cs_exec_set_index_buffer(struct wined3d_cs *cs, const void *data)
1502 const struct wined3d_cs_set_index_buffer *op = data;
1503 struct wined3d_buffer *prev;
1505 prev = cs->state.index_buffer;
1506 cs->state.index_buffer = op->buffer;
1507 cs->state.index_format = op->format_id;
1508 cs->state.index_offset = op->offset;
1510 if (op->buffer)
1511 InterlockedIncrement(&op->buffer->resource.bind_count);
1512 if (prev)
1513 InterlockedDecrement(&prev->resource.bind_count);
1515 device_invalidate_state(cs->c.device, STATE_INDEXBUFFER);
1518 void wined3d_device_context_emit_set_index_buffer(struct wined3d_device_context *context, struct wined3d_buffer *buffer,
1519 enum wined3d_format_id format_id, unsigned int offset)
1521 struct wined3d_cs_set_index_buffer *op;
1523 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1524 op->opcode = WINED3D_CS_OP_SET_INDEX_BUFFER;
1525 op->buffer = buffer;
1526 op->format_id = format_id;
1527 op->offset = offset;
1529 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1532 static void wined3d_cs_exec_set_constant_buffer(struct wined3d_cs *cs, const void *data)
1534 const struct wined3d_cs_set_constant_buffer *op = data;
1535 struct wined3d_buffer *prev;
1537 prev = cs->state.cb[op->type][op->cb_idx];
1538 cs->state.cb[op->type][op->cb_idx] = op->buffer;
1540 if (op->buffer)
1541 InterlockedIncrement(&op->buffer->resource.bind_count);
1542 if (prev)
1543 InterlockedDecrement(&prev->resource.bind_count);
1545 device_invalidate_state(cs->c.device, STATE_CONSTANT_BUFFER(op->type));
1548 void wined3d_device_context_emit_set_constant_buffer(struct wined3d_device_context *context,
1549 enum wined3d_shader_type type, UINT cb_idx, struct wined3d_buffer *buffer)
1551 struct wined3d_cs_set_constant_buffer *op;
1553 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1554 op->opcode = WINED3D_CS_OP_SET_CONSTANT_BUFFER;
1555 op->type = type;
1556 op->cb_idx = cb_idx;
1557 op->buffer = buffer;
1559 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1562 static void wined3d_cs_exec_set_texture(struct wined3d_cs *cs, const void *data)
1564 const struct wined3d_d3d_info *d3d_info = &cs->c.device->adapter->d3d_info;
1565 const struct wined3d_cs_set_texture *op = data;
1566 struct wined3d_texture *prev;
1567 BOOL old_use_color_key = FALSE, new_use_color_key = FALSE;
1569 prev = cs->state.textures[op->stage];
1570 cs->state.textures[op->stage] = op->texture;
1572 if (op->texture)
1574 const struct wined3d_format *new_format = op->texture->resource.format;
1575 const struct wined3d_format *old_format = prev ? prev->resource.format : NULL;
1576 unsigned int old_fmt_flags = prev ? prev->resource.format_flags : 0;
1577 unsigned int new_fmt_flags = op->texture->resource.format_flags;
1579 if (InterlockedIncrement(&op->texture->resource.bind_count) == 1)
1580 op->texture->sampler = op->stage;
1582 if (!prev || wined3d_texture_gl(op->texture)->target != wined3d_texture_gl(prev)->target
1583 || (!is_same_fixup(new_format->color_fixup, old_format->color_fixup)
1584 && !(can_use_texture_swizzle(d3d_info, new_format) && can_use_texture_swizzle(d3d_info, old_format)))
1585 || (new_fmt_flags & WINED3DFMT_FLAG_SHADOW) != (old_fmt_flags & WINED3DFMT_FLAG_SHADOW))
1586 device_invalidate_state(cs->c.device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1588 if (!prev && op->stage < d3d_info->limits.ffp_blend_stages)
1590 /* The source arguments for color and alpha ops have different
1591 * meanings when a NULL texture is bound, so the COLOR_OP and
1592 * ALPHA_OP have to be dirtified. */
1593 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1594 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1597 if (!op->stage && op->texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1598 new_use_color_key = TRUE;
1601 if (prev)
1603 if (InterlockedDecrement(&prev->resource.bind_count) && prev->sampler == op->stage)
1605 unsigned int i;
1607 /* Search for other stages the texture is bound to. Shouldn't
1608 * happen if applications bind textures to a single stage only. */
1609 TRACE("Searching for other stages the texture is bound to.\n");
1610 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1612 if (cs->state.textures[i] == prev)
1614 TRACE("Texture is also bound to stage %u.\n", i);
1615 prev->sampler = i;
1616 break;
1621 if (!op->texture && op->stage < d3d_info->limits.ffp_blend_stages)
1623 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1624 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1627 if (!op->stage && prev->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1628 old_use_color_key = TRUE;
1631 device_invalidate_state(cs->c.device, STATE_SAMPLER(op->stage));
1633 if (new_use_color_key != old_use_color_key)
1634 device_invalidate_state(cs->c.device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1636 if (new_use_color_key)
1637 device_invalidate_state(cs->c.device, STATE_COLOR_KEY);
1640 void wined3d_device_context_emit_set_texture(struct wined3d_device_context *context, unsigned int stage,
1641 struct wined3d_texture *texture)
1643 struct wined3d_cs_set_texture *op;
1645 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1646 op->opcode = WINED3D_CS_OP_SET_TEXTURE;
1647 op->stage = stage;
1648 op->texture = texture;
1650 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1653 static void wined3d_cs_exec_set_shader_resource_view(struct wined3d_cs *cs, const void *data)
1655 const struct wined3d_cs_set_shader_resource_view *op = data;
1656 struct wined3d_shader_resource_view *prev;
1658 prev = cs->state.shader_resource_view[op->type][op->view_idx];
1659 cs->state.shader_resource_view[op->type][op->view_idx] = op->view;
1661 if (op->view)
1662 InterlockedIncrement(&op->view->resource->bind_count);
1663 if (prev)
1664 InterlockedDecrement(&prev->resource->bind_count);
1666 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1667 device_invalidate_state(cs->c.device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1668 else
1669 device_invalidate_state(cs->c.device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1672 void wined3d_device_context_emit_set_shader_resource_view(struct wined3d_device_context *context,
1673 enum wined3d_shader_type type, unsigned int view_idx, struct wined3d_shader_resource_view *view)
1675 struct wined3d_cs_set_shader_resource_view *op;
1677 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1678 op->opcode = WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW;
1679 op->type = type;
1680 op->view_idx = view_idx;
1681 op->view = view;
1683 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1686 static void wined3d_cs_exec_set_unordered_access_view(struct wined3d_cs *cs, const void *data)
1688 const struct wined3d_cs_set_unordered_access_view *op = data;
1689 struct wined3d_unordered_access_view *prev;
1691 prev = cs->state.unordered_access_view[op->pipeline][op->view_idx];
1692 cs->state.unordered_access_view[op->pipeline][op->view_idx] = op->view;
1694 if (op->view)
1695 InterlockedIncrement(&op->view->resource->bind_count);
1696 if (prev)
1697 InterlockedDecrement(&prev->resource->bind_count);
1699 if (op->view && op->initial_count != ~0u)
1700 wined3d_unordered_access_view_set_counter(op->view, op->initial_count);
1702 device_invalidate_state(cs->c.device, STATE_UNORDERED_ACCESS_VIEW_BINDING(op->pipeline));
1705 void wined3d_device_context_emit_set_unordered_access_view(struct wined3d_device_context *context,
1706 enum wined3d_pipeline pipeline, unsigned int view_idx, struct wined3d_unordered_access_view *view,
1707 unsigned int initial_count)
1709 struct wined3d_cs_set_unordered_access_view *op;
1711 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1712 op->opcode = WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW;
1713 op->pipeline = pipeline;
1714 op->view_idx = view_idx;
1715 op->view = view;
1716 op->initial_count = initial_count;
1718 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1721 static void wined3d_cs_exec_set_sampler(struct wined3d_cs *cs, const void *data)
1723 const struct wined3d_cs_set_sampler *op = data;
1725 cs->state.sampler[op->type][op->sampler_idx] = op->sampler;
1726 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1727 device_invalidate_state(cs->c.device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1728 else
1729 device_invalidate_state(cs->c.device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1732 void wined3d_device_context_emit_set_sampler(struct wined3d_device_context *context, enum wined3d_shader_type type,
1733 unsigned int sampler_idx, struct wined3d_sampler *sampler)
1735 struct wined3d_cs_set_sampler *op;
1737 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1738 op->opcode = WINED3D_CS_OP_SET_SAMPLER;
1739 op->type = type;
1740 op->sampler_idx = sampler_idx;
1741 op->sampler = sampler;
1743 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1746 static void wined3d_cs_exec_set_shader(struct wined3d_cs *cs, const void *data)
1748 const struct wined3d_cs_set_shader *op = data;
1750 cs->state.shader[op->type] = op->shader;
1751 device_invalidate_state(cs->c.device, STATE_SHADER(op->type));
1752 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1753 device_invalidate_state(cs->c.device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1754 else
1755 device_invalidate_state(cs->c.device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1758 void wined3d_device_context_emit_set_shader(struct wined3d_device_context *context,
1759 enum wined3d_shader_type type, struct wined3d_shader *shader)
1761 struct wined3d_cs_set_shader *op;
1763 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1764 op->opcode = WINED3D_CS_OP_SET_SHADER;
1765 op->type = type;
1766 op->shader = shader;
1768 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1771 static void wined3d_cs_exec_set_blend_state(struct wined3d_cs *cs, const void *data)
1773 const struct wined3d_cs_set_blend_state *op = data;
1774 struct wined3d_state *state = &cs->state;
1776 if (state->blend_state != op->state)
1778 state->blend_state = op->state;
1779 device_invalidate_state(cs->c.device, STATE_BLEND);
1781 state->blend_factor = op->factor;
1782 device_invalidate_state(cs->c.device, STATE_BLEND_FACTOR);
1783 state->sample_mask = op->sample_mask;
1784 device_invalidate_state(cs->c.device, STATE_SAMPLE_MASK);
1787 void wined3d_device_context_emit_set_blend_state(struct wined3d_device_context *context,
1788 struct wined3d_blend_state *state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
1790 struct wined3d_cs_set_blend_state *op;
1792 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1793 op->opcode = WINED3D_CS_OP_SET_BLEND_STATE;
1794 op->state = state;
1795 op->factor = *blend_factor;
1796 op->sample_mask = sample_mask;
1798 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1801 static void wined3d_cs_exec_set_depth_stencil_state(struct wined3d_cs *cs, const void *data)
1803 const struct wined3d_cs_set_depth_stencil_state *op = data;
1804 struct wined3d_state *state = &cs->state;
1806 if (state->depth_stencil_state != op->state)
1808 state->depth_stencil_state = op->state;
1809 device_invalidate_state(cs->c.device, STATE_DEPTH_STENCIL);
1811 state->stencil_ref = op->stencil_ref;
1812 device_invalidate_state(cs->c.device, STATE_STENCIL_REF);
1815 void wined3d_device_context_emit_set_depth_stencil_state(struct wined3d_device_context *context,
1816 struct wined3d_depth_stencil_state *state, unsigned int stencil_ref)
1818 struct wined3d_cs_set_depth_stencil_state *op;
1820 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1821 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE;
1822 op->state = state;
1823 op->stencil_ref = stencil_ref;
1825 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1828 static void wined3d_cs_exec_set_rasterizer_state(struct wined3d_cs *cs, const void *data)
1830 const struct wined3d_cs_set_rasterizer_state *op = data;
1832 cs->state.rasterizer_state = op->state;
1833 device_invalidate_state(cs->c.device, STATE_RASTERIZER);
1836 void wined3d_device_context_emit_set_rasterizer_state(struct wined3d_device_context *context,
1837 struct wined3d_rasterizer_state *rasterizer_state)
1839 struct wined3d_cs_set_rasterizer_state *op;
1841 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1842 op->opcode = WINED3D_CS_OP_SET_RASTERIZER_STATE;
1843 op->state = rasterizer_state;
1845 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1848 static void wined3d_cs_exec_set_render_state(struct wined3d_cs *cs, const void *data)
1850 const struct wined3d_cs_set_render_state *op = data;
1852 cs->state.render_states[op->state] = op->value;
1853 device_invalidate_state(cs->c.device, STATE_RENDER(op->state));
1856 void wined3d_device_context_emit_set_render_state(struct wined3d_device_context *context,
1857 enum wined3d_render_state state, unsigned int value)
1859 struct wined3d_cs_set_render_state *op;
1861 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1862 op->opcode = WINED3D_CS_OP_SET_RENDER_STATE;
1863 op->state = state;
1864 op->value = value;
1866 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1869 static void wined3d_cs_exec_set_texture_state(struct wined3d_cs *cs, const void *data)
1871 const struct wined3d_cs_set_texture_state *op = data;
1873 cs->state.texture_states[op->stage][op->state] = op->value;
1874 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, op->state));
1877 void wined3d_device_context_emit_set_texture_state(struct wined3d_device_context *context, unsigned int stage,
1878 enum wined3d_texture_stage_state state, unsigned int value)
1880 struct wined3d_cs_set_texture_state *op;
1882 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1883 op->opcode = WINED3D_CS_OP_SET_TEXTURE_STATE;
1884 op->stage = stage;
1885 op->state = state;
1886 op->value = value;
1888 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1891 static void wined3d_cs_exec_set_sampler_state(struct wined3d_cs *cs, const void *data)
1893 const struct wined3d_cs_set_sampler_state *op = data;
1895 cs->state.sampler_states[op->sampler_idx][op->state] = op->value;
1896 device_invalidate_state(cs->c.device, STATE_SAMPLER(op->sampler_idx));
1899 void wined3d_device_context_emit_set_sampler_state(struct wined3d_device_context *context, unsigned int sampler_idx,
1900 enum wined3d_sampler_state state, unsigned int value)
1902 struct wined3d_cs_set_sampler_state *op;
1904 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1905 op->opcode = WINED3D_CS_OP_SET_SAMPLER_STATE;
1906 op->sampler_idx = sampler_idx;
1907 op->state = state;
1908 op->value = value;
1910 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1913 static void wined3d_cs_exec_set_transform(struct wined3d_cs *cs, const void *data)
1915 const struct wined3d_cs_set_transform *op = data;
1917 cs->state.transforms[op->state] = op->matrix;
1918 if (op->state < WINED3D_TS_WORLD_MATRIX(cs->c.device->adapter->d3d_info.limits.ffp_vertex_blend_matrices))
1919 device_invalidate_state(cs->c.device, STATE_TRANSFORM(op->state));
1922 void wined3d_device_context_emit_set_transform(struct wined3d_device_context *context,
1923 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1925 struct wined3d_cs_set_transform *op;
1927 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1928 op->opcode = WINED3D_CS_OP_SET_TRANSFORM;
1929 op->state = state;
1930 op->matrix = *matrix;
1932 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1935 static void wined3d_cs_exec_set_clip_plane(struct wined3d_cs *cs, const void *data)
1937 const struct wined3d_cs_set_clip_plane *op = data;
1939 cs->state.clip_planes[op->plane_idx] = op->plane;
1940 device_invalidate_state(cs->c.device, STATE_CLIPPLANE(op->plane_idx));
1943 void wined3d_device_context_emit_set_clip_plane(struct wined3d_device_context *context,
1944 unsigned int plane_idx, const struct wined3d_vec4 *plane)
1946 struct wined3d_cs_set_clip_plane *op;
1948 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1949 op->opcode = WINED3D_CS_OP_SET_CLIP_PLANE;
1950 op->plane_idx = plane_idx;
1951 op->plane = *plane;
1953 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1956 static void wined3d_cs_exec_set_color_key(struct wined3d_cs *cs, const void *data)
1958 const struct wined3d_cs_set_color_key *op = data;
1959 struct wined3d_texture *texture = op->texture;
1961 if (op->set)
1963 switch (op->flags)
1965 case WINED3D_CKEY_DST_BLT:
1966 texture->async.dst_blt_color_key = op->color_key;
1967 texture->async.color_key_flags |= WINED3D_CKEY_DST_BLT;
1968 break;
1970 case WINED3D_CKEY_DST_OVERLAY:
1971 texture->async.dst_overlay_color_key = op->color_key;
1972 texture->async.color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
1973 break;
1975 case WINED3D_CKEY_SRC_BLT:
1976 if (texture == cs->state.textures[0])
1978 device_invalidate_state(cs->c.device, STATE_COLOR_KEY);
1979 if (!(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1980 device_invalidate_state(cs->c.device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1983 texture->async.src_blt_color_key = op->color_key;
1984 texture->async.color_key_flags |= WINED3D_CKEY_SRC_BLT;
1985 break;
1987 case WINED3D_CKEY_SRC_OVERLAY:
1988 texture->async.src_overlay_color_key = op->color_key;
1989 texture->async.color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
1990 break;
1993 else
1995 switch (op->flags)
1997 case WINED3D_CKEY_DST_BLT:
1998 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_BLT;
1999 break;
2001 case WINED3D_CKEY_DST_OVERLAY:
2002 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
2003 break;
2005 case WINED3D_CKEY_SRC_BLT:
2006 if (texture == cs->state.textures[0] && texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
2007 device_invalidate_state(cs->c.device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
2009 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
2010 break;
2012 case WINED3D_CKEY_SRC_OVERLAY:
2013 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
2014 break;
2019 void wined3d_cs_emit_set_color_key(struct wined3d_cs *cs, struct wined3d_texture *texture,
2020 WORD flags, const struct wined3d_color_key *color_key)
2022 struct wined3d_cs_set_color_key *op;
2024 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2025 op->opcode = WINED3D_CS_OP_SET_COLOR_KEY;
2026 op->texture = texture;
2027 op->flags = flags;
2028 if (color_key)
2030 op->color_key = *color_key;
2031 op->set = 1;
2033 else
2034 op->set = 0;
2036 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2039 static void wined3d_cs_exec_set_material(struct wined3d_cs *cs, const void *data)
2041 const struct wined3d_cs_set_material *op = data;
2043 cs->state.material = op->material;
2044 device_invalidate_state(cs->c.device, STATE_MATERIAL);
2047 void wined3d_device_context_emit_set_material(struct wined3d_device_context *context,
2048 const struct wined3d_material *material)
2050 struct wined3d_cs_set_material *op;
2052 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2053 op->opcode = WINED3D_CS_OP_SET_MATERIAL;
2054 op->material = *material;
2056 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2059 static void wined3d_cs_exec_set_light(struct wined3d_cs *cs, const void *data)
2061 const struct wined3d_cs_set_light *op = data;
2062 struct wined3d_light_info *light_info;
2063 unsigned int light_idx, hash_idx;
2065 light_idx = op->light.OriginalIndex;
2067 if (!(light_info = wined3d_light_state_get_light(&cs->state.light_state, light_idx)))
2069 TRACE("Adding new light.\n");
2070 if (!(light_info = heap_alloc_zero(sizeof(*light_info))))
2072 ERR("Failed to allocate light info.\n");
2073 return;
2076 hash_idx = LIGHTMAP_HASHFUNC(light_idx);
2077 list_add_head(&cs->state.light_state.light_map[hash_idx], &light_info->entry);
2078 light_info->glIndex = -1;
2079 light_info->OriginalIndex = light_idx;
2082 if (light_info->glIndex != -1)
2084 if (light_info->OriginalParms.type != op->light.OriginalParms.type)
2085 device_invalidate_state(cs->c.device, STATE_LIGHT_TYPE);
2086 device_invalidate_state(cs->c.device, STATE_ACTIVELIGHT(light_info->glIndex));
2089 light_info->OriginalParms = op->light.OriginalParms;
2090 light_info->position = op->light.position;
2091 light_info->direction = op->light.direction;
2092 light_info->exponent = op->light.exponent;
2093 light_info->cutoff = op->light.cutoff;
2096 void wined3d_device_context_emit_set_light(struct wined3d_device_context *context,
2097 const struct wined3d_light_info *light)
2099 struct wined3d_cs_set_light *op;
2101 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2102 op->opcode = WINED3D_CS_OP_SET_LIGHT;
2103 op->light = *light;
2105 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2108 static void wined3d_cs_exec_set_light_enable(struct wined3d_cs *cs, const void *data)
2110 const struct wined3d_cs_set_light_enable *op = data;
2111 struct wined3d_device *device = cs->c.device;
2112 struct wined3d_light_info *light_info;
2113 int prev_idx;
2115 if (!(light_info = wined3d_light_state_get_light(&cs->state.light_state, op->idx)))
2117 ERR("Light doesn't exist.\n");
2118 return;
2121 prev_idx = light_info->glIndex;
2122 wined3d_light_state_enable_light(&cs->state.light_state, &device->adapter->d3d_info, light_info, op->enable);
2123 if (light_info->glIndex != prev_idx)
2125 device_invalidate_state(device, STATE_LIGHT_TYPE);
2126 device_invalidate_state(device, STATE_ACTIVELIGHT(op->enable ? light_info->glIndex : prev_idx));
2130 void wined3d_device_context_emit_set_light_enable(struct wined3d_device_context *context, unsigned int idx, BOOL enable)
2132 struct wined3d_cs_set_light_enable *op;
2134 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2135 op->opcode = WINED3D_CS_OP_SET_LIGHT_ENABLE;
2136 op->idx = idx;
2137 op->enable = enable;
2139 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2142 static void wined3d_cs_exec_set_feature_level(struct wined3d_cs *cs, const void *data)
2144 const struct wined3d_cs_set_feature_level *op = data;
2146 cs->state.feature_level = op->level;
2149 void wined3d_device_context_emit_set_feature_level(struct wined3d_device_context *context,
2150 enum wined3d_feature_level level)
2152 struct wined3d_cs_set_feature_level *op;
2154 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2155 op->opcode = WINED3D_CS_OP_SET_FEATURE_LEVEL;
2156 op->level = level;
2158 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2161 static const struct
2163 size_t offset;
2164 size_t size;
2165 DWORD mask;
2167 wined3d_cs_push_constant_info[] =
2169 /* WINED3D_PUSH_CONSTANTS_VS_F */
2170 {FIELD_OFFSET(struct wined3d_state, vs_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_VS_F},
2171 /* WINED3D_PUSH_CONSTANTS_PS_F */
2172 {FIELD_OFFSET(struct wined3d_state, ps_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_PS_F},
2173 /* WINED3D_PUSH_CONSTANTS_VS_I */
2174 {FIELD_OFFSET(struct wined3d_state, vs_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_VS_I},
2175 /* WINED3D_PUSH_CONSTANTS_PS_I */
2176 {FIELD_OFFSET(struct wined3d_state, ps_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_PS_I},
2177 /* WINED3D_PUSH_CONSTANTS_VS_B */
2178 {FIELD_OFFSET(struct wined3d_state, vs_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_VS_B},
2179 /* WINED3D_PUSH_CONSTANTS_PS_B */
2180 {FIELD_OFFSET(struct wined3d_state, ps_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_PS_B},
2183 static void wined3d_cs_st_push_constants(struct wined3d_device_context *context, enum wined3d_push_constants p,
2184 unsigned int start_idx, unsigned int count, const void *constants)
2186 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2187 struct wined3d_device *device = cs->c.device;
2188 unsigned int context_count;
2189 unsigned int i;
2190 size_t offset;
2192 if (p == WINED3D_PUSH_CONSTANTS_VS_F)
2193 device->shader_backend->shader_update_float_vertex_constants(device, start_idx, count);
2194 else if (p == WINED3D_PUSH_CONSTANTS_PS_F)
2195 device->shader_backend->shader_update_float_pixel_constants(device, start_idx, count);
2197 offset = wined3d_cs_push_constant_info[p].offset + start_idx * wined3d_cs_push_constant_info[p].size;
2198 memcpy((BYTE *)&cs->state + offset, constants, count * wined3d_cs_push_constant_info[p].size);
2199 for (i = 0, context_count = device->context_count; i < context_count; ++i)
2201 device->contexts[i]->constant_update_mask |= wined3d_cs_push_constant_info[p].mask;
2205 static void wined3d_cs_exec_push_constants(struct wined3d_cs *cs, const void *data)
2207 const struct wined3d_cs_push_constants *op = data;
2209 wined3d_cs_st_push_constants(&cs->c, op->type, op->start_idx, op->count, op->constants);
2212 static void wined3d_cs_mt_push_constants(struct wined3d_device_context *context, enum wined3d_push_constants p,
2213 unsigned int start_idx, unsigned int count, const void *constants)
2215 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2216 struct wined3d_cs_push_constants *op;
2217 size_t size;
2219 size = count * wined3d_cs_push_constant_info[p].size;
2220 op = wined3d_device_context_require_space(&cs->c, FIELD_OFFSET(struct wined3d_cs_push_constants, constants[size]),
2221 WINED3D_CS_QUEUE_DEFAULT);
2222 op->opcode = WINED3D_CS_OP_PUSH_CONSTANTS;
2223 op->type = p;
2224 op->start_idx = start_idx;
2225 op->count = count;
2226 memcpy(op->constants, constants, size);
2228 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2231 static void wined3d_cs_exec_reset_state(struct wined3d_cs *cs, const void *data)
2233 const struct wined3d_device *device = cs->c.device;
2234 const struct wined3d_cs_reset_state *op = data;
2235 const struct wined3d_state_entry *state_table;
2236 unsigned int state;
2238 state_cleanup(&cs->state);
2239 wined3d_state_reset(&cs->state, &device->adapter->d3d_info);
2240 if (op->invalidate)
2242 state_table = device->state_table;
2243 for (state = 0; state <= STATE_HIGHEST; ++state)
2245 if (state_table[state].representative)
2246 device_invalidate_state(device, state);
2251 void wined3d_device_context_emit_reset_state(struct wined3d_device_context *context, bool invalidate)
2253 struct wined3d_cs_reset_state *op;
2255 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2256 op->opcode = WINED3D_CS_OP_RESET_STATE;
2257 op->invalidate = invalidate;
2259 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2262 static void wined3d_cs_exec_callback(struct wined3d_cs *cs, const void *data)
2264 const struct wined3d_cs_callback *op = data;
2266 op->callback(op->object);
2269 static void wined3d_cs_emit_callback(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2271 struct wined3d_cs_callback *op;
2273 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2274 op->opcode = WINED3D_CS_OP_CALLBACK;
2275 op->callback = callback;
2276 op->object = object;
2278 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2281 void wined3d_cs_destroy_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2283 wined3d_cs_emit_callback(cs, callback, object);
2286 void wined3d_cs_init_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2288 wined3d_cs_emit_callback(cs, callback, object);
2291 static void wined3d_cs_exec_query_issue(struct wined3d_cs *cs, const void *data)
2293 const struct wined3d_cs_query_issue *op = data;
2294 struct wined3d_query *query = op->query;
2295 BOOL poll;
2297 poll = query->query_ops->query_issue(query, op->flags);
2299 if (!cs->thread)
2300 return;
2302 if (poll && list_empty(&query->poll_list_entry))
2304 if (query->buffer_object)
2305 InterlockedIncrement(&query->counter_retrieved);
2306 else
2307 list_add_tail(&cs->query_poll_list, &query->poll_list_entry);
2308 return;
2311 /* This can happen if occlusion queries are restarted. This discards the
2312 * old result, since polling it could result in a GL error. */
2313 if ((op->flags & WINED3DISSUE_BEGIN) && !poll && !list_empty(&query->poll_list_entry))
2315 list_remove(&query->poll_list_entry);
2316 list_init(&query->poll_list_entry);
2317 InterlockedIncrement(&query->counter_retrieved);
2318 return;
2321 /* This can happen when an occlusion query is ended without being started,
2322 * in which case we don't want to poll, but still have to counter-balance
2323 * the increment of the main counter.
2325 * This can also happen if an event query is re-issued before the first
2326 * fence was reached. In this case the query is already in the list and
2327 * the poll function will check the new fence. We have to counter-balance
2328 * the discarded increment. */
2329 if (op->flags & WINED3DISSUE_END)
2330 InterlockedIncrement(&query->counter_retrieved);
2333 static void wined3d_cs_issue_query(struct wined3d_device_context *context,
2334 struct wined3d_query *query, unsigned int flags)
2336 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2337 struct wined3d_cs_query_issue *op;
2339 if (flags & WINED3DISSUE_END)
2340 ++query->counter_main;
2342 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2343 op->opcode = WINED3D_CS_OP_QUERY_ISSUE;
2344 op->query = query;
2345 op->flags = flags;
2347 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2348 cs->queries_flushed = FALSE;
2350 if (flags & WINED3DISSUE_BEGIN)
2351 query->state = QUERY_BUILDING;
2352 else
2353 query->state = QUERY_SIGNALLED;
2356 static void wined3d_cs_execute_command_list(struct wined3d_device_context *context,
2357 struct wined3d_command_list *list, bool restore_state)
2359 struct wined3d_cs_execute_command_list *op;
2360 SIZE_T i;
2362 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2363 op->opcode = WINED3D_CS_OP_EXECUTE_COMMAND_LIST;
2364 op->list = list;
2366 for (i = 0; i < list->resource_count; ++i)
2367 wined3d_resource_acquire(list->resources[i]);
2369 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2371 if (restore_state)
2372 wined3d_device_context_set_state(context, context->state);
2373 else
2374 wined3d_device_context_reset_state(context);
2377 static void wined3d_cs_exec_preload_resource(struct wined3d_cs *cs, const void *data)
2379 const struct wined3d_cs_preload_resource *op = data;
2380 struct wined3d_resource *resource = op->resource;
2382 resource->resource_ops->resource_preload(resource);
2383 wined3d_resource_release(resource);
2386 void wined3d_cs_emit_preload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
2388 struct wined3d_cs_preload_resource *op;
2390 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2391 op->opcode = WINED3D_CS_OP_PRELOAD_RESOURCE;
2392 op->resource = resource;
2394 wined3d_resource_acquire(resource);
2396 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2399 static void wined3d_cs_exec_unload_resource(struct wined3d_cs *cs, const void *data)
2401 const struct wined3d_cs_unload_resource *op = data;
2402 struct wined3d_resource *resource = op->resource;
2404 resource->resource_ops->resource_unload(resource);
2405 wined3d_resource_release(resource);
2408 void wined3d_cs_emit_unload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
2410 struct wined3d_cs_unload_resource *op;
2412 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2413 op->opcode = WINED3D_CS_OP_UNLOAD_RESOURCE;
2414 op->resource = resource;
2416 wined3d_resource_acquire(resource);
2418 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2421 static void wined3d_device_context_upload_bo(struct wined3d_device_context *context,
2422 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
2423 const struct wined3d_const_bo_address *addr, unsigned int row_pitch, unsigned int slice_pitch)
2425 struct wined3d_cs_update_sub_resource *op;
2427 TRACE("context %p, resource %p, sub_resource_idx %u, box %s, addr %s, row_pitch %u, slice_pitch %u.\n",
2428 context, resource, sub_resource_idx, debug_box(box), debug_const_bo_address(addr), row_pitch, slice_pitch);
2430 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2431 op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2432 op->resource = resource;
2433 op->sub_resource_idx = sub_resource_idx;
2434 op->box = *box;
2435 op->addr = *addr;
2436 op->row_pitch = row_pitch;
2437 op->slice_pitch = slice_pitch;
2439 wined3d_device_context_acquire_resource(context, resource);
2441 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2444 static void wined3d_cs_exec_map(struct wined3d_cs *cs, const void *data)
2446 const struct wined3d_cs_map *op = data;
2447 struct wined3d_resource *resource = op->resource;
2449 *op->hr = resource->resource_ops->resource_sub_resource_map(resource,
2450 op->sub_resource_idx, op->map_ptr, op->box, op->flags);
2453 HRESULT wined3d_device_context_emit_map(struct wined3d_device_context *context, struct wined3d_resource *resource,
2454 unsigned int sub_resource_idx, void **map_ptr, const struct wined3d_box *box, unsigned int flags)
2456 struct wined3d_const_bo_address addr;
2457 unsigned int row_pitch, slice_pitch;
2458 struct wined3d_cs_map *op;
2459 HRESULT hr;
2461 wined3d_resource_get_sub_resource_map_pitch(resource, sub_resource_idx, &row_pitch, &slice_pitch);
2463 if ((*map_ptr = context->ops->prepare_upload_bo(context, resource,
2464 sub_resource_idx, box, row_pitch, slice_pitch, flags, &addr)))
2466 TRACE("Returning upload bo %s, map pointer %p, row pitch %u, slice pitch %u.\n",
2467 debug_const_bo_address(&addr), *map_ptr, row_pitch, slice_pitch);
2468 return WINED3D_OK;
2471 /* Mapping resources from the worker thread isn't an issue by itself, but
2472 * increasing the map count would be visible to applications. */
2473 wined3d_not_from_cs(context->device);
2475 wined3d_resource_wait_idle(resource);
2477 if (!(op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_MAP)))
2478 return E_OUTOFMEMORY;
2479 op->opcode = WINED3D_CS_OP_MAP;
2480 op->resource = resource;
2481 op->sub_resource_idx = sub_resource_idx;
2482 op->map_ptr = map_ptr;
2483 op->box = box;
2484 op->flags = flags;
2485 op->hr = &hr;
2487 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_MAP);
2488 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_MAP);
2490 return hr;
2493 static void wined3d_cs_exec_unmap(struct wined3d_cs *cs, const void *data)
2495 const struct wined3d_cs_unmap *op = data;
2496 struct wined3d_resource *resource = op->resource;
2498 *op->hr = resource->resource_ops->resource_sub_resource_unmap(resource, op->sub_resource_idx);
2501 HRESULT wined3d_device_context_emit_unmap(struct wined3d_device_context *context,
2502 struct wined3d_resource *resource, unsigned int sub_resource_idx)
2504 struct wined3d_const_bo_address addr;
2505 struct wined3d_cs_unmap *op;
2506 struct wined3d_box box;
2507 HRESULT hr;
2509 if (context->ops->get_upload_bo(context, resource, sub_resource_idx, &box, &addr))
2511 unsigned int row_pitch, slice_pitch;
2513 wined3d_resource_get_sub_resource_map_pitch(resource, sub_resource_idx, &row_pitch, &slice_pitch);
2514 wined3d_device_context_upload_bo(context, resource, sub_resource_idx, &box, &addr, row_pitch, slice_pitch);
2515 return WINED3D_OK;
2518 wined3d_not_from_cs(context->device);
2520 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2521 op->opcode = WINED3D_CS_OP_UNMAP;
2522 op->resource = resource;
2523 op->sub_resource_idx = sub_resource_idx;
2524 op->hr = &hr;
2526 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_MAP);
2527 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_MAP);
2529 return hr;
2532 static void wined3d_cs_exec_blt_sub_resource(struct wined3d_cs *cs, const void *data)
2534 const struct wined3d_cs_blt_sub_resource *op = data;
2536 if (op->dst_resource->type == WINED3D_RTYPE_BUFFER)
2538 wined3d_buffer_copy(buffer_from_resource(op->dst_resource), op->dst_box.left,
2539 buffer_from_resource(op->src_resource), op->src_box.left,
2540 op->src_box.right - op->src_box.left);
2542 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_3D)
2544 struct wined3d_texture *src_texture, *dst_texture;
2545 unsigned int level, update_w, update_h, update_d;
2546 unsigned int row_pitch, slice_pitch;
2547 struct wined3d_context *context;
2548 struct wined3d_bo_address addr;
2550 if (op->flags & ~WINED3D_BLT_RAW)
2552 FIXME("Flags %#x not implemented for %s resources.\n",
2553 op->flags, debug_d3dresourcetype(op->dst_resource->type));
2554 goto error;
2557 if (!(op->flags & WINED3D_BLT_RAW) && op->src_resource->format != op->dst_resource->format)
2559 FIXME("Format conversion not implemented for %s resources.\n",
2560 debug_d3dresourcetype(op->dst_resource->type));
2561 goto error;
2564 update_w = op->dst_box.right - op->dst_box.left;
2565 update_h = op->dst_box.bottom - op->dst_box.top;
2566 update_d = op->dst_box.back - op->dst_box.front;
2567 if (op->src_box.right - op->src_box.left != update_w
2568 || op->src_box.bottom - op->src_box.top != update_h
2569 || op->src_box.back - op->src_box.front != update_d)
2571 FIXME("Stretching not implemented for %s resources.\n",
2572 debug_d3dresourcetype(op->dst_resource->type));
2573 goto error;
2576 dst_texture = texture_from_resource(op->dst_resource);
2577 src_texture = texture_from_resource(op->src_resource);
2579 context = context_acquire(cs->c.device, NULL, 0);
2581 if (!wined3d_texture_load_location(src_texture, op->src_sub_resource_idx,
2582 context, src_texture->resource.map_binding))
2584 ERR("Failed to load source sub-resource into %s.\n",
2585 wined3d_debug_location(src_texture->resource.map_binding));
2586 context_release(context);
2587 goto error;
2590 level = op->dst_sub_resource_idx % dst_texture->level_count;
2591 if (update_w == wined3d_texture_get_level_width(dst_texture, level)
2592 && update_h == wined3d_texture_get_level_height(dst_texture, level)
2593 && update_d == wined3d_texture_get_level_depth(dst_texture, level))
2595 wined3d_texture_prepare_location(dst_texture, op->dst_sub_resource_idx,
2596 context, WINED3D_LOCATION_TEXTURE_RGB);
2598 else if (!wined3d_texture_load_location(dst_texture, op->dst_sub_resource_idx,
2599 context, WINED3D_LOCATION_TEXTURE_RGB))
2601 ERR("Failed to load destination sub-resource.\n");
2602 context_release(context);
2603 goto error;
2606 wined3d_texture_get_memory(src_texture, op->src_sub_resource_idx, &addr, src_texture->resource.map_binding);
2607 wined3d_texture_get_pitch(src_texture, op->src_sub_resource_idx % src_texture->level_count,
2608 &row_pitch, &slice_pitch);
2610 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&addr),
2611 dst_texture->resource.format, &op->src_box, row_pitch, slice_pitch, dst_texture,
2612 op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
2613 op->dst_box.left, op->dst_box.top, op->dst_box.front);
2614 wined3d_texture_validate_location(dst_texture, op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2615 wined3d_texture_invalidate_location(dst_texture, op->dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2617 context_release(context);
2619 else
2621 if (FAILED(texture2d_blt(texture_from_resource(op->dst_resource), op->dst_sub_resource_idx,
2622 &op->dst_box, texture_from_resource(op->src_resource), op->src_sub_resource_idx,
2623 &op->src_box, op->flags, &op->fx, op->filter)))
2624 FIXME("Blit failed.\n");
2627 error:
2628 if (op->src_resource)
2629 wined3d_resource_release(op->src_resource);
2630 wined3d_resource_release(op->dst_resource);
2633 void wined3d_device_context_emit_blt_sub_resource(struct wined3d_device_context *context,
2634 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, const struct wined3d_box *dst_box,
2635 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx, const struct wined3d_box *src_box,
2636 unsigned int flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2638 struct wined3d_cs_blt_sub_resource *op;
2640 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2641 op->opcode = WINED3D_CS_OP_BLT_SUB_RESOURCE;
2642 op->dst_resource = dst_resource;
2643 op->dst_sub_resource_idx = dst_sub_resource_idx;
2644 op->dst_box = *dst_box;
2645 op->src_resource = src_resource;
2646 op->src_sub_resource_idx = src_sub_resource_idx;
2647 op->src_box = *src_box;
2648 op->flags = flags;
2649 if (fx)
2650 op->fx = *fx;
2651 else
2652 memset(&op->fx, 0, sizeof(op->fx));
2653 op->filter = filter;
2655 wined3d_device_context_acquire_resource(context, dst_resource);
2656 if (src_resource)
2657 wined3d_device_context_acquire_resource(context, src_resource);
2659 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2660 if (flags & WINED3D_BLT_SYNCHRONOUS)
2661 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_DEFAULT);
2664 static void wined3d_cs_exec_update_sub_resource(struct wined3d_cs *cs, const void *data)
2666 const struct wined3d_cs_update_sub_resource *op = data;
2667 struct wined3d_resource *resource = op->resource;
2668 const struct wined3d_box *box = &op->box;
2669 unsigned int width, height, depth, level;
2670 struct wined3d_context *context;
2671 struct wined3d_texture *texture;
2672 struct wined3d_box src_box;
2674 context = context_acquire(cs->c.device, NULL, 0);
2676 if (resource->type == WINED3D_RTYPE_BUFFER)
2678 struct wined3d_buffer *buffer = buffer_from_resource(resource);
2680 wined3d_buffer_copy_bo_address(buffer, context, box->left, &op->addr, box->right - box->left);
2681 goto done;
2684 texture = wined3d_texture_from_resource(resource);
2686 level = op->sub_resource_idx % texture->level_count;
2687 width = wined3d_texture_get_level_width(texture, level);
2688 height = wined3d_texture_get_level_height(texture, level);
2689 depth = wined3d_texture_get_level_depth(texture, level);
2691 /* Only load the sub-resource for partial updates. */
2692 if (!box->left && !box->top && !box->front
2693 && box->right == width && box->bottom == height && box->back == depth)
2694 wined3d_texture_prepare_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2695 else
2696 wined3d_texture_load_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2698 wined3d_box_set(&src_box, 0, 0, box->right - box->left, box->bottom - box->top, 0, box->back - box->front);
2699 texture->texture_ops->texture_upload_data(context, &op->addr, texture->resource.format, &src_box,
2700 op->row_pitch, op->slice_pitch, texture, op->sub_resource_idx,
2701 WINED3D_LOCATION_TEXTURE_RGB, box->left, box->top, box->front);
2703 wined3d_texture_validate_location(texture, op->sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2704 wined3d_texture_invalidate_location(texture, op->sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2706 done:
2707 context_release(context);
2709 wined3d_resource_release(resource);
2712 void wined3d_device_context_emit_update_sub_resource(struct wined3d_device_context *context,
2713 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
2714 const void *data, unsigned int row_pitch, unsigned int slice_pitch)
2716 struct wined3d_cs_update_sub_resource *op;
2717 struct wined3d_const_bo_address src_addr;
2718 void *map_ptr;
2720 if ((map_ptr = context->ops->prepare_upload_bo(context, resource, sub_resource_idx, box,
2721 row_pitch, slice_pitch, WINED3D_MAP_WRITE, &src_addr)))
2723 wined3d_format_copy_data(resource->format, data, row_pitch, slice_pitch, map_ptr, row_pitch, slice_pitch,
2724 box->right - box->left, box->bottom - box->top, box->back - box->front);
2725 wined3d_device_context_upload_bo(context, resource, sub_resource_idx, box, &src_addr, row_pitch, slice_pitch);
2726 return;
2729 wined3d_resource_wait_idle(resource);
2731 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2732 op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2733 op->resource = resource;
2734 op->sub_resource_idx = sub_resource_idx;
2735 op->box = *box;
2736 op->addr.buffer_object = 0;
2737 op->addr.addr = data;
2738 op->row_pitch = row_pitch;
2739 op->slice_pitch = slice_pitch;
2741 wined3d_device_context_acquire_resource(context, resource);
2743 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_MAP);
2744 /* The data pointer may go away, so we need to wait until it is read.
2745 * Copying the data may be faster if it's small. */
2746 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_MAP);
2749 static void wined3d_cs_exec_add_dirty_texture_region(struct wined3d_cs *cs, const void *data)
2751 const struct wined3d_cs_add_dirty_texture_region *op = data;
2752 struct wined3d_texture *texture = op->texture;
2753 unsigned int sub_resource_idx, i;
2754 struct wined3d_context *context;
2756 context = context_acquire(cs->c.device, NULL, 0);
2757 sub_resource_idx = op->layer * texture->level_count;
2758 for (i = 0; i < texture->level_count; ++i, ++sub_resource_idx)
2760 if (wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
2761 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2762 else
2763 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
2765 context_release(context);
2767 wined3d_resource_release(&texture->resource);
2770 void wined3d_cs_emit_add_dirty_texture_region(struct wined3d_cs *cs,
2771 struct wined3d_texture *texture, unsigned int layer)
2773 struct wined3d_cs_add_dirty_texture_region *op;
2775 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2776 op->opcode = WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION;
2777 op->texture = texture;
2778 op->layer = layer;
2780 wined3d_resource_acquire(&texture->resource);
2782 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2785 static void wined3d_cs_exec_clear_unordered_access_view(struct wined3d_cs *cs, const void *data)
2787 const struct wined3d_cs_clear_unordered_access_view *op = data;
2788 struct wined3d_unordered_access_view *view = op->view;
2789 struct wined3d_context *context;
2791 context = context_acquire(cs->c.device, NULL, 0);
2792 cs->c.device->adapter->adapter_ops->adapter_clear_uav(context, view, &op->clear_value, op->fp);
2793 context_release(context);
2795 wined3d_resource_release(view->resource);
2798 void wined3d_device_context_emit_clear_uav(struct wined3d_device_context *context,
2799 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value, bool fp)
2801 struct wined3d_cs_clear_unordered_access_view *op;
2803 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2804 op->opcode = WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW;
2805 op->view = view;
2806 op->clear_value = *clear_value;
2807 op->fp = fp;
2809 wined3d_device_context_acquire_resource(context, view->resource);
2811 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2814 static void wined3d_cs_exec_copy_uav_counter(struct wined3d_cs *cs, const void *data)
2816 const struct wined3d_cs_copy_uav_counter *op = data;
2817 struct wined3d_unordered_access_view *view = op->view;
2818 struct wined3d_context *context;
2820 context = context_acquire(cs->c.device, NULL, 0);
2821 wined3d_unordered_access_view_copy_counter(view, op->buffer, op->offset, context);
2822 context_release(context);
2824 wined3d_resource_release(&op->buffer->resource);
2827 void wined3d_device_context_emit_copy_uav_counter(struct wined3d_device_context *context,
2828 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
2830 struct wined3d_cs_copy_uav_counter *op;
2832 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2833 op->opcode = WINED3D_CS_OP_COPY_UAV_COUNTER;
2834 op->buffer = dst_buffer;
2835 op->offset = offset;
2836 op->view = uav;
2838 wined3d_device_context_acquire_resource(context, &dst_buffer->resource);
2840 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2843 static void wined3d_cs_exec_generate_mipmaps(struct wined3d_cs *cs, const void *data)
2845 const struct wined3d_cs_generate_mipmaps *op = data;
2846 struct wined3d_shader_resource_view *view = op->view;
2847 struct wined3d_context *context;
2849 context = context_acquire(cs->c.device, NULL, 0);
2850 cs->c.device->adapter->adapter_ops->adapter_generate_mipmap(context, view);
2851 context_release(context);
2853 wined3d_resource_release(view->resource);
2856 void wined3d_device_context_emit_generate_mipmaps(struct wined3d_device_context *context,
2857 struct wined3d_shader_resource_view *view)
2859 struct wined3d_cs_generate_mipmaps *op;
2861 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2862 op->opcode = WINED3D_CS_OP_GENERATE_MIPMAPS;
2863 op->view = view;
2865 wined3d_device_context_acquire_resource(context, view->resource);
2867 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2870 static void wined3d_cs_emit_stop(struct wined3d_cs *cs)
2872 struct wined3d_cs_stop *op;
2874 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2875 op->opcode = WINED3D_CS_OP_STOP;
2877 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2878 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2881 static void wined3d_cs_acquire_resource(struct wined3d_device_context *context, struct wined3d_resource *resource)
2883 wined3d_resource_acquire(resource);
2886 static void wined3d_cs_exec_execute_command_list(struct wined3d_cs *cs, const void *data);
2888 static void (* const wined3d_cs_op_handlers[])(struct wined3d_cs *cs, const void *data) =
2890 /* WINED3D_CS_OP_NOP */ wined3d_cs_exec_nop,
2891 /* WINED3D_CS_OP_PRESENT */ wined3d_cs_exec_present,
2892 /* WINED3D_CS_OP_CLEAR */ wined3d_cs_exec_clear,
2893 /* WINED3D_CS_OP_DISPATCH */ wined3d_cs_exec_dispatch,
2894 /* WINED3D_CS_OP_DRAW */ wined3d_cs_exec_draw,
2895 /* WINED3D_CS_OP_FLUSH */ wined3d_cs_exec_flush,
2896 /* WINED3D_CS_OP_SET_PREDICATION */ wined3d_cs_exec_set_predication,
2897 /* WINED3D_CS_OP_SET_VIEWPORTS */ wined3d_cs_exec_set_viewports,
2898 /* WINED3D_CS_OP_SET_SCISSOR_RECTS */ wined3d_cs_exec_set_scissor_rects,
2899 /* WINED3D_CS_OP_SET_RENDERTARGET_VIEW */ wined3d_cs_exec_set_rendertarget_view,
2900 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW */ wined3d_cs_exec_set_depth_stencil_view,
2901 /* WINED3D_CS_OP_SET_VERTEX_DECLARATION */ wined3d_cs_exec_set_vertex_declaration,
2902 /* WINED3D_CS_OP_SET_STREAM_SOURCE */ wined3d_cs_exec_set_stream_source,
2903 /* WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ */ wined3d_cs_exec_set_stream_source_freq,
2904 /* WINED3D_CS_OP_SET_STREAM_OUTPUT */ wined3d_cs_exec_set_stream_output,
2905 /* WINED3D_CS_OP_SET_INDEX_BUFFER */ wined3d_cs_exec_set_index_buffer,
2906 /* WINED3D_CS_OP_SET_CONSTANT_BUFFER */ wined3d_cs_exec_set_constant_buffer,
2907 /* WINED3D_CS_OP_SET_TEXTURE */ wined3d_cs_exec_set_texture,
2908 /* WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW */ wined3d_cs_exec_set_shader_resource_view,
2909 /* WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_set_unordered_access_view,
2910 /* WINED3D_CS_OP_SET_SAMPLER */ wined3d_cs_exec_set_sampler,
2911 /* WINED3D_CS_OP_SET_SHADER */ wined3d_cs_exec_set_shader,
2912 /* WINED3D_CS_OP_SET_BLEND_STATE */ wined3d_cs_exec_set_blend_state,
2913 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE */ wined3d_cs_exec_set_depth_stencil_state,
2914 /* WINED3D_CS_OP_SET_RASTERIZER_STATE */ wined3d_cs_exec_set_rasterizer_state,
2915 /* WINED3D_CS_OP_SET_RENDER_STATE */ wined3d_cs_exec_set_render_state,
2916 /* WINED3D_CS_OP_SET_TEXTURE_STATE */ wined3d_cs_exec_set_texture_state,
2917 /* WINED3D_CS_OP_SET_SAMPLER_STATE */ wined3d_cs_exec_set_sampler_state,
2918 /* WINED3D_CS_OP_SET_TRANSFORM */ wined3d_cs_exec_set_transform,
2919 /* WINED3D_CS_OP_SET_CLIP_PLANE */ wined3d_cs_exec_set_clip_plane,
2920 /* WINED3D_CS_OP_SET_COLOR_KEY */ wined3d_cs_exec_set_color_key,
2921 /* WINED3D_CS_OP_SET_MATERIAL */ wined3d_cs_exec_set_material,
2922 /* WINED3D_CS_OP_SET_LIGHT */ wined3d_cs_exec_set_light,
2923 /* WINED3D_CS_OP_SET_LIGHT_ENABLE */ wined3d_cs_exec_set_light_enable,
2924 /* WINED3D_CS_OP_SET_FEATURE_LEVEL */ wined3d_cs_exec_set_feature_level,
2925 /* WINED3D_CS_OP_PUSH_CONSTANTS */ wined3d_cs_exec_push_constants,
2926 /* WINED3D_CS_OP_RESET_STATE */ wined3d_cs_exec_reset_state,
2927 /* WINED3D_CS_OP_CALLBACK */ wined3d_cs_exec_callback,
2928 /* WINED3D_CS_OP_QUERY_ISSUE */ wined3d_cs_exec_query_issue,
2929 /* WINED3D_CS_OP_PRELOAD_RESOURCE */ wined3d_cs_exec_preload_resource,
2930 /* WINED3D_CS_OP_UNLOAD_RESOURCE */ wined3d_cs_exec_unload_resource,
2931 /* WINED3D_CS_OP_MAP */ wined3d_cs_exec_map,
2932 /* WINED3D_CS_OP_UNMAP */ wined3d_cs_exec_unmap,
2933 /* WINED3D_CS_OP_BLT_SUB_RESOURCE */ wined3d_cs_exec_blt_sub_resource,
2934 /* WINED3D_CS_OP_UPDATE_SUB_RESOURCE */ wined3d_cs_exec_update_sub_resource,
2935 /* WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION */ wined3d_cs_exec_add_dirty_texture_region,
2936 /* WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_clear_unordered_access_view,
2937 /* WINED3D_CS_OP_COPY_UAV_COUNTER */ wined3d_cs_exec_copy_uav_counter,
2938 /* WINED3D_CS_OP_GENERATE_MIPMAPS */ wined3d_cs_exec_generate_mipmaps,
2939 /* WINED3D_CS_OP_EXECUTE_COMMAND_LIST */ wined3d_cs_exec_execute_command_list,
2942 static void wined3d_cs_exec_execute_command_list(struct wined3d_cs *cs, const void *data)
2944 const struct wined3d_cs_execute_command_list *op = data;
2945 size_t start = 0, end = op->list->data_size;
2946 const BYTE *cs_data = op->list->data;
2948 TRACE("Executing command list %p.\n", op->list);
2950 while (start < end)
2952 const struct wined3d_cs_packet *packet = (const struct wined3d_cs_packet *)&cs_data[start];
2953 enum wined3d_cs_op opcode = *(const enum wined3d_cs_op *)packet->data;
2955 if (opcode >= WINED3D_CS_OP_STOP)
2956 ERR("Invalid opcode %#x.\n", opcode);
2957 else
2958 wined3d_cs_op_handlers[opcode](cs, packet->data);
2959 TRACE("%s executed.\n", debug_cs_op(opcode));
2961 start += offsetof(struct wined3d_cs_packet, data[packet->size]);
2965 static void *wined3d_cs_st_require_space(struct wined3d_device_context *context,
2966 size_t size, enum wined3d_cs_queue_id queue_id)
2968 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2970 if (size > (cs->data_size - cs->end))
2972 size_t new_size;
2973 void *new_data;
2975 new_size = max(size, cs->data_size * 2);
2976 if (!cs->end)
2977 new_data = heap_realloc(cs->data, new_size);
2978 else
2979 new_data = heap_alloc(new_size);
2980 if (!new_data)
2981 return NULL;
2983 cs->data_size = new_size;
2984 cs->start = cs->end = 0;
2985 cs->data = new_data;
2988 cs->end += size;
2990 return (BYTE *)cs->data + cs->start;
2993 static void wined3d_cs_st_submit(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
2995 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2996 enum wined3d_cs_op opcode;
2997 size_t start;
2998 BYTE *data;
3000 data = cs->data;
3001 start = cs->start;
3002 cs->start = cs->end;
3004 opcode = *(const enum wined3d_cs_op *)&data[start];
3005 if (opcode >= WINED3D_CS_OP_STOP)
3006 ERR("Invalid opcode %#x.\n", opcode);
3007 else
3008 wined3d_cs_op_handlers[opcode](cs, &data[start]);
3010 if (cs->data == data)
3011 cs->start = cs->end = start;
3012 else if (!start)
3013 heap_free(data);
3016 static void wined3d_cs_st_finish(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
3020 static void *wined3d_cs_prepare_upload_bo(struct wined3d_device_context *context, struct wined3d_resource *resource,
3021 unsigned int sub_resource_idx, const struct wined3d_box *box, unsigned int row_pitch,
3022 unsigned int slice_pitch, uint32_t flags, struct wined3d_const_bo_address *address)
3024 /* FIXME: We would like to return mapped or newly allocated memory here. */
3025 return NULL;
3028 static bool wined3d_cs_get_upload_bo(struct wined3d_device_context *context, struct wined3d_resource *resource,
3029 unsigned int sub_resource_idx, struct wined3d_box *box, struct wined3d_const_bo_address *address)
3031 return false;
3034 static const struct wined3d_device_context_ops wined3d_cs_st_ops =
3036 wined3d_cs_st_require_space,
3037 wined3d_cs_st_submit,
3038 wined3d_cs_st_finish,
3039 wined3d_cs_st_push_constants,
3040 wined3d_cs_prepare_upload_bo,
3041 wined3d_cs_get_upload_bo,
3042 wined3d_cs_issue_query,
3043 wined3d_cs_flush,
3044 wined3d_cs_acquire_resource,
3045 wined3d_cs_execute_command_list,
3048 static BOOL wined3d_cs_queue_is_empty(const struct wined3d_cs *cs, const struct wined3d_cs_queue *queue)
3050 wined3d_from_cs(cs);
3051 return *(volatile LONG *)&queue->head == queue->tail;
3054 static void wined3d_cs_queue_submit(struct wined3d_cs_queue *queue, struct wined3d_cs *cs)
3056 struct wined3d_cs_packet *packet;
3057 size_t packet_size;
3059 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
3060 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
3061 InterlockedExchange(&queue->head, (queue->head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1));
3063 if (InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
3064 SetEvent(cs->event);
3067 static void wined3d_cs_mt_submit(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
3069 struct wined3d_cs *cs = wined3d_cs_from_context(context);
3071 if (cs->thread_id == GetCurrentThreadId())
3072 return wined3d_cs_st_submit(context, queue_id);
3074 wined3d_cs_queue_submit(&cs->queue[queue_id], cs);
3077 static void *wined3d_cs_queue_require_space(struct wined3d_cs_queue *queue, size_t size, struct wined3d_cs *cs)
3079 size_t queue_size = ARRAY_SIZE(queue->data);
3080 size_t header_size, packet_size, remaining;
3081 struct wined3d_cs_packet *packet;
3083 header_size = FIELD_OFFSET(struct wined3d_cs_packet, data[0]);
3084 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[size]);
3085 packet_size = (packet_size + header_size - 1) & ~(header_size - 1);
3086 size = packet_size - header_size;
3087 if (packet_size >= WINED3D_CS_QUEUE_SIZE)
3089 ERR("Packet size %lu >= queue size %u.\n",
3090 (unsigned long)packet_size, WINED3D_CS_QUEUE_SIZE);
3091 return NULL;
3094 remaining = queue_size - queue->head;
3095 if (remaining < packet_size)
3097 size_t nop_size = remaining - header_size;
3098 struct wined3d_cs_nop *nop;
3100 TRACE("Inserting a nop for %lu + %lu bytes.\n",
3101 (unsigned long)header_size, (unsigned long)nop_size);
3103 nop = wined3d_cs_queue_require_space(queue, nop_size, cs);
3104 if (nop_size)
3105 nop->opcode = WINED3D_CS_OP_NOP;
3107 wined3d_cs_queue_submit(queue, cs);
3108 assert(!queue->head);
3111 for (;;)
3113 LONG tail = *(volatile LONG *)&queue->tail;
3114 LONG head = queue->head;
3115 LONG new_pos;
3117 /* Empty. */
3118 if (head == tail)
3119 break;
3120 new_pos = (head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1);
3121 /* Head ahead of tail. We checked the remaining size above, so we only
3122 * need to make sure we don't make head equal to tail. */
3123 if (head > tail && (new_pos != tail))
3124 break;
3125 /* Tail ahead of head. Make sure the new head is before the tail as
3126 * well. Note that new_pos is 0 when it's at the end of the queue. */
3127 if (new_pos < tail && new_pos)
3128 break;
3130 TRACE("Waiting for free space. Head %u, tail %u, packet size %lu.\n",
3131 head, tail, (unsigned long)packet_size);
3134 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
3135 packet->size = size;
3136 return packet->data;
3139 static void *wined3d_cs_mt_require_space(struct wined3d_device_context *context,
3140 size_t size, enum wined3d_cs_queue_id queue_id)
3142 struct wined3d_cs *cs = wined3d_cs_from_context(context);
3144 if (cs->thread_id == GetCurrentThreadId())
3145 return wined3d_cs_st_require_space(context, size, queue_id);
3147 return wined3d_cs_queue_require_space(&cs->queue[queue_id], size, cs);
3150 static void wined3d_cs_mt_finish(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
3152 struct wined3d_cs *cs = wined3d_cs_from_context(context);
3154 if (cs->thread_id == GetCurrentThreadId())
3155 return wined3d_cs_st_finish(context, queue_id);
3157 while (cs->queue[queue_id].head != *(volatile LONG *)&cs->queue[queue_id].tail)
3158 YieldProcessor();
3161 static const struct wined3d_device_context_ops wined3d_cs_mt_ops =
3163 wined3d_cs_mt_require_space,
3164 wined3d_cs_mt_submit,
3165 wined3d_cs_mt_finish,
3166 wined3d_cs_mt_push_constants,
3167 wined3d_cs_prepare_upload_bo,
3168 wined3d_cs_get_upload_bo,
3169 wined3d_cs_issue_query,
3170 wined3d_cs_flush,
3171 wined3d_cs_acquire_resource,
3172 wined3d_cs_execute_command_list,
3175 static void poll_queries(struct wined3d_cs *cs)
3177 struct wined3d_query *query, *cursor;
3179 LIST_FOR_EACH_ENTRY_SAFE(query, cursor, &cs->query_poll_list, struct wined3d_query, poll_list_entry)
3181 if (!query->query_ops->query_poll(query, 0))
3182 continue;
3184 list_remove(&query->poll_list_entry);
3185 list_init(&query->poll_list_entry);
3186 InterlockedIncrement(&query->counter_retrieved);
3190 static void wined3d_cs_wait_event(struct wined3d_cs *cs)
3192 InterlockedExchange(&cs->waiting_for_event, TRUE);
3194 /* The main thread might have enqueued a command and blocked on it after
3195 * the CS thread decided to enter wined3d_cs_wait_event(), but before
3196 * "waiting_for_event" was set.
3198 * Likewise, we can race with the main thread when resetting
3199 * "waiting_for_event", in which case we would need to call
3200 * WaitForSingleObject() because the main thread called SetEvent(). */
3201 if (!(wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_DEFAULT])
3202 && wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_MAP]))
3203 && InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
3204 return;
3206 WaitForSingleObject(cs->event, INFINITE);
3209 static void wined3d_cs_command_lock(const struct wined3d_cs *cs)
3211 if (cs->serialize_commands)
3212 EnterCriticalSection(&wined3d_command_cs);
3215 static void wined3d_cs_command_unlock(const struct wined3d_cs *cs)
3217 if (cs->serialize_commands)
3218 LeaveCriticalSection(&wined3d_command_cs);
3221 static DWORD WINAPI wined3d_cs_run(void *ctx)
3223 struct wined3d_cs_packet *packet;
3224 struct wined3d_cs_queue *queue;
3225 unsigned int spin_count = 0;
3226 struct wined3d_cs *cs = ctx;
3227 enum wined3d_cs_op opcode;
3228 HMODULE wined3d_module;
3229 unsigned int poll = 0;
3230 LONG tail;
3232 TRACE("Started.\n");
3234 /* Copy the module handle to a local variable to avoid racing with the
3235 * thread freeing "cs" before the FreeLibraryAndExitThread() call. */
3236 wined3d_module = cs->wined3d_module;
3238 list_init(&cs->query_poll_list);
3239 cs->thread_id = GetCurrentThreadId();
3240 for (;;)
3242 if (++poll == WINED3D_CS_QUERY_POLL_INTERVAL)
3244 wined3d_cs_command_lock(cs);
3245 poll_queries(cs);
3246 wined3d_cs_command_unlock(cs);
3247 poll = 0;
3250 queue = &cs->queue[WINED3D_CS_QUEUE_MAP];
3251 if (wined3d_cs_queue_is_empty(cs, queue))
3253 queue = &cs->queue[WINED3D_CS_QUEUE_DEFAULT];
3254 if (wined3d_cs_queue_is_empty(cs, queue))
3256 if (++spin_count >= WINED3D_CS_SPIN_COUNT && list_empty(&cs->query_poll_list))
3257 wined3d_cs_wait_event(cs);
3258 continue;
3261 spin_count = 0;
3263 tail = queue->tail;
3264 packet = (struct wined3d_cs_packet *)&queue->data[tail];
3265 if (packet->size)
3267 opcode = *(const enum wined3d_cs_op *)packet->data;
3269 TRACE("Executing %s.\n", debug_cs_op(opcode));
3270 if (opcode >= WINED3D_CS_OP_STOP)
3272 if (opcode > WINED3D_CS_OP_STOP)
3273 ERR("Invalid opcode %#x.\n", opcode);
3274 break;
3277 wined3d_cs_command_lock(cs);
3278 wined3d_cs_op_handlers[opcode](cs, packet->data);
3279 wined3d_cs_command_unlock(cs);
3280 TRACE("%s executed.\n", debug_cs_op(opcode));
3283 tail += FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
3284 tail &= (WINED3D_CS_QUEUE_SIZE - 1);
3285 InterlockedExchange(&queue->tail, tail);
3288 cs->queue[WINED3D_CS_QUEUE_MAP].tail = cs->queue[WINED3D_CS_QUEUE_MAP].head;
3289 cs->queue[WINED3D_CS_QUEUE_DEFAULT].tail = cs->queue[WINED3D_CS_QUEUE_DEFAULT].head;
3290 TRACE("Stopped.\n");
3291 FreeLibraryAndExitThread(wined3d_module, 0);
3294 struct wined3d_cs *wined3d_cs_create(struct wined3d_device *device,
3295 const enum wined3d_feature_level *levels, unsigned int level_count)
3297 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3298 struct wined3d_cs *cs;
3300 if (!(cs = heap_alloc_zero(sizeof(*cs))))
3301 return NULL;
3303 if (FAILED(wined3d_state_create(device, levels, level_count, &cs->c.state)))
3305 heap_free(cs);
3306 return NULL;
3309 cs->c.ops = &wined3d_cs_st_ops;
3310 cs->c.device = device;
3311 cs->serialize_commands = TRACE_ON(d3d_sync) || wined3d_settings.cs_multithreaded & WINED3D_CSMT_SERIALIZE;
3313 if (cs->serialize_commands)
3314 ERR_(d3d_sync)("Forcing serialization of all command streams.\n");
3316 state_init(&cs->state, d3d_info, WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT, cs->c.state->feature_level);
3318 cs->data_size = WINED3D_INITIAL_CS_SIZE;
3319 if (!(cs->data = heap_alloc(cs->data_size)))
3320 goto fail;
3322 if (wined3d_settings.cs_multithreaded & WINED3D_CSMT_ENABLE
3323 && !RtlIsCriticalSectionLockedByThread(NtCurrentTeb()->Peb->LoaderLock))
3325 cs->c.ops = &wined3d_cs_mt_ops;
3327 if (!(cs->event = CreateEventW(NULL, FALSE, FALSE, NULL)))
3329 ERR("Failed to create command stream event.\n");
3330 heap_free(cs->data);
3331 goto fail;
3334 if (!(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
3335 (const WCHAR *)wined3d_cs_run, &cs->wined3d_module)))
3337 ERR("Failed to get wined3d module handle.\n");
3338 CloseHandle(cs->event);
3339 heap_free(cs->data);
3340 goto fail;
3343 if (!(cs->thread = CreateThread(NULL, 0, wined3d_cs_run, cs, 0, NULL)))
3345 ERR("Failed to create wined3d command stream thread.\n");
3346 FreeLibrary(cs->wined3d_module);
3347 CloseHandle(cs->event);
3348 heap_free(cs->data);
3349 goto fail;
3353 return cs;
3355 fail:
3356 wined3d_state_destroy(cs->c.state);
3357 state_cleanup(&cs->state);
3358 heap_free(cs);
3359 return NULL;
3362 void wined3d_cs_destroy(struct wined3d_cs *cs)
3364 if (cs->thread)
3366 wined3d_cs_emit_stop(cs);
3367 CloseHandle(cs->thread);
3368 if (!CloseHandle(cs->event))
3369 ERR("Closing event failed.\n");
3372 wined3d_state_destroy(cs->c.state);
3373 state_cleanup(&cs->state);
3374 heap_free(cs->data);
3375 heap_free(cs);
3378 struct wined3d_deferred_context
3380 struct wined3d_device_context c;
3382 SIZE_T data_size, data_capacity;
3383 void *data;
3385 SIZE_T resource_count, resources_capacity;
3386 struct wined3d_resource **resources;
3388 SIZE_T upload_count, uploads_capacity;
3389 struct wined3d_deferred_upload *uploads;
3391 /* List of command lists queued for execution on this context. A command
3392 * list can be the only thing holding a pointer to another command list, so
3393 * we need to hold a reference here and in wined3d_command_list as well. */
3394 SIZE_T command_list_count, command_lists_capacity;
3395 struct wined3d_command_list **command_lists;
3398 static struct wined3d_deferred_context *wined3d_deferred_context_from_context(struct wined3d_device_context *context)
3400 return CONTAINING_RECORD(context, struct wined3d_deferred_context, c);
3403 static void *wined3d_deferred_context_require_space(struct wined3d_device_context *context,
3404 size_t size, enum wined3d_cs_queue_id queue_id)
3406 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
3407 struct wined3d_cs_packet *packet;
3408 size_t header_size, packet_size;
3410 if (queue_id != WINED3D_CS_QUEUE_DEFAULT)
3411 return NULL;
3413 header_size = offsetof(struct wined3d_cs_packet, data[0]);
3414 packet_size = offsetof(struct wined3d_cs_packet, data[size]);
3415 packet_size = (packet_size + header_size - 1) & ~(header_size - 1);
3417 if (!wined3d_array_reserve(&deferred->data, &deferred->data_capacity, deferred->data_size + packet_size, 1))
3418 return NULL;
3420 packet = (struct wined3d_cs_packet *)((BYTE *)deferred->data + deferred->data_size);
3421 TRACE("size was %zu, adding %zu\n", (size_t)deferred->data_size, packet_size);
3422 deferred->data_size += packet_size;
3423 packet->size = packet_size - header_size;
3424 return &packet->data;
3427 static void wined3d_deferred_context_submit(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
3429 assert(queue_id == WINED3D_CS_QUEUE_DEFAULT);
3431 /* Nothing to do. */
3434 static void wined3d_deferred_context_finish(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
3436 /* This should not happen; we cannot meaningfully finish a deferred context. */
3437 ERR("Ignoring finish() on a deferred context.\n");
3440 static void wined3d_deferred_context_push_constants(struct wined3d_device_context *context,
3441 enum wined3d_push_constants p, unsigned int start_idx, unsigned int count, const void *constants)
3443 FIXME("context %p, p %#x, start_idx %u, count %u, constants %p, stub!\n", context, p, start_idx, count, constants);
3446 static const struct wined3d_deferred_upload *deferred_context_get_upload(struct wined3d_deferred_context *deferred,
3447 struct wined3d_resource *resource, unsigned int sub_resource_idx)
3449 SIZE_T i = deferred->upload_count;
3451 while (i--)
3453 struct wined3d_deferred_upload *upload = &deferred->uploads[i];
3455 if (upload->resource == resource && upload->sub_resource_idx == sub_resource_idx)
3456 return upload;
3459 return NULL;
3462 static void *wined3d_deferred_context_prepare_upload_bo(struct wined3d_device_context *context,
3463 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
3464 unsigned int row_pitch, unsigned int slice_pitch, uint32_t flags, struct wined3d_const_bo_address *address)
3466 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
3467 const struct wined3d_format *format = resource->format;
3468 struct wined3d_deferred_upload *upload;
3469 uint8_t *sysmem, *map_ptr;
3470 size_t size;
3472 size = (box->back - box->front - 1) * slice_pitch
3473 + ((box->bottom - box->top - 1) / format->block_height) * row_pitch
3474 + ((box->right - box->left + format->block_width - 1) / format->block_width) * format->block_byte_count;
3476 if (!(flags & WINED3D_MAP_WRITE))
3478 WARN("Flags %#x are not valid on a deferred context.\n", flags);
3479 return NULL;
3482 if (flags & ~(WINED3D_MAP_WRITE | WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
3484 FIXME("Unhandled flags %#x.\n", flags);
3485 return NULL;
3488 if (flags & WINED3D_MAP_NOOVERWRITE)
3490 const struct wined3d_deferred_upload *upload;
3492 if ((upload = deferred_context_get_upload(deferred, resource, sub_resource_idx)))
3494 map_ptr = (uint8_t *)align((size_t)upload->sysmem, RESOURCE_ALIGNMENT);
3495 address->buffer_object = 0;
3496 address->addr = map_ptr;
3497 return map_ptr;
3500 return NULL;
3503 if (!wined3d_array_reserve((void **)&deferred->uploads, &deferred->uploads_capacity,
3504 deferred->upload_count + 1, sizeof(*deferred->uploads)))
3505 return NULL;
3507 if (!(sysmem = heap_alloc(size + RESOURCE_ALIGNMENT - 1)))
3508 return NULL;
3510 upload = &deferred->uploads[deferred->upload_count++];
3511 upload->resource = resource;
3512 wined3d_resource_incref(resource);
3513 upload->sub_resource_idx = sub_resource_idx;
3514 upload->sysmem = sysmem;
3515 upload->box = *box;
3517 address->buffer_object = 0;
3518 map_ptr = (uint8_t *)align((size_t)sysmem, RESOURCE_ALIGNMENT);
3519 address->addr = map_ptr;
3520 return map_ptr;
3523 static bool wined3d_deferred_context_get_upload_bo(struct wined3d_device_context *context,
3524 struct wined3d_resource *resource, unsigned int sub_resource_idx,
3525 struct wined3d_box *box, struct wined3d_const_bo_address *address)
3527 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
3528 const struct wined3d_deferred_upload *upload;
3530 if ((upload = deferred_context_get_upload(deferred, resource, sub_resource_idx)))
3532 *box = upload->box;
3533 address->buffer_object = 0;
3534 address->addr = (uint8_t *)align((size_t)upload->sysmem, RESOURCE_ALIGNMENT);
3535 return true;
3538 return false;
3541 static void wined3d_deferred_context_issue_query(struct wined3d_device_context *context,
3542 struct wined3d_query *query, unsigned int flags)
3544 FIXME("context %p, query %p, flags %#x, stub!\n", context, query, flags);
3547 static void wined3d_deferred_context_flush(struct wined3d_device_context *context)
3549 FIXME("context %p, stub!\n", context);
3552 static void wined3d_deferred_context_acquire_resource(struct wined3d_device_context *context,
3553 struct wined3d_resource *resource)
3555 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
3557 if (!wined3d_array_reserve((void **)&deferred->resources, &deferred->resources_capacity,
3558 deferred->resource_count + 1, sizeof(*deferred->resources)))
3559 return;
3561 deferred->resources[deferred->resource_count++] = resource;
3562 wined3d_resource_incref(resource);
3565 static void wined3d_deferred_context_execute_command_list(struct wined3d_device_context *context,
3566 struct wined3d_command_list *list, bool restore_state)
3568 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
3569 struct wined3d_cs_execute_command_list *op;
3571 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
3572 op->opcode = WINED3D_CS_OP_EXECUTE_COMMAND_LIST;
3573 op->list = list;
3575 if (restore_state)
3576 wined3d_device_context_set_state(context, context->state);
3577 else
3578 wined3d_device_context_reset_state(context);
3580 /* Grab a reference to the command list. Note that this implicitly prevents
3581 * any dependent command lists or resources from being freed as well. */
3582 if (!wined3d_array_reserve((void **)&deferred->command_lists, &deferred->command_lists_capacity,
3583 deferred->command_list_count + 1, sizeof(*deferred->command_lists)))
3584 return;
3586 wined3d_command_list_incref(deferred->command_lists[deferred->command_list_count++] = list);
3589 static const struct wined3d_device_context_ops wined3d_deferred_context_ops =
3591 wined3d_deferred_context_require_space,
3592 wined3d_deferred_context_submit,
3593 wined3d_deferred_context_finish,
3594 wined3d_deferred_context_push_constants,
3595 wined3d_deferred_context_prepare_upload_bo,
3596 wined3d_deferred_context_get_upload_bo,
3597 wined3d_deferred_context_issue_query,
3598 wined3d_deferred_context_flush,
3599 wined3d_deferred_context_acquire_resource,
3600 wined3d_deferred_context_execute_command_list,
3603 HRESULT CDECL wined3d_deferred_context_create(struct wined3d_device *device, struct wined3d_device_context **context)
3605 struct wined3d_deferred_context *object;
3606 HRESULT hr;
3608 TRACE("device %p, context %p.\n", device, context);
3610 if (!(object = heap_alloc_zero(sizeof(*object))))
3611 return E_OUTOFMEMORY;
3613 if (FAILED(hr = wined3d_state_create(device, &device->cs->c.state->feature_level, 1, &object->c.state)))
3615 heap_free(object);
3616 return hr;
3619 object->c.ops = &wined3d_deferred_context_ops;
3620 object->c.device = device;
3622 TRACE("Created deferred context %p.\n", object);
3623 *context = &object->c;
3625 return S_OK;
3628 void CDECL wined3d_deferred_context_destroy(struct wined3d_device_context *context)
3630 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
3631 SIZE_T i;
3633 TRACE("context %p.\n", context);
3635 for (i = 0; i < deferred->resource_count; ++i)
3636 wined3d_resource_decref(deferred->resources[i]);
3638 for (i = 0; i < deferred->upload_count; ++i)
3640 wined3d_resource_decref(deferred->uploads[i].resource);
3641 heap_free(deferred->uploads[i].sysmem);
3643 heap_free(deferred->resources);
3645 wined3d_state_destroy(deferred->c.state);
3646 heap_free(deferred->data);
3647 heap_free(deferred);
3650 HRESULT CDECL wined3d_deferred_context_record_command_list(struct wined3d_device_context *context,
3651 bool restore, struct wined3d_command_list **list)
3653 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
3654 struct wined3d_command_list *object;
3656 TRACE("context %p, list %p.\n", context, list);
3658 if (!(object = heap_alloc_zero(sizeof(*object))))
3659 return E_OUTOFMEMORY;
3661 object->refcount = 1;
3662 object->device = deferred->c.device;
3664 if (!(object->data = heap_alloc(deferred->data_size)))
3666 heap_free(object);
3667 return E_OUTOFMEMORY;
3669 object->data_size = deferred->data_size;
3670 memcpy(object->data, deferred->data, deferred->data_size);
3672 if (!(object->resources = heap_alloc(deferred->resource_count * sizeof(*object->resources))))
3674 heap_free(object->data);
3675 heap_free(object);
3676 return E_OUTOFMEMORY;
3678 object->resource_count = deferred->resource_count;
3679 memcpy(object->resources, deferred->resources, deferred->resource_count * sizeof(*object->resources));
3680 /* Transfer our references to the resources to the command list. */
3682 if (!(object->command_lists = heap_alloc(deferred->command_list_count * sizeof(*object->command_lists))))
3684 heap_free(object->resources);
3685 heap_free(object->data);
3686 heap_free(object);
3687 return E_OUTOFMEMORY;
3689 object->command_list_count = deferred->command_list_count;
3690 memcpy(object->command_lists, deferred->command_lists,
3691 deferred->command_list_count * sizeof(*object->command_lists));
3692 /* Transfer our references to the command lists to the command list. */
3694 deferred->data_size = 0;
3695 deferred->resource_count = 0;
3696 deferred->command_list_count = 0;
3698 /* This is in fact recorded into a subsequent command list. */
3699 if (restore)
3700 wined3d_device_context_set_state(&deferred->c, deferred->c.state);
3701 else
3702 wined3d_device_context_reset_state(&deferred->c);
3704 TRACE("Created command list %p.\n", object);
3705 *list = object;
3707 return S_OK;