wined3d: Check the wined3d resource type and usage instead of the GL target in textur...
[wine.git] / dlls / wined3d / cs.c
blob96207bcc525dfb085cac463938e306d44f464cc2
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 "wined3d_private.h"
20 #include "wined3d_gl.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
23 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
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 struct wined3d_bo *bo;
34 uint8_t *sysmem, *map_ptr;
35 struct wined3d_box box;
36 uint32_t upload_flags;
39 struct wined3d_deferred_query_issue
41 struct wined3d_query *query;
42 unsigned int flags;
45 struct wined3d_command_list
47 LONG refcount;
49 struct wined3d_device *device;
51 SIZE_T data_size;
52 void *data;
54 SIZE_T resource_count;
55 struct wined3d_resource **resources;
57 SIZE_T upload_count;
58 struct wined3d_deferred_upload *uploads;
60 HANDLE upload_heap;
61 LONG *upload_heap_refcount;
63 /* List of command lists queued for execution on this command list. We might
64 * be the only thing holding a pointer to another command list, so we need
65 * to hold a reference here (and in wined3d_deferred_context) as well. */
66 SIZE_T command_list_count;
67 struct wined3d_command_list **command_lists;
69 SIZE_T query_count;
70 struct wined3d_deferred_query_issue *queries;
73 static void discard_client_address(struct wined3d_resource *resource)
75 struct wined3d_client_resource *client = &resource->client;
77 client->addr.buffer_object = CLIENT_BO_DISCARDED;
78 client->addr.addr = NULL;
81 static void invalidate_client_address(struct wined3d_resource *resource)
83 struct wined3d_client_resource *client = &resource->client;
85 memset(&client->addr, 0, sizeof(client->addr));
88 enum wined3d_cs_op
90 WINED3D_CS_OP_NOP,
91 WINED3D_CS_OP_PRESENT,
92 WINED3D_CS_OP_CLEAR,
93 WINED3D_CS_OP_DISPATCH,
94 WINED3D_CS_OP_DRAW,
95 WINED3D_CS_OP_FLUSH,
96 WINED3D_CS_OP_SET_PREDICATION,
97 WINED3D_CS_OP_SET_VIEWPORTS,
98 WINED3D_CS_OP_SET_SCISSOR_RECTS,
99 WINED3D_CS_OP_SET_RENDERTARGET_VIEWS,
100 WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW,
101 WINED3D_CS_OP_SET_VERTEX_DECLARATION,
102 WINED3D_CS_OP_SET_STREAM_SOURCES,
103 WINED3D_CS_OP_SET_STREAM_OUTPUTS,
104 WINED3D_CS_OP_SET_INDEX_BUFFER,
105 WINED3D_CS_OP_SET_CONSTANT_BUFFERS,
106 WINED3D_CS_OP_SET_TEXTURE,
107 WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEWS,
108 WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEWS,
109 WINED3D_CS_OP_SET_SAMPLERS,
110 WINED3D_CS_OP_SET_SHADER,
111 WINED3D_CS_OP_SET_BLEND_STATE,
112 WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE,
113 WINED3D_CS_OP_SET_RASTERIZER_STATE,
114 WINED3D_CS_OP_SET_DEPTH_BOUNDS,
115 WINED3D_CS_OP_SET_RENDER_STATE,
116 WINED3D_CS_OP_SET_TEXTURE_STATE,
117 WINED3D_CS_OP_SET_TRANSFORM,
118 WINED3D_CS_OP_SET_CLIP_PLANE,
119 WINED3D_CS_OP_SET_COLOR_KEY,
120 WINED3D_CS_OP_SET_MATERIAL,
121 WINED3D_CS_OP_SET_LIGHT,
122 WINED3D_CS_OP_SET_LIGHT_ENABLE,
123 WINED3D_CS_OP_SET_FEATURE_LEVEL,
124 WINED3D_CS_OP_PUSH_CONSTANTS,
125 WINED3D_CS_OP_RESET_STATE,
126 WINED3D_CS_OP_CALLBACK,
127 WINED3D_CS_OP_QUERY_ISSUE,
128 WINED3D_CS_OP_PRELOAD_RESOURCE,
129 WINED3D_CS_OP_UNLOAD_RESOURCE,
130 WINED3D_CS_OP_MAP,
131 WINED3D_CS_OP_UNMAP,
132 WINED3D_CS_OP_MAP_BO_ADDRESS,
133 WINED3D_CS_OP_BLT_SUB_RESOURCE,
134 WINED3D_CS_OP_UPDATE_SUB_RESOURCE,
135 WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION,
136 WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW,
137 WINED3D_CS_OP_COPY_UAV_COUNTER,
138 WINED3D_CS_OP_GENERATE_MIPMAPS,
139 WINED3D_CS_OP_EXECUTE_COMMAND_LIST,
140 WINED3D_CS_OP_STOP,
143 struct wined3d_cs_packet
145 size_t size;
146 BYTE data[1];
149 struct wined3d_cs_nop
151 enum wined3d_cs_op opcode;
154 struct wined3d_cs_present
156 enum wined3d_cs_op opcode;
157 HWND dst_window_override;
158 struct wined3d_swapchain *swapchain;
159 RECT src_rect;
160 RECT dst_rect;
161 unsigned int swap_interval;
162 uint32_t flags;
165 struct wined3d_cs_clear
167 enum wined3d_cs_op opcode;
168 uint32_t flags;
169 unsigned int rt_count;
170 struct wined3d_fb_state fb;
171 RECT draw_rect;
172 struct wined3d_color color;
173 float depth;
174 DWORD stencil;
175 unsigned int rect_count;
176 RECT rects[1];
179 struct wined3d_cs_dispatch
181 enum wined3d_cs_op opcode;
182 struct wined3d_dispatch_parameters parameters;
185 struct wined3d_cs_draw
187 enum wined3d_cs_op opcode;
188 enum wined3d_primitive_type primitive_type;
189 unsigned int patch_vertex_count;
190 struct wined3d_draw_parameters parameters;
193 struct wined3d_cs_flush
195 enum wined3d_cs_op opcode;
198 struct wined3d_cs_set_predication
200 enum wined3d_cs_op opcode;
201 struct wined3d_query *predicate;
202 BOOL value;
205 struct wined3d_cs_set_viewports
207 enum wined3d_cs_op opcode;
208 unsigned int viewport_count;
209 struct wined3d_viewport viewports[1];
212 struct wined3d_cs_set_scissor_rects
214 enum wined3d_cs_op opcode;
215 unsigned int rect_count;
216 RECT rects[1];
219 struct wined3d_cs_set_rendertarget_views
221 enum wined3d_cs_op opcode;
222 unsigned int start_idx;
223 unsigned int count;
224 struct wined3d_rendertarget_view *views[1];
227 struct wined3d_cs_set_depth_stencil_view
229 enum wined3d_cs_op opcode;
230 struct wined3d_rendertarget_view *view;
233 struct wined3d_cs_set_vertex_declaration
235 enum wined3d_cs_op opcode;
236 struct wined3d_vertex_declaration *declaration;
239 struct wined3d_cs_set_stream_sources
241 enum wined3d_cs_op opcode;
242 unsigned int start_idx;
243 unsigned int count;
244 struct wined3d_stream_state streams[1];
247 struct wined3d_cs_set_stream_outputs
249 enum wined3d_cs_op opcode;
250 struct wined3d_stream_output outputs[WINED3D_MAX_STREAM_OUTPUT_BUFFERS];
253 struct wined3d_cs_set_index_buffer
255 enum wined3d_cs_op opcode;
256 struct wined3d_buffer *buffer;
257 enum wined3d_format_id format_id;
258 unsigned int offset;
261 struct wined3d_cs_set_constant_buffers
263 enum wined3d_cs_op opcode;
264 enum wined3d_shader_type type;
265 unsigned int start_idx;
266 unsigned int count;
267 struct wined3d_constant_buffer_state buffers[1];
270 struct wined3d_cs_set_texture
272 enum wined3d_cs_op opcode;
273 UINT stage;
274 struct wined3d_texture *texture;
277 struct wined3d_cs_set_color_key
279 enum wined3d_cs_op opcode;
280 struct wined3d_texture *texture;
281 WORD flags;
282 WORD set;
283 struct wined3d_color_key color_key;
286 struct wined3d_cs_set_shader_resource_views
288 enum wined3d_cs_op opcode;
289 enum wined3d_shader_type type;
290 unsigned int start_idx;
291 unsigned int count;
292 struct wined3d_shader_resource_view *views[1];
295 struct wined3d_cs_set_unordered_access_views
297 enum wined3d_cs_op opcode;
298 enum wined3d_pipeline pipeline;
299 unsigned int start_idx;
300 unsigned int count;
301 struct
303 struct wined3d_unordered_access_view *view;
304 unsigned int initial_count;
305 } uavs[1];
308 struct wined3d_cs_set_samplers
310 enum wined3d_cs_op opcode;
311 enum wined3d_shader_type type;
312 unsigned int start_idx;
313 unsigned int count;
314 struct wined3d_sampler *samplers[1];
317 struct wined3d_cs_set_shader
319 enum wined3d_cs_op opcode;
320 enum wined3d_shader_type type;
321 struct wined3d_shader *shader;
324 struct wined3d_cs_set_blend_state
326 enum wined3d_cs_op opcode;
327 struct wined3d_blend_state *state;
328 struct wined3d_color factor;
329 unsigned int sample_mask;
332 struct wined3d_cs_set_depth_stencil_state
334 enum wined3d_cs_op opcode;
335 struct wined3d_depth_stencil_state *state;
336 unsigned int stencil_ref;
339 struct wined3d_cs_set_rasterizer_state
341 enum wined3d_cs_op opcode;
342 struct wined3d_rasterizer_state *state;
345 struct wined3d_cs_set_depth_bounds
347 enum wined3d_cs_op opcode;
348 bool enable;
349 float min_depth, max_depth;
352 struct wined3d_cs_set_render_state
354 enum wined3d_cs_op opcode;
355 enum wined3d_render_state state;
356 DWORD value;
359 struct wined3d_cs_set_texture_state
361 enum wined3d_cs_op opcode;
362 UINT stage;
363 enum wined3d_texture_stage_state state;
364 DWORD value;
367 struct wined3d_cs_set_transform
369 enum wined3d_cs_op opcode;
370 enum wined3d_transform_state state;
371 struct wined3d_matrix matrix;
374 struct wined3d_cs_set_clip_plane
376 enum wined3d_cs_op opcode;
377 UINT plane_idx;
378 struct wined3d_vec4 plane;
381 struct wined3d_cs_set_material
383 enum wined3d_cs_op opcode;
384 struct wined3d_material material;
387 struct wined3d_cs_set_light
389 enum wined3d_cs_op opcode;
390 struct wined3d_light_info light;
393 struct wined3d_cs_set_light_enable
395 enum wined3d_cs_op opcode;
396 unsigned int idx;
397 BOOL enable;
400 struct wined3d_cs_set_feature_level
402 enum wined3d_cs_op opcode;
403 enum wined3d_feature_level level;
406 struct wined3d_cs_push_constants
408 enum wined3d_cs_op opcode;
409 enum wined3d_push_constants type;
410 unsigned int start_idx;
411 unsigned int count;
414 struct wined3d_cs_reset_state
416 enum wined3d_cs_op opcode;
417 bool invalidate;
420 struct wined3d_cs_callback
422 enum wined3d_cs_op opcode;
423 void (*callback)(void *object);
424 void *object;
427 struct wined3d_cs_query_issue
429 enum wined3d_cs_op opcode;
430 struct wined3d_query *query;
431 uint32_t flags;
434 struct wined3d_cs_preload_resource
436 enum wined3d_cs_op opcode;
437 struct wined3d_resource *resource;
440 struct wined3d_cs_unload_resource
442 enum wined3d_cs_op opcode;
443 struct wined3d_resource *resource;
446 struct wined3d_cs_map
448 enum wined3d_cs_op opcode;
449 struct wined3d_resource *resource;
450 unsigned int sub_resource_idx;
451 void **map_ptr;
452 const struct wined3d_box *box;
453 uint32_t flags;
454 HRESULT *hr;
457 struct wined3d_cs_unmap
459 enum wined3d_cs_op opcode;
460 struct wined3d_resource *resource;
461 unsigned int sub_resource_idx;
462 HRESULT *hr;
465 struct wined3d_cs_map_bo_address
467 enum wined3d_cs_op opcode;
468 struct wined3d_bo_address addr;
469 size_t size;
470 uint32_t flags;
473 struct wined3d_cs_blt_sub_resource
475 enum wined3d_cs_op opcode;
476 struct wined3d_resource *dst_resource;
477 unsigned int dst_sub_resource_idx;
478 struct wined3d_box dst_box;
479 struct wined3d_resource *src_resource;
480 unsigned int src_sub_resource_idx;
481 struct wined3d_box src_box;
482 uint32_t flags;
483 struct wined3d_blt_fx fx;
484 enum wined3d_texture_filter_type filter;
487 struct wined3d_cs_update_sub_resource
489 enum wined3d_cs_op opcode;
490 struct wined3d_resource *resource;
491 unsigned int sub_resource_idx;
492 struct wined3d_box box;
493 struct upload_bo bo;
494 unsigned int row_pitch, slice_pitch;
497 struct wined3d_cs_add_dirty_texture_region
499 enum wined3d_cs_op opcode;
500 struct wined3d_texture *texture;
501 unsigned int layer;
504 struct wined3d_cs_clear_unordered_access_view
506 enum wined3d_cs_op opcode;
507 struct wined3d_unordered_access_view *view;
508 struct wined3d_uvec4 clear_value;
509 bool fp;
512 struct wined3d_cs_copy_uav_counter
514 enum wined3d_cs_op opcode;
515 struct wined3d_buffer *buffer;
516 unsigned int offset;
517 struct wined3d_unordered_access_view *view;
520 struct wined3d_cs_generate_mipmaps
522 enum wined3d_cs_op opcode;
523 struct wined3d_shader_resource_view *view;
526 struct wined3d_cs_execute_command_list
528 enum wined3d_cs_op opcode;
529 struct wined3d_command_list *list;
532 struct wined3d_cs_stop
534 enum wined3d_cs_op opcode;
537 static inline void *wined3d_device_context_require_space(struct wined3d_device_context *context,
538 size_t size, enum wined3d_cs_queue_id queue_id)
540 return context->ops->require_space(context, size, queue_id);
543 static inline void wined3d_device_context_submit(struct wined3d_device_context *context,
544 enum wined3d_cs_queue_id queue_id)
546 context->ops->submit(context, queue_id);
549 static inline void wined3d_device_context_finish(struct wined3d_device_context *context,
550 enum wined3d_cs_queue_id queue_id)
552 context->ops->finish(context, queue_id);
555 static inline void wined3d_device_context_reference_resource(struct wined3d_device_context *context,
556 struct wined3d_resource *resource)
558 context->ops->reference_resource(context, resource);
561 static struct wined3d_cs *wined3d_cs_from_context(struct wined3d_device_context *context)
563 return CONTAINING_RECORD(context, struct wined3d_cs, c);
566 static const char *debug_cs_op(enum wined3d_cs_op op)
568 switch (op)
570 #define WINED3D_TO_STR(type) case type: return #type
571 WINED3D_TO_STR(WINED3D_CS_OP_NOP);
572 WINED3D_TO_STR(WINED3D_CS_OP_PRESENT);
573 WINED3D_TO_STR(WINED3D_CS_OP_CLEAR);
574 WINED3D_TO_STR(WINED3D_CS_OP_DISPATCH);
575 WINED3D_TO_STR(WINED3D_CS_OP_DRAW);
576 WINED3D_TO_STR(WINED3D_CS_OP_FLUSH);
577 WINED3D_TO_STR(WINED3D_CS_OP_SET_PREDICATION);
578 WINED3D_TO_STR(WINED3D_CS_OP_SET_VIEWPORTS);
579 WINED3D_TO_STR(WINED3D_CS_OP_SET_SCISSOR_RECTS);
580 WINED3D_TO_STR(WINED3D_CS_OP_SET_RENDERTARGET_VIEWS);
581 WINED3D_TO_STR(WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW);
582 WINED3D_TO_STR(WINED3D_CS_OP_SET_VERTEX_DECLARATION);
583 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_SOURCES);
584 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_OUTPUTS);
585 WINED3D_TO_STR(WINED3D_CS_OP_SET_INDEX_BUFFER);
586 WINED3D_TO_STR(WINED3D_CS_OP_SET_CONSTANT_BUFFERS);
587 WINED3D_TO_STR(WINED3D_CS_OP_SET_TEXTURE);
588 WINED3D_TO_STR(WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEWS);
589 WINED3D_TO_STR(WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEWS);
590 WINED3D_TO_STR(WINED3D_CS_OP_SET_SAMPLERS);
591 WINED3D_TO_STR(WINED3D_CS_OP_SET_SHADER);
592 WINED3D_TO_STR(WINED3D_CS_OP_SET_BLEND_STATE);
593 WINED3D_TO_STR(WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE);
594 WINED3D_TO_STR(WINED3D_CS_OP_SET_RASTERIZER_STATE);
595 WINED3D_TO_STR(WINED3D_CS_OP_SET_DEPTH_BOUNDS);
596 WINED3D_TO_STR(WINED3D_CS_OP_SET_RENDER_STATE);
597 WINED3D_TO_STR(WINED3D_CS_OP_SET_TEXTURE_STATE);
598 WINED3D_TO_STR(WINED3D_CS_OP_SET_TRANSFORM);
599 WINED3D_TO_STR(WINED3D_CS_OP_SET_CLIP_PLANE);
600 WINED3D_TO_STR(WINED3D_CS_OP_SET_COLOR_KEY);
601 WINED3D_TO_STR(WINED3D_CS_OP_SET_MATERIAL);
602 WINED3D_TO_STR(WINED3D_CS_OP_SET_LIGHT);
603 WINED3D_TO_STR(WINED3D_CS_OP_SET_LIGHT_ENABLE);
604 WINED3D_TO_STR(WINED3D_CS_OP_SET_FEATURE_LEVEL);
605 WINED3D_TO_STR(WINED3D_CS_OP_PUSH_CONSTANTS);
606 WINED3D_TO_STR(WINED3D_CS_OP_RESET_STATE);
607 WINED3D_TO_STR(WINED3D_CS_OP_CALLBACK);
608 WINED3D_TO_STR(WINED3D_CS_OP_QUERY_ISSUE);
609 WINED3D_TO_STR(WINED3D_CS_OP_PRELOAD_RESOURCE);
610 WINED3D_TO_STR(WINED3D_CS_OP_UNLOAD_RESOURCE);
611 WINED3D_TO_STR(WINED3D_CS_OP_MAP);
612 WINED3D_TO_STR(WINED3D_CS_OP_UNMAP);
613 WINED3D_TO_STR(WINED3D_CS_OP_MAP_BO_ADDRESS);
614 WINED3D_TO_STR(WINED3D_CS_OP_BLT_SUB_RESOURCE);
615 WINED3D_TO_STR(WINED3D_CS_OP_UPDATE_SUB_RESOURCE);
616 WINED3D_TO_STR(WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION);
617 WINED3D_TO_STR(WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW);
618 WINED3D_TO_STR(WINED3D_CS_OP_COPY_UAV_COUNTER);
619 WINED3D_TO_STR(WINED3D_CS_OP_GENERATE_MIPMAPS);
620 WINED3D_TO_STR(WINED3D_CS_OP_EXECUTE_COMMAND_LIST);
621 WINED3D_TO_STR(WINED3D_CS_OP_STOP);
622 #undef WINED3D_TO_STR
624 return wine_dbg_sprintf("UNKNOWN_OP(%#x)", op);
627 static struct wined3d_cs_packet *wined3d_next_cs_packet(const uint8_t *data, SIZE_T *offset, SIZE_T mask)
629 struct wined3d_cs_packet *packet = (struct wined3d_cs_packet *)&data[*offset & mask];
631 *offset += offsetof(struct wined3d_cs_packet, data[packet->size]);
633 return packet;
636 static void wined3d_cs_exec_nop(struct wined3d_cs *cs, const void *data)
640 static void wined3d_cs_exec_present(struct wined3d_cs *cs, const void *data)
642 struct wined3d_texture *logo_texture, *cursor_texture, *back_buffer;
643 struct wined3d_rendertarget_view *dsv = cs->state.fb.depth_stencil;
644 const struct wined3d_cs_present *op = data;
645 const struct wined3d_swapchain_desc *desc;
646 struct wined3d_swapchain *swapchain;
648 swapchain = op->swapchain;
649 desc = &swapchain->state.desc;
650 back_buffer = swapchain->back_buffers[0];
651 wined3d_swapchain_set_window(swapchain, op->dst_window_override);
653 if ((logo_texture = swapchain->device->logo_texture))
655 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
657 /* Blit the logo into the upper left corner of the back-buffer. */
658 wined3d_device_context_blt(&cs->c, back_buffer, 0, &rect, logo_texture, 0,
659 &rect, WINED3D_BLT_SRC_CKEY, NULL, WINED3D_TEXF_POINT);
662 if ((cursor_texture = swapchain->device->cursor_texture)
663 && swapchain->device->bCursorVisible && !swapchain->device->hardwareCursor)
665 RECT dst_rect =
667 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
668 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
669 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
670 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
672 RECT src_rect =
674 0, 0, cursor_texture->resource.width, cursor_texture->resource.height
676 const RECT clip_rect = {0, 0, back_buffer->resource.width, back_buffer->resource.height};
678 TRACE("Rendering the software cursor.\n");
680 if (desc->windowed)
681 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
682 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
683 wined3d_device_context_blt(&cs->c, back_buffer, 0, &dst_rect, cursor_texture, 0,
684 &src_rect, WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
687 swapchain->swapchain_ops->swapchain_present(swapchain, &op->src_rect, &op->dst_rect, op->swap_interval, op->flags);
689 /* Discard buffers if the swap effect allows it. */
690 back_buffer = swapchain->back_buffers[desc->backbuffer_count - 1];
691 if (desc->swap_effect == WINED3D_SWAP_EFFECT_DISCARD || desc->swap_effect == WINED3D_SWAP_EFFECT_FLIP_DISCARD)
692 wined3d_texture_validate_location(back_buffer, 0, WINED3D_LOCATION_DISCARDED);
694 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
696 struct wined3d_texture *ds = texture_from_resource(dsv->resource);
698 if ((desc->flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL || ds->flags & WINED3D_TEXTURE_DISCARD))
699 wined3d_rendertarget_view_validate_location(dsv, WINED3D_LOCATION_DISCARDED);
702 if (TRACE_ON(fps))
704 DWORD time = GetTickCount();
705 ++swapchain->frames;
707 /* every 1.5 seconds */
708 if (time - swapchain->prev_time > 1500)
710 TRACE_(fps)("%p @ approx %.2ffps\n",
711 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
712 swapchain->prev_time = time;
713 swapchain->frames = 0;
717 InterlockedDecrement(&cs->pending_presents);
718 if (InterlockedCompareExchange(&cs->waiting_for_present, FALSE, TRUE))
719 SetEvent(cs->present_event);
722 void wined3d_cs_emit_present(struct wined3d_cs *cs, struct wined3d_swapchain *swapchain,
723 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
724 unsigned int swap_interval, uint32_t flags)
726 struct wined3d_cs_present *op;
727 unsigned int i;
728 LONG pending;
730 wined3d_not_from_cs(cs);
732 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
733 op->opcode = WINED3D_CS_OP_PRESENT;
734 op->dst_window_override = dst_window_override;
735 op->swapchain = swapchain;
736 op->src_rect = *src_rect;
737 op->dst_rect = *dst_rect;
738 op->swap_interval = swap_interval;
739 op->flags = flags;
741 pending = InterlockedIncrement(&cs->pending_presents);
743 wined3d_resource_reference(&swapchain->front_buffer->resource);
744 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
746 wined3d_resource_reference(&swapchain->back_buffers[i]->resource);
749 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
751 /* Limit input latency by limiting the number of presents that we can get
752 * ahead of the worker thread. */
753 while (pending >= swapchain->max_frame_latency)
755 InterlockedExchange(&cs->waiting_for_present, TRUE);
757 pending = InterlockedCompareExchange(&cs->pending_presents, 0, 0);
758 if (pending >= swapchain->max_frame_latency || !InterlockedCompareExchange(&cs->waiting_for_present, FALSE, TRUE))
760 TRACE_(d3d_perf)("Reached latency limit (%u frames), blocking to wait.\n", swapchain->max_frame_latency);
761 wined3d_mutex_unlock();
762 WaitForSingleObject(cs->present_event, INFINITE);
763 wined3d_mutex_lock();
764 TRACE_(d3d_perf)("Woken up from the wait.\n");
769 static void wined3d_cs_exec_clear(struct wined3d_cs *cs, const void *data)
771 struct wined3d_device *device = cs->c.device;
772 const struct wined3d_cs_clear *op = data;
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);
778 void wined3d_cs_emit_clear(struct wined3d_cs *cs, DWORD rect_count, const RECT *rects,
779 uint32_t flags, const struct wined3d_color *color, float depth, DWORD stencil)
781 const struct wined3d_state *state = cs->c.state;
782 const struct wined3d_viewport *vp = &state->viewports[0];
783 struct wined3d_rendertarget_view *view;
784 struct wined3d_cs_clear *op;
785 unsigned int rt_count, i;
787 rt_count = flags & WINED3DCLEAR_TARGET ? cs->c.device->adapter->d3d_info.limits.max_rt_count : 0;
789 op = wined3d_device_context_require_space(&cs->c, FIELD_OFFSET(struct wined3d_cs_clear, rects[rect_count]),
790 WINED3D_CS_QUEUE_DEFAULT);
791 op->opcode = WINED3D_CS_OP_CLEAR;
792 op->flags = flags & (WINED3DCLEAR_TARGET | WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
793 op->rt_count = rt_count;
794 op->fb = state->fb;
795 SetRect(&op->draw_rect, vp->x, vp->y, vp->x + vp->width, vp->y + vp->height);
796 if (state->rasterizer_state && state->rasterizer_state->desc.scissor)
797 IntersectRect(&op->draw_rect, &op->draw_rect, &state->scissor_rects[0]);
798 op->color = *color;
799 op->depth = depth;
800 op->stencil = stencil;
801 op->rect_count = rect_count;
802 memcpy(op->rects, rects, sizeof(*rects) * rect_count);
804 for (i = 0; i < rt_count; ++i)
806 if ((view = state->fb.render_targets[i]))
807 wined3d_resource_reference(view->resource);
809 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
811 view = state->fb.depth_stencil;
812 wined3d_resource_reference(view->resource);
815 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
818 void wined3d_device_context_emit_clear_rendertarget_view(struct wined3d_device_context *context,
819 struct wined3d_rendertarget_view *view, const RECT *rect, unsigned int flags,
820 const struct wined3d_color *color, float depth, unsigned int stencil)
822 struct wined3d_cs_clear *op;
823 size_t size;
825 size = FIELD_OFFSET(struct wined3d_cs_clear, rects[1]);
826 op = wined3d_device_context_require_space(context, size, WINED3D_CS_QUEUE_DEFAULT);
828 op->opcode = WINED3D_CS_OP_CLEAR;
829 op->flags = flags & (WINED3DCLEAR_TARGET | WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
830 if (flags & WINED3DCLEAR_TARGET)
832 op->rt_count = 1;
833 op->fb.render_targets[0] = view;
834 op->fb.depth_stencil = NULL;
835 op->color = *color;
837 else
839 op->rt_count = 0;
840 op->fb.render_targets[0] = NULL;
841 op->fb.depth_stencil = view;
842 op->depth = depth;
843 op->stencil = stencil;
845 SetRect(&op->draw_rect, 0, 0, view->width, view->height);
846 op->rect_count = 1;
847 op->rects[0] = *rect;
849 wined3d_device_context_reference_resource(context, view->resource);
851 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
852 if (flags & WINED3DCLEAR_SYNCHRONOUS)
853 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_DEFAULT);
856 static void reference_shader_resources(struct wined3d_device_context *context, unsigned int shader_mask)
858 const struct wined3d_state *state = context->state;
859 struct wined3d_shader_sampler_map_entry *entry;
860 struct wined3d_shader_resource_view *view;
861 struct wined3d_shader *shader;
862 unsigned int i, j;
864 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
866 if (!(shader_mask & (1u << i)))
867 continue;
869 if (!(shader = state->shader[i]))
870 continue;
872 for (j = 0; j < WINED3D_MAX_CBS; ++j)
874 if (state->cb[i][j].buffer)
875 wined3d_device_context_reference_resource(context, &state->cb[i][j].buffer->resource);
878 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
880 entry = &shader->reg_maps.sampler_map.entries[j];
882 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
883 continue;
885 wined3d_device_context_reference_resource(context, view->resource);
890 static void reference_unordered_access_resources(struct wined3d_device_context *context,
891 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
893 unsigned int i;
895 if (!shader)
896 return;
898 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
900 if (!shader->reg_maps.uav_resource_info[i].type)
901 continue;
903 if (!views[i])
904 continue;
906 wined3d_device_context_reference_resource(context, views[i]->resource);
910 static void wined3d_cs_exec_dispatch(struct wined3d_cs *cs, const void *data)
912 const struct wined3d_cs_dispatch *op = data;
913 struct wined3d_state *state = &cs->state;
915 if (!state->shader[WINED3D_SHADER_TYPE_COMPUTE])
916 WARN("No compute shader bound, skipping dispatch.\n");
917 else
918 cs->c.device->adapter->adapter_ops->adapter_dispatch_compute(cs->c.device, state, &op->parameters);
921 static void reference_compute_pipeline_resources(struct wined3d_device_context *context)
923 const struct wined3d_state *state = context->state;
925 reference_shader_resources(context, 1u << WINED3D_SHADER_TYPE_COMPUTE);
926 reference_unordered_access_resources(context, state->shader[WINED3D_SHADER_TYPE_COMPUTE],
927 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
930 void CDECL wined3d_device_context_dispatch(struct wined3d_device_context *context,
931 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
933 struct wined3d_cs_dispatch *op;
935 wined3d_device_context_lock(context);
936 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
937 op->opcode = WINED3D_CS_OP_DISPATCH;
938 op->parameters.indirect = FALSE;
939 op->parameters.u.direct.group_count_x = group_count_x;
940 op->parameters.u.direct.group_count_y = group_count_y;
941 op->parameters.u.direct.group_count_z = group_count_z;
943 reference_compute_pipeline_resources(context);
945 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
946 wined3d_device_context_unlock(context);
949 void CDECL wined3d_device_context_dispatch_indirect(struct wined3d_device_context *context,
950 struct wined3d_buffer *buffer, unsigned int offset)
952 struct wined3d_cs_dispatch *op;
954 wined3d_device_context_lock(context);
955 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
956 op->opcode = WINED3D_CS_OP_DISPATCH;
957 op->parameters.indirect = TRUE;
958 op->parameters.u.indirect.buffer = buffer;
959 op->parameters.u.indirect.offset = offset;
961 reference_compute_pipeline_resources(context);
962 wined3d_device_context_reference_resource(context, &buffer->resource);
964 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
965 wined3d_device_context_unlock(context);
968 static void wined3d_cs_exec_draw(struct wined3d_cs *cs, const void *data)
970 const struct wined3d_d3d_info *d3d_info = &cs->c.device->adapter->d3d_info;
971 const struct wined3d_shader *geometry_shader;
972 struct wined3d_device *device = cs->c.device;
973 int base_vertex_idx, load_base_vertex_idx;
974 struct wined3d_state *state = &cs->state;
975 const struct wined3d_cs_draw *op = data;
976 unsigned int i;
978 base_vertex_idx = 0;
979 if (!op->parameters.indirect)
981 const struct wined3d_direct_draw_parameters *direct = &op->parameters.u.direct;
983 if (op->parameters.indexed && d3d_info->draw_base_vertex_offset)
984 base_vertex_idx = direct->base_vertex_idx;
985 else if (!op->parameters.indexed)
986 base_vertex_idx = direct->start_idx;
989 /* ARB_draw_indirect always supports a base vertex offset. */
990 if (!op->parameters.indirect && !d3d_info->draw_base_vertex_offset)
991 load_base_vertex_idx = op->parameters.u.direct.base_vertex_idx;
992 else
993 load_base_vertex_idx = 0;
995 if (state->base_vertex_index != base_vertex_idx)
997 state->base_vertex_index = base_vertex_idx;
998 for (i = 0; i < device->context_count; ++i)
999 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_BASE_VERTEX_ID;
1002 if (state->load_base_vertex_index != load_base_vertex_idx)
1004 state->load_base_vertex_index = load_base_vertex_idx;
1005 device_invalidate_state(cs->c.device, STATE_BASEVERTEXINDEX);
1008 if (state->primitive_type != op->primitive_type)
1010 if ((geometry_shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]) && !geometry_shader->function)
1011 device_invalidate_state(cs->c.device, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY));
1012 if (state->primitive_type == WINED3D_PT_POINTLIST || op->primitive_type == WINED3D_PT_POINTLIST)
1013 device_invalidate_state(cs->c.device, STATE_POINT_ENABLE);
1014 state->primitive_type = op->primitive_type;
1016 state->patch_vertex_count = op->patch_vertex_count;
1018 cs->c.device->adapter->adapter_ops->adapter_draw_primitive(cs->c.device, state, &op->parameters);
1021 static void reference_graphics_pipeline_resources(struct wined3d_device_context *context,
1022 BOOL indexed, const struct wined3d_d3d_info *d3d_info)
1024 const struct wined3d_state *state = context->state;
1025 unsigned int i;
1027 if (indexed)
1028 wined3d_device_context_reference_resource(context, &state->index_buffer->resource);
1029 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
1031 if (state->streams[i].buffer)
1032 wined3d_device_context_reference_resource(context, &state->streams[i].buffer->resource);
1034 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
1036 if (state->stream_output[i].buffer)
1037 wined3d_device_context_reference_resource(context, &state->stream_output[i].buffer->resource);
1039 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
1041 if (state->textures[i])
1042 wined3d_device_context_reference_resource(context, &state->textures[i]->resource);
1044 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
1046 if (state->fb.render_targets[i])
1047 wined3d_device_context_reference_resource(context, state->fb.render_targets[i]->resource);
1049 for (i = 0; i < WINED3D_PUSH_CONSTANTS_COUNT; ++i)
1051 if (context->device->push_constants[i])
1052 wined3d_device_context_reference_resource(context, &context->device->push_constants[i]->resource);
1054 if (state->fb.depth_stencil)
1055 wined3d_device_context_reference_resource(context, state->fb.depth_stencil->resource);
1056 reference_shader_resources(context, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
1057 reference_unordered_access_resources(context, state->shader[WINED3D_SHADER_TYPE_PIXEL],
1058 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
1061 void wined3d_device_context_emit_draw(struct wined3d_device_context *context,
1062 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count, int base_vertex_idx,
1063 unsigned int start_idx, unsigned int index_count, unsigned int start_instance, unsigned int instance_count,
1064 bool indexed)
1066 const struct wined3d_d3d_info *d3d_info = &context->device->adapter->d3d_info;
1067 struct wined3d_cs_draw *op;
1069 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1070 op->opcode = WINED3D_CS_OP_DRAW;
1071 op->primitive_type = primitive_type;
1072 op->patch_vertex_count = patch_vertex_count;
1073 op->parameters.indirect = FALSE;
1074 op->parameters.u.direct.base_vertex_idx = base_vertex_idx;
1075 op->parameters.u.direct.start_idx = start_idx;
1076 op->parameters.u.direct.index_count = index_count;
1077 op->parameters.u.direct.start_instance = start_instance;
1078 op->parameters.u.direct.instance_count = instance_count;
1079 op->parameters.indexed = indexed;
1081 reference_graphics_pipeline_resources(context, indexed, d3d_info);
1083 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1086 void CDECL wined3d_device_context_draw_indirect(struct wined3d_device_context *context,
1087 struct wined3d_buffer *buffer, unsigned int offset, bool indexed)
1089 const struct wined3d_d3d_info *d3d_info = &context->device->adapter->d3d_info;
1090 const struct wined3d_state *state = context->state;
1091 struct wined3d_cs_draw *op;
1093 wined3d_device_context_lock(context);
1094 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1095 op->opcode = WINED3D_CS_OP_DRAW;
1096 op->primitive_type = state->primitive_type;
1097 op->patch_vertex_count = state->patch_vertex_count;
1098 op->parameters.indirect = TRUE;
1099 op->parameters.u.indirect.buffer = buffer;
1100 op->parameters.u.indirect.offset = offset;
1101 op->parameters.indexed = indexed;
1103 reference_graphics_pipeline_resources(context, indexed, d3d_info);
1104 wined3d_device_context_reference_resource(context, &buffer->resource);
1106 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1107 wined3d_device_context_unlock(context);
1110 static void wined3d_cs_exec_flush(struct wined3d_cs *cs, const void *data)
1112 struct wined3d_context *context;
1114 context = context_acquire(cs->c.device, NULL, 0);
1115 cs->c.device->adapter->adapter_ops->adapter_flush_context(context);
1116 context_release(context);
1119 static void wined3d_cs_flush(struct wined3d_device_context *context)
1121 struct wined3d_cs *cs = wined3d_cs_from_context(context);
1122 struct wined3d_cs_flush *op;
1124 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1125 op->opcode = WINED3D_CS_OP_FLUSH;
1127 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1128 cs->queries_flushed = TRUE;
1131 static void wined3d_cs_exec_set_predication(struct wined3d_cs *cs, const void *data)
1133 const struct wined3d_cs_set_predication *op = data;
1135 cs->state.predicate = op->predicate;
1136 cs->state.predicate_value = op->value;
1139 void wined3d_device_context_emit_set_predication(struct wined3d_device_context *context,
1140 struct wined3d_query *predicate, BOOL value)
1142 struct wined3d_cs_set_predication *op;
1144 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1145 op->opcode = WINED3D_CS_OP_SET_PREDICATION;
1146 op->predicate = predicate;
1147 op->value = value;
1149 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1152 static void wined3d_cs_exec_set_viewports(struct wined3d_cs *cs, const void *data)
1154 const struct wined3d_cs_set_viewports *op = data;
1156 if (op->viewport_count)
1157 memcpy(cs->state.viewports, op->viewports, op->viewport_count * sizeof(*op->viewports));
1158 else
1159 memset(cs->state.viewports, 0, sizeof(*cs->state.viewports));
1160 cs->state.viewport_count = op->viewport_count;
1161 device_invalidate_state(cs->c.device, STATE_VIEWPORT);
1164 void wined3d_device_context_emit_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
1165 const struct wined3d_viewport *viewports)
1167 struct wined3d_cs_set_viewports *op;
1169 op = wined3d_device_context_require_space(context,
1170 FIELD_OFFSET(struct wined3d_cs_set_viewports,viewports[viewport_count]), WINED3D_CS_QUEUE_DEFAULT);
1171 op->opcode = WINED3D_CS_OP_SET_VIEWPORTS;
1172 memcpy(op->viewports, viewports, viewport_count * sizeof(*viewports));
1173 op->viewport_count = viewport_count;
1175 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1178 static void wined3d_cs_exec_set_scissor_rects(struct wined3d_cs *cs, const void *data)
1180 const struct wined3d_cs_set_scissor_rects *op = data;
1182 if (op->rect_count)
1183 memcpy(cs->state.scissor_rects, op->rects, op->rect_count * sizeof(*op->rects));
1184 else
1185 SetRectEmpty(cs->state.scissor_rects);
1186 cs->state.scissor_rect_count = op->rect_count;
1187 device_invalidate_state(cs->c.device, STATE_SCISSORRECT);
1190 void wined3d_device_context_emit_set_scissor_rects(struct wined3d_device_context *context,
1191 unsigned int rect_count, const RECT *rects)
1193 struct wined3d_cs_set_scissor_rects *op;
1195 op = wined3d_device_context_require_space(context, FIELD_OFFSET(struct wined3d_cs_set_scissor_rects, rects[rect_count]),
1196 WINED3D_CS_QUEUE_DEFAULT);
1197 op->opcode = WINED3D_CS_OP_SET_SCISSOR_RECTS;
1198 memcpy(op->rects, rects, rect_count * sizeof(*rects));
1199 op->rect_count = rect_count;
1201 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1204 static void wined3d_cs_exec_set_rendertarget_views(struct wined3d_cs *cs, const void *data)
1206 const struct wined3d_cs_set_rendertarget_views *op = data;
1207 struct wined3d_device *device = cs->c.device;
1208 unsigned int i;
1210 for (i = 0; i < op->count; ++i)
1212 struct wined3d_rendertarget_view *prev = cs->state.fb.render_targets[op->start_idx + i];
1213 struct wined3d_rendertarget_view *view = op->views[i];
1214 bool prev_alpha_swizzle, curr_alpha_swizzle;
1215 bool prev_srgb_write, curr_srgb_write;
1217 cs->state.fb.render_targets[op->start_idx + i] = view;
1219 prev_alpha_swizzle = prev && prev->format->id == WINED3DFMT_A8_UNORM;
1220 curr_alpha_swizzle = view && view->format->id == WINED3DFMT_A8_UNORM;
1221 if (prev_alpha_swizzle != curr_alpha_swizzle)
1222 device_invalidate_state(device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1224 if (!(device->adapter->d3d_info.wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1225 || cs->state.render_states[WINED3D_RS_SRGBWRITEENABLE])
1227 prev_srgb_write = prev && prev->format_caps & WINED3D_FORMAT_CAP_SRGB_WRITE;
1228 curr_srgb_write = view && view->format_caps & WINED3D_FORMAT_CAP_SRGB_WRITE;
1229 if (prev_srgb_write != curr_srgb_write)
1230 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
1234 device_invalidate_state(device, STATE_FRAMEBUFFER);
1237 void wined3d_device_context_emit_set_rendertarget_views(struct wined3d_device_context *context,
1238 unsigned int start_idx, unsigned int count, struct wined3d_rendertarget_view *const *views)
1240 struct wined3d_cs_set_rendertarget_views *op;
1242 op = wined3d_device_context_require_space(context,
1243 offsetof(struct wined3d_cs_set_rendertarget_views, views[count]), WINED3D_CS_QUEUE_DEFAULT);
1244 op->opcode = WINED3D_CS_OP_SET_RENDERTARGET_VIEWS;
1245 op->start_idx = start_idx;
1246 op->count = count;
1247 memcpy(op->views, views, count * sizeof(*views));
1249 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1252 static void wined3d_cs_exec_set_depth_stencil_view(struct wined3d_cs *cs, const void *data)
1254 const struct wined3d_cs_set_depth_stencil_view *op = data;
1255 struct wined3d_device *device = cs->c.device;
1256 struct wined3d_rendertarget_view *prev;
1258 if ((prev = cs->state.fb.depth_stencil) && prev->resource->type != WINED3D_RTYPE_BUFFER)
1260 struct wined3d_texture *prev_texture = texture_from_resource(prev->resource);
1262 if (device->swapchains[0]->state.desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
1263 || prev_texture->flags & WINED3D_TEXTURE_DISCARD)
1264 wined3d_texture_validate_location(prev_texture,
1265 prev->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
1268 cs->state.fb.depth_stencil = op->view;
1270 if (!prev != !op->view)
1272 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
1273 device_invalidate_state(device, STATE_DEPTH_STENCIL);
1274 device_invalidate_state(device, STATE_STENCIL_REF);
1275 device_invalidate_state(device, STATE_RASTERIZER);
1277 else if (prev)
1279 if (prev->format->depth_bias_scale != op->view->format->depth_bias_scale)
1280 device_invalidate_state(device, STATE_RASTERIZER);
1281 if (prev->format->stencil_size != op->view->format->stencil_size)
1282 device_invalidate_state(device, STATE_STENCIL_REF);
1285 device_invalidate_state(device, STATE_FRAMEBUFFER);
1288 void wined3d_device_context_emit_set_depth_stencil_view(struct wined3d_device_context *context,
1289 struct wined3d_rendertarget_view *view)
1291 struct wined3d_cs_set_depth_stencil_view *op;
1293 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1294 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW;
1295 op->view = view;
1297 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1300 static void wined3d_cs_exec_set_vertex_declaration(struct wined3d_cs *cs, const void *data)
1302 const struct wined3d_cs_set_vertex_declaration *op = data;
1304 cs->state.vertex_declaration = op->declaration;
1305 device_invalidate_state(cs->c.device, STATE_VDECL);
1308 void wined3d_device_context_emit_set_vertex_declaration(struct wined3d_device_context *context,
1309 struct wined3d_vertex_declaration *declaration)
1311 struct wined3d_cs_set_vertex_declaration *op;
1313 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1314 op->opcode = WINED3D_CS_OP_SET_VERTEX_DECLARATION;
1315 op->declaration = declaration;
1317 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1320 static void wined3d_cs_exec_set_stream_sources(struct wined3d_cs *cs, const void *data)
1322 const struct wined3d_cs_set_stream_sources *op = data;
1323 unsigned int i;
1325 for (i = 0; i < op->count; ++i)
1327 struct wined3d_buffer *prev = cs->state.streams[op->start_idx + i].buffer;
1328 struct wined3d_buffer *buffer = op->streams[i].buffer;
1330 if (buffer)
1331 InterlockedIncrement(&buffer->resource.bind_count);
1332 if (prev)
1333 InterlockedDecrement(&prev->resource.bind_count);
1336 memcpy(&cs->state.streams[op->start_idx], op->streams, op->count * sizeof(*op->streams));
1337 device_invalidate_state(cs->c.device, STATE_STREAMSRC);
1340 void wined3d_device_context_emit_set_stream_sources(struct wined3d_device_context *context,
1341 unsigned int start_idx, unsigned int count, const struct wined3d_stream_state *streams)
1343 struct wined3d_cs_set_stream_sources *op;
1345 op = wined3d_device_context_require_space(context,
1346 offsetof(struct wined3d_cs_set_stream_sources, streams[count]), WINED3D_CS_QUEUE_DEFAULT);
1347 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCES;
1348 op->start_idx = start_idx;
1349 op->count = count;
1350 memcpy(op->streams, streams, count * sizeof(*streams));
1352 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1355 static void wined3d_cs_exec_set_stream_outputs(struct wined3d_cs *cs, const void *data)
1357 const struct wined3d_cs_set_stream_outputs *op = data;
1358 unsigned int i;
1360 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
1362 struct wined3d_buffer *prev = cs->state.stream_output[i].buffer;
1363 struct wined3d_buffer *buffer = op->outputs[i].buffer;
1365 if (buffer)
1366 InterlockedIncrement(&buffer->resource.bind_count);
1367 if (prev)
1368 InterlockedDecrement(&prev->resource.bind_count);
1371 memcpy(cs->state.stream_output, op->outputs, sizeof(op->outputs));
1372 device_invalidate_state(cs->c.device, STATE_STREAM_OUTPUT);
1375 void wined3d_device_context_emit_set_stream_outputs(struct wined3d_device_context *context,
1376 const struct wined3d_stream_output outputs[WINED3D_MAX_STREAM_OUTPUT_BUFFERS])
1378 struct wined3d_cs_set_stream_outputs *op;
1380 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1381 op->opcode = WINED3D_CS_OP_SET_STREAM_OUTPUTS;
1382 memcpy(op->outputs, outputs, sizeof(op->outputs));
1384 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1387 static void wined3d_cs_exec_set_index_buffer(struct wined3d_cs *cs, const void *data)
1389 const struct wined3d_cs_set_index_buffer *op = data;
1390 struct wined3d_buffer *prev;
1392 prev = cs->state.index_buffer;
1393 cs->state.index_buffer = op->buffer;
1394 cs->state.index_format = op->format_id;
1395 cs->state.index_offset = op->offset;
1397 if (op->buffer)
1398 InterlockedIncrement(&op->buffer->resource.bind_count);
1399 if (prev)
1400 InterlockedDecrement(&prev->resource.bind_count);
1402 device_invalidate_state(cs->c.device, STATE_INDEXBUFFER);
1405 void wined3d_device_context_emit_set_index_buffer(struct wined3d_device_context *context, struct wined3d_buffer *buffer,
1406 enum wined3d_format_id format_id, unsigned int offset)
1408 struct wined3d_cs_set_index_buffer *op;
1410 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1411 op->opcode = WINED3D_CS_OP_SET_INDEX_BUFFER;
1412 op->buffer = buffer;
1413 op->format_id = format_id;
1414 op->offset = offset;
1416 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1419 static void wined3d_cs_exec_set_constant_buffers(struct wined3d_cs *cs, const void *data)
1421 const struct wined3d_cs_set_constant_buffers *op = data;
1422 unsigned int i;
1424 for (i = 0; i < op->count; ++i)
1426 struct wined3d_buffer *prev = cs->state.cb[op->type][op->start_idx + i].buffer;
1427 struct wined3d_buffer *buffer = op->buffers[i].buffer;
1429 cs->state.cb[op->type][op->start_idx + i] = op->buffers[i];
1431 if (buffer)
1432 InterlockedIncrement(&buffer->resource.bind_count);
1433 if (prev)
1434 InterlockedDecrement(&prev->resource.bind_count);
1437 device_invalidate_state(cs->c.device, STATE_CONSTANT_BUFFER(op->type));
1440 void wined3d_device_context_emit_set_constant_buffers(struct wined3d_device_context *context,
1441 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
1442 const struct wined3d_constant_buffer_state *buffers)
1444 struct wined3d_cs_set_constant_buffers *op;
1446 op = wined3d_device_context_require_space(context, offsetof(struct wined3d_cs_set_constant_buffers, buffers[count]),
1447 WINED3D_CS_QUEUE_DEFAULT);
1448 op->opcode = WINED3D_CS_OP_SET_CONSTANT_BUFFERS;
1449 op->type = type;
1450 op->start_idx = start_idx;
1451 op->count = count;
1452 memcpy(op->buffers, buffers, count * sizeof(*buffers));
1454 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1457 static bool texture_binding_might_invalidate_ps(struct wined3d_texture *texture,
1458 struct wined3d_texture *prev, const struct wined3d_d3d_info *d3d_info)
1460 unsigned int old_usage, new_usage, old_caps, new_caps;
1461 const struct wined3d_format *old_format, *new_format;
1463 if (!prev)
1464 return true;
1466 /* 1.x pixel shaders need to be recompiled based on the resource type. */
1467 old_usage = prev->resource.usage;
1468 new_usage = texture->resource.usage;
1469 if (texture->resource.type != prev->resource.type
1470 || ((old_usage & WINED3DUSAGE_LEGACY_CUBEMAP) != (new_usage & WINED3DUSAGE_LEGACY_CUBEMAP)))
1471 return true;
1473 old_format = prev->resource.format;
1474 new_format = texture->resource.format;
1475 old_caps = prev->resource.format_caps;
1476 new_caps = texture->resource.format_caps;
1477 if ((old_caps & WINED3D_FORMAT_CAP_SHADOW) != (new_caps & WINED3D_FORMAT_CAP_SHADOW))
1478 return true;
1480 if (is_same_fixup(old_format->color_fixup, new_format->color_fixup))
1481 return false;
1483 if (can_use_texture_swizzle(d3d_info, new_format) && can_use_texture_swizzle(d3d_info, old_format))
1484 return false;
1486 return true;
1489 static void wined3d_cs_exec_set_texture(struct wined3d_cs *cs, const void *data)
1491 const struct wined3d_d3d_info *d3d_info = &cs->c.device->adapter->d3d_info;
1492 const struct wined3d_cs_set_texture *op = data;
1493 struct wined3d_texture *prev;
1494 BOOL old_use_color_key = FALSE, new_use_color_key = FALSE;
1496 prev = cs->state.textures[op->stage];
1497 cs->state.textures[op->stage] = op->texture;
1499 if (op->texture)
1501 if (InterlockedIncrement(&op->texture->resource.bind_count) == 1)
1502 op->texture->sampler = op->stage;
1504 if (texture_binding_might_invalidate_ps(op->texture, prev, d3d_info))
1505 device_invalidate_state(cs->c.device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1507 if (!prev && op->stage < d3d_info->ffp_fragment_caps.max_blend_stages)
1509 /* The source arguments for color and alpha ops have different
1510 * meanings when a NULL texture is bound, so the COLOR_OP and
1511 * ALPHA_OP have to be dirtified. */
1512 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1513 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1516 if (!op->stage && op->texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1517 new_use_color_key = TRUE;
1520 if (prev)
1522 if (InterlockedDecrement(&prev->resource.bind_count) && prev->sampler == op->stage)
1524 unsigned int i;
1526 /* Search for other stages the texture is bound to. Shouldn't
1527 * happen if applications bind textures to a single stage only. */
1528 TRACE("Searching for other stages the texture is bound to.\n");
1529 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1531 if (cs->state.textures[i] == prev)
1533 TRACE("Texture is also bound to stage %u.\n", i);
1534 prev->sampler = i;
1535 break;
1540 if (!op->texture && op->stage < d3d_info->ffp_fragment_caps.max_blend_stages)
1542 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1543 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1546 if (!op->stage && prev->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1547 old_use_color_key = TRUE;
1550 device_invalidate_state(cs->c.device, STATE_SAMPLER(op->stage));
1552 if (new_use_color_key != old_use_color_key)
1553 device_invalidate_state(cs->c.device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1555 if (new_use_color_key)
1556 device_invalidate_state(cs->c.device, STATE_COLOR_KEY);
1559 void wined3d_device_context_emit_set_texture(struct wined3d_device_context *context, unsigned int stage,
1560 struct wined3d_texture *texture)
1562 struct wined3d_cs_set_texture *op;
1564 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1565 op->opcode = WINED3D_CS_OP_SET_TEXTURE;
1566 op->stage = stage;
1567 op->texture = texture;
1569 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1572 static void wined3d_cs_exec_set_shader_resource_views(struct wined3d_cs *cs, const void *data)
1574 const struct wined3d_cs_set_shader_resource_views *op = data;
1575 unsigned int i;
1577 for (i = 0; i < op->count; ++i)
1579 struct wined3d_shader_resource_view *prev = cs->state.shader_resource_view[op->type][op->start_idx + i];
1580 struct wined3d_shader_resource_view *view = op->views[i];
1582 cs->state.shader_resource_view[op->type][op->start_idx + i] = view;
1584 if (view)
1585 InterlockedIncrement(&view->resource->bind_count);
1586 if (prev)
1587 InterlockedDecrement(&prev->resource->bind_count);
1590 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1591 device_invalidate_state(cs->c.device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1592 else
1593 device_invalidate_state(cs->c.device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1596 void wined3d_device_context_emit_set_shader_resource_views(struct wined3d_device_context *context,
1597 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
1598 struct wined3d_shader_resource_view *const *views)
1600 struct wined3d_cs_set_shader_resource_views *op;
1602 op = wined3d_device_context_require_space(context,
1603 offsetof(struct wined3d_cs_set_shader_resource_views, views[count]), WINED3D_CS_QUEUE_DEFAULT);
1604 op->opcode = WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEWS;
1605 op->type = type;
1606 op->start_idx = start_idx;
1607 op->count = count;
1608 memcpy(op->views, views, count * sizeof(*views));
1610 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1613 static void wined3d_cs_exec_set_unordered_access_views(struct wined3d_cs *cs, const void *data)
1615 const struct wined3d_cs_set_unordered_access_views *op = data;
1616 unsigned int i;
1618 for (i = 0; i < op->count; ++i)
1620 struct wined3d_unordered_access_view *prev = cs->state.unordered_access_view[op->pipeline][op->start_idx + i];
1621 struct wined3d_unordered_access_view *view = op->uavs[i].view;
1622 unsigned int initial_count = op->uavs[i].initial_count;
1624 cs->state.unordered_access_view[op->pipeline][op->start_idx + i] = view;
1626 if (view)
1627 InterlockedIncrement(&view->resource->bind_count);
1628 if (prev)
1629 InterlockedDecrement(&prev->resource->bind_count);
1631 if (view && initial_count != ~0u)
1632 wined3d_unordered_access_view_set_counter(view, initial_count);
1635 device_invalidate_state(cs->c.device, STATE_UNORDERED_ACCESS_VIEW_BINDING(op->pipeline));
1638 void wined3d_device_context_emit_set_unordered_access_views(struct wined3d_device_context *context,
1639 enum wined3d_pipeline pipeline, unsigned int start_idx, unsigned int count,
1640 struct wined3d_unordered_access_view *const *views, const unsigned int *initial_counts)
1642 struct wined3d_cs_set_unordered_access_views *op;
1643 unsigned int i;
1645 op = wined3d_device_context_require_space(context,
1646 offsetof(struct wined3d_cs_set_unordered_access_views, uavs[count]), WINED3D_CS_QUEUE_DEFAULT);
1647 op->opcode = WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEWS;
1648 op->pipeline = pipeline;
1649 op->start_idx = start_idx;
1650 op->count = count;
1651 for (i = 0; i < count; ++i)
1653 op->uavs[i].view = views[i];
1654 op->uavs[i].initial_count = initial_counts ? initial_counts[i] : ~0u;
1657 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1660 static void wined3d_cs_exec_set_samplers(struct wined3d_cs *cs, const void *data)
1662 const struct wined3d_cs_set_samplers *op = data;
1663 unsigned int i;
1665 for (i = 0; i < op->count; ++i)
1667 cs->state.sampler[op->type][op->start_idx + i] = op->samplers[i];
1669 if (op->type == WINED3D_SHADER_TYPE_PIXEL && i < WINED3D_MAX_FRAGMENT_SAMPLERS)
1670 device_invalidate_state(cs->c.device, STATE_SAMPLER(i));
1671 else if (op->type == WINED3D_SHADER_TYPE_VERTEX && i < WINED3D_MAX_VERTEX_SAMPLERS)
1672 device_invalidate_state(cs->c.device, STATE_SAMPLER(WINED3D_VERTEX_SAMPLER_OFFSET + i));
1675 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1676 device_invalidate_state(cs->c.device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1677 else
1678 device_invalidate_state(cs->c.device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1681 void wined3d_device_context_emit_set_samplers(struct wined3d_device_context *context, enum wined3d_shader_type type,
1682 unsigned int start_idx, unsigned int count, struct wined3d_sampler *const *samplers)
1684 struct wined3d_cs_set_samplers *op;
1686 op = wined3d_device_context_require_space(context, offsetof(struct wined3d_cs_set_samplers, samplers[count]),
1687 WINED3D_CS_QUEUE_DEFAULT);
1688 op->opcode = WINED3D_CS_OP_SET_SAMPLERS;
1689 op->type = type;
1690 op->start_idx = start_idx;
1691 op->count = count;
1692 memcpy(op->samplers, samplers, count * sizeof(*samplers));
1694 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1697 static void wined3d_cs_exec_set_shader(struct wined3d_cs *cs, const void *data)
1699 const struct wined3d_cs_set_shader *op = data;
1701 /* CB binding may have been skipped earlier if the shader wasn't set, so make it happen. */
1702 if (!cs->state.shader[op->type] && op->shader)
1703 device_invalidate_state(cs->c.device, STATE_CONSTANT_BUFFER(op->type));
1704 cs->state.shader[op->type] = op->shader;
1705 device_invalidate_state(cs->c.device, STATE_SHADER(op->type));
1706 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1707 device_invalidate_state(cs->c.device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1708 else
1709 device_invalidate_state(cs->c.device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1712 void wined3d_device_context_emit_set_shader(struct wined3d_device_context *context,
1713 enum wined3d_shader_type type, struct wined3d_shader *shader)
1715 struct wined3d_cs_set_shader *op;
1717 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1718 op->opcode = WINED3D_CS_OP_SET_SHADER;
1719 op->type = type;
1720 op->shader = shader;
1722 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1725 static void wined3d_cs_exec_set_blend_state(struct wined3d_cs *cs, const void *data)
1727 const struct wined3d_cs_set_blend_state *op = data;
1728 struct wined3d_state *state = &cs->state;
1730 if (state->blend_state != op->state)
1732 state->blend_state = op->state;
1733 device_invalidate_state(cs->c.device, STATE_BLEND);
1735 state->blend_factor = op->factor;
1736 device_invalidate_state(cs->c.device, STATE_BLEND_FACTOR);
1737 state->sample_mask = op->sample_mask;
1738 device_invalidate_state(cs->c.device, STATE_SAMPLE_MASK);
1741 void wined3d_device_context_emit_set_blend_state(struct wined3d_device_context *context,
1742 struct wined3d_blend_state *state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
1744 struct wined3d_cs_set_blend_state *op;
1746 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1747 op->opcode = WINED3D_CS_OP_SET_BLEND_STATE;
1748 op->state = state;
1749 op->factor = *blend_factor;
1750 op->sample_mask = sample_mask;
1752 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1755 static void wined3d_cs_exec_set_depth_stencil_state(struct wined3d_cs *cs, const void *data)
1757 const struct wined3d_cs_set_depth_stencil_state *op = data;
1758 struct wined3d_state *state = &cs->state;
1760 if (state->depth_stencil_state != op->state)
1762 state->depth_stencil_state = op->state;
1763 device_invalidate_state(cs->c.device, STATE_DEPTH_STENCIL);
1765 state->stencil_ref = op->stencil_ref;
1766 device_invalidate_state(cs->c.device, STATE_STENCIL_REF);
1769 void wined3d_device_context_emit_set_depth_stencil_state(struct wined3d_device_context *context,
1770 struct wined3d_depth_stencil_state *state, unsigned int stencil_ref)
1772 struct wined3d_cs_set_depth_stencil_state *op;
1774 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1775 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE;
1776 op->state = state;
1777 op->stencil_ref = stencil_ref;
1779 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1782 static void wined3d_cs_exec_set_rasterizer_state(struct wined3d_cs *cs, const void *data)
1784 const struct wined3d_cs_set_rasterizer_state *op = data;
1786 cs->state.rasterizer_state = op->state;
1787 device_invalidate_state(cs->c.device, STATE_RASTERIZER);
1790 void wined3d_device_context_emit_set_rasterizer_state(struct wined3d_device_context *context,
1791 struct wined3d_rasterizer_state *rasterizer_state)
1793 struct wined3d_cs_set_rasterizer_state *op;
1795 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1796 op->opcode = WINED3D_CS_OP_SET_RASTERIZER_STATE;
1797 op->state = rasterizer_state;
1799 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1802 static void wined3d_cs_exec_set_depth_bounds(struct wined3d_cs *cs, const void *data)
1804 const struct wined3d_cs_set_depth_bounds *op = data;
1806 cs->state.depth_bounds_enable = op->enable;
1807 cs->state.depth_bounds_min = op->min_depth;
1808 cs->state.depth_bounds_max = op->max_depth;
1809 device_invalidate_state(cs->c.device, STATE_DEPTH_BOUNDS);
1812 void wined3d_device_context_set_depth_bounds(struct wined3d_device_context *context,
1813 bool enable, float min_depth, float max_depth)
1815 struct wined3d_cs_set_depth_bounds *op;
1817 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1818 op->opcode = WINED3D_CS_OP_SET_DEPTH_BOUNDS;
1819 op->enable = enable;
1820 op->min_depth = min_depth;
1821 op->max_depth = max_depth;
1823 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1826 static void wined3d_cs_exec_set_render_state(struct wined3d_cs *cs, const void *data)
1828 const struct wined3d_cs_set_render_state *op = data;
1830 cs->state.render_states[op->state] = op->value;
1831 device_invalidate_state(cs->c.device, STATE_RENDER(op->state));
1834 void wined3d_device_context_emit_set_render_state(struct wined3d_device_context *context,
1835 enum wined3d_render_state state, unsigned int value)
1837 struct wined3d_cs_set_render_state *op;
1839 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1840 op->opcode = WINED3D_CS_OP_SET_RENDER_STATE;
1841 op->state = state;
1842 op->value = value;
1844 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1847 static void wined3d_cs_exec_set_texture_state(struct wined3d_cs *cs, const void *data)
1849 const struct wined3d_cs_set_texture_state *op = data;
1851 cs->state.texture_states[op->stage][op->state] = op->value;
1852 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, op->state));
1855 void wined3d_device_context_emit_set_texture_state(struct wined3d_device_context *context, unsigned int stage,
1856 enum wined3d_texture_stage_state state, unsigned int value)
1858 struct wined3d_cs_set_texture_state *op;
1860 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1861 op->opcode = WINED3D_CS_OP_SET_TEXTURE_STATE;
1862 op->stage = stage;
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_transform(struct wined3d_cs *cs, const void *data)
1871 const struct wined3d_cs_set_transform *op = data;
1873 cs->state.transforms[op->state] = op->matrix;
1874 if (op->state < WINED3D_TS_WORLD_MATRIX(cs->c.device->adapter->d3d_info.limits.ffp_vertex_blend_matrices))
1875 device_invalidate_state(cs->c.device, STATE_TRANSFORM(op->state));
1878 void wined3d_device_context_emit_set_transform(struct wined3d_device_context *context,
1879 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1881 struct wined3d_cs_set_transform *op;
1883 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1884 op->opcode = WINED3D_CS_OP_SET_TRANSFORM;
1885 op->state = state;
1886 op->matrix = *matrix;
1888 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1891 static void wined3d_cs_exec_set_clip_plane(struct wined3d_cs *cs, const void *data)
1893 const struct wined3d_cs_set_clip_plane *op = data;
1895 cs->state.clip_planes[op->plane_idx] = op->plane;
1896 device_invalidate_state(cs->c.device, STATE_CLIPPLANE(op->plane_idx));
1899 void wined3d_device_context_emit_set_clip_plane(struct wined3d_device_context *context,
1900 unsigned int plane_idx, const struct wined3d_vec4 *plane)
1902 struct wined3d_cs_set_clip_plane *op;
1904 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1905 op->opcode = WINED3D_CS_OP_SET_CLIP_PLANE;
1906 op->plane_idx = plane_idx;
1907 op->plane = *plane;
1909 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1912 static void wined3d_cs_exec_set_color_key(struct wined3d_cs *cs, const void *data)
1914 const struct wined3d_cs_set_color_key *op = data;
1915 struct wined3d_texture *texture = op->texture;
1917 if (op->set)
1919 switch (op->flags)
1921 case WINED3D_CKEY_DST_BLT:
1922 texture->async.dst_blt_color_key = op->color_key;
1923 texture->async.color_key_flags |= WINED3D_CKEY_DST_BLT;
1924 break;
1926 case WINED3D_CKEY_DST_OVERLAY:
1927 texture->async.dst_overlay_color_key = op->color_key;
1928 texture->async.color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
1929 break;
1931 case WINED3D_CKEY_SRC_BLT:
1932 if (texture == cs->state.textures[0])
1934 device_invalidate_state(cs->c.device, STATE_COLOR_KEY);
1935 if (!(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1936 device_invalidate_state(cs->c.device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1939 texture->async.src_blt_color_key = op->color_key;
1940 texture->async.color_key_flags |= WINED3D_CKEY_SRC_BLT;
1941 break;
1943 case WINED3D_CKEY_SRC_OVERLAY:
1944 texture->async.src_overlay_color_key = op->color_key;
1945 texture->async.color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
1946 break;
1949 else
1951 switch (op->flags)
1953 case WINED3D_CKEY_DST_BLT:
1954 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_BLT;
1955 break;
1957 case WINED3D_CKEY_DST_OVERLAY:
1958 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
1959 break;
1961 case WINED3D_CKEY_SRC_BLT:
1962 if (texture == cs->state.textures[0] && texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1963 device_invalidate_state(cs->c.device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1965 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
1966 break;
1968 case WINED3D_CKEY_SRC_OVERLAY:
1969 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
1970 break;
1975 void wined3d_cs_emit_set_color_key(struct wined3d_cs *cs, struct wined3d_texture *texture,
1976 WORD flags, const struct wined3d_color_key *color_key)
1978 struct wined3d_cs_set_color_key *op;
1980 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1981 op->opcode = WINED3D_CS_OP_SET_COLOR_KEY;
1982 op->texture = texture;
1983 op->flags = flags;
1984 if (color_key)
1986 op->color_key = *color_key;
1987 op->set = 1;
1989 else
1990 op->set = 0;
1992 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1995 static void wined3d_cs_exec_set_material(struct wined3d_cs *cs, const void *data)
1997 const struct wined3d_cs_set_material *op = data;
1999 cs->state.material = op->material;
2000 device_invalidate_state(cs->c.device, STATE_MATERIAL);
2003 void wined3d_device_context_emit_set_material(struct wined3d_device_context *context,
2004 const struct wined3d_material *material)
2006 struct wined3d_cs_set_material *op;
2008 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2009 op->opcode = WINED3D_CS_OP_SET_MATERIAL;
2010 op->material = *material;
2012 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2015 static void wined3d_cs_exec_set_light(struct wined3d_cs *cs, const void *data)
2017 const struct wined3d_cs_set_light *op = data;
2018 struct wined3d_light_info *light_info;
2019 unsigned int light_idx;
2021 light_idx = op->light.OriginalIndex;
2023 if (!(light_info = wined3d_light_state_get_light(&cs->state.light_state, light_idx)))
2025 TRACE("Adding new light.\n");
2026 if (!(light_info = heap_alloc_zero(sizeof(*light_info))))
2028 ERR("Failed to allocate light info.\n");
2029 return;
2032 light_info->glIndex = -1;
2033 light_info->OriginalIndex = light_idx;
2034 rb_put(&cs->state.light_state.lights_tree, (void *)(ULONG_PTR)light_idx, &light_info->entry);
2037 if (light_info->glIndex != -1)
2039 if (light_info->OriginalParms.type != op->light.OriginalParms.type)
2040 device_invalidate_state(cs->c.device, STATE_LIGHT_TYPE);
2041 device_invalidate_state(cs->c.device, STATE_ACTIVELIGHT(light_info->glIndex));
2044 light_info->OriginalParms = op->light.OriginalParms;
2045 light_info->position = op->light.position;
2046 light_info->direction = op->light.direction;
2047 light_info->exponent = op->light.exponent;
2048 light_info->cutoff = op->light.cutoff;
2051 void wined3d_device_context_emit_set_light(struct wined3d_device_context *context,
2052 const struct wined3d_light_info *light)
2054 struct wined3d_cs_set_light *op;
2056 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2057 op->opcode = WINED3D_CS_OP_SET_LIGHT;
2058 op->light = *light;
2060 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2063 static void wined3d_cs_exec_set_light_enable(struct wined3d_cs *cs, const void *data)
2065 const struct wined3d_cs_set_light_enable *op = data;
2066 struct wined3d_device *device = cs->c.device;
2067 struct wined3d_light_info *light_info;
2068 int prev_idx;
2070 if (!(light_info = wined3d_light_state_get_light(&cs->state.light_state, op->idx)))
2072 ERR("Light doesn't exist.\n");
2073 return;
2076 prev_idx = light_info->glIndex;
2077 if (wined3d_light_state_enable_light(&cs->state.light_state, &device->adapter->d3d_info, light_info, op->enable))
2079 device_invalidate_state(device, STATE_LIGHT_TYPE);
2080 device_invalidate_state(device, STATE_ACTIVELIGHT(op->enable ? light_info->glIndex : prev_idx));
2084 void wined3d_device_context_emit_set_light_enable(struct wined3d_device_context *context, unsigned int idx, BOOL enable)
2086 struct wined3d_cs_set_light_enable *op;
2088 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2089 op->opcode = WINED3D_CS_OP_SET_LIGHT_ENABLE;
2090 op->idx = idx;
2091 op->enable = enable;
2093 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2096 static void wined3d_cs_exec_set_feature_level(struct wined3d_cs *cs, const void *data)
2098 const struct wined3d_cs_set_feature_level *op = data;
2100 cs->state.feature_level = op->level;
2103 void wined3d_device_context_emit_set_feature_level(struct wined3d_device_context *context,
2104 enum wined3d_feature_level level)
2106 struct wined3d_cs_set_feature_level *op;
2108 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2109 op->opcode = WINED3D_CS_OP_SET_FEATURE_LEVEL;
2110 op->level = level;
2112 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2115 static const struct push_constant_info
2117 size_t size;
2118 unsigned int max_count;
2119 DWORD mask;
2121 wined3d_cs_push_constant_info[] =
2123 [WINED3D_PUSH_CONSTANTS_VS_F] = {sizeof(struct wined3d_vec4), WINED3D_MAX_VS_CONSTS_F, WINED3D_SHADER_CONST_VS_F},
2124 [WINED3D_PUSH_CONSTANTS_PS_F] = {sizeof(struct wined3d_vec4), WINED3D_MAX_PS_CONSTS_F, WINED3D_SHADER_CONST_PS_F},
2125 [WINED3D_PUSH_CONSTANTS_VS_I] = {sizeof(struct wined3d_ivec4), WINED3D_MAX_CONSTS_I, WINED3D_SHADER_CONST_VS_I},
2126 [WINED3D_PUSH_CONSTANTS_PS_I] = {sizeof(struct wined3d_ivec4), WINED3D_MAX_CONSTS_I, WINED3D_SHADER_CONST_PS_I},
2127 [WINED3D_PUSH_CONSTANTS_VS_B] = {sizeof(BOOL), WINED3D_MAX_CONSTS_B, WINED3D_SHADER_CONST_VS_B},
2128 [WINED3D_PUSH_CONSTANTS_PS_B] = {sizeof(BOOL), WINED3D_MAX_CONSTS_B, WINED3D_SHADER_CONST_PS_B},
2131 static bool prepare_push_constant_buffer(struct wined3d_device *device, enum wined3d_push_constants type)
2133 const struct push_constant_info *info = &wined3d_cs_push_constant_info[type];
2134 HRESULT hr;
2136 const struct wined3d_buffer_desc desc =
2138 .byte_width = info->max_count * info->size,
2139 .bind_flags = 0,
2140 .access = WINED3D_RESOURCE_ACCESS_CPU | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W,
2143 if (!device->push_constants[type] && FAILED(hr = wined3d_buffer_create(device,
2144 &desc, NULL, NULL, &wined3d_null_parent_ops, &device->push_constants[type])))
2146 ERR("Failed to create push constant buffer, hr %#lx.\n", hr);
2147 return false;
2150 return true;
2153 static void wined3d_cs_exec_push_constants(struct wined3d_cs *cs, const void *data)
2155 const struct wined3d_cs_push_constants *op = data;
2156 struct wined3d_device *device = cs->c.device;
2157 unsigned int context_count, i;
2159 /* The constant buffers were already updated; this op is just to mark the
2160 * constants as invalid in the device state. */
2162 if (op->type == WINED3D_PUSH_CONSTANTS_VS_F)
2163 device->shader_backend->shader_update_float_vertex_constants(device, op->start_idx, op->count);
2164 else if (op->type == WINED3D_PUSH_CONSTANTS_PS_F)
2165 device->shader_backend->shader_update_float_pixel_constants(device, op->start_idx, op->count);
2167 for (i = 0, context_count = device->context_count; i < context_count; ++i)
2168 device->contexts[i]->constant_update_mask |= wined3d_cs_push_constant_info[op->type].mask;
2171 static void wined3d_device_context_emit_push_constants(struct wined3d_device_context *context,
2172 enum wined3d_push_constants type, unsigned int start_idx, unsigned int count)
2174 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2175 struct wined3d_cs_push_constants *op;
2177 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2178 op->opcode = WINED3D_CS_OP_PUSH_CONSTANTS;
2179 op->type = type;
2180 op->start_idx = start_idx;
2181 op->count = count;
2183 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2186 void wined3d_device_context_push_constants(struct wined3d_device_context *context,
2187 enum wined3d_push_constants type, unsigned int start_idx,
2188 unsigned int count, const void *constants)
2190 const struct push_constant_info *info = &wined3d_cs_push_constant_info[type];
2191 unsigned int byte_offset = start_idx * info->size;
2192 unsigned int byte_size = count * info->size;
2193 struct wined3d_box box;
2195 if (!prepare_push_constant_buffer(context->device, type))
2196 return;
2198 wined3d_box_set(&box, byte_offset, 0, byte_offset + byte_size, 1, 0, 1);
2199 wined3d_device_context_emit_update_sub_resource(context,
2200 &context->device->push_constants[type]->resource, 0, &box, constants, byte_size, byte_size);
2201 wined3d_device_context_emit_push_constants(context, type, start_idx, count);
2204 static void wined3d_cs_exec_reset_state(struct wined3d_cs *cs, const void *data)
2206 const struct wined3d_device *device = cs->c.device;
2207 const struct wined3d_cs_reset_state *op = data;
2208 const struct wined3d_state_entry *state_table;
2209 unsigned int state;
2211 state_cleanup(&cs->state);
2212 wined3d_state_reset(&cs->state, &device->adapter->d3d_info);
2213 if (op->invalidate)
2215 state_table = device->state_table;
2216 for (state = 0; state <= STATE_HIGHEST; ++state)
2218 if (state_table[state].representative)
2219 device_invalidate_state(device, state);
2224 void wined3d_device_context_emit_reset_state(struct wined3d_device_context *context, bool invalidate)
2226 struct wined3d_cs_reset_state *op;
2228 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2229 op->opcode = WINED3D_CS_OP_RESET_STATE;
2230 op->invalidate = invalidate;
2232 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2235 static void wined3d_cs_exec_callback(struct wined3d_cs *cs, const void *data)
2237 const struct wined3d_cs_callback *op = data;
2239 op->callback(op->object);
2242 static void wined3d_cs_emit_callback(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2244 struct wined3d_cs_callback *op;
2246 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2247 op->opcode = WINED3D_CS_OP_CALLBACK;
2248 op->callback = callback;
2249 op->object = object;
2251 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2254 void wined3d_cs_destroy_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2256 wined3d_cs_emit_callback(cs, callback, object);
2259 void wined3d_cs_init_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2261 wined3d_cs_emit_callback(cs, callback, object);
2264 static void wined3d_cs_exec_query_issue(struct wined3d_cs *cs, const void *data)
2266 const struct wined3d_cs_query_issue *op = data;
2267 struct wined3d_query *query = op->query;
2268 BOOL poll;
2270 poll = query->query_ops->query_issue(query, op->flags);
2272 if (!query->poll_in_cs)
2274 if (op->flags & WINED3DISSUE_END)
2275 InterlockedIncrement(&query->counter_retrieved);
2276 return;
2279 if (poll && list_empty(&query->poll_list_entry))
2281 if (query->buffer_object)
2282 InterlockedIncrement(&query->counter_retrieved);
2283 else
2284 list_add_tail(&cs->query_poll_list, &query->poll_list_entry);
2285 return;
2288 /* This can happen if occlusion queries are restarted. This discards the
2289 * old result, since polling it could result in a GL error. */
2290 if ((op->flags & WINED3DISSUE_BEGIN) && !poll && !list_empty(&query->poll_list_entry))
2292 list_remove(&query->poll_list_entry);
2293 list_init(&query->poll_list_entry);
2294 InterlockedIncrement(&query->counter_retrieved);
2295 return;
2298 /* This can happen when an occlusion query is ended without being started,
2299 * in which case we don't want to poll, but still have to counter-balance
2300 * the increment of the main counter.
2302 * This can also happen if an event query is re-issued before the first
2303 * fence was reached. In this case the query is already in the list and
2304 * the poll function will check the new fence. We have to counter-balance
2305 * the discarded increment. */
2306 if (op->flags & WINED3DISSUE_END)
2307 InterlockedIncrement(&query->counter_retrieved);
2310 static void wined3d_cs_issue_query(struct wined3d_device_context *context,
2311 struct wined3d_query *query, unsigned int flags)
2313 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2314 struct wined3d_cs_query_issue *op;
2316 if (flags & WINED3DISSUE_END)
2317 ++query->counter_main;
2319 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2320 op->opcode = WINED3D_CS_OP_QUERY_ISSUE;
2321 op->query = query;
2322 op->flags = flags;
2324 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2325 cs->queries_flushed = FALSE;
2327 if (flags & WINED3DISSUE_BEGIN)
2328 query->state = QUERY_BUILDING;
2329 else
2330 query->state = QUERY_SIGNALLED;
2333 static void wined3d_cs_reference_command_list(struct wined3d_device_context *context, struct wined3d_command_list *list)
2335 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2336 SIZE_T i;
2338 if (list->query_count)
2340 cs->queries_flushed = FALSE;
2342 for (i = 0; i < list->query_count; ++i)
2344 if (list->queries[i].flags & WINED3DISSUE_END)
2346 list->queries[i].query->counter_main++;
2347 list->queries[i].query->state = QUERY_SIGNALLED;
2349 else
2351 list->queries[i].query->state = QUERY_BUILDING;
2356 for (i = 0; i < list->resource_count; ++i)
2357 wined3d_resource_reference(list->resources[i]);
2359 for (i = 0; i < list->command_list_count; ++i)
2360 wined3d_cs_reference_command_list(context, list->command_lists[i]);
2362 for (i = 0; i < list->upload_count; ++i)
2363 invalidate_client_address(list->uploads[i].resource);
2366 static void wined3d_cs_exec_preload_resource(struct wined3d_cs *cs, const void *data)
2368 const struct wined3d_cs_preload_resource *op = data;
2369 struct wined3d_resource *resource = op->resource;
2371 resource->resource_ops->resource_preload(resource);
2374 void wined3d_cs_emit_preload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
2376 struct wined3d_cs_preload_resource *op;
2378 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2379 op->opcode = WINED3D_CS_OP_PRELOAD_RESOURCE;
2380 op->resource = resource;
2382 wined3d_resource_reference(resource);
2384 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2387 static void wined3d_cs_exec_unload_resource(struct wined3d_cs *cs, const void *data)
2389 const struct wined3d_cs_unload_resource *op = data;
2390 struct wined3d_resource *resource = op->resource;
2392 resource->resource_ops->resource_unload(resource);
2395 void wined3d_cs_emit_unload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
2397 struct wined3d_cs_unload_resource *op;
2399 discard_client_address(resource);
2401 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2402 op->opcode = WINED3D_CS_OP_UNLOAD_RESOURCE;
2403 op->resource = resource;
2405 wined3d_resource_reference(resource);
2407 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2410 static void wined3d_device_context_upload_bo(struct wined3d_device_context *context,
2411 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
2412 const struct upload_bo *bo, unsigned int row_pitch, unsigned int slice_pitch)
2414 struct wined3d_cs_update_sub_resource *op;
2416 TRACE("context %p, resource %p, sub_resource_idx %u, box %s, bo %s, flags %#x, row_pitch %u, slice_pitch %u.\n",
2417 context, resource, sub_resource_idx, debug_box(box),
2418 debug_const_bo_address(&bo->addr), bo->flags, row_pitch, slice_pitch);
2420 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2421 op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2422 op->resource = resource;
2423 op->sub_resource_idx = sub_resource_idx;
2424 op->box = *box;
2425 op->bo = *bo;
2426 op->row_pitch = row_pitch;
2427 op->slice_pitch = slice_pitch;
2429 wined3d_device_context_reference_resource(context, resource);
2431 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2434 static void wined3d_cs_exec_map(struct wined3d_cs *cs, const void *data)
2436 const struct wined3d_cs_map *op = data;
2437 struct wined3d_resource *resource = op->resource;
2439 *op->hr = resource->resource_ops->resource_sub_resource_map(resource,
2440 op->sub_resource_idx, op->map_ptr, op->box, op->flags);
2443 HRESULT wined3d_device_context_emit_map(struct wined3d_device_context *context,
2444 struct wined3d_resource *resource, unsigned int sub_resource_idx,
2445 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
2447 struct wined3d_cs_map *op;
2448 HRESULT hr;
2450 /* Mapping resources from the worker thread isn't an issue by itself, but
2451 * increasing the map count would be visible to applications. */
2452 wined3d_not_from_cs(context->device->cs);
2454 if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
2455 && context->ops->map_upload_bo(context, resource, sub_resource_idx, map_desc, box, flags))
2457 TRACE("Returning map pointer %p, row pitch %u, slice pitch %u.\n",
2458 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
2459 return WINED3D_OK;
2462 wined3d_resource_wait_idle(resource);
2464 /* We might end up invalidating the resource on the CS thread. */
2465 invalidate_client_address(resource);
2467 if (!(op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_MAP)))
2468 return E_OUTOFMEMORY;
2469 op->opcode = WINED3D_CS_OP_MAP;
2470 op->resource = resource;
2471 op->sub_resource_idx = sub_resource_idx;
2472 op->map_ptr = &map_desc->data;
2473 op->box = box;
2474 op->flags = flags;
2475 op->hr = &hr;
2477 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_MAP);
2478 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_MAP);
2480 if (SUCCEEDED(hr))
2481 wined3d_resource_get_sub_resource_map_pitch(resource, sub_resource_idx,
2482 &map_desc->row_pitch, &map_desc->slice_pitch);
2483 return hr;
2486 static void wined3d_cs_exec_unmap(struct wined3d_cs *cs, const void *data)
2488 const struct wined3d_cs_unmap *op = data;
2489 struct wined3d_resource *resource = op->resource;
2491 *op->hr = resource->resource_ops->resource_sub_resource_unmap(resource, op->sub_resource_idx);
2494 HRESULT wined3d_device_context_emit_unmap(struct wined3d_device_context *context,
2495 struct wined3d_resource *resource, unsigned int sub_resource_idx)
2497 struct wined3d_cs_unmap *op;
2498 struct wined3d_box box;
2499 struct upload_bo bo;
2500 HRESULT hr;
2502 if (context->ops->unmap_upload_bo(context, resource, sub_resource_idx, &box, &bo))
2504 unsigned int row_pitch, slice_pitch;
2506 wined3d_resource_get_sub_resource_map_pitch(resource, sub_resource_idx, &row_pitch, &slice_pitch);
2507 if (bo.flags & UPLOAD_BO_UPLOAD_ON_UNMAP)
2508 wined3d_device_context_upload_bo(context, resource, sub_resource_idx, &box, &bo, row_pitch, slice_pitch);
2509 return WINED3D_OK;
2512 wined3d_not_from_cs(context->device->cs);
2514 if (!(op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_MAP)))
2515 return E_OUTOFMEMORY;
2516 op->opcode = WINED3D_CS_OP_UNMAP;
2517 op->resource = resource;
2518 op->sub_resource_idx = sub_resource_idx;
2519 op->hr = &hr;
2521 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_MAP);
2522 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_MAP);
2524 return hr;
2527 static void wined3d_cs_exec_map_bo_address(struct wined3d_cs *cs, const void *data)
2529 const struct wined3d_cs_map_bo_address *op = data;
2530 struct wined3d_context *context;
2532 context = context_acquire(cs->c.device, NULL, 0);
2533 wined3d_context_map_bo_address(context, &op->addr, op->size, op->flags);
2534 context_release(context);
2537 void wined3d_cs_map_bo_address(struct wined3d_cs *cs,
2538 struct wined3d_bo_address *addr, size_t size, unsigned int flags)
2540 struct wined3d_device_context *context = &cs->c;
2541 struct wined3d_cs_map_bo_address *op;
2543 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2544 op->opcode = WINED3D_CS_OP_MAP_BO_ADDRESS;
2545 op->addr = *addr;
2546 op->size = size;
2547 op->flags = flags;
2548 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_MAP);
2549 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_MAP);
2552 static void wined3d_cs_exec_blt_sub_resource(struct wined3d_cs *cs, const void *data)
2554 const struct wined3d_cs_blt_sub_resource *op = data;
2556 if (op->dst_resource->type == WINED3D_RTYPE_BUFFER)
2558 wined3d_buffer_copy(buffer_from_resource(op->dst_resource), op->dst_box.left,
2559 buffer_from_resource(op->src_resource), op->src_box.left,
2560 op->src_box.right - op->src_box.left);
2562 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_3D)
2564 struct wined3d_texture *src_texture, *dst_texture;
2565 unsigned int level, update_w, update_h, update_d;
2566 unsigned int row_pitch, slice_pitch;
2567 struct wined3d_context *context;
2568 struct wined3d_bo_address addr;
2569 unsigned int location;
2571 if (op->flags & ~WINED3D_BLT_RAW)
2573 FIXME("Flags %#x not implemented for %s resources.\n",
2574 op->flags, debug_d3dresourcetype(op->dst_resource->type));
2575 return;
2578 if (!(op->flags & WINED3D_BLT_RAW) && op->src_resource->format != op->dst_resource->format)
2580 FIXME("Format conversion not implemented for %s resources.\n",
2581 debug_d3dresourcetype(op->dst_resource->type));
2582 return;
2585 update_w = op->dst_box.right - op->dst_box.left;
2586 update_h = op->dst_box.bottom - op->dst_box.top;
2587 update_d = op->dst_box.back - op->dst_box.front;
2588 if (op->src_box.right - op->src_box.left != update_w
2589 || op->src_box.bottom - op->src_box.top != update_h
2590 || op->src_box.back - op->src_box.front != update_d)
2592 FIXME("Stretching not implemented for %s resources.\n",
2593 debug_d3dresourcetype(op->dst_resource->type));
2594 return;
2597 dst_texture = texture_from_resource(op->dst_resource);
2598 src_texture = texture_from_resource(op->src_resource);
2600 context = context_acquire(cs->c.device, NULL, 0);
2602 location = src_texture->resource.map_binding;
2603 if (location == WINED3D_LOCATION_SYSMEM
2604 && wined3d_texture_can_use_pbo(src_texture, &context->device->adapter->d3d_info))
2605 location = WINED3D_LOCATION_BUFFER;
2607 if (!wined3d_texture_load_location(src_texture, op->src_sub_resource_idx,
2608 context, location))
2610 ERR("Failed to load source sub-resource into %s.\n",
2611 wined3d_debug_location(location));
2612 context_release(context);
2613 return;
2616 level = op->dst_sub_resource_idx % dst_texture->level_count;
2617 if (update_w == wined3d_texture_get_level_width(dst_texture, level)
2618 && update_h == wined3d_texture_get_level_height(dst_texture, level)
2619 && update_d == wined3d_texture_get_level_depth(dst_texture, level))
2621 wined3d_texture_prepare_location(dst_texture, op->dst_sub_resource_idx,
2622 context, WINED3D_LOCATION_TEXTURE_RGB);
2624 else if (!wined3d_texture_load_location(dst_texture, op->dst_sub_resource_idx,
2625 context, WINED3D_LOCATION_TEXTURE_RGB))
2627 ERR("Failed to load destination sub-resource.\n");
2628 context_release(context);
2629 return;
2632 wined3d_texture_get_bo_address(src_texture, op->src_sub_resource_idx, &addr, location);
2633 wined3d_texture_get_pitch(src_texture, op->src_sub_resource_idx % src_texture->level_count,
2634 &row_pitch, &slice_pitch);
2636 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&addr),
2637 dst_texture->resource.format, &op->src_box, row_pitch, slice_pitch, dst_texture,
2638 op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
2639 op->dst_box.left, op->dst_box.top, op->dst_box.front);
2640 wined3d_texture_validate_location(dst_texture, op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2641 wined3d_texture_invalidate_location(dst_texture, op->dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2643 context_release(context);
2645 else
2647 if (FAILED(texture2d_blt(texture_from_resource(op->dst_resource), op->dst_sub_resource_idx,
2648 &op->dst_box, texture_from_resource(op->src_resource), op->src_sub_resource_idx,
2649 &op->src_box, op->flags, &op->fx, op->filter)))
2650 FIXME("Blit failed.\n");
2654 void wined3d_device_context_emit_blt_sub_resource(struct wined3d_device_context *context,
2655 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, const struct wined3d_box *dst_box,
2656 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx, const struct wined3d_box *src_box,
2657 unsigned int flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2659 struct wined3d_cs_blt_sub_resource *op;
2661 /* If we are replacing the whole resource, the CS thread might discard and
2662 * rename the buffer object, in which case ours is no longer valid. */
2663 if (dst_resource->type == WINED3D_RTYPE_BUFFER && dst_box->right - dst_box->left == dst_resource->size)
2664 invalidate_client_address(dst_resource);
2666 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2667 op->opcode = WINED3D_CS_OP_BLT_SUB_RESOURCE;
2668 op->dst_resource = dst_resource;
2669 op->dst_sub_resource_idx = dst_sub_resource_idx;
2670 op->dst_box = *dst_box;
2671 op->src_resource = src_resource;
2672 op->src_sub_resource_idx = src_sub_resource_idx;
2673 op->src_box = *src_box;
2674 op->flags = flags;
2675 if (fx)
2676 op->fx = *fx;
2677 else
2678 memset(&op->fx, 0, sizeof(op->fx));
2679 op->filter = filter;
2681 wined3d_device_context_reference_resource(context, dst_resource);
2682 if (src_resource)
2683 wined3d_device_context_reference_resource(context, src_resource);
2685 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2686 if (flags & WINED3D_BLT_SYNCHRONOUS)
2687 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_DEFAULT);
2690 static void wined3d_cs_exec_update_sub_resource(struct wined3d_cs *cs, const void *data)
2692 const struct wined3d_cs_update_sub_resource *op = data;
2693 struct wined3d_resource *resource = op->resource;
2694 const struct wined3d_box *box = &op->box;
2695 struct wined3d_context *context;
2697 context = context_acquire(cs->c.device, NULL, 0);
2699 if (resource->type == WINED3D_RTYPE_BUFFER)
2700 wined3d_buffer_update_sub_resource(buffer_from_resource(resource),
2701 context, &op->bo, box->left, box->right - box->left);
2702 else
2703 wined3d_texture_update_sub_resource(texture_from_resource(resource),
2704 op->sub_resource_idx, context, &op->bo, box, op->row_pitch, op->slice_pitch);
2706 context_release(context);
2708 if (op->bo.flags & UPLOAD_BO_FREE_ON_UNMAP)
2710 if (op->bo.addr.buffer_object)
2711 FIXME("Free BO address %s.\n", debug_const_bo_address(&op->bo.addr));
2712 else
2713 heap_free((void *)op->bo.addr.addr);
2717 void wined3d_device_context_emit_update_sub_resource(struct wined3d_device_context *context,
2718 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
2719 const void *data, unsigned int row_pitch, unsigned int slice_pitch)
2721 struct wined3d_cs_update_sub_resource *op;
2722 struct wined3d_map_desc map_desc;
2723 struct wined3d_box dummy_box;
2724 struct upload_bo bo;
2726 /* If we are replacing the whole resource, the CS thread might discard and
2727 * rename the buffer object, in which case ours is no longer valid. */
2728 if (resource->type == WINED3D_RTYPE_BUFFER && box->right - box->left == resource->size)
2729 invalidate_client_address(resource);
2731 if (context->ops->map_upload_bo(context, resource, sub_resource_idx, &map_desc, box, WINED3D_MAP_WRITE))
2733 wined3d_format_copy_data(resource->format, data, row_pitch, slice_pitch, map_desc.data, map_desc.row_pitch,
2734 map_desc.slice_pitch, box->right - box->left, box->bottom - box->top, box->back - box->front);
2735 context->ops->unmap_upload_bo(context, resource, sub_resource_idx, &dummy_box, &bo);
2736 wined3d_device_context_upload_bo(context, resource, sub_resource_idx,
2737 box, &bo, map_desc.row_pitch, map_desc.slice_pitch);
2738 return;
2741 wined3d_resource_wait_idle(resource);
2743 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2744 op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2745 op->resource = resource;
2746 op->sub_resource_idx = sub_resource_idx;
2747 op->box = *box;
2748 op->bo.addr.buffer_object = 0;
2749 op->bo.addr.addr = data;
2750 op->bo.flags = 0;
2751 op->row_pitch = row_pitch;
2752 op->slice_pitch = slice_pitch;
2754 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_MAP);
2755 /* The data pointer may go away, so we need to wait until it is read.
2756 * Copying the data may be faster if it's small. */
2757 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_MAP);
2760 static void wined3d_cs_exec_add_dirty_texture_region(struct wined3d_cs *cs, const void *data)
2762 const struct wined3d_cs_add_dirty_texture_region *op = data;
2763 struct wined3d_texture *texture = op->texture;
2764 unsigned int sub_resource_idx, i;
2765 struct wined3d_context *context;
2767 context = context_acquire(cs->c.device, NULL, 0);
2768 sub_resource_idx = op->layer * texture->level_count;
2769 for (i = 0; i < texture->level_count; ++i, ++sub_resource_idx)
2771 if (wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
2772 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2773 else
2774 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
2776 context_release(context);
2779 void wined3d_cs_emit_add_dirty_texture_region(struct wined3d_cs *cs,
2780 struct wined3d_texture *texture, unsigned int layer)
2782 struct wined3d_cs_add_dirty_texture_region *op;
2784 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2785 op->opcode = WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION;
2786 op->texture = texture;
2787 op->layer = layer;
2789 wined3d_resource_reference(&texture->resource);
2791 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2794 static void wined3d_cs_exec_clear_unordered_access_view(struct wined3d_cs *cs, const void *data)
2796 const struct wined3d_cs_clear_unordered_access_view *op = data;
2797 struct wined3d_unordered_access_view *view = op->view;
2798 struct wined3d_context *context;
2800 context = context_acquire(cs->c.device, NULL, 0);
2801 cs->c.device->adapter->adapter_ops->adapter_clear_uav(context, view, &op->clear_value, op->fp);
2802 context_release(context);
2805 void wined3d_device_context_emit_clear_uav(struct wined3d_device_context *context,
2806 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value, bool fp)
2808 struct wined3d_cs_clear_unordered_access_view *op;
2810 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2811 op->opcode = WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW;
2812 op->view = view;
2813 op->clear_value = *clear_value;
2814 op->fp = fp;
2816 wined3d_device_context_reference_resource(context, view->resource);
2818 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2821 static void wined3d_cs_exec_copy_uav_counter(struct wined3d_cs *cs, const void *data)
2823 const struct wined3d_cs_copy_uav_counter *op = data;
2824 struct wined3d_unordered_access_view *view = op->view;
2825 struct wined3d_context *context;
2827 context = context_acquire(cs->c.device, NULL, 0);
2828 wined3d_unordered_access_view_copy_counter(view, op->buffer, op->offset, context);
2829 context_release(context);
2832 void wined3d_device_context_emit_copy_uav_counter(struct wined3d_device_context *context,
2833 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
2835 struct wined3d_cs_copy_uav_counter *op;
2837 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2838 op->opcode = WINED3D_CS_OP_COPY_UAV_COUNTER;
2839 op->buffer = dst_buffer;
2840 op->offset = offset;
2841 op->view = uav;
2843 wined3d_device_context_reference_resource(context, &dst_buffer->resource);
2845 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2848 static void wined3d_cs_exec_generate_mipmaps(struct wined3d_cs *cs, const void *data)
2850 const struct wined3d_cs_generate_mipmaps *op = data;
2851 struct wined3d_shader_resource_view *view = op->view;
2852 struct wined3d_context *context;
2854 context = context_acquire(cs->c.device, NULL, 0);
2855 cs->c.device->adapter->adapter_ops->adapter_generate_mipmap(context, view);
2856 context_release(context);
2859 void wined3d_device_context_emit_generate_mipmaps(struct wined3d_device_context *context,
2860 struct wined3d_shader_resource_view *view)
2862 struct wined3d_cs_generate_mipmaps *op;
2864 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2865 op->opcode = WINED3D_CS_OP_GENERATE_MIPMAPS;
2866 op->view = view;
2868 wined3d_device_context_reference_resource(context, view->resource);
2870 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2873 static void wined3d_cs_emit_stop(struct wined3d_cs *cs)
2875 struct wined3d_cs_stop *op;
2877 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2878 op->opcode = WINED3D_CS_OP_STOP;
2880 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2881 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2884 static void wined3d_cs_reference_resource(struct wined3d_device_context *context, struct wined3d_resource *resource)
2886 wined3d_resource_reference(resource);
2889 static void wined3d_cs_exec_execute_command_list(struct wined3d_cs *cs, const void *data);
2891 static void (* const wined3d_cs_op_handlers[])(struct wined3d_cs *cs, const void *data) =
2893 /* WINED3D_CS_OP_NOP */ wined3d_cs_exec_nop,
2894 /* WINED3D_CS_OP_PRESENT */ wined3d_cs_exec_present,
2895 /* WINED3D_CS_OP_CLEAR */ wined3d_cs_exec_clear,
2896 /* WINED3D_CS_OP_DISPATCH */ wined3d_cs_exec_dispatch,
2897 /* WINED3D_CS_OP_DRAW */ wined3d_cs_exec_draw,
2898 /* WINED3D_CS_OP_FLUSH */ wined3d_cs_exec_flush,
2899 /* WINED3D_CS_OP_SET_PREDICATION */ wined3d_cs_exec_set_predication,
2900 /* WINED3D_CS_OP_SET_VIEWPORTS */ wined3d_cs_exec_set_viewports,
2901 /* WINED3D_CS_OP_SET_SCISSOR_RECTS */ wined3d_cs_exec_set_scissor_rects,
2902 /* WINED3D_CS_OP_SET_RENDERTARGET_VIEWS */ wined3d_cs_exec_set_rendertarget_views,
2903 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW */ wined3d_cs_exec_set_depth_stencil_view,
2904 /* WINED3D_CS_OP_SET_VERTEX_DECLARATION */ wined3d_cs_exec_set_vertex_declaration,
2905 /* WINED3D_CS_OP_SET_STREAM_SOURCES */ wined3d_cs_exec_set_stream_sources,
2906 /* WINED3D_CS_OP_SET_STREAM_OUTPUTS */ wined3d_cs_exec_set_stream_outputs,
2907 /* WINED3D_CS_OP_SET_INDEX_BUFFER */ wined3d_cs_exec_set_index_buffer,
2908 /* WINED3D_CS_OP_SET_CONSTANT_BUFFERS */ wined3d_cs_exec_set_constant_buffers,
2909 /* WINED3D_CS_OP_SET_TEXTURE */ wined3d_cs_exec_set_texture,
2910 /* WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEWS */ wined3d_cs_exec_set_shader_resource_views,
2911 /* WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEWS */ wined3d_cs_exec_set_unordered_access_views,
2912 /* WINED3D_CS_OP_SET_SAMPLERS */ wined3d_cs_exec_set_samplers,
2913 /* WINED3D_CS_OP_SET_SHADER */ wined3d_cs_exec_set_shader,
2914 /* WINED3D_CS_OP_SET_BLEND_STATE */ wined3d_cs_exec_set_blend_state,
2915 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE */ wined3d_cs_exec_set_depth_stencil_state,
2916 /* WINED3D_CS_OP_SET_RASTERIZER_STATE */ wined3d_cs_exec_set_rasterizer_state,
2917 /* WINED3D_CS_OP_SET_DEPTH_BOUNDS */ wined3d_cs_exec_set_depth_bounds,
2918 /* WINED3D_CS_OP_SET_RENDER_STATE */ wined3d_cs_exec_set_render_state,
2919 /* WINED3D_CS_OP_SET_TEXTURE_STATE */ wined3d_cs_exec_set_texture_state,
2920 /* WINED3D_CS_OP_SET_TRANSFORM */ wined3d_cs_exec_set_transform,
2921 /* WINED3D_CS_OP_SET_CLIP_PLANE */ wined3d_cs_exec_set_clip_plane,
2922 /* WINED3D_CS_OP_SET_COLOR_KEY */ wined3d_cs_exec_set_color_key,
2923 /* WINED3D_CS_OP_SET_MATERIAL */ wined3d_cs_exec_set_material,
2924 /* WINED3D_CS_OP_SET_LIGHT */ wined3d_cs_exec_set_light,
2925 /* WINED3D_CS_OP_SET_LIGHT_ENABLE */ wined3d_cs_exec_set_light_enable,
2926 /* WINED3D_CS_OP_SET_FEATURE_LEVEL */ wined3d_cs_exec_set_feature_level,
2927 /* WINED3D_CS_OP_PUSH_CONSTANTS */ wined3d_cs_exec_push_constants,
2928 /* WINED3D_CS_OP_RESET_STATE */ wined3d_cs_exec_reset_state,
2929 /* WINED3D_CS_OP_CALLBACK */ wined3d_cs_exec_callback,
2930 /* WINED3D_CS_OP_QUERY_ISSUE */ wined3d_cs_exec_query_issue,
2931 /* WINED3D_CS_OP_PRELOAD_RESOURCE */ wined3d_cs_exec_preload_resource,
2932 /* WINED3D_CS_OP_UNLOAD_RESOURCE */ wined3d_cs_exec_unload_resource,
2933 /* WINED3D_CS_OP_MAP */ wined3d_cs_exec_map,
2934 /* WINED3D_CS_OP_UNMAP */ wined3d_cs_exec_unmap,
2935 /* WINED3D_CS_OP_MAP_BO_ADDRESS */ wined3d_cs_exec_map_bo_address,
2936 /* WINED3D_CS_OP_BLT_SUB_RESOURCE */ wined3d_cs_exec_blt_sub_resource,
2937 /* WINED3D_CS_OP_UPDATE_SUB_RESOURCE */ wined3d_cs_exec_update_sub_resource,
2938 /* WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION */ wined3d_cs_exec_add_dirty_texture_region,
2939 /* WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_clear_unordered_access_view,
2940 /* WINED3D_CS_OP_COPY_UAV_COUNTER */ wined3d_cs_exec_copy_uav_counter,
2941 /* WINED3D_CS_OP_GENERATE_MIPMAPS */ wined3d_cs_exec_generate_mipmaps,
2942 /* WINED3D_CS_OP_EXECUTE_COMMAND_LIST */ wined3d_cs_exec_execute_command_list,
2945 void wined3d_device_context_emit_execute_command_list(struct wined3d_device_context *context,
2946 struct wined3d_command_list *list, bool restore_state)
2948 struct wined3d_cs_execute_command_list *op;
2950 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2951 op->opcode = WINED3D_CS_OP_EXECUTE_COMMAND_LIST;
2952 op->list = list;
2954 context->ops->reference_command_list(context, list);
2956 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2958 if (restore_state)
2959 wined3d_device_context_set_state(context, context->state);
2960 else
2961 wined3d_device_context_reset_state(context);
2964 static void *wined3d_cs_st_require_space(struct wined3d_device_context *context,
2965 size_t size, enum wined3d_cs_queue_id queue_id)
2967 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2969 if (size > (cs->data_size - cs->end))
2971 size_t new_size;
2972 void *new_data;
2974 new_size = max(size, cs->data_size * 2);
2975 if (!cs->end)
2976 new_data = heap_realloc(cs->data, new_size);
2977 else
2978 new_data = heap_alloc(new_size);
2979 if (!new_data)
2980 return NULL;
2982 cs->data_size = new_size;
2983 cs->start = cs->end = 0;
2984 cs->data = new_data;
2987 cs->end += size;
2989 return (BYTE *)cs->data + cs->start;
2992 static void wined3d_cs_st_submit(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
2994 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2995 enum wined3d_cs_op opcode;
2996 size_t start;
2997 BYTE *data;
2999 data = cs->data;
3000 start = cs->start;
3001 cs->start = cs->end;
3003 opcode = *(const enum wined3d_cs_op *)&data[start];
3004 if (opcode >= WINED3D_CS_OP_STOP)
3005 ERR("Invalid opcode %#x.\n", opcode);
3006 else
3007 wined3d_cs_op_handlers[opcode](cs, &data[start]);
3009 if (cs->data == data)
3010 cs->start = cs->end = start;
3011 else if (!start)
3012 heap_free(data);
3015 static void wined3d_cs_st_finish(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
3019 static bool wined3d_cs_map_upload_bo(struct wined3d_device_context *context, struct wined3d_resource *resource,
3020 unsigned int sub_resource_idx, struct wined3d_map_desc *map_desc, const struct wined3d_box *box, uint32_t flags)
3022 struct wined3d_client_resource *client = &resource->client;
3023 const struct wined3d_format *format = resource->format;
3024 size_t size;
3026 if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
3028 const struct wined3d_d3d_info *d3d_info = &context->device->adapter->d3d_info;
3029 struct wined3d_device *device = context->device;
3030 struct wined3d_bo_address addr;
3031 struct wined3d_bo *bo;
3032 uint8_t *map_ptr;
3034 /* We can't use persistent maps if we might need to do vertex attribute
3035 * conversion; that will cause the CS thread to invalidate the BO. */
3036 if (!d3d_info->xyzrhw || !d3d_info->vertex_bgra || !d3d_info->ffp_generic_attributes)
3038 TRACE("Not returning a persistent buffer because we might need to do vertex attribute conversion.\n");
3039 return false;
3042 if (resource->pin_sysmem)
3044 TRACE("Not allocating an upload buffer because system memory is pinned for this resource.\n");
3045 return false;
3048 if ((flags & WINED3D_MAP_NOOVERWRITE) && client->addr.buffer_object == CLIENT_BO_DISCARDED)
3049 flags = (flags & ~WINED3D_MAP_NOOVERWRITE) | WINED3D_MAP_DISCARD;
3051 if (flags & WINED3D_MAP_DISCARD)
3053 if (!device->adapter->adapter_ops->adapter_alloc_bo(device, resource, sub_resource_idx, &addr))
3054 return false;
3056 /* Limit NOOVERWRITE maps to buffers for now; there are too many
3057 * ways that a texture can be invalidated to even count. */
3058 if (resource->type == WINED3D_RTYPE_BUFFER)
3059 client->addr = addr;
3061 else
3063 addr = client->addr;
3066 map_ptr = NULL;
3067 if ((bo = addr.buffer_object))
3069 wined3d_device_bo_map_lock(device);
3070 if ((map_ptr = bo->map_ptr))
3071 ++bo->client_map_count;
3072 wined3d_device_bo_map_unlock(device);
3074 if (!map_ptr)
3076 /* adapter_alloc_bo() should have given us a mapped BO if we are
3077 * discarding. */
3078 assert(flags & WINED3D_MAP_NOOVERWRITE);
3079 WARN_(d3d_perf)("Not accelerating a NOOVERWRITE map because the BO is not mapped.\n");
3080 return false;
3083 map_ptr += (uintptr_t)addr.addr;
3085 if (!map_ptr)
3087 assert(flags & WINED3D_MAP_NOOVERWRITE);
3088 WARN_(d3d_perf)("Not accelerating a NOOVERWRITE map because the sub-resource has no valid address.\n");
3089 return false;
3092 wined3d_resource_get_sub_resource_map_pitch(resource, sub_resource_idx,
3093 &map_desc->row_pitch, &map_desc->slice_pitch);
3095 client->mapped_upload.addr = *wined3d_const_bo_address(&addr);
3096 client->mapped_upload.flags = 0;
3097 if (bo)
3099 map_ptr += bo->memory_offset;
3100 /* If we are not mapping all buffers persistently, use
3101 * UPDATE_SUB_RESOURCE as a means of telling the CS thread to try
3102 * to unmap the resource, so that we can free VA space. */
3103 if (!bo->coherent || !wined3d_map_persistent())
3104 client->mapped_upload.flags |= UPLOAD_BO_UPLOAD_ON_UNMAP;
3106 map_desc->data = resource_offset_map_pointer(resource, sub_resource_idx, map_ptr, box);
3108 if (flags & WINED3D_MAP_DISCARD)
3109 client->mapped_upload.flags |= UPLOAD_BO_UPLOAD_ON_UNMAP | UPLOAD_BO_RENAME_ON_UNMAP;
3111 client->mapped_box = *box;
3113 TRACE("Returning bo %s, flags %#x.\n", debug_const_bo_address(&client->mapped_upload.addr),
3114 client->mapped_upload.flags);
3115 return true;
3118 wined3d_format_calculate_pitch(format, 1, box->right - box->left,
3119 box->bottom - box->top, &map_desc->row_pitch, &map_desc->slice_pitch);
3121 size = (box->back - box->front - 1) * map_desc->slice_pitch
3122 + ((box->bottom - box->top - 1) / format->block_height) * map_desc->row_pitch
3123 + ((box->right - box->left + format->block_width - 1) / format->block_width) * format->block_byte_count;
3125 if (!(map_desc->data = heap_alloc(size)))
3127 WARN_(d3d_perf)("Failed to allocate a heap memory buffer.\n");
3128 return false;
3130 client->mapped_upload.addr.buffer_object = 0;
3131 client->mapped_upload.addr.addr = map_desc->data;
3132 client->mapped_upload.flags = UPLOAD_BO_UPLOAD_ON_UNMAP | UPLOAD_BO_FREE_ON_UNMAP;
3133 client->mapped_box = *box;
3134 return true;
3137 static bool wined3d_bo_address_is_null(struct wined3d_const_bo_address *addr)
3139 return !addr->buffer_object && !addr->addr;
3142 static bool wined3d_cs_unmap_upload_bo(struct wined3d_device_context *context, struct wined3d_resource *resource,
3143 unsigned int sub_resource_idx, struct wined3d_box *box, struct upload_bo *upload_bo)
3145 struct wined3d_client_resource *client = &resource->client;
3146 struct wined3d_device *device = context->device;
3147 struct wined3d_bo *bo;
3149 if (wined3d_bo_address_is_null(&client->mapped_upload.addr))
3150 return false;
3152 if ((bo = client->mapped_upload.addr.buffer_object))
3154 wined3d_device_bo_map_lock(device);
3155 --bo->client_map_count;
3156 wined3d_device_bo_map_unlock(device);
3159 *upload_bo = client->mapped_upload;
3160 *box = client->mapped_box;
3161 memset(&client->mapped_upload, 0, sizeof(client->mapped_upload));
3162 memset(&client->mapped_box, 0, sizeof(client->mapped_box));
3163 return true;
3166 static const struct wined3d_device_context_ops wined3d_cs_st_ops =
3168 wined3d_cs_st_require_space,
3169 wined3d_cs_st_submit,
3170 wined3d_cs_st_finish,
3171 wined3d_cs_map_upload_bo,
3172 wined3d_cs_unmap_upload_bo,
3173 wined3d_cs_issue_query,
3174 wined3d_cs_flush,
3175 wined3d_cs_reference_resource,
3176 wined3d_cs_reference_command_list,
3179 static BOOL wined3d_cs_queue_is_empty(const struct wined3d_cs *cs, const struct wined3d_cs_queue *queue)
3181 wined3d_from_cs(cs);
3182 return *(volatile ULONG *)&queue->head == queue->tail;
3185 static void wined3d_cs_queue_submit(struct wined3d_cs_queue *queue, struct wined3d_cs *cs)
3187 struct wined3d_cs_packet *packet;
3188 size_t packet_size;
3190 packet = (struct wined3d_cs_packet *)&queue->data[queue->head & WINED3D_CS_QUEUE_MASK];
3191 TRACE("Queuing op %s at %p.\n", debug_cs_op(*(const enum wined3d_cs_op *)packet->data), packet);
3192 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
3193 InterlockedExchange((LONG *)&queue->head, queue->head + packet_size);
3195 if (InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
3196 SetEvent(cs->event);
3199 static void wined3d_cs_mt_submit(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
3201 struct wined3d_cs *cs = wined3d_cs_from_context(context);
3203 if (cs->thread_id == GetCurrentThreadId())
3204 return wined3d_cs_st_submit(context, queue_id);
3206 wined3d_cs_queue_submit(&cs->queue[queue_id], cs);
3209 static void *wined3d_cs_queue_require_space(struct wined3d_cs_queue *queue, size_t size, struct wined3d_cs *cs)
3211 size_t queue_size = ARRAY_SIZE(queue->data);
3212 size_t header_size, packet_size, remaining;
3213 struct wined3d_cs_packet *packet;
3214 ULONG head = queue->head & WINED3D_CS_QUEUE_MASK;
3216 header_size = FIELD_OFFSET(struct wined3d_cs_packet, data[0]);
3217 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[size]);
3218 packet_size = (packet_size + header_size - 1) & ~(header_size - 1);
3219 size = packet_size - header_size;
3220 if (packet_size >= WINED3D_CS_QUEUE_SIZE)
3222 ERR("Packet size %Iu >= queue size %u.\n", packet_size, WINED3D_CS_QUEUE_SIZE);
3223 return NULL;
3226 remaining = queue_size - head;
3227 if (remaining < packet_size)
3229 size_t nop_size = remaining - header_size;
3230 struct wined3d_cs_nop *nop;
3232 TRACE("Inserting a nop for %Iu + %Iu bytes.\n", header_size, nop_size);
3234 nop = wined3d_cs_queue_require_space(queue, nop_size, cs);
3235 if (nop_size)
3236 nop->opcode = WINED3D_CS_OP_NOP;
3238 wined3d_cs_queue_submit(queue, cs);
3239 head = queue->head & WINED3D_CS_QUEUE_MASK;
3240 assert(!head);
3243 for (;;)
3245 ULONG tail = (*(volatile ULONG *)&queue->tail) & WINED3D_CS_QUEUE_MASK;
3246 ULONG new_pos;
3248 /* Empty. */
3249 if (head == tail)
3250 break;
3251 new_pos = (head + packet_size) & WINED3D_CS_QUEUE_MASK;
3252 /* Head ahead of tail. We checked the remaining size above, so we only
3253 * need to make sure we don't make head equal to tail. */
3254 if (head > tail && (new_pos != tail))
3255 break;
3256 /* Tail ahead of head. Make sure the new head is before the tail as
3257 * well. Note that new_pos is 0 when it's at the end of the queue. */
3258 if (new_pos < tail && new_pos)
3259 break;
3261 TRACE("Waiting for free space. Head %lu, tail %lu, packet size %Iu.\n",
3262 head, tail, packet_size);
3265 packet = (struct wined3d_cs_packet *)&queue->data[head];
3266 packet->size = size;
3267 return packet->data;
3270 static void *wined3d_cs_mt_require_space(struct wined3d_device_context *context,
3271 size_t size, enum wined3d_cs_queue_id queue_id)
3273 struct wined3d_cs *cs = wined3d_cs_from_context(context);
3275 if (cs->thread_id == GetCurrentThreadId())
3276 return wined3d_cs_st_require_space(context, size, queue_id);
3278 return wined3d_cs_queue_require_space(&cs->queue[queue_id], size, cs);
3281 static void wined3d_cs_mt_finish(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
3283 struct wined3d_cs *cs = wined3d_cs_from_context(context);
3285 if (cs->thread_id == GetCurrentThreadId())
3286 return wined3d_cs_st_finish(context, queue_id);
3288 while (cs->queue[queue_id].head != *(volatile ULONG *)&cs->queue[queue_id].tail)
3289 YieldProcessor();
3292 static const struct wined3d_device_context_ops wined3d_cs_mt_ops =
3294 wined3d_cs_mt_require_space,
3295 wined3d_cs_mt_submit,
3296 wined3d_cs_mt_finish,
3297 wined3d_cs_map_upload_bo,
3298 wined3d_cs_unmap_upload_bo,
3299 wined3d_cs_issue_query,
3300 wined3d_cs_flush,
3301 wined3d_cs_reference_resource,
3302 wined3d_cs_reference_command_list,
3305 static void poll_queries(struct wined3d_cs *cs)
3307 struct wined3d_query *query, *cursor;
3309 LIST_FOR_EACH_ENTRY_SAFE(query, cursor, &cs->query_poll_list, struct wined3d_query, poll_list_entry)
3311 if (!query->query_ops->query_poll(query, 0))
3312 continue;
3314 list_remove(&query->poll_list_entry);
3315 list_init(&query->poll_list_entry);
3316 InterlockedIncrement(&query->counter_retrieved);
3320 static void wined3d_cs_wait_event(struct wined3d_cs *cs)
3322 InterlockedExchange(&cs->waiting_for_event, TRUE);
3324 /* The main thread might have enqueued a command and blocked on it after
3325 * the CS thread decided to enter wined3d_cs_wait_event(), but before
3326 * "waiting_for_event" was set.
3328 * Likewise, we can race with the main thread when resetting
3329 * "waiting_for_event", in which case we would need to call
3330 * WaitForSingleObject() because the main thread called SetEvent(). */
3331 if (!(wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_DEFAULT])
3332 && wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_MAP]))
3333 && InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
3334 return;
3336 WaitForSingleObject(cs->event, INFINITE);
3339 static void wined3d_cs_command_lock(const struct wined3d_cs *cs)
3341 if (cs->serialize_commands)
3342 EnterCriticalSection(&wined3d_command_cs);
3345 static void wined3d_cs_command_unlock(const struct wined3d_cs *cs)
3347 if (cs->serialize_commands)
3348 LeaveCriticalSection(&wined3d_command_cs);
3351 static inline bool wined3d_cs_execute_next(struct wined3d_cs *cs, struct wined3d_cs_queue *queue)
3353 struct wined3d_cs_packet *packet;
3354 enum wined3d_cs_op opcode;
3355 SIZE_T tail;
3357 tail = queue->tail;
3358 packet = wined3d_next_cs_packet(queue->data, &tail, WINED3D_CS_QUEUE_MASK);
3360 if (packet->size)
3362 opcode = *(const enum wined3d_cs_op *)packet->data;
3364 TRACE("Executing %s at %p.\n", debug_cs_op(opcode), packet);
3365 if (opcode >= WINED3D_CS_OP_STOP)
3367 if (opcode > WINED3D_CS_OP_STOP)
3368 ERR("Invalid opcode %#x.\n", opcode);
3369 return false;
3372 wined3d_cs_command_lock(cs);
3373 wined3d_cs_op_handlers[opcode](cs, packet->data);
3374 wined3d_cs_command_unlock(cs);
3375 TRACE("%s at %p executed.\n", debug_cs_op(opcode), packet);
3378 InterlockedExchange((LONG *)&queue->tail, tail);
3379 return true;
3382 static void wined3d_cs_exec_execute_command_list(struct wined3d_cs *cs, const void *data)
3384 const struct wined3d_cs_execute_command_list *op = data;
3385 SIZE_T start = 0, end = op->list->data_size;
3386 const BYTE *cs_data = op->list->data;
3387 struct wined3d_cs_queue *queue;
3389 TRACE("Executing command list %p.\n", op->list);
3391 queue = &cs->queue[WINED3D_CS_QUEUE_MAP];
3392 while (start < end)
3394 const struct wined3d_cs_packet *packet;
3395 enum wined3d_cs_op opcode;
3397 while (!wined3d_cs_queue_is_empty(cs, queue))
3398 wined3d_cs_execute_next(cs, queue);
3400 packet = wined3d_next_cs_packet(cs_data, &start, WINED3D_CS_QUEUE_MASK);
3401 opcode = *(const enum wined3d_cs_op *)packet->data;
3403 if (opcode >= WINED3D_CS_OP_STOP)
3404 ERR("Invalid opcode %#x.\n", opcode);
3405 else
3406 wined3d_cs_op_handlers[opcode](cs, packet->data);
3407 TRACE("%s executed.\n", debug_cs_op(opcode));
3411 static DWORD WINAPI wined3d_cs_run(void *ctx)
3413 struct wined3d_cs_queue *queue;
3414 unsigned int spin_count = 0;
3415 struct wined3d_cs *cs = ctx;
3416 HMODULE wined3d_module;
3417 unsigned int poll = 0;
3418 bool run = true;
3420 TRACE("Started.\n");
3421 SetThreadDescription(GetCurrentThread(), L"wined3d_cs");
3423 /* Copy the module handle to a local variable to avoid racing with the
3424 * thread freeing "cs" before the FreeLibraryAndExitThread() call. */
3425 wined3d_module = cs->wined3d_module;
3427 list_init(&cs->query_poll_list);
3428 cs->thread_id = GetCurrentThreadId();
3429 while (run)
3431 if (++poll == WINED3D_CS_QUERY_POLL_INTERVAL)
3433 wined3d_cs_command_lock(cs);
3434 poll_queries(cs);
3435 wined3d_cs_command_unlock(cs);
3436 poll = 0;
3439 queue = &cs->queue[WINED3D_CS_QUEUE_MAP];
3440 if (wined3d_cs_queue_is_empty(cs, queue))
3442 queue = &cs->queue[WINED3D_CS_QUEUE_DEFAULT];
3443 if (wined3d_cs_queue_is_empty(cs, queue))
3445 YieldProcessor();
3446 if (++spin_count >= WINED3D_CS_SPIN_COUNT)
3448 if (list_empty(&cs->query_poll_list))
3449 wined3d_cs_wait_event(cs);
3450 else
3451 Sleep(0);
3453 continue;
3456 spin_count = 0;
3458 run = wined3d_cs_execute_next(cs, queue);
3461 cs->queue[WINED3D_CS_QUEUE_MAP].tail = cs->queue[WINED3D_CS_QUEUE_MAP].head;
3462 cs->queue[WINED3D_CS_QUEUE_DEFAULT].tail = cs->queue[WINED3D_CS_QUEUE_DEFAULT].head;
3463 TRACE("Stopped.\n");
3464 FreeLibraryAndExitThread(wined3d_module, 0);
3467 struct wined3d_cs *wined3d_cs_create(struct wined3d_device *device,
3468 const enum wined3d_feature_level *levels, unsigned int level_count)
3470 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3471 struct wined3d_cs *cs;
3473 if (!(cs = heap_alloc_zero(sizeof(*cs))))
3474 return NULL;
3476 if (FAILED(wined3d_state_create(device, levels, level_count, &cs->c.state)))
3478 heap_free(cs);
3479 return NULL;
3482 cs->c.ops = &wined3d_cs_st_ops;
3483 cs->c.device = device;
3484 cs->serialize_commands = TRACE_ON(d3d_sync) || wined3d_settings.cs_multithreaded & WINED3D_CSMT_SERIALIZE;
3486 if (cs->serialize_commands)
3487 ERR_(d3d_sync)("Forcing serialization of all command streams.\n");
3489 state_init(&cs->state, d3d_info, WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT, cs->c.state->feature_level);
3491 cs->data_size = WINED3D_INITIAL_CS_SIZE;
3492 if (!(cs->data = heap_alloc(cs->data_size)))
3493 goto fail;
3495 if (wined3d_settings.cs_multithreaded & WINED3D_CSMT_ENABLE)
3497 if (!d3d_info->fences)
3499 WARN("Disabling CSMT, adapter doesn't support fences.\n");
3500 wined3d_settings.cs_multithreaded &= ~WINED3D_CSMT_ENABLE;
3504 if (wined3d_settings.cs_multithreaded & WINED3D_CSMT_ENABLE
3505 && !RtlIsCriticalSectionLockedByThread(NtCurrentTeb()->Peb->LoaderLock))
3507 cs->c.ops = &wined3d_cs_mt_ops;
3509 if (!(cs->event = CreateEventW(NULL, FALSE, FALSE, NULL)))
3511 ERR("Failed to create command stream event.\n");
3512 heap_free(cs->data);
3513 goto fail;
3515 if (!(cs->present_event = CreateEventW(NULL, FALSE, FALSE, NULL)))
3517 ERR("Failed to create command stream present event.\n");
3518 heap_free(cs->data);
3519 goto fail;
3522 if (!(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
3523 (const WCHAR *)wined3d_cs_run, &cs->wined3d_module)))
3525 ERR("Failed to get wined3d module handle.\n");
3526 CloseHandle(cs->present_event);
3527 CloseHandle(cs->event);
3528 heap_free(cs->data);
3529 goto fail;
3532 if (!(cs->thread = CreateThread(NULL, 0, wined3d_cs_run, cs, 0, NULL)))
3534 ERR("Failed to create wined3d command stream thread.\n");
3535 FreeLibrary(cs->wined3d_module);
3536 CloseHandle(cs->present_event);
3537 CloseHandle(cs->event);
3538 heap_free(cs->data);
3539 goto fail;
3543 TRACE("Created command stream %p.\n", cs);
3544 return cs;
3546 fail:
3547 wined3d_state_destroy(cs->c.state);
3548 state_cleanup(&cs->state);
3549 heap_free(cs);
3550 return NULL;
3553 void wined3d_cs_destroy(struct wined3d_cs *cs)
3555 if (cs->thread)
3557 wined3d_cs_emit_stop(cs);
3558 CloseHandle(cs->thread);
3559 if (!CloseHandle(cs->present_event))
3560 ERR("Closing present event failed.\n");
3561 if (!CloseHandle(cs->event))
3562 ERR("Closing event failed.\n");
3565 wined3d_state_destroy(cs->c.state);
3566 state_cleanup(&cs->state);
3567 heap_free(cs->data);
3568 heap_free(cs);
3571 static void wined3d_cs_packet_decref_objects(const struct wined3d_cs_packet *packet)
3573 enum wined3d_cs_op opcode = *(const enum wined3d_cs_op *)packet->data;
3574 unsigned int i;
3576 switch (opcode)
3578 case WINED3D_CS_OP_SET_SAMPLERS:
3580 struct wined3d_cs_set_samplers *op = (struct wined3d_cs_set_samplers *)packet->data;
3582 for (i = 0; i < op->count; ++i)
3584 if (op->samplers[i])
3585 wined3d_sampler_decref(op->samplers[i]);
3587 break;
3590 case WINED3D_CS_OP_SET_SHADER:
3592 struct wined3d_cs_set_shader *op = (struct wined3d_cs_set_shader *)packet->data;
3594 if (op->shader)
3595 wined3d_shader_decref(op->shader);
3596 break;
3599 case WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE:
3601 struct wined3d_cs_set_depth_stencil_state *op;
3603 op = (struct wined3d_cs_set_depth_stencil_state *)packet->data;
3604 if (op->state)
3605 wined3d_depth_stencil_state_decref(op->state);
3606 break;
3609 case WINED3D_CS_OP_SET_RASTERIZER_STATE:
3611 struct wined3d_cs_set_rasterizer_state *op;
3613 op = (struct wined3d_cs_set_rasterizer_state *)packet->data;
3614 if (op->state)
3615 wined3d_rasterizer_state_decref(op->state);
3616 break;
3619 case WINED3D_CS_OP_SET_BLEND_STATE:
3621 struct wined3d_cs_set_blend_state *op;
3623 op = (struct wined3d_cs_set_blend_state *)packet->data;
3624 if (op->state)
3625 wined3d_blend_state_decref(op->state);
3626 break;
3629 case WINED3D_CS_OP_SET_RENDERTARGET_VIEWS:
3631 struct wined3d_cs_set_rendertarget_views *op;
3633 op = (struct wined3d_cs_set_rendertarget_views *)packet->data;
3634 for (i = 0; i < op->count; ++i)
3636 if (op->views[i])
3637 wined3d_rendertarget_view_decref(op->views[i]);
3639 break;
3642 case WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEWS:
3644 struct wined3d_cs_set_shader_resource_views *op;
3646 op = (struct wined3d_cs_set_shader_resource_views *)packet->data;
3647 for (i = 0; i < op->count; ++i)
3649 if (op->views[i])
3650 wined3d_shader_resource_view_decref(op->views[i]);
3652 break;
3655 case WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEWS:
3657 struct wined3d_cs_set_unordered_access_views *op;
3659 op = (struct wined3d_cs_set_unordered_access_views *)packet->data;
3660 for (i = 0; i < op->count; ++i)
3662 if (op->uavs[i].view)
3663 wined3d_unordered_access_view_decref(op->uavs[i].view);
3665 break;
3668 case WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW:
3670 struct wined3d_cs_set_depth_stencil_view *op;
3672 op = (struct wined3d_cs_set_depth_stencil_view *)packet->data;
3673 if (op->view)
3674 wined3d_rendertarget_view_decref(op->view);
3675 break;
3678 case WINED3D_CS_OP_SET_CONSTANT_BUFFERS:
3680 struct wined3d_cs_set_constant_buffers *op;
3682 op = (struct wined3d_cs_set_constant_buffers *)packet->data;
3683 for (i = 0; i < op->count; ++i)
3685 if (op->buffers[i].buffer)
3686 wined3d_buffer_decref(op->buffers[i].buffer);
3688 break;
3691 case WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW:
3693 struct wined3d_cs_clear_unordered_access_view *op;
3695 op = (struct wined3d_cs_clear_unordered_access_view *)packet->data;
3696 wined3d_unordered_access_view_decref(op->view);
3697 break;
3700 case WINED3D_CS_OP_CLEAR:
3702 struct wined3d_cs_clear *op = (struct wined3d_cs_clear *)packet->data;
3704 for (i = 0; i < op->rt_count; ++i)
3706 if (op->fb.render_targets[i])
3707 wined3d_rendertarget_view_decref(op->fb.render_targets[i]);
3709 if (op->fb.depth_stencil)
3710 wined3d_rendertarget_view_decref(op->fb.depth_stencil);
3711 break;
3714 case WINED3D_CS_OP_DISPATCH:
3716 struct wined3d_cs_dispatch *op = (struct wined3d_cs_dispatch *)packet->data;
3718 if (op->parameters.indirect)
3719 wined3d_buffer_decref(op->parameters.u.indirect.buffer);
3720 break;
3723 case WINED3D_CS_OP_DRAW:
3725 struct wined3d_cs_draw *op = (struct wined3d_cs_draw *)packet->data;
3727 if (op->parameters.indirect)
3728 wined3d_buffer_decref(op->parameters.u.indirect.buffer);
3729 break;
3732 case WINED3D_CS_OP_SET_INDEX_BUFFER:
3734 struct wined3d_cs_set_index_buffer *op;
3736 op = (struct wined3d_cs_set_index_buffer *)packet->data;
3737 if (op->buffer)
3738 wined3d_buffer_decref(op->buffer);
3739 break;
3742 case WINED3D_CS_OP_SET_STREAM_OUTPUTS:
3744 struct wined3d_cs_set_stream_outputs *op;
3746 op = (struct wined3d_cs_set_stream_outputs *)packet->data;
3747 for (i = 0; i < ARRAY_SIZE(op->outputs); ++i)
3749 if (op->outputs[i].buffer)
3750 wined3d_buffer_decref(op->outputs[i].buffer);
3752 break;
3755 case WINED3D_CS_OP_SET_STREAM_SOURCES:
3757 struct wined3d_cs_set_stream_sources *op;
3759 op = (struct wined3d_cs_set_stream_sources *)packet->data;
3760 for (i = 0; i < op->count; ++i)
3762 if (op->streams[i].buffer)
3763 wined3d_buffer_decref(op->streams[i].buffer);
3765 break;
3768 case WINED3D_CS_OP_UPDATE_SUB_RESOURCE:
3770 struct wined3d_cs_update_sub_resource *op;
3772 op = (struct wined3d_cs_update_sub_resource *)packet->data;
3773 wined3d_resource_decref(op->resource);
3774 break;
3777 case WINED3D_CS_OP_BLT_SUB_RESOURCE:
3779 struct wined3d_cs_blt_sub_resource *op;
3781 op = (struct wined3d_cs_blt_sub_resource *)packet->data;
3782 if (op->src_resource)
3783 wined3d_resource_decref(op->src_resource);
3784 wined3d_resource_decref(op->dst_resource);
3785 break;
3788 case WINED3D_CS_OP_COPY_UAV_COUNTER:
3790 struct wined3d_cs_copy_uav_counter *op;
3792 op = (struct wined3d_cs_copy_uav_counter *)packet->data;
3793 wined3d_buffer_decref(op->buffer);
3794 wined3d_unordered_access_view_decref(op->view);
3795 break;
3798 case WINED3D_CS_OP_GENERATE_MIPMAPS:
3800 struct wined3d_cs_generate_mipmaps *op;
3802 op = (struct wined3d_cs_generate_mipmaps *)packet->data;
3803 wined3d_shader_resource_view_decref(op->view);
3804 break;
3807 default:
3808 break;
3812 static void wined3d_cs_packet_incref_objects(struct wined3d_cs_packet *packet)
3814 enum wined3d_cs_op opcode = *(const enum wined3d_cs_op *)packet->data;
3815 unsigned int i;
3817 switch (opcode)
3819 case WINED3D_CS_OP_SET_SAMPLERS:
3821 struct wined3d_cs_set_samplers *op = (struct wined3d_cs_set_samplers *)packet->data;
3823 for (i = 0; i < op->count; ++i)
3825 if (op->samplers[i])
3826 wined3d_sampler_incref(op->samplers[i]);
3828 break;
3831 case WINED3D_CS_OP_SET_SHADER:
3833 struct wined3d_cs_set_shader *op = (struct wined3d_cs_set_shader *)packet->data;
3835 if (op->shader)
3836 wined3d_shader_incref(op->shader);
3837 break;
3840 case WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE:
3842 struct wined3d_cs_set_depth_stencil_state *op;
3844 op = (struct wined3d_cs_set_depth_stencil_state *)packet->data;
3845 if (op->state)
3846 wined3d_depth_stencil_state_incref(op->state);
3847 break;
3850 case WINED3D_CS_OP_SET_RASTERIZER_STATE:
3852 struct wined3d_cs_set_rasterizer_state *op;
3854 op = (struct wined3d_cs_set_rasterizer_state *)packet->data;
3855 if (op->state)
3856 wined3d_rasterizer_state_incref(op->state);
3857 break;
3860 case WINED3D_CS_OP_SET_BLEND_STATE:
3862 struct wined3d_cs_set_blend_state *op;
3864 op = (struct wined3d_cs_set_blend_state *)packet->data;
3865 if (op->state)
3866 wined3d_blend_state_incref(op->state);
3867 break;
3870 case WINED3D_CS_OP_SET_RENDERTARGET_VIEWS:
3872 struct wined3d_cs_set_rendertarget_views *op;
3874 op = (struct wined3d_cs_set_rendertarget_views *)packet->data;
3875 for (i = 0; i < op->count; ++i)
3877 if (op->views[i])
3878 wined3d_rendertarget_view_incref(op->views[i]);
3880 break;
3883 case WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEWS:
3885 struct wined3d_cs_set_shader_resource_views *op;
3887 op = (struct wined3d_cs_set_shader_resource_views *)packet->data;
3888 for (i = 0; i < op->count; ++i)
3890 if (op->views[i])
3891 wined3d_shader_resource_view_incref(op->views[i]);
3893 break;
3896 case WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEWS:
3898 struct wined3d_cs_set_unordered_access_views *op;
3900 op = (struct wined3d_cs_set_unordered_access_views *)packet->data;
3901 for (i = 0; i < op->count; ++i)
3903 if (op->uavs[i].view)
3904 wined3d_unordered_access_view_incref(op->uavs[i].view);
3906 break;
3909 case WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW:
3911 struct wined3d_cs_set_depth_stencil_view *op;
3913 op = (struct wined3d_cs_set_depth_stencil_view *)packet->data;
3914 if (op->view)
3915 wined3d_rendertarget_view_incref(op->view);
3916 break;
3919 case WINED3D_CS_OP_SET_CONSTANT_BUFFERS:
3921 struct wined3d_cs_set_constant_buffers *op;
3923 op = (struct wined3d_cs_set_constant_buffers *)packet->data;
3924 for (i = 0; i < op->count; ++i)
3926 if (op->buffers[i].buffer)
3927 wined3d_buffer_incref(op->buffers[i].buffer);
3929 break;
3932 case WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW:
3934 struct wined3d_cs_clear_unordered_access_view *op;
3936 op = (struct wined3d_cs_clear_unordered_access_view *)packet->data;
3937 wined3d_unordered_access_view_incref(op->view);
3938 break;
3941 case WINED3D_CS_OP_CLEAR:
3943 struct wined3d_cs_clear *op = (struct wined3d_cs_clear *)packet->data;
3945 for (i = 0; i < op->rt_count; ++i)
3947 if (op->fb.render_targets[i])
3948 wined3d_rendertarget_view_incref(op->fb.render_targets[i]);
3950 if (op->fb.depth_stencil)
3951 wined3d_rendertarget_view_incref(op->fb.depth_stencil);
3952 break;
3955 case WINED3D_CS_OP_DISPATCH:
3957 struct wined3d_cs_dispatch *op = (struct wined3d_cs_dispatch *)packet->data;
3959 if (op->parameters.indirect)
3960 wined3d_buffer_incref(op->parameters.u.indirect.buffer);
3961 break;
3964 case WINED3D_CS_OP_DRAW:
3966 struct wined3d_cs_draw *op = (struct wined3d_cs_draw *)packet->data;
3968 if (op->parameters.indirect)
3969 wined3d_buffer_incref(op->parameters.u.indirect.buffer);
3970 break;
3973 case WINED3D_CS_OP_SET_INDEX_BUFFER:
3975 struct wined3d_cs_set_index_buffer *op;
3977 op = (struct wined3d_cs_set_index_buffer *)packet->data;
3978 if (op->buffer)
3979 wined3d_buffer_incref(op->buffer);
3980 break;
3983 case WINED3D_CS_OP_SET_STREAM_OUTPUTS:
3985 struct wined3d_cs_set_stream_outputs *op;
3987 op = (struct wined3d_cs_set_stream_outputs *)packet->data;
3988 for (i = 0; i < ARRAY_SIZE(op->outputs); ++i)
3990 if (op->outputs[i].buffer)
3991 wined3d_buffer_incref(op->outputs[i].buffer);
3993 break;
3996 case WINED3D_CS_OP_SET_STREAM_SOURCES:
3998 struct wined3d_cs_set_stream_sources *op;
4000 op = (struct wined3d_cs_set_stream_sources *)packet->data;
4001 for (i = 0; i < op->count; ++i)
4003 if (op->streams[i].buffer)
4004 wined3d_buffer_incref(op->streams[i].buffer);
4006 break;
4009 case WINED3D_CS_OP_UPDATE_SUB_RESOURCE:
4011 struct wined3d_cs_update_sub_resource *op;
4013 op = (struct wined3d_cs_update_sub_resource *)packet->data;
4014 wined3d_resource_incref(op->resource);
4015 break;
4018 case WINED3D_CS_OP_BLT_SUB_RESOURCE:
4020 struct wined3d_cs_blt_sub_resource *op;
4022 op = (struct wined3d_cs_blt_sub_resource *)packet->data;
4023 if (op->src_resource)
4024 wined3d_resource_incref(op->src_resource);
4025 wined3d_resource_incref(op->dst_resource);
4026 break;
4029 case WINED3D_CS_OP_COPY_UAV_COUNTER:
4031 struct wined3d_cs_copy_uav_counter *op;
4033 op = (struct wined3d_cs_copy_uav_counter *)packet->data;
4034 wined3d_buffer_incref(op->buffer);
4035 wined3d_unordered_access_view_incref(op->view);
4036 break;
4039 case WINED3D_CS_OP_GENERATE_MIPMAPS:
4041 struct wined3d_cs_generate_mipmaps *op;
4043 op = (struct wined3d_cs_generate_mipmaps *)packet->data;
4044 wined3d_shader_resource_view_incref(op->view);
4045 break;
4048 default:
4049 break;
4053 struct wined3d_deferred_context
4055 struct wined3d_device_context c;
4057 SIZE_T data_size, data_capacity;
4058 void *data;
4060 SIZE_T resource_count, resources_capacity;
4061 struct wined3d_resource **resources;
4063 SIZE_T upload_count, uploads_capacity;
4064 struct wined3d_deferred_upload *uploads;
4066 HANDLE upload_heap;
4067 LONG *upload_heap_refcount;
4069 /* List of command lists queued for execution on this context. A command
4070 * list can be the only thing holding a pointer to another command list, so
4071 * we need to hold a reference here and in wined3d_command_list as well. */
4072 SIZE_T command_list_count, command_lists_capacity;
4073 struct wined3d_command_list **command_lists;
4075 SIZE_T query_count, queries_capacity;
4076 struct wined3d_deferred_query_issue *queries;
4079 static struct wined3d_deferred_context *wined3d_deferred_context_from_context(struct wined3d_device_context *context)
4081 return CONTAINING_RECORD(context, struct wined3d_deferred_context, c);
4084 static void *wined3d_deferred_context_require_space(struct wined3d_device_context *context,
4085 size_t size, enum wined3d_cs_queue_id queue_id)
4087 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
4088 struct wined3d_cs_packet *packet;
4089 size_t header_size, packet_size;
4091 if (queue_id != WINED3D_CS_QUEUE_DEFAULT)
4092 return NULL;
4094 header_size = offsetof(struct wined3d_cs_packet, data[0]);
4095 packet_size = offsetof(struct wined3d_cs_packet, data[size]);
4096 packet_size = (packet_size + header_size - 1) & ~(header_size - 1);
4098 if (!wined3d_array_reserve(&deferred->data, &deferred->data_capacity, deferred->data_size + packet_size, 1))
4099 return NULL;
4101 packet = (struct wined3d_cs_packet *)((BYTE *)deferred->data + deferred->data_size);
4102 TRACE("size was %Iu, adding %Iu\n", (size_t)deferred->data_size, packet_size);
4103 packet->size = packet_size - header_size;
4104 return &packet->data;
4107 static void wined3d_deferred_context_submit(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
4109 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
4110 struct wined3d_cs_packet *packet;
4112 assert(queue_id == WINED3D_CS_QUEUE_DEFAULT);
4113 packet = wined3d_next_cs_packet(deferred->data, &deferred->data_size, ~(SIZE_T)0);
4114 wined3d_cs_packet_incref_objects(packet);
4117 static void wined3d_deferred_context_finish(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
4119 /* This should not happen; we cannot meaningfully finish a deferred context. */
4120 ERR("Ignoring finish() on a deferred context.\n");
4123 static struct wined3d_deferred_upload *deferred_context_get_upload(struct wined3d_deferred_context *deferred,
4124 struct wined3d_resource *resource, unsigned int sub_resource_idx)
4126 SIZE_T i = deferred->upload_count;
4128 while (i--)
4130 struct wined3d_deferred_upload *upload = &deferred->uploads[i];
4132 if (upload->resource == resource && upload->sub_resource_idx == sub_resource_idx)
4133 return upload;
4136 return NULL;
4139 static bool wined3d_deferred_context_map_upload_bo(struct wined3d_device_context *context,
4140 struct wined3d_resource *resource, unsigned int sub_resource_idx,
4141 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, uint32_t flags)
4143 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
4144 const struct wined3d_format *format = resource->format;
4145 struct wined3d_device *device = context->device;
4146 struct wined3d_deferred_upload *upload;
4147 struct wined3d_bo_address addr;
4148 uint8_t *sysmem;
4149 size_t size;
4151 wined3d_format_calculate_pitch(format, 1, box->right - box->left,
4152 box->bottom - box->top, &map_desc->row_pitch, &map_desc->slice_pitch);
4154 size = (box->back - box->front - 1) * map_desc->slice_pitch
4155 + ((box->bottom - box->top - 1) / format->block_height) * map_desc->row_pitch
4156 + ((box->right - box->left + format->block_width - 1) / format->block_width) * format->block_byte_count;
4158 if (!(flags & WINED3D_MAP_WRITE))
4160 WARN("Flags %#x are not valid on a deferred context.\n", flags);
4161 return false;
4164 if (flags & ~(WINED3D_MAP_WRITE | WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4166 FIXME("Unhandled flags %#x.\n", flags);
4167 return false;
4170 if (flags & WINED3D_MAP_NOOVERWRITE)
4172 if (!(upload = deferred_context_get_upload(deferred, resource, sub_resource_idx)))
4173 return false;
4175 upload->upload_flags = 0;
4176 map_desc->data = upload->map_ptr;
4177 return true;
4180 if (!wined3d_array_reserve((void **)&deferred->uploads, &deferred->uploads_capacity,
4181 deferred->upload_count + 1, sizeof(*deferred->uploads)))
4182 return false;
4184 upload = &deferred->uploads[deferred->upload_count++];
4186 if ((flags & WINED3D_MAP_DISCARD)
4187 && device->adapter->adapter_ops->adapter_alloc_bo(device, resource, sub_resource_idx, &addr))
4189 upload->bo = addr.buffer_object;
4190 upload->sysmem = NULL;
4192 TRACE("Allocated BO %s.\n", debug_bo_address(&addr));
4194 wined3d_device_bo_map_lock(device);
4195 upload->map_ptr = addr.buffer_object->map_ptr;
4196 wined3d_device_bo_map_unlock(device);
4197 upload->map_ptr += addr.buffer_object->memory_offset;
4198 assert(upload->map_ptr);
4200 else
4202 if (!deferred->upload_heap)
4204 if (!(deferred->upload_heap = HeapCreate(0, 0, 0)))
4206 ERR("Failed to create upload heap.\n");
4207 return false;
4210 if (!(deferred->upload_heap_refcount = heap_alloc(sizeof(*deferred->upload_heap_refcount))))
4212 HeapDestroy(deferred->upload_heap);
4213 deferred->upload_heap = 0;
4214 return false;
4217 *deferred->upload_heap_refcount = 1;
4220 if (!(sysmem = HeapAlloc(deferred->upload_heap, 0, size + RESOURCE_ALIGNMENT - 1)))
4221 return false;
4223 upload->bo = NULL;
4224 upload->sysmem = sysmem;
4225 upload->map_ptr = (void *)align((size_t)upload->sysmem, RESOURCE_ALIGNMENT);
4228 upload->upload_flags = UPLOAD_BO_UPLOAD_ON_UNMAP;
4229 upload->resource = resource;
4230 wined3d_resource_incref(resource);
4231 upload->sub_resource_idx = sub_resource_idx;
4232 upload->box = *box;
4234 map_desc->data = upload->map_ptr;
4235 return true;
4238 static bool wined3d_deferred_context_unmap_upload_bo(struct wined3d_device_context *context,
4239 struct wined3d_resource *resource, unsigned int sub_resource_idx, struct wined3d_box *box, struct upload_bo *bo)
4241 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
4242 struct wined3d_deferred_upload *upload;
4244 if ((upload = deferred_context_get_upload(deferred, resource, sub_resource_idx)))
4246 *box = upload->box;
4247 if ((bo->addr.buffer_object = upload->bo))
4248 bo->addr.addr = NULL;
4249 else
4250 bo->addr.addr = upload->map_ptr;
4251 bo->flags = upload->upload_flags;
4252 upload->upload_flags = 0;
4253 return true;
4256 return false;
4259 static void wined3d_deferred_context_issue_query(struct wined3d_device_context *context,
4260 struct wined3d_query *query, unsigned int flags)
4262 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
4263 struct wined3d_cs_query_issue *op;
4265 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
4266 op->opcode = WINED3D_CS_OP_QUERY_ISSUE;
4267 op->query = query;
4268 op->flags = flags;
4270 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
4272 if (!wined3d_array_reserve((void **)&deferred->queries, &deferred->queries_capacity,
4273 deferred->query_count + 1, sizeof(*deferred->queries)))
4275 ERR("Failed to reserve memory.\n");
4276 return;
4279 deferred->queries[deferred->query_count].flags = flags;
4280 wined3d_query_incref(deferred->queries[deferred->query_count++].query = query);
4283 static void wined3d_deferred_context_flush(struct wined3d_device_context *context)
4285 FIXME("context %p, stub!\n", context);
4288 static void wined3d_deferred_context_reference_resource(struct wined3d_device_context *context,
4289 struct wined3d_resource *resource)
4291 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
4293 if (!wined3d_array_reserve((void **)&deferred->resources, &deferred->resources_capacity,
4294 deferred->resource_count + 1, sizeof(*deferred->resources)))
4295 return;
4297 deferred->resources[deferred->resource_count++] = resource;
4298 wined3d_resource_incref(resource);
4301 static void wined3d_deferred_context_reference_command_list(struct wined3d_device_context *context,
4302 struct wined3d_command_list *list)
4304 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
4306 /* Grab a reference to the command list. Note that this implicitly prevents
4307 * any dependent command lists or resources from being freed as well. */
4308 if (!wined3d_array_reserve((void **)&deferred->command_lists, &deferred->command_lists_capacity,
4309 deferred->command_list_count + 1, sizeof(*deferred->command_lists)))
4310 return;
4312 wined3d_command_list_incref(deferred->command_lists[deferred->command_list_count++] = list);
4315 static const struct wined3d_device_context_ops wined3d_deferred_context_ops =
4317 wined3d_deferred_context_require_space,
4318 wined3d_deferred_context_submit,
4319 wined3d_deferred_context_finish,
4320 wined3d_deferred_context_map_upload_bo,
4321 wined3d_deferred_context_unmap_upload_bo,
4322 wined3d_deferred_context_issue_query,
4323 wined3d_deferred_context_flush,
4324 wined3d_deferred_context_reference_resource,
4325 wined3d_deferred_context_reference_command_list,
4328 HRESULT CDECL wined3d_deferred_context_create(struct wined3d_device *device, struct wined3d_device_context **context)
4330 struct wined3d_deferred_context *object;
4331 HRESULT hr;
4333 TRACE("device %p, context %p.\n", device, context);
4335 if (!(object = heap_alloc_zero(sizeof(*object))))
4336 return E_OUTOFMEMORY;
4338 if (FAILED(hr = wined3d_state_create(device, &device->cs->c.state->feature_level, 1, &object->c.state)))
4340 heap_free(object);
4341 return hr;
4344 object->c.ops = &wined3d_deferred_context_ops;
4345 object->c.device = device;
4347 /* Make sure the first command list gets the state reset when executed.
4348 * Resets for subsequent command lists are encoded in wined3d_deferred_context_record_command_list(). */
4349 wined3d_device_context_emit_reset_state(&object->c, true);
4351 TRACE("Created deferred context %p.\n", object);
4352 *context = &object->c;
4354 return S_OK;
4357 void CDECL wined3d_deferred_context_destroy(struct wined3d_device_context *context)
4359 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
4360 const struct wined3d_cs_packet *packet;
4361 SIZE_T i, offset = 0;
4363 TRACE("context %p.\n", context);
4365 for (i = 0; i < deferred->resource_count; ++i)
4366 wined3d_resource_decref(deferred->resources[i]);
4367 heap_free(deferred->resources);
4369 for (i = 0; i < deferred->upload_count; ++i)
4371 wined3d_resource_decref(deferred->uploads[i].resource);
4372 HeapFree(deferred->upload_heap, 0, deferred->uploads[i].resource);
4375 if (deferred->upload_heap)
4377 if (!InterlockedDecrement(deferred->upload_heap_refcount))
4379 HeapDestroy(deferred->upload_heap);
4380 heap_free(deferred->upload_heap_refcount);
4384 heap_free(deferred->uploads);
4386 for (i = 0; i < deferred->command_list_count; ++i)
4387 wined3d_command_list_decref(deferred->command_lists[i]);
4388 heap_free(deferred->command_lists);
4390 for (i = 0; i < deferred->query_count; ++i)
4391 wined3d_query_decref(deferred->queries[i].query);
4392 heap_free(deferred->queries);
4394 while (offset < deferred->data_size)
4396 packet = wined3d_next_cs_packet(deferred->data, &offset, ~(SIZE_T)0);
4397 wined3d_cs_packet_decref_objects(packet);
4400 wined3d_state_destroy(deferred->c.state);
4401 heap_free(deferred->data);
4402 heap_free(deferred);
4405 HRESULT CDECL wined3d_deferred_context_record_command_list(struct wined3d_device_context *context,
4406 bool restore, struct wined3d_command_list **list)
4408 struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context);
4409 struct wined3d_command_list *object;
4410 void *memory;
4412 TRACE("context %p, list %p.\n", context, list);
4414 wined3d_device_context_lock(context);
4415 memory = heap_alloc(sizeof(*object) + deferred->resource_count * sizeof(*object->resources)
4416 + deferred->upload_count * sizeof(*object->uploads)
4417 + deferred->command_list_count * sizeof(*object->command_lists)
4418 + deferred->query_count * sizeof(*object->queries)
4419 + deferred->data_size);
4421 if (!memory)
4423 wined3d_device_context_unlock(context);
4424 return E_OUTOFMEMORY;
4427 object = memory;
4428 memory = &object[1];
4429 memset(object, 0, sizeof(*object));
4430 object->refcount = 1;
4431 object->device = deferred->c.device;
4433 object->resources = memory;
4434 memory = &object->resources[deferred->resource_count];
4435 object->resource_count = deferred->resource_count;
4436 memcpy(object->resources, deferred->resources, deferred->resource_count * sizeof(*object->resources));
4437 /* Transfer our references to the resources to the command list. */
4439 object->uploads = memory;
4440 memory = &object->uploads[deferred->upload_count];
4441 object->upload_count = deferred->upload_count;
4442 memcpy(object->uploads, deferred->uploads, deferred->upload_count * sizeof(*object->uploads));
4443 /* Transfer our references to the resources to the command list. */
4445 object->command_lists = memory;
4446 memory = &object->command_lists[deferred->command_list_count];
4447 object->command_list_count = deferred->command_list_count;
4448 memcpy(object->command_lists, deferred->command_lists,
4449 deferred->command_list_count * sizeof(*object->command_lists));
4450 /* Transfer our references to the command lists to the command list. */
4452 object->queries = memory;
4453 memory = &object->queries[deferred->query_count];
4454 object->query_count = deferred->query_count;
4455 memcpy(object->queries, deferred->queries, deferred->query_count * sizeof(*object->queries));
4456 /* Transfer our references to the queries to the command list. */
4458 object->data = memory;
4459 object->data_size = deferred->data_size;
4460 memcpy(object->data, deferred->data, deferred->data_size);
4462 deferred->data_size = 0;
4463 deferred->resource_count = 0;
4464 deferred->upload_count = 0;
4465 deferred->command_list_count = 0;
4466 deferred->query_count = 0;
4468 object->upload_heap = deferred->upload_heap;
4469 if ((object->upload_heap_refcount = deferred->upload_heap_refcount))
4470 InterlockedIncrement(object->upload_heap_refcount);
4472 /* This is in fact recorded into a subsequent command list. */
4473 if (restore)
4474 wined3d_device_context_set_state(&deferred->c, deferred->c.state);
4475 else
4476 wined3d_device_context_reset_state(&deferred->c);
4478 TRACE("Created command list %p.\n", object);
4479 *list = object;
4480 wined3d_device_context_unlock(context);
4482 return S_OK;
4485 static void wined3d_command_list_destroy_object(void *object)
4487 struct wined3d_command_list *list = object;
4488 struct wined3d_context *context;
4489 unsigned int i;
4491 TRACE("list %p.\n", list);
4493 context = context_acquire(list->device, NULL, 0);
4495 for (i = 0; i < list->upload_count; ++i)
4497 struct wined3d_bo *bo;
4499 if ((bo = list->uploads[i].bo))
4501 if (!--bo->refcount)
4503 wined3d_context_destroy_bo(context, bo);
4504 heap_free(bo);
4507 else
4509 HeapFree(list->upload_heap, 0, list->uploads[i].sysmem);
4513 context_release(context);
4515 if (list->upload_heap)
4517 if (!InterlockedDecrement(list->upload_heap_refcount))
4519 HeapDestroy(list->upload_heap);
4520 heap_free(list->upload_heap_refcount);
4524 heap_free(list);
4527 ULONG CDECL wined3d_command_list_incref(struct wined3d_command_list *list)
4529 unsigned int refcount = InterlockedIncrement(&list->refcount);
4531 TRACE("%p increasing refcount to %u.\n", list, refcount);
4533 return refcount;
4536 ULONG CDECL wined3d_command_list_decref(struct wined3d_command_list *list)
4538 unsigned int refcount = InterlockedDecrement(&list->refcount);
4539 struct wined3d_device *device = list->device;
4540 const struct wined3d_cs_packet *packet;
4541 SIZE_T i, offset;
4543 TRACE("%p decreasing refcount to %u.\n", list, refcount);
4545 if (!refcount)
4547 for (i = 0; i < list->command_list_count; ++i)
4548 wined3d_command_list_decref(list->command_lists[i]);
4549 for (i = 0; i < list->resource_count; ++i)
4550 wined3d_resource_decref(list->resources[i]);
4551 for (i = 0; i < list->upload_count; ++i)
4552 wined3d_resource_decref(list->uploads[i].resource);
4553 for (i = 0; i < list->query_count; ++i)
4554 wined3d_query_decref(list->queries[i].query);
4556 offset = 0;
4557 while (offset < list->data_size)
4559 packet = wined3d_next_cs_packet(list->data, &offset, ~(SIZE_T)0);
4560 wined3d_cs_packet_decref_objects(packet);
4563 wined3d_mutex_lock();
4564 wined3d_cs_destroy_object(device->cs, wined3d_command_list_destroy_object, list);
4565 wined3d_mutex_unlock();
4568 return refcount;