ws2_32: Add support for hints in GetAddrInfoExW.
[wine.git] / dlls / wined3d / cs.c
blobe94d1a19dc2c8dddcbfe7f127e8d2cf5781c206e
1 /*
2 * Copyright 2013 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
21 #include "wined3d_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
24 WINE_DECLARE_DEBUG_CHANNEL(d3d_sync);
25 WINE_DECLARE_DEBUG_CHANNEL(fps);
27 #define WINED3D_INITIAL_CS_SIZE 4096
29 enum wined3d_cs_op
31 WINED3D_CS_OP_NOP,
32 WINED3D_CS_OP_PRESENT,
33 WINED3D_CS_OP_CLEAR,
34 WINED3D_CS_OP_DISPATCH,
35 WINED3D_CS_OP_DRAW,
36 WINED3D_CS_OP_FLUSH,
37 WINED3D_CS_OP_SET_PREDICATION,
38 WINED3D_CS_OP_SET_VIEWPORTS,
39 WINED3D_CS_OP_SET_SCISSOR_RECTS,
40 WINED3D_CS_OP_SET_RENDERTARGET_VIEW,
41 WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW,
42 WINED3D_CS_OP_SET_VERTEX_DECLARATION,
43 WINED3D_CS_OP_SET_STREAM_SOURCE,
44 WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ,
45 WINED3D_CS_OP_SET_STREAM_OUTPUT,
46 WINED3D_CS_OP_SET_INDEX_BUFFER,
47 WINED3D_CS_OP_SET_CONSTANT_BUFFER,
48 WINED3D_CS_OP_SET_TEXTURE,
49 WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW,
50 WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW,
51 WINED3D_CS_OP_SET_SAMPLER,
52 WINED3D_CS_OP_SET_SHADER,
53 WINED3D_CS_OP_SET_BLEND_STATE,
54 WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE,
55 WINED3D_CS_OP_SET_RASTERIZER_STATE,
56 WINED3D_CS_OP_SET_RENDER_STATE,
57 WINED3D_CS_OP_SET_TEXTURE_STATE,
58 WINED3D_CS_OP_SET_SAMPLER_STATE,
59 WINED3D_CS_OP_SET_TRANSFORM,
60 WINED3D_CS_OP_SET_CLIP_PLANE,
61 WINED3D_CS_OP_SET_COLOR_KEY,
62 WINED3D_CS_OP_SET_MATERIAL,
63 WINED3D_CS_OP_SET_LIGHT,
64 WINED3D_CS_OP_SET_LIGHT_ENABLE,
65 WINED3D_CS_OP_SET_FEATURE_LEVEL,
66 WINED3D_CS_OP_PUSH_CONSTANTS,
67 WINED3D_CS_OP_RESET_STATE,
68 WINED3D_CS_OP_CALLBACK,
69 WINED3D_CS_OP_QUERY_ISSUE,
70 WINED3D_CS_OP_PRELOAD_RESOURCE,
71 WINED3D_CS_OP_UNLOAD_RESOURCE,
72 WINED3D_CS_OP_MAP,
73 WINED3D_CS_OP_UNMAP,
74 WINED3D_CS_OP_BLT_SUB_RESOURCE,
75 WINED3D_CS_OP_UPDATE_SUB_RESOURCE,
76 WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION,
77 WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW,
78 WINED3D_CS_OP_COPY_UAV_COUNTER,
79 WINED3D_CS_OP_GENERATE_MIPMAPS,
80 WINED3D_CS_OP_STOP,
83 struct wined3d_cs_packet
85 size_t size;
86 BYTE data[1];
89 struct wined3d_cs_nop
91 enum wined3d_cs_op opcode;
94 struct wined3d_cs_present
96 enum wined3d_cs_op opcode;
97 HWND dst_window_override;
98 struct wined3d_swapchain *swapchain;
99 RECT src_rect;
100 RECT dst_rect;
101 unsigned int swap_interval;
102 DWORD flags;
105 struct wined3d_cs_clear
107 enum wined3d_cs_op opcode;
108 DWORD flags;
109 unsigned int rt_count;
110 struct wined3d_fb_state *fb;
111 RECT draw_rect;
112 struct wined3d_color color;
113 float depth;
114 DWORD stencil;
115 unsigned int rect_count;
116 RECT rects[1];
119 struct wined3d_cs_dispatch
121 enum wined3d_cs_op opcode;
122 struct wined3d_dispatch_parameters parameters;
125 struct wined3d_cs_draw
127 enum wined3d_cs_op opcode;
128 enum wined3d_primitive_type primitive_type;
129 GLint patch_vertex_count;
130 struct wined3d_draw_parameters parameters;
133 struct wined3d_cs_flush
135 enum wined3d_cs_op opcode;
138 struct wined3d_cs_set_predication
140 enum wined3d_cs_op opcode;
141 struct wined3d_query *predicate;
142 BOOL value;
145 struct wined3d_cs_set_viewports
147 enum wined3d_cs_op opcode;
148 unsigned int viewport_count;
149 struct wined3d_viewport viewports[1];
152 struct wined3d_cs_set_scissor_rects
154 enum wined3d_cs_op opcode;
155 unsigned int rect_count;
156 RECT rects[1];
159 struct wined3d_cs_set_rendertarget_view
161 enum wined3d_cs_op opcode;
162 unsigned int view_idx;
163 struct wined3d_rendertarget_view *view;
166 struct wined3d_cs_set_depth_stencil_view
168 enum wined3d_cs_op opcode;
169 struct wined3d_rendertarget_view *view;
172 struct wined3d_cs_set_vertex_declaration
174 enum wined3d_cs_op opcode;
175 struct wined3d_vertex_declaration *declaration;
178 struct wined3d_cs_set_stream_source
180 enum wined3d_cs_op opcode;
181 UINT stream_idx;
182 struct wined3d_buffer *buffer;
183 UINT offset;
184 UINT stride;
187 struct wined3d_cs_set_stream_source_freq
189 enum wined3d_cs_op opcode;
190 UINT stream_idx;
191 UINT frequency;
192 UINT flags;
195 struct wined3d_cs_set_stream_output
197 enum wined3d_cs_op opcode;
198 UINT stream_idx;
199 struct wined3d_buffer *buffer;
200 UINT offset;
203 struct wined3d_cs_set_index_buffer
205 enum wined3d_cs_op opcode;
206 struct wined3d_buffer *buffer;
207 enum wined3d_format_id format_id;
208 unsigned int offset;
211 struct wined3d_cs_set_constant_buffer
213 enum wined3d_cs_op opcode;
214 enum wined3d_shader_type type;
215 UINT cb_idx;
216 struct wined3d_buffer *buffer;
219 struct wined3d_cs_set_texture
221 enum wined3d_cs_op opcode;
222 UINT stage;
223 struct wined3d_texture *texture;
226 struct wined3d_cs_set_color_key
228 enum wined3d_cs_op opcode;
229 struct wined3d_texture *texture;
230 WORD flags;
231 WORD set;
232 struct wined3d_color_key color_key;
235 struct wined3d_cs_set_shader_resource_view
237 enum wined3d_cs_op opcode;
238 enum wined3d_shader_type type;
239 UINT view_idx;
240 struct wined3d_shader_resource_view *view;
243 struct wined3d_cs_set_unordered_access_view
245 enum wined3d_cs_op opcode;
246 enum wined3d_pipeline pipeline;
247 unsigned int view_idx;
248 struct wined3d_unordered_access_view *view;
249 unsigned int initial_count;
252 struct wined3d_cs_set_sampler
254 enum wined3d_cs_op opcode;
255 enum wined3d_shader_type type;
256 UINT sampler_idx;
257 struct wined3d_sampler *sampler;
260 struct wined3d_cs_set_shader
262 enum wined3d_cs_op opcode;
263 enum wined3d_shader_type type;
264 struct wined3d_shader *shader;
267 struct wined3d_cs_set_blend_state
269 enum wined3d_cs_op opcode;
270 struct wined3d_blend_state *state;
271 struct wined3d_color factor;
272 unsigned int sample_mask;
275 struct wined3d_cs_set_depth_stencil_state
277 enum wined3d_cs_op opcode;
278 struct wined3d_depth_stencil_state *state;
279 unsigned int stencil_ref;
282 struct wined3d_cs_set_rasterizer_state
284 enum wined3d_cs_op opcode;
285 struct wined3d_rasterizer_state *state;
288 struct wined3d_cs_set_render_state
290 enum wined3d_cs_op opcode;
291 enum wined3d_render_state state;
292 DWORD value;
295 struct wined3d_cs_set_texture_state
297 enum wined3d_cs_op opcode;
298 UINT stage;
299 enum wined3d_texture_stage_state state;
300 DWORD value;
303 struct wined3d_cs_set_sampler_state
305 enum wined3d_cs_op opcode;
306 UINT sampler_idx;
307 enum wined3d_sampler_state state;
308 DWORD value;
311 struct wined3d_cs_set_transform
313 enum wined3d_cs_op opcode;
314 enum wined3d_transform_state state;
315 struct wined3d_matrix matrix;
318 struct wined3d_cs_set_clip_plane
320 enum wined3d_cs_op opcode;
321 UINT plane_idx;
322 struct wined3d_vec4 plane;
325 struct wined3d_cs_set_material
327 enum wined3d_cs_op opcode;
328 struct wined3d_material material;
331 struct wined3d_cs_set_light
333 enum wined3d_cs_op opcode;
334 struct wined3d_light_info light;
337 struct wined3d_cs_set_light_enable
339 enum wined3d_cs_op opcode;
340 unsigned int idx;
341 BOOL enable;
344 struct wined3d_cs_set_feature_level
346 enum wined3d_cs_op opcode;
347 enum wined3d_feature_level level;
350 struct wined3d_cs_push_constants
352 enum wined3d_cs_op opcode;
353 enum wined3d_push_constants type;
354 unsigned int start_idx;
355 unsigned int count;
356 BYTE constants[1];
359 struct wined3d_cs_reset_state
361 enum wined3d_cs_op opcode;
364 struct wined3d_cs_callback
366 enum wined3d_cs_op opcode;
367 void (*callback)(void *object);
368 void *object;
371 struct wined3d_cs_query_issue
373 enum wined3d_cs_op opcode;
374 struct wined3d_query *query;
375 DWORD flags;
378 struct wined3d_cs_preload_resource
380 enum wined3d_cs_op opcode;
381 struct wined3d_resource *resource;
384 struct wined3d_cs_unload_resource
386 enum wined3d_cs_op opcode;
387 struct wined3d_resource *resource;
390 struct wined3d_cs_map
392 enum wined3d_cs_op opcode;
393 struct wined3d_resource *resource;
394 unsigned int sub_resource_idx;
395 struct wined3d_map_desc *map_desc;
396 const struct wined3d_box *box;
397 DWORD flags;
398 HRESULT *hr;
401 struct wined3d_cs_unmap
403 enum wined3d_cs_op opcode;
404 struct wined3d_resource *resource;
405 unsigned int sub_resource_idx;
406 HRESULT *hr;
409 struct wined3d_cs_blt_sub_resource
411 enum wined3d_cs_op opcode;
412 struct wined3d_resource *dst_resource;
413 unsigned int dst_sub_resource_idx;
414 struct wined3d_box dst_box;
415 struct wined3d_resource *src_resource;
416 unsigned int src_sub_resource_idx;
417 struct wined3d_box src_box;
418 DWORD flags;
419 struct wined3d_blt_fx fx;
420 enum wined3d_texture_filter_type filter;
423 struct wined3d_cs_update_sub_resource
425 enum wined3d_cs_op opcode;
426 struct wined3d_resource *resource;
427 unsigned int sub_resource_idx;
428 struct wined3d_box box;
429 struct wined3d_sub_resource_data data;
432 struct wined3d_cs_add_dirty_texture_region
434 enum wined3d_cs_op opcode;
435 struct wined3d_texture *texture;
436 unsigned int layer;
439 struct wined3d_cs_clear_unordered_access_view
441 enum wined3d_cs_op opcode;
442 struct wined3d_unordered_access_view *view;
443 struct wined3d_uvec4 clear_value;
446 struct wined3d_cs_copy_uav_counter
448 enum wined3d_cs_op opcode;
449 struct wined3d_buffer *buffer;
450 unsigned int offset;
451 struct wined3d_unordered_access_view *view;
454 struct wined3d_cs_generate_mipmaps
456 enum wined3d_cs_op opcode;
457 struct wined3d_shader_resource_view *view;
460 struct wined3d_cs_stop
462 enum wined3d_cs_op opcode;
465 static inline void *wined3d_device_context_require_space(struct wined3d_device_context *context,
466 size_t size, enum wined3d_cs_queue_id queue_id)
468 return context->ops->require_space(context, size, queue_id);
471 static inline void wined3d_device_context_submit(struct wined3d_device_context *context,
472 enum wined3d_cs_queue_id queue_id)
474 context->ops->submit(context, queue_id);
477 static inline void wined3d_device_context_finish(struct wined3d_device_context *context,
478 enum wined3d_cs_queue_id queue_id)
480 context->ops->finish(context, queue_id);
483 static inline void wined3d_device_context_acquire_resource(struct wined3d_device_context *context,
484 struct wined3d_resource *resource)
486 context->ops->acquire_resource(context, resource);
489 static struct wined3d_cs *wined3d_cs_from_context(struct wined3d_device_context *context)
491 return CONTAINING_RECORD(context, struct wined3d_cs, c);
494 static const char *debug_cs_op(enum wined3d_cs_op op)
496 switch (op)
498 #define WINED3D_TO_STR(type) case type: return #type
499 WINED3D_TO_STR(WINED3D_CS_OP_NOP);
500 WINED3D_TO_STR(WINED3D_CS_OP_PRESENT);
501 WINED3D_TO_STR(WINED3D_CS_OP_CLEAR);
502 WINED3D_TO_STR(WINED3D_CS_OP_DISPATCH);
503 WINED3D_TO_STR(WINED3D_CS_OP_DRAW);
504 WINED3D_TO_STR(WINED3D_CS_OP_FLUSH);
505 WINED3D_TO_STR(WINED3D_CS_OP_SET_PREDICATION);
506 WINED3D_TO_STR(WINED3D_CS_OP_SET_VIEWPORTS);
507 WINED3D_TO_STR(WINED3D_CS_OP_SET_SCISSOR_RECTS);
508 WINED3D_TO_STR(WINED3D_CS_OP_SET_RENDERTARGET_VIEW);
509 WINED3D_TO_STR(WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW);
510 WINED3D_TO_STR(WINED3D_CS_OP_SET_VERTEX_DECLARATION);
511 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_SOURCE);
512 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ);
513 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_OUTPUT);
514 WINED3D_TO_STR(WINED3D_CS_OP_SET_INDEX_BUFFER);
515 WINED3D_TO_STR(WINED3D_CS_OP_SET_CONSTANT_BUFFER);
516 WINED3D_TO_STR(WINED3D_CS_OP_SET_TEXTURE);
517 WINED3D_TO_STR(WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW);
518 WINED3D_TO_STR(WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW);
519 WINED3D_TO_STR(WINED3D_CS_OP_SET_SAMPLER);
520 WINED3D_TO_STR(WINED3D_CS_OP_SET_SHADER);
521 WINED3D_TO_STR(WINED3D_CS_OP_SET_BLEND_STATE);
522 WINED3D_TO_STR(WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE);
523 WINED3D_TO_STR(WINED3D_CS_OP_SET_RASTERIZER_STATE);
524 WINED3D_TO_STR(WINED3D_CS_OP_SET_RENDER_STATE);
525 WINED3D_TO_STR(WINED3D_CS_OP_SET_TEXTURE_STATE);
526 WINED3D_TO_STR(WINED3D_CS_OP_SET_SAMPLER_STATE);
527 WINED3D_TO_STR(WINED3D_CS_OP_SET_TRANSFORM);
528 WINED3D_TO_STR(WINED3D_CS_OP_SET_CLIP_PLANE);
529 WINED3D_TO_STR(WINED3D_CS_OP_SET_COLOR_KEY);
530 WINED3D_TO_STR(WINED3D_CS_OP_SET_MATERIAL);
531 WINED3D_TO_STR(WINED3D_CS_OP_SET_LIGHT);
532 WINED3D_TO_STR(WINED3D_CS_OP_SET_LIGHT_ENABLE);
533 WINED3D_TO_STR(WINED3D_CS_OP_SET_FEATURE_LEVEL);
534 WINED3D_TO_STR(WINED3D_CS_OP_PUSH_CONSTANTS);
535 WINED3D_TO_STR(WINED3D_CS_OP_RESET_STATE);
536 WINED3D_TO_STR(WINED3D_CS_OP_CALLBACK);
537 WINED3D_TO_STR(WINED3D_CS_OP_QUERY_ISSUE);
538 WINED3D_TO_STR(WINED3D_CS_OP_PRELOAD_RESOURCE);
539 WINED3D_TO_STR(WINED3D_CS_OP_UNLOAD_RESOURCE);
540 WINED3D_TO_STR(WINED3D_CS_OP_MAP);
541 WINED3D_TO_STR(WINED3D_CS_OP_UNMAP);
542 WINED3D_TO_STR(WINED3D_CS_OP_BLT_SUB_RESOURCE);
543 WINED3D_TO_STR(WINED3D_CS_OP_UPDATE_SUB_RESOURCE);
544 WINED3D_TO_STR(WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION);
545 WINED3D_TO_STR(WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW);
546 WINED3D_TO_STR(WINED3D_CS_OP_COPY_UAV_COUNTER);
547 WINED3D_TO_STR(WINED3D_CS_OP_GENERATE_MIPMAPS);
548 WINED3D_TO_STR(WINED3D_CS_OP_STOP);
549 #undef WINED3D_TO_STR
551 return wine_dbg_sprintf("UNKNOWN_OP(%#x)", op);
554 static void wined3d_cs_exec_nop(struct wined3d_cs *cs, const void *data)
558 static void wined3d_cs_exec_present(struct wined3d_cs *cs, const void *data)
560 struct wined3d_texture *logo_texture, *cursor_texture, *back_buffer;
561 struct wined3d_rendertarget_view *dsv = cs->state.fb.depth_stencil;
562 const struct wined3d_cs_present *op = data;
563 const struct wined3d_swapchain_desc *desc;
564 struct wined3d_swapchain *swapchain;
565 unsigned int i;
567 swapchain = op->swapchain;
568 desc = &swapchain->state.desc;
569 back_buffer = swapchain->back_buffers[0];
570 wined3d_swapchain_set_window(swapchain, op->dst_window_override);
572 if ((logo_texture = swapchain->device->logo_texture))
574 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
576 /* Blit the logo into the upper left corner of the back-buffer. */
577 wined3d_device_context_blt(&cs->c, back_buffer, 0, &rect, logo_texture, 0,
578 &rect, WINED3D_BLT_SRC_CKEY, NULL, WINED3D_TEXF_POINT);
581 if ((cursor_texture = swapchain->device->cursor_texture)
582 && swapchain->device->bCursorVisible && !swapchain->device->hardwareCursor)
584 RECT dst_rect =
586 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
587 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
588 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
589 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
591 RECT src_rect =
593 0, 0, cursor_texture->resource.width, cursor_texture->resource.height
595 const RECT clip_rect = {0, 0, back_buffer->resource.width, back_buffer->resource.height};
597 TRACE("Rendering the software cursor.\n");
599 if (desc->windowed)
600 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
601 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
602 wined3d_device_context_blt(&cs->c, back_buffer, 0, &dst_rect, cursor_texture, 0,
603 &src_rect, WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
606 swapchain->swapchain_ops->swapchain_present(swapchain, &op->src_rect, &op->dst_rect, op->swap_interval, op->flags);
608 /* Discard buffers if the swap effect allows it. */
609 back_buffer = swapchain->back_buffers[desc->backbuffer_count - 1];
610 if (desc->swap_effect == WINED3D_SWAP_EFFECT_DISCARD || desc->swap_effect == WINED3D_SWAP_EFFECT_FLIP_DISCARD)
611 wined3d_texture_validate_location(back_buffer, 0, WINED3D_LOCATION_DISCARDED);
613 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
615 struct wined3d_texture *ds = texture_from_resource(dsv->resource);
617 if ((desc->flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL || ds->flags & WINED3D_TEXTURE_DISCARD))
618 wined3d_rendertarget_view_validate_location(dsv, WINED3D_LOCATION_DISCARDED);
621 if (TRACE_ON(fps))
623 DWORD time = GetTickCount();
624 ++swapchain->frames;
626 /* every 1.5 seconds */
627 if (time - swapchain->prev_time > 1500)
629 TRACE_(fps)("%p @ approx %.2ffps\n",
630 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
631 swapchain->prev_time = time;
632 swapchain->frames = 0;
636 wined3d_resource_release(&swapchain->front_buffer->resource);
637 for (i = 0; i < desc->backbuffer_count; ++i)
639 wined3d_resource_release(&swapchain->back_buffers[i]->resource);
642 InterlockedDecrement(&cs->pending_presents);
645 void wined3d_cs_emit_present(struct wined3d_cs *cs, struct wined3d_swapchain *swapchain,
646 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
647 unsigned int swap_interval, DWORD flags)
649 struct wined3d_cs_present *op;
650 unsigned int i;
651 LONG pending;
653 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
654 op->opcode = WINED3D_CS_OP_PRESENT;
655 op->dst_window_override = dst_window_override;
656 op->swapchain = swapchain;
657 op->src_rect = *src_rect;
658 op->dst_rect = *dst_rect;
659 op->swap_interval = swap_interval;
660 op->flags = flags;
662 pending = InterlockedIncrement(&cs->pending_presents);
664 wined3d_resource_acquire(&swapchain->front_buffer->resource);
665 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
667 wined3d_resource_acquire(&swapchain->back_buffers[i]->resource);
670 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
672 /* Limit input latency by limiting the number of presents that we can get
673 * ahead of the worker thread. */
674 while (pending >= swapchain->max_frame_latency)
676 YieldProcessor();
677 pending = InterlockedCompareExchange(&cs->pending_presents, 0, 0);
681 static void wined3d_cs_exec_clear(struct wined3d_cs *cs, const void *data)
683 struct wined3d_device *device = cs->c.device;
684 const struct wined3d_cs_clear *op = data;
685 unsigned int i;
687 device->blitter->ops->blitter_clear(device->blitter, device, op->rt_count, op->fb,
688 op->rect_count, op->rects, &op->draw_rect, op->flags, &op->color, op->depth, op->stencil);
690 if (op->flags & WINED3DCLEAR_TARGET)
692 for (i = 0; i < op->rt_count; ++i)
694 if (op->fb->render_targets[i])
695 wined3d_resource_release(op->fb->render_targets[i]->resource);
698 if (op->flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
699 wined3d_resource_release(op->fb->depth_stencil->resource);
702 void wined3d_cs_emit_clear(struct wined3d_cs *cs, DWORD rect_count, const RECT *rects,
703 DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
705 const struct wined3d_state *state = cs->c.state;
706 const struct wined3d_viewport *vp = &state->viewports[0];
707 struct wined3d_rendertarget_view *view;
708 struct wined3d_cs_clear *op;
709 unsigned int rt_count, i;
711 rt_count = flags & WINED3DCLEAR_TARGET ? cs->c.device->adapter->d3d_info.limits.max_rt_count : 0;
713 op = wined3d_device_context_require_space(&cs->c, FIELD_OFFSET(struct wined3d_cs_clear, rects[rect_count]),
714 WINED3D_CS_QUEUE_DEFAULT);
715 op->opcode = WINED3D_CS_OP_CLEAR;
716 op->flags = flags & (WINED3DCLEAR_TARGET | WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
717 op->rt_count = rt_count;
718 op->fb = &cs->state.fb;
719 SetRect(&op->draw_rect, vp->x, vp->y, vp->x + vp->width, vp->y + vp->height);
720 if (state->rasterizer_state && state->rasterizer_state->desc.scissor)
721 IntersectRect(&op->draw_rect, &op->draw_rect, &state->scissor_rects[0]);
722 op->color = *color;
723 op->depth = depth;
724 op->stencil = stencil;
725 op->rect_count = rect_count;
726 memcpy(op->rects, rects, sizeof(*rects) * rect_count);
728 for (i = 0; i < rt_count; ++i)
730 if ((view = state->fb.render_targets[i]))
731 wined3d_resource_acquire(view->resource);
733 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
735 view = state->fb.depth_stencil;
736 wined3d_resource_acquire(view->resource);
739 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
742 void wined3d_device_context_emit_clear_rendertarget_view(struct wined3d_device_context *context,
743 struct wined3d_rendertarget_view *view, const RECT *rect, unsigned int flags,
744 const struct wined3d_color *color, float depth, unsigned int stencil)
746 struct wined3d_cs_clear *op;
747 size_t size;
749 size = FIELD_OFFSET(struct wined3d_cs_clear, rects[1]) + sizeof(struct wined3d_fb_state);
750 op = wined3d_device_context_require_space(context, size, WINED3D_CS_QUEUE_DEFAULT);
751 op->fb = (void *)&op->rects[1];
753 op->opcode = WINED3D_CS_OP_CLEAR;
754 op->flags = flags & (WINED3DCLEAR_TARGET | WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
755 if (flags & WINED3DCLEAR_TARGET)
757 op->rt_count = 1;
758 op->fb->render_targets[0] = view;
759 op->fb->depth_stencil = NULL;
760 op->color = *color;
762 else
764 op->rt_count = 0;
765 op->fb->render_targets[0] = NULL;
766 op->fb->depth_stencil = view;
767 op->depth = depth;
768 op->stencil = stencil;
770 SetRect(&op->draw_rect, 0, 0, view->width, view->height);
771 op->rect_count = 1;
772 op->rects[0] = *rect;
774 wined3d_device_context_acquire_resource(context, view->resource);
776 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
777 if (flags & WINED3DCLEAR_SYNCHRONOUS)
778 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_DEFAULT);
781 static void acquire_shader_resources(struct wined3d_device_context *context, unsigned int shader_mask)
783 const struct wined3d_state *state = context->state;
784 struct wined3d_shader_sampler_map_entry *entry;
785 struct wined3d_shader_resource_view *view;
786 struct wined3d_shader *shader;
787 unsigned int i, j;
789 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
791 if (!(shader_mask & (1u << i)))
792 continue;
794 if (!(shader = state->shader[i]))
795 continue;
797 for (j = 0; j < WINED3D_MAX_CBS; ++j)
799 if (state->cb[i][j])
800 wined3d_device_context_acquire_resource(context, &state->cb[i][j]->resource);
803 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
805 entry = &shader->reg_maps.sampler_map.entries[j];
807 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
808 continue;
810 wined3d_device_context_acquire_resource(context, view->resource);
815 static void release_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
817 struct wined3d_shader_sampler_map_entry *entry;
818 struct wined3d_shader_resource_view *view;
819 struct wined3d_shader *shader;
820 unsigned int i, j;
822 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
824 if (!(shader_mask & (1u << i)))
825 continue;
827 if (!(shader = state->shader[i]))
828 continue;
830 for (j = 0; j < WINED3D_MAX_CBS; ++j)
832 if (state->cb[i][j])
833 wined3d_resource_release(&state->cb[i][j]->resource);
836 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
838 entry = &shader->reg_maps.sampler_map.entries[j];
840 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
841 continue;
843 wined3d_resource_release(view->resource);
848 static void acquire_unordered_access_resources(struct wined3d_device_context *context,
849 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
851 unsigned int i;
853 if (!shader)
854 return;
856 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
858 if (!shader->reg_maps.uav_resource_info[i].type)
859 continue;
861 if (!views[i])
862 continue;
864 wined3d_device_context_acquire_resource(context, views[i]->resource);
868 static void release_unordered_access_resources(const struct wined3d_shader *shader,
869 struct wined3d_unordered_access_view * const *views)
871 unsigned int i;
873 if (!shader)
874 return;
876 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
878 if (!shader->reg_maps.uav_resource_info[i].type)
879 continue;
881 if (!views[i])
882 continue;
884 wined3d_resource_release(views[i]->resource);
888 static void wined3d_cs_exec_dispatch(struct wined3d_cs *cs, const void *data)
890 const struct wined3d_cs_dispatch *op = data;
891 struct wined3d_state *state = &cs->state;
893 if (!state->shader[WINED3D_SHADER_TYPE_COMPUTE])
894 WARN("No compute shader bound, skipping dispatch.\n");
895 else
896 cs->c.device->adapter->adapter_ops->adapter_dispatch_compute(cs->c.device, state, &op->parameters);
898 if (op->parameters.indirect)
899 wined3d_resource_release(&op->parameters.u.indirect.buffer->resource);
901 release_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
902 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
903 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
906 static void acquire_compute_pipeline_resources(struct wined3d_device_context *context)
908 const struct wined3d_state *state = context->state;
910 acquire_shader_resources(context, 1u << WINED3D_SHADER_TYPE_COMPUTE);
911 acquire_unordered_access_resources(context, state->shader[WINED3D_SHADER_TYPE_COMPUTE],
912 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
915 void CDECL wined3d_device_context_dispatch(struct wined3d_device_context *context,
916 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
918 struct wined3d_cs_dispatch *op;
920 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
921 op->opcode = WINED3D_CS_OP_DISPATCH;
922 op->parameters.indirect = FALSE;
923 op->parameters.u.direct.group_count_x = group_count_x;
924 op->parameters.u.direct.group_count_y = group_count_y;
925 op->parameters.u.direct.group_count_z = group_count_z;
927 acquire_compute_pipeline_resources(context);
929 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
932 void CDECL wined3d_device_context_dispatch_indirect(struct wined3d_device_context *context,
933 struct wined3d_buffer *buffer, unsigned int offset)
935 struct wined3d_cs_dispatch *op;
937 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
938 op->opcode = WINED3D_CS_OP_DISPATCH;
939 op->parameters.indirect = TRUE;
940 op->parameters.u.indirect.buffer = buffer;
941 op->parameters.u.indirect.offset = offset;
943 acquire_compute_pipeline_resources(context);
944 wined3d_device_context_acquire_resource(context, &buffer->resource);
946 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
949 static void wined3d_cs_exec_draw(struct wined3d_cs *cs, const void *data)
951 const struct wined3d_d3d_info *d3d_info = &cs->c.device->adapter->d3d_info;
952 const struct wined3d_shader *geometry_shader;
953 struct wined3d_device *device = cs->c.device;
954 int base_vertex_idx, load_base_vertex_idx;
955 struct wined3d_state *state = &cs->state;
956 const struct wined3d_cs_draw *op = data;
957 unsigned int i;
959 base_vertex_idx = 0;
960 if (!op->parameters.indirect)
962 const struct wined3d_direct_draw_parameters *direct = &op->parameters.u.direct;
964 if (op->parameters.indexed && d3d_info->draw_base_vertex_offset)
965 base_vertex_idx = direct->base_vertex_idx;
966 else if (!op->parameters.indexed)
967 base_vertex_idx = direct->start_idx;
970 /* ARB_draw_indirect always supports a base vertex offset. */
971 if (!op->parameters.indirect && !d3d_info->draw_base_vertex_offset)
972 load_base_vertex_idx = op->parameters.u.direct.base_vertex_idx;
973 else
974 load_base_vertex_idx = 0;
976 if (state->base_vertex_index != base_vertex_idx)
978 state->base_vertex_index = base_vertex_idx;
979 for (i = 0; i < device->context_count; ++i)
980 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_BASE_VERTEX_ID;
983 if (state->load_base_vertex_index != load_base_vertex_idx)
985 state->load_base_vertex_index = load_base_vertex_idx;
986 device_invalidate_state(cs->c.device, STATE_BASEVERTEXINDEX);
989 if (state->primitive_type != op->primitive_type)
991 if ((geometry_shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]) && !geometry_shader->function)
992 device_invalidate_state(cs->c.device, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY));
993 if (state->primitive_type == WINED3D_PT_POINTLIST || op->primitive_type == WINED3D_PT_POINTLIST)
994 device_invalidate_state(cs->c.device, STATE_POINT_ENABLE);
995 state->primitive_type = op->primitive_type;
997 state->patch_vertex_count = op->patch_vertex_count;
999 cs->c.device->adapter->adapter_ops->adapter_draw_primitive(cs->c.device, state, &op->parameters);
1001 if (op->parameters.indirect)
1003 struct wined3d_buffer *buffer = op->parameters.u.indirect.buffer;
1004 wined3d_resource_release(&buffer->resource);
1007 if (op->parameters.indexed)
1008 wined3d_resource_release(&state->index_buffer->resource);
1009 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
1011 if (state->streams[i].buffer)
1012 wined3d_resource_release(&state->streams[i].buffer->resource);
1014 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
1016 if (state->stream_output[i].buffer)
1017 wined3d_resource_release(&state->stream_output[i].buffer->resource);
1019 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
1021 if (state->textures[i])
1022 wined3d_resource_release(&state->textures[i]->resource);
1024 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
1026 if (state->fb.render_targets[i])
1027 wined3d_resource_release(state->fb.render_targets[i]->resource);
1029 if (state->fb.depth_stencil)
1030 wined3d_resource_release(state->fb.depth_stencil->resource);
1031 release_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
1032 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
1033 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
1036 static void acquire_graphics_pipeline_resources(struct wined3d_device_context *context,
1037 BOOL indexed, const struct wined3d_d3d_info *d3d_info)
1039 const struct wined3d_state *state = context->state;
1040 unsigned int i;
1042 if (indexed)
1043 wined3d_device_context_acquire_resource(context, &state->index_buffer->resource);
1044 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
1046 if (state->streams[i].buffer)
1047 wined3d_device_context_acquire_resource(context, &state->streams[i].buffer->resource);
1049 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
1051 if (state->stream_output[i].buffer)
1052 wined3d_device_context_acquire_resource(context, &state->stream_output[i].buffer->resource);
1054 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
1056 if (state->textures[i])
1057 wined3d_device_context_acquire_resource(context, &state->textures[i]->resource);
1059 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
1061 if (state->fb.render_targets[i])
1062 wined3d_device_context_acquire_resource(context, state->fb.render_targets[i]->resource);
1064 if (state->fb.depth_stencil)
1065 wined3d_device_context_acquire_resource(context, state->fb.depth_stencil->resource);
1066 acquire_shader_resources(context, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
1067 acquire_unordered_access_resources(context, state->shader[WINED3D_SHADER_TYPE_PIXEL],
1068 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
1071 void wined3d_device_context_emit_draw(struct wined3d_device_context *context,
1072 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count, int base_vertex_idx,
1073 unsigned int start_idx, unsigned int index_count, unsigned int start_instance, unsigned int instance_count,
1074 bool indexed)
1076 const struct wined3d_d3d_info *d3d_info = &context->device->adapter->d3d_info;
1077 struct wined3d_cs_draw *op;
1079 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1080 op->opcode = WINED3D_CS_OP_DRAW;
1081 op->primitive_type = primitive_type;
1082 op->patch_vertex_count = patch_vertex_count;
1083 op->parameters.indirect = FALSE;
1084 op->parameters.u.direct.base_vertex_idx = base_vertex_idx;
1085 op->parameters.u.direct.start_idx = start_idx;
1086 op->parameters.u.direct.index_count = index_count;
1087 op->parameters.u.direct.start_instance = start_instance;
1088 op->parameters.u.direct.instance_count = instance_count;
1089 op->parameters.indexed = indexed;
1091 acquire_graphics_pipeline_resources(context, indexed, d3d_info);
1093 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1096 void CDECL wined3d_device_context_draw_indirect(struct wined3d_device_context *context,
1097 struct wined3d_buffer *buffer, unsigned int offset, bool indexed)
1099 const struct wined3d_d3d_info *d3d_info = &context->device->adapter->d3d_info;
1100 const struct wined3d_state *state = context->state;
1101 struct wined3d_cs_draw *op;
1103 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1104 op->opcode = WINED3D_CS_OP_DRAW;
1105 op->primitive_type = state->primitive_type;
1106 op->patch_vertex_count = state->patch_vertex_count;
1107 op->parameters.indirect = TRUE;
1108 op->parameters.u.indirect.buffer = buffer;
1109 op->parameters.u.indirect.offset = offset;
1110 op->parameters.indexed = indexed;
1112 acquire_graphics_pipeline_resources(context, indexed, d3d_info);
1113 wined3d_device_context_acquire_resource(context, &buffer->resource);
1115 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1118 static void wined3d_cs_exec_flush(struct wined3d_cs *cs, const void *data)
1120 struct wined3d_context *context;
1122 context = context_acquire(cs->c.device, NULL, 0);
1123 cs->c.device->adapter->adapter_ops->adapter_flush_context(context);
1124 context_release(context);
1127 static void wined3d_cs_flush(struct wined3d_device_context *context)
1129 struct wined3d_cs *cs = wined3d_cs_from_context(context);
1130 struct wined3d_cs_flush *op;
1132 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1133 op->opcode = WINED3D_CS_OP_FLUSH;
1135 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1136 cs->queries_flushed = TRUE;
1139 static void wined3d_cs_exec_set_predication(struct wined3d_cs *cs, const void *data)
1141 const struct wined3d_cs_set_predication *op = data;
1143 cs->state.predicate = op->predicate;
1144 cs->state.predicate_value = op->value;
1147 void wined3d_device_context_emit_set_predication(struct wined3d_device_context *context,
1148 struct wined3d_query *predicate, BOOL value)
1150 struct wined3d_cs_set_predication *op;
1152 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1153 op->opcode = WINED3D_CS_OP_SET_PREDICATION;
1154 op->predicate = predicate;
1155 op->value = value;
1157 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1160 static void wined3d_cs_exec_set_viewports(struct wined3d_cs *cs, const void *data)
1162 const struct wined3d_cs_set_viewports *op = data;
1164 if (op->viewport_count)
1165 memcpy(cs->state.viewports, op->viewports, op->viewport_count * sizeof(*op->viewports));
1166 else
1167 memset(cs->state.viewports, 0, sizeof(*cs->state.viewports));
1168 cs->state.viewport_count = op->viewport_count;
1169 device_invalidate_state(cs->c.device, STATE_VIEWPORT);
1172 void wined3d_device_context_emit_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
1173 const struct wined3d_viewport *viewports)
1175 struct wined3d_cs_set_viewports *op;
1177 op = wined3d_device_context_require_space(context,
1178 FIELD_OFFSET(struct wined3d_cs_set_viewports,viewports[viewport_count]), WINED3D_CS_QUEUE_DEFAULT);
1179 op->opcode = WINED3D_CS_OP_SET_VIEWPORTS;
1180 memcpy(op->viewports, viewports, viewport_count * sizeof(*viewports));
1181 op->viewport_count = viewport_count;
1183 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1186 static void wined3d_cs_exec_set_scissor_rects(struct wined3d_cs *cs, const void *data)
1188 const struct wined3d_cs_set_scissor_rects *op = data;
1190 if (op->rect_count)
1191 memcpy(cs->state.scissor_rects, op->rects, op->rect_count * sizeof(*op->rects));
1192 else
1193 SetRectEmpty(cs->state.scissor_rects);
1194 cs->state.scissor_rect_count = op->rect_count;
1195 device_invalidate_state(cs->c.device, STATE_SCISSORRECT);
1198 void wined3d_device_context_emit_set_scissor_rects(struct wined3d_device_context *context,
1199 unsigned int rect_count, const RECT *rects)
1201 struct wined3d_cs_set_scissor_rects *op;
1203 op = wined3d_device_context_require_space(context, FIELD_OFFSET(struct wined3d_cs_set_scissor_rects, rects[rect_count]),
1204 WINED3D_CS_QUEUE_DEFAULT);
1205 op->opcode = WINED3D_CS_OP_SET_SCISSOR_RECTS;
1206 memcpy(op->rects, rects, rect_count * sizeof(*rects));
1207 op->rect_count = rect_count;
1209 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1212 static void wined3d_cs_exec_set_rendertarget_view(struct wined3d_cs *cs, const void *data)
1214 const struct wined3d_cs_set_rendertarget_view *op = data;
1215 bool prev_alpha_swizzle, curr_alpha_swizzle;
1216 struct wined3d_rendertarget_view *prev;
1217 bool prev_srgb_write, curr_srgb_write;
1218 struct wined3d_device *device;
1220 device = cs->c.device;
1221 prev = cs->state.fb.render_targets[op->view_idx];
1222 cs->state.fb.render_targets[op->view_idx] = op->view;
1223 device_invalidate_state(device, STATE_FRAMEBUFFER);
1225 prev_alpha_swizzle = prev && prev->format->id == WINED3DFMT_A8_UNORM;
1226 curr_alpha_swizzle = op->view && op->view->format->id == WINED3DFMT_A8_UNORM;
1227 if (prev_alpha_swizzle != curr_alpha_swizzle)
1228 device_invalidate_state(device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1230 if (!(device->adapter->d3d_info.wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1231 || cs->state.render_states[WINED3D_RS_SRGBWRITEENABLE])
1233 prev_srgb_write = prev && prev->format_flags & WINED3DFMT_FLAG_SRGB_WRITE;
1234 curr_srgb_write = op->view && op->view->format_flags & WINED3DFMT_FLAG_SRGB_WRITE;
1235 if (prev_srgb_write != curr_srgb_write)
1236 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
1240 void wined3d_device_context_emit_set_rendertarget_view(struct wined3d_device_context *context, unsigned int view_idx,
1241 struct wined3d_rendertarget_view *view)
1243 struct wined3d_cs_set_rendertarget_view *op;
1245 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1246 op->opcode = WINED3D_CS_OP_SET_RENDERTARGET_VIEW;
1247 op->view_idx = view_idx;
1248 op->view = view;
1250 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1253 static void wined3d_cs_exec_set_depth_stencil_view(struct wined3d_cs *cs, const void *data)
1255 const struct wined3d_cs_set_depth_stencil_view *op = data;
1256 struct wined3d_device *device = cs->c.device;
1257 struct wined3d_rendertarget_view *prev;
1259 if ((prev = cs->state.fb.depth_stencil) && prev->resource->type != WINED3D_RTYPE_BUFFER)
1261 struct wined3d_texture *prev_texture = texture_from_resource(prev->resource);
1263 if (device->swapchains[0]->state.desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
1264 || prev_texture->flags & WINED3D_TEXTURE_DISCARD)
1265 wined3d_texture_validate_location(prev_texture,
1266 prev->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
1269 cs->state.fb.depth_stencil = op->view;
1271 if (!prev != !op->view)
1273 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
1274 device_invalidate_state(device, STATE_DEPTH_STENCIL);
1275 device_invalidate_state(device, STATE_STENCIL_REF);
1276 device_invalidate_state(device, STATE_RASTERIZER);
1278 else if (prev)
1280 if (prev->format->depth_bias_scale != op->view->format->depth_bias_scale)
1281 device_invalidate_state(device, STATE_RASTERIZER);
1282 if (prev->format->stencil_size != op->view->format->stencil_size)
1283 device_invalidate_state(device, STATE_STENCIL_REF);
1286 device_invalidate_state(device, STATE_FRAMEBUFFER);
1289 void wined3d_device_context_emit_set_depth_stencil_view(struct wined3d_device_context *context,
1290 struct wined3d_rendertarget_view *view)
1292 struct wined3d_cs_set_depth_stencil_view *op;
1294 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1295 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW;
1296 op->view = view;
1298 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1301 static void wined3d_cs_exec_set_vertex_declaration(struct wined3d_cs *cs, const void *data)
1303 const struct wined3d_cs_set_vertex_declaration *op = data;
1305 cs->state.vertex_declaration = op->declaration;
1306 device_invalidate_state(cs->c.device, STATE_VDECL);
1309 void wined3d_device_context_emit_set_vertex_declaration(struct wined3d_device_context *context,
1310 struct wined3d_vertex_declaration *declaration)
1312 struct wined3d_cs_set_vertex_declaration *op;
1314 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1315 op->opcode = WINED3D_CS_OP_SET_VERTEX_DECLARATION;
1316 op->declaration = declaration;
1318 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1321 static void wined3d_cs_exec_set_stream_source(struct wined3d_cs *cs, const void *data)
1323 const struct wined3d_cs_set_stream_source *op = data;
1324 struct wined3d_stream_state *stream;
1325 struct wined3d_buffer *prev;
1327 stream = &cs->state.streams[op->stream_idx];
1328 prev = stream->buffer;
1329 stream->buffer = op->buffer;
1330 stream->offset = op->offset;
1331 stream->stride = op->stride;
1333 if (op->buffer)
1334 InterlockedIncrement(&op->buffer->resource.bind_count);
1335 if (prev)
1336 InterlockedDecrement(&prev->resource.bind_count);
1338 device_invalidate_state(cs->c.device, STATE_STREAMSRC);
1341 void wined3d_device_context_emit_set_stream_source(struct wined3d_device_context *context, unsigned int stream_idx,
1342 struct wined3d_buffer *buffer, unsigned int offset, unsigned int stride)
1344 struct wined3d_cs_set_stream_source *op;
1346 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1347 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE;
1348 op->stream_idx = stream_idx;
1349 op->buffer = buffer;
1350 op->offset = offset;
1351 op->stride = stride;
1353 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1356 static void wined3d_cs_exec_set_stream_source_freq(struct wined3d_cs *cs, const void *data)
1358 const struct wined3d_cs_set_stream_source_freq *op = data;
1359 struct wined3d_stream_state *stream;
1361 stream = &cs->state.streams[op->stream_idx];
1362 stream->frequency = op->frequency;
1363 stream->flags = op->flags;
1365 device_invalidate_state(cs->c.device, STATE_STREAMSRC);
1368 void wined3d_cs_emit_set_stream_source_freq(struct wined3d_cs *cs, UINT stream_idx, UINT frequency, UINT flags)
1370 struct wined3d_cs_set_stream_source_freq *op;
1372 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1373 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ;
1374 op->stream_idx = stream_idx;
1375 op->frequency = frequency;
1376 op->flags = flags;
1378 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1381 static void wined3d_cs_exec_set_stream_output(struct wined3d_cs *cs, const void *data)
1383 const struct wined3d_cs_set_stream_output *op = data;
1384 struct wined3d_stream_output *stream;
1385 struct wined3d_buffer *prev;
1387 stream = &cs->state.stream_output[op->stream_idx];
1388 prev = stream->buffer;
1389 stream->buffer = op->buffer;
1390 stream->offset = op->offset;
1392 if (op->buffer)
1393 InterlockedIncrement(&op->buffer->resource.bind_count);
1394 if (prev)
1395 InterlockedDecrement(&prev->resource.bind_count);
1397 device_invalidate_state(cs->c.device, STATE_STREAM_OUTPUT);
1400 void wined3d_device_context_emit_set_stream_output(struct wined3d_device_context *context, unsigned int stream_idx,
1401 struct wined3d_buffer *buffer, unsigned int offset)
1403 struct wined3d_cs_set_stream_output *op;
1405 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1406 op->opcode = WINED3D_CS_OP_SET_STREAM_OUTPUT;
1407 op->stream_idx = stream_idx;
1408 op->buffer = buffer;
1409 op->offset = offset;
1411 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1414 static void wined3d_cs_exec_set_index_buffer(struct wined3d_cs *cs, const void *data)
1416 const struct wined3d_cs_set_index_buffer *op = data;
1417 struct wined3d_buffer *prev;
1419 prev = cs->state.index_buffer;
1420 cs->state.index_buffer = op->buffer;
1421 cs->state.index_format = op->format_id;
1422 cs->state.index_offset = op->offset;
1424 if (op->buffer)
1425 InterlockedIncrement(&op->buffer->resource.bind_count);
1426 if (prev)
1427 InterlockedDecrement(&prev->resource.bind_count);
1429 device_invalidate_state(cs->c.device, STATE_INDEXBUFFER);
1432 void wined3d_device_context_emit_set_index_buffer(struct wined3d_device_context *context, struct wined3d_buffer *buffer,
1433 enum wined3d_format_id format_id, unsigned int offset)
1435 struct wined3d_cs_set_index_buffer *op;
1437 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1438 op->opcode = WINED3D_CS_OP_SET_INDEX_BUFFER;
1439 op->buffer = buffer;
1440 op->format_id = format_id;
1441 op->offset = offset;
1443 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1446 static void wined3d_cs_exec_set_constant_buffer(struct wined3d_cs *cs, const void *data)
1448 const struct wined3d_cs_set_constant_buffer *op = data;
1449 struct wined3d_buffer *prev;
1451 prev = cs->state.cb[op->type][op->cb_idx];
1452 cs->state.cb[op->type][op->cb_idx] = op->buffer;
1454 if (op->buffer)
1455 InterlockedIncrement(&op->buffer->resource.bind_count);
1456 if (prev)
1457 InterlockedDecrement(&prev->resource.bind_count);
1459 device_invalidate_state(cs->c.device, STATE_CONSTANT_BUFFER(op->type));
1462 void wined3d_device_context_emit_set_constant_buffer(struct wined3d_device_context *context,
1463 enum wined3d_shader_type type, UINT cb_idx, struct wined3d_buffer *buffer)
1465 struct wined3d_cs_set_constant_buffer *op;
1467 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1468 op->opcode = WINED3D_CS_OP_SET_CONSTANT_BUFFER;
1469 op->type = type;
1470 op->cb_idx = cb_idx;
1471 op->buffer = buffer;
1473 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1476 static void wined3d_cs_exec_set_texture(struct wined3d_cs *cs, const void *data)
1478 const struct wined3d_d3d_info *d3d_info = &cs->c.device->adapter->d3d_info;
1479 const struct wined3d_cs_set_texture *op = data;
1480 struct wined3d_texture *prev;
1481 BOOL old_use_color_key = FALSE, new_use_color_key = FALSE;
1483 prev = cs->state.textures[op->stage];
1484 cs->state.textures[op->stage] = op->texture;
1486 if (op->texture)
1488 const struct wined3d_format *new_format = op->texture->resource.format;
1489 const struct wined3d_format *old_format = prev ? prev->resource.format : NULL;
1490 unsigned int old_fmt_flags = prev ? prev->resource.format_flags : 0;
1491 unsigned int new_fmt_flags = op->texture->resource.format_flags;
1493 if (InterlockedIncrement(&op->texture->resource.bind_count) == 1)
1494 op->texture->sampler = op->stage;
1496 if (!prev || wined3d_texture_gl(op->texture)->target != wined3d_texture_gl(prev)->target
1497 || (!is_same_fixup(new_format->color_fixup, old_format->color_fixup)
1498 && !(can_use_texture_swizzle(d3d_info, new_format) && can_use_texture_swizzle(d3d_info, old_format)))
1499 || (new_fmt_flags & WINED3DFMT_FLAG_SHADOW) != (old_fmt_flags & WINED3DFMT_FLAG_SHADOW))
1500 device_invalidate_state(cs->c.device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1502 if (!prev && op->stage < d3d_info->limits.ffp_blend_stages)
1504 /* The source arguments for color and alpha ops have different
1505 * meanings when a NULL texture is bound, so the COLOR_OP and
1506 * ALPHA_OP have to be dirtified. */
1507 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1508 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1511 if (!op->stage && op->texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1512 new_use_color_key = TRUE;
1515 if (prev)
1517 if (InterlockedDecrement(&prev->resource.bind_count) && prev->sampler == op->stage)
1519 unsigned int i;
1521 /* Search for other stages the texture is bound to. Shouldn't
1522 * happen if applications bind textures to a single stage only. */
1523 TRACE("Searching for other stages the texture is bound to.\n");
1524 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1526 if (cs->state.textures[i] == prev)
1528 TRACE("Texture is also bound to stage %u.\n", i);
1529 prev->sampler = i;
1530 break;
1535 if (!op->texture && op->stage < d3d_info->limits.ffp_blend_stages)
1537 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1538 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1541 if (!op->stage && prev->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1542 old_use_color_key = TRUE;
1545 device_invalidate_state(cs->c.device, STATE_SAMPLER(op->stage));
1547 if (new_use_color_key != old_use_color_key)
1548 device_invalidate_state(cs->c.device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1550 if (new_use_color_key)
1551 device_invalidate_state(cs->c.device, STATE_COLOR_KEY);
1554 void wined3d_device_context_emit_set_texture(struct wined3d_device_context *context, unsigned int stage,
1555 struct wined3d_texture *texture)
1557 struct wined3d_cs_set_texture *op;
1559 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1560 op->opcode = WINED3D_CS_OP_SET_TEXTURE;
1561 op->stage = stage;
1562 op->texture = texture;
1564 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1567 static void wined3d_cs_exec_set_shader_resource_view(struct wined3d_cs *cs, const void *data)
1569 const struct wined3d_cs_set_shader_resource_view *op = data;
1570 struct wined3d_shader_resource_view *prev;
1572 prev = cs->state.shader_resource_view[op->type][op->view_idx];
1573 cs->state.shader_resource_view[op->type][op->view_idx] = op->view;
1575 if (op->view)
1576 InterlockedIncrement(&op->view->resource->bind_count);
1577 if (prev)
1578 InterlockedDecrement(&prev->resource->bind_count);
1580 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1581 device_invalidate_state(cs->c.device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1582 else
1583 device_invalidate_state(cs->c.device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1586 void wined3d_device_context_emit_set_shader_resource_view(struct wined3d_device_context *context,
1587 enum wined3d_shader_type type, unsigned int view_idx, struct wined3d_shader_resource_view *view)
1589 struct wined3d_cs_set_shader_resource_view *op;
1591 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1592 op->opcode = WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW;
1593 op->type = type;
1594 op->view_idx = view_idx;
1595 op->view = view;
1597 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1600 static void wined3d_cs_exec_set_unordered_access_view(struct wined3d_cs *cs, const void *data)
1602 const struct wined3d_cs_set_unordered_access_view *op = data;
1603 struct wined3d_unordered_access_view *prev;
1605 prev = cs->state.unordered_access_view[op->pipeline][op->view_idx];
1606 cs->state.unordered_access_view[op->pipeline][op->view_idx] = op->view;
1608 if (op->view)
1609 InterlockedIncrement(&op->view->resource->bind_count);
1610 if (prev)
1611 InterlockedDecrement(&prev->resource->bind_count);
1613 if (op->view && op->initial_count != ~0u)
1614 wined3d_unordered_access_view_set_counter(op->view, op->initial_count);
1616 device_invalidate_state(cs->c.device, STATE_UNORDERED_ACCESS_VIEW_BINDING(op->pipeline));
1619 void wined3d_device_context_emit_set_unordered_access_view(struct wined3d_device_context *context,
1620 enum wined3d_pipeline pipeline, unsigned int view_idx, struct wined3d_unordered_access_view *view,
1621 unsigned int initial_count)
1623 struct wined3d_cs_set_unordered_access_view *op;
1625 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1626 op->opcode = WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW;
1627 op->pipeline = pipeline;
1628 op->view_idx = view_idx;
1629 op->view = view;
1630 op->initial_count = initial_count;
1632 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1635 static void wined3d_cs_exec_set_sampler(struct wined3d_cs *cs, const void *data)
1637 const struct wined3d_cs_set_sampler *op = data;
1639 cs->state.sampler[op->type][op->sampler_idx] = op->sampler;
1640 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1641 device_invalidate_state(cs->c.device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1642 else
1643 device_invalidate_state(cs->c.device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1646 void wined3d_device_context_emit_set_sampler(struct wined3d_device_context *context, enum wined3d_shader_type type,
1647 unsigned int sampler_idx, struct wined3d_sampler *sampler)
1649 struct wined3d_cs_set_sampler *op;
1651 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1652 op->opcode = WINED3D_CS_OP_SET_SAMPLER;
1653 op->type = type;
1654 op->sampler_idx = sampler_idx;
1655 op->sampler = sampler;
1657 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1660 static void wined3d_cs_exec_set_shader(struct wined3d_cs *cs, const void *data)
1662 const struct wined3d_cs_set_shader *op = data;
1664 cs->state.shader[op->type] = op->shader;
1665 device_invalidate_state(cs->c.device, STATE_SHADER(op->type));
1666 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1667 device_invalidate_state(cs->c.device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1668 else
1669 device_invalidate_state(cs->c.device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1672 void wined3d_device_context_emit_set_shader(struct wined3d_device_context *context,
1673 enum wined3d_shader_type type, struct wined3d_shader *shader)
1675 struct wined3d_cs_set_shader *op;
1677 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1678 op->opcode = WINED3D_CS_OP_SET_SHADER;
1679 op->type = type;
1680 op->shader = shader;
1682 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1685 static void wined3d_cs_exec_set_blend_state(struct wined3d_cs *cs, const void *data)
1687 const struct wined3d_cs_set_blend_state *op = data;
1688 struct wined3d_state *state = &cs->state;
1690 if (state->blend_state != op->state)
1692 state->blend_state = op->state;
1693 device_invalidate_state(cs->c.device, STATE_BLEND);
1695 state->blend_factor = op->factor;
1696 device_invalidate_state(cs->c.device, STATE_BLEND_FACTOR);
1697 state->sample_mask = op->sample_mask;
1698 device_invalidate_state(cs->c.device, STATE_SAMPLE_MASK);
1701 void wined3d_device_context_emit_set_blend_state(struct wined3d_device_context *context,
1702 struct wined3d_blend_state *state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
1704 struct wined3d_cs_set_blend_state *op;
1706 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1707 op->opcode = WINED3D_CS_OP_SET_BLEND_STATE;
1708 op->state = state;
1709 op->factor = *blend_factor;
1710 op->sample_mask = sample_mask;
1712 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1715 static void wined3d_cs_exec_set_depth_stencil_state(struct wined3d_cs *cs, const void *data)
1717 const struct wined3d_cs_set_depth_stencil_state *op = data;
1718 struct wined3d_state *state = &cs->state;
1720 if (state->depth_stencil_state != op->state)
1722 state->depth_stencil_state = op->state;
1723 device_invalidate_state(cs->c.device, STATE_DEPTH_STENCIL);
1725 state->stencil_ref = op->stencil_ref;
1726 device_invalidate_state(cs->c.device, STATE_STENCIL_REF);
1729 void wined3d_device_context_emit_set_depth_stencil_state(struct wined3d_device_context *context,
1730 struct wined3d_depth_stencil_state *state, unsigned int stencil_ref)
1732 struct wined3d_cs_set_depth_stencil_state *op;
1734 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1735 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE;
1736 op->state = state;
1737 op->stencil_ref = stencil_ref;
1739 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1742 static void wined3d_cs_exec_set_rasterizer_state(struct wined3d_cs *cs, const void *data)
1744 const struct wined3d_cs_set_rasterizer_state *op = data;
1746 cs->state.rasterizer_state = op->state;
1747 device_invalidate_state(cs->c.device, STATE_RASTERIZER);
1750 void wined3d_device_context_emit_set_rasterizer_state(struct wined3d_device_context *context,
1751 struct wined3d_rasterizer_state *rasterizer_state)
1753 struct wined3d_cs_set_rasterizer_state *op;
1755 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1756 op->opcode = WINED3D_CS_OP_SET_RASTERIZER_STATE;
1757 op->state = rasterizer_state;
1759 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1762 static void wined3d_cs_exec_set_render_state(struct wined3d_cs *cs, const void *data)
1764 const struct wined3d_cs_set_render_state *op = data;
1766 cs->state.render_states[op->state] = op->value;
1767 device_invalidate_state(cs->c.device, STATE_RENDER(op->state));
1770 void wined3d_cs_emit_set_render_state(struct wined3d_cs *cs, enum wined3d_render_state state, DWORD value)
1772 struct wined3d_cs_set_render_state *op;
1774 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1775 op->opcode = WINED3D_CS_OP_SET_RENDER_STATE;
1776 op->state = state;
1777 op->value = value;
1779 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1782 static void wined3d_cs_exec_set_texture_state(struct wined3d_cs *cs, const void *data)
1784 const struct wined3d_cs_set_texture_state *op = data;
1786 cs->state.texture_states[op->stage][op->state] = op->value;
1787 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, op->state));
1790 void wined3d_device_context_emit_set_texture_state(struct wined3d_device_context *context, unsigned int stage,
1791 enum wined3d_texture_stage_state state, unsigned int value)
1793 struct wined3d_cs_set_texture_state *op;
1795 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1796 op->opcode = WINED3D_CS_OP_SET_TEXTURE_STATE;
1797 op->stage = stage;
1798 op->state = state;
1799 op->value = value;
1801 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1804 static void wined3d_cs_exec_set_sampler_state(struct wined3d_cs *cs, const void *data)
1806 const struct wined3d_cs_set_sampler_state *op = data;
1808 cs->state.sampler_states[op->sampler_idx][op->state] = op->value;
1809 device_invalidate_state(cs->c.device, STATE_SAMPLER(op->sampler_idx));
1812 void wined3d_device_context_emit_set_sampler_state(struct wined3d_device_context *context, unsigned int sampler_idx,
1813 enum wined3d_sampler_state state, unsigned int value)
1815 struct wined3d_cs_set_sampler_state *op;
1817 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1818 op->opcode = WINED3D_CS_OP_SET_SAMPLER_STATE;
1819 op->sampler_idx = sampler_idx;
1820 op->state = state;
1821 op->value = value;
1823 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1826 static void wined3d_cs_exec_set_transform(struct wined3d_cs *cs, const void *data)
1828 const struct wined3d_cs_set_transform *op = data;
1830 cs->state.transforms[op->state] = op->matrix;
1831 if (op->state < WINED3D_TS_WORLD_MATRIX(cs->c.device->adapter->d3d_info.limits.ffp_vertex_blend_matrices))
1832 device_invalidate_state(cs->c.device, STATE_TRANSFORM(op->state));
1835 void wined3d_cs_emit_set_transform(struct wined3d_cs *cs, enum wined3d_transform_state state,
1836 const struct wined3d_matrix *matrix)
1838 struct wined3d_cs_set_transform *op;
1840 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1841 op->opcode = WINED3D_CS_OP_SET_TRANSFORM;
1842 op->state = state;
1843 op->matrix = *matrix;
1845 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1848 static void wined3d_cs_exec_set_clip_plane(struct wined3d_cs *cs, const void *data)
1850 const struct wined3d_cs_set_clip_plane *op = data;
1852 cs->state.clip_planes[op->plane_idx] = op->plane;
1853 device_invalidate_state(cs->c.device, STATE_CLIPPLANE(op->plane_idx));
1856 void wined3d_cs_emit_set_clip_plane(struct wined3d_cs *cs, UINT plane_idx, const struct wined3d_vec4 *plane)
1858 struct wined3d_cs_set_clip_plane *op;
1860 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1861 op->opcode = WINED3D_CS_OP_SET_CLIP_PLANE;
1862 op->plane_idx = plane_idx;
1863 op->plane = *plane;
1865 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1868 static void wined3d_cs_exec_set_color_key(struct wined3d_cs *cs, const void *data)
1870 const struct wined3d_cs_set_color_key *op = data;
1871 struct wined3d_texture *texture = op->texture;
1873 if (op->set)
1875 switch (op->flags)
1877 case WINED3D_CKEY_DST_BLT:
1878 texture->async.dst_blt_color_key = op->color_key;
1879 texture->async.color_key_flags |= WINED3D_CKEY_DST_BLT;
1880 break;
1882 case WINED3D_CKEY_DST_OVERLAY:
1883 texture->async.dst_overlay_color_key = op->color_key;
1884 texture->async.color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
1885 break;
1887 case WINED3D_CKEY_SRC_BLT:
1888 if (texture == cs->state.textures[0])
1890 device_invalidate_state(cs->c.device, STATE_COLOR_KEY);
1891 if (!(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1892 device_invalidate_state(cs->c.device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1895 texture->async.src_blt_color_key = op->color_key;
1896 texture->async.color_key_flags |= WINED3D_CKEY_SRC_BLT;
1897 break;
1899 case WINED3D_CKEY_SRC_OVERLAY:
1900 texture->async.src_overlay_color_key = op->color_key;
1901 texture->async.color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
1902 break;
1905 else
1907 switch (op->flags)
1909 case WINED3D_CKEY_DST_BLT:
1910 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_BLT;
1911 break;
1913 case WINED3D_CKEY_DST_OVERLAY:
1914 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
1915 break;
1917 case WINED3D_CKEY_SRC_BLT:
1918 if (texture == cs->state.textures[0] && texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1919 device_invalidate_state(cs->c.device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1921 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
1922 break;
1924 case WINED3D_CKEY_SRC_OVERLAY:
1925 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
1926 break;
1931 void wined3d_cs_emit_set_color_key(struct wined3d_cs *cs, struct wined3d_texture *texture,
1932 WORD flags, const struct wined3d_color_key *color_key)
1934 struct wined3d_cs_set_color_key *op;
1936 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1937 op->opcode = WINED3D_CS_OP_SET_COLOR_KEY;
1938 op->texture = texture;
1939 op->flags = flags;
1940 if (color_key)
1942 op->color_key = *color_key;
1943 op->set = 1;
1945 else
1946 op->set = 0;
1948 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1951 static void wined3d_cs_exec_set_material(struct wined3d_cs *cs, const void *data)
1953 const struct wined3d_cs_set_material *op = data;
1955 cs->state.material = op->material;
1956 device_invalidate_state(cs->c.device, STATE_MATERIAL);
1959 void wined3d_cs_emit_set_material(struct wined3d_cs *cs, const struct wined3d_material *material)
1961 struct wined3d_cs_set_material *op;
1963 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1964 op->opcode = WINED3D_CS_OP_SET_MATERIAL;
1965 op->material = *material;
1967 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1970 static void wined3d_cs_exec_set_light(struct wined3d_cs *cs, const void *data)
1972 const struct wined3d_cs_set_light *op = data;
1973 struct wined3d_light_info *light_info;
1974 unsigned int light_idx, hash_idx;
1976 light_idx = op->light.OriginalIndex;
1978 if (!(light_info = wined3d_light_state_get_light(&cs->state.light_state, light_idx)))
1980 TRACE("Adding new light.\n");
1981 if (!(light_info = heap_alloc_zero(sizeof(*light_info))))
1983 ERR("Failed to allocate light info.\n");
1984 return;
1987 hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1988 list_add_head(&cs->state.light_state.light_map[hash_idx], &light_info->entry);
1989 light_info->glIndex = -1;
1990 light_info->OriginalIndex = light_idx;
1993 if (light_info->glIndex != -1)
1995 if (light_info->OriginalParms.type != op->light.OriginalParms.type)
1996 device_invalidate_state(cs->c.device, STATE_LIGHT_TYPE);
1997 device_invalidate_state(cs->c.device, STATE_ACTIVELIGHT(light_info->glIndex));
2000 light_info->OriginalParms = op->light.OriginalParms;
2001 light_info->position = op->light.position;
2002 light_info->direction = op->light.direction;
2003 light_info->exponent = op->light.exponent;
2004 light_info->cutoff = op->light.cutoff;
2007 void wined3d_cs_emit_set_light(struct wined3d_cs *cs, const struct wined3d_light_info *light)
2009 struct wined3d_cs_set_light *op;
2011 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2012 op->opcode = WINED3D_CS_OP_SET_LIGHT;
2013 op->light = *light;
2015 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2018 static void wined3d_cs_exec_set_light_enable(struct wined3d_cs *cs, const void *data)
2020 const struct wined3d_cs_set_light_enable *op = data;
2021 struct wined3d_device *device = cs->c.device;
2022 struct wined3d_light_info *light_info;
2023 int prev_idx;
2025 if (!(light_info = wined3d_light_state_get_light(&cs->state.light_state, op->idx)))
2027 ERR("Light doesn't exist.\n");
2028 return;
2031 prev_idx = light_info->glIndex;
2032 wined3d_light_state_enable_light(&cs->state.light_state, &device->adapter->d3d_info, light_info, op->enable);
2033 if (light_info->glIndex != prev_idx)
2035 device_invalidate_state(device, STATE_LIGHT_TYPE);
2036 device_invalidate_state(device, STATE_ACTIVELIGHT(op->enable ? light_info->glIndex : prev_idx));
2040 void wined3d_cs_emit_set_light_enable(struct wined3d_cs *cs, unsigned int idx, BOOL enable)
2042 struct wined3d_cs_set_light_enable *op;
2044 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2045 op->opcode = WINED3D_CS_OP_SET_LIGHT_ENABLE;
2046 op->idx = idx;
2047 op->enable = enable;
2049 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2052 static void wined3d_cs_exec_set_feature_level(struct wined3d_cs *cs, const void *data)
2054 const struct wined3d_cs_set_feature_level *op = data;
2056 cs->state.feature_level = op->level;
2059 void wined3d_device_context_emit_set_feature_level(struct wined3d_device_context *context,
2060 enum wined3d_feature_level level)
2062 struct wined3d_cs_set_feature_level *op;
2064 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2065 op->opcode = WINED3D_CS_OP_SET_FEATURE_LEVEL;
2066 op->level = level;
2068 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2071 static const struct
2073 size_t offset;
2074 size_t size;
2075 DWORD mask;
2077 wined3d_cs_push_constant_info[] =
2079 /* WINED3D_PUSH_CONSTANTS_VS_F */
2080 {FIELD_OFFSET(struct wined3d_state, vs_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_VS_F},
2081 /* WINED3D_PUSH_CONSTANTS_PS_F */
2082 {FIELD_OFFSET(struct wined3d_state, ps_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_PS_F},
2083 /* WINED3D_PUSH_CONSTANTS_VS_I */
2084 {FIELD_OFFSET(struct wined3d_state, vs_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_VS_I},
2085 /* WINED3D_PUSH_CONSTANTS_PS_I */
2086 {FIELD_OFFSET(struct wined3d_state, ps_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_PS_I},
2087 /* WINED3D_PUSH_CONSTANTS_VS_B */
2088 {FIELD_OFFSET(struct wined3d_state, vs_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_VS_B},
2089 /* WINED3D_PUSH_CONSTANTS_PS_B */
2090 {FIELD_OFFSET(struct wined3d_state, ps_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_PS_B},
2093 static void wined3d_cs_st_push_constants(struct wined3d_device_context *context, enum wined3d_push_constants p,
2094 unsigned int start_idx, unsigned int count, const void *constants)
2096 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2097 struct wined3d_device *device = cs->c.device;
2098 unsigned int context_count;
2099 unsigned int i;
2100 size_t offset;
2102 if (p == WINED3D_PUSH_CONSTANTS_VS_F)
2103 device->shader_backend->shader_update_float_vertex_constants(device, start_idx, count);
2104 else if (p == WINED3D_PUSH_CONSTANTS_PS_F)
2105 device->shader_backend->shader_update_float_pixel_constants(device, start_idx, count);
2107 offset = wined3d_cs_push_constant_info[p].offset + start_idx * wined3d_cs_push_constant_info[p].size;
2108 memcpy((BYTE *)&cs->state + offset, constants, count * wined3d_cs_push_constant_info[p].size);
2109 for (i = 0, context_count = device->context_count; i < context_count; ++i)
2111 device->contexts[i]->constant_update_mask |= wined3d_cs_push_constant_info[p].mask;
2115 static void wined3d_cs_exec_push_constants(struct wined3d_cs *cs, const void *data)
2117 const struct wined3d_cs_push_constants *op = data;
2119 wined3d_cs_st_push_constants(&cs->c, op->type, op->start_idx, op->count, op->constants);
2122 static void wined3d_cs_mt_push_constants(struct wined3d_device_context *context, enum wined3d_push_constants p,
2123 unsigned int start_idx, unsigned int count, const void *constants)
2125 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2126 struct wined3d_cs_push_constants *op;
2127 size_t size;
2129 size = count * wined3d_cs_push_constant_info[p].size;
2130 op = wined3d_device_context_require_space(&cs->c, FIELD_OFFSET(struct wined3d_cs_push_constants, constants[size]),
2131 WINED3D_CS_QUEUE_DEFAULT);
2132 op->opcode = WINED3D_CS_OP_PUSH_CONSTANTS;
2133 op->type = p;
2134 op->start_idx = start_idx;
2135 op->count = count;
2136 memcpy(op->constants, constants, size);
2138 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2141 static void wined3d_cs_exec_reset_state(struct wined3d_cs *cs, const void *data)
2143 state_cleanup(&cs->state);
2144 wined3d_state_reset(&cs->state, &cs->c.device->adapter->d3d_info);
2147 void wined3d_cs_emit_reset_state(struct wined3d_cs *cs)
2149 struct wined3d_cs_reset_state *op;
2151 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2152 op->opcode = WINED3D_CS_OP_RESET_STATE;
2154 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2157 static void wined3d_cs_exec_callback(struct wined3d_cs *cs, const void *data)
2159 const struct wined3d_cs_callback *op = data;
2161 op->callback(op->object);
2164 static void wined3d_cs_emit_callback(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2166 struct wined3d_cs_callback *op;
2168 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2169 op->opcode = WINED3D_CS_OP_CALLBACK;
2170 op->callback = callback;
2171 op->object = object;
2173 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2176 void wined3d_cs_destroy_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2178 wined3d_cs_emit_callback(cs, callback, object);
2181 void wined3d_cs_init_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2183 wined3d_cs_emit_callback(cs, callback, object);
2186 static void wined3d_cs_exec_query_issue(struct wined3d_cs *cs, const void *data)
2188 const struct wined3d_cs_query_issue *op = data;
2189 struct wined3d_query *query = op->query;
2190 BOOL poll;
2192 poll = query->query_ops->query_issue(query, op->flags);
2194 if (!cs->thread)
2195 return;
2197 if (poll && list_empty(&query->poll_list_entry))
2199 if (query->buffer_object)
2200 InterlockedIncrement(&query->counter_retrieved);
2201 else
2202 list_add_tail(&cs->query_poll_list, &query->poll_list_entry);
2203 return;
2206 /* This can happen if occlusion queries are restarted. This discards the
2207 * old result, since polling it could result in a GL error. */
2208 if ((op->flags & WINED3DISSUE_BEGIN) && !poll && !list_empty(&query->poll_list_entry))
2210 list_remove(&query->poll_list_entry);
2211 list_init(&query->poll_list_entry);
2212 InterlockedIncrement(&query->counter_retrieved);
2213 return;
2216 /* This can happen when an occlusion query is ended without being started,
2217 * in which case we don't want to poll, but still have to counter-balance
2218 * the increment of the main counter.
2220 * This can also happen if an event query is re-issued before the first
2221 * fence was reached. In this case the query is already in the list and
2222 * the poll function will check the new fence. We have to counter-balance
2223 * the discarded increment. */
2224 if (op->flags & WINED3DISSUE_END)
2225 InterlockedIncrement(&query->counter_retrieved);
2228 static void wined3d_cs_issue_query(struct wined3d_device_context *context,
2229 struct wined3d_query *query, unsigned int flags)
2231 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2232 struct wined3d_cs_query_issue *op;
2234 if (flags & WINED3DISSUE_END)
2235 ++query->counter_main;
2237 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2238 op->opcode = WINED3D_CS_OP_QUERY_ISSUE;
2239 op->query = query;
2240 op->flags = flags;
2242 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2243 cs->queries_flushed = FALSE;
2245 if (flags & WINED3DISSUE_BEGIN)
2246 query->state = QUERY_BUILDING;
2247 else
2248 query->state = QUERY_SIGNALLED;
2251 static void wined3d_cs_exec_preload_resource(struct wined3d_cs *cs, const void *data)
2253 const struct wined3d_cs_preload_resource *op = data;
2254 struct wined3d_resource *resource = op->resource;
2256 resource->resource_ops->resource_preload(resource);
2257 wined3d_resource_release(resource);
2260 void wined3d_cs_emit_preload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
2262 struct wined3d_cs_preload_resource *op;
2264 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2265 op->opcode = WINED3D_CS_OP_PRELOAD_RESOURCE;
2266 op->resource = resource;
2268 wined3d_resource_acquire(resource);
2270 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2273 static void wined3d_cs_exec_unload_resource(struct wined3d_cs *cs, const void *data)
2275 const struct wined3d_cs_unload_resource *op = data;
2276 struct wined3d_resource *resource = op->resource;
2278 resource->resource_ops->resource_unload(resource);
2279 wined3d_resource_release(resource);
2282 void wined3d_cs_emit_unload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
2284 struct wined3d_cs_unload_resource *op;
2286 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2287 op->opcode = WINED3D_CS_OP_UNLOAD_RESOURCE;
2288 op->resource = resource;
2290 wined3d_resource_acquire(resource);
2292 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2295 static void wined3d_cs_exec_map(struct wined3d_cs *cs, const void *data)
2297 const struct wined3d_cs_map *op = data;
2298 struct wined3d_resource *resource = op->resource;
2300 *op->hr = resource->resource_ops->resource_sub_resource_map(resource,
2301 op->sub_resource_idx, op->map_desc, op->box, op->flags);
2304 static HRESULT wined3d_cs_map(struct wined3d_device_context *context, struct wined3d_resource *resource,
2305 unsigned int sub_resource_idx, struct wined3d_map_desc *map_desc, const struct wined3d_box *box,
2306 unsigned int flags)
2308 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2309 struct wined3d_cs_map *op;
2310 HRESULT hr;
2312 /* Mapping resources from the worker thread isn't an issue by itself, but
2313 * increasing the map count would be visible to applications. */
2314 wined3d_not_from_cs(cs);
2316 wined3d_resource_wait_idle(resource);
2318 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2319 op->opcode = WINED3D_CS_OP_MAP;
2320 op->resource = resource;
2321 op->sub_resource_idx = sub_resource_idx;
2322 op->map_desc = map_desc;
2323 op->box = box;
2324 op->flags = flags;
2325 op->hr = &hr;
2327 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_MAP);
2328 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_MAP);
2330 return hr;
2333 static void wined3d_cs_exec_unmap(struct wined3d_cs *cs, const void *data)
2335 const struct wined3d_cs_unmap *op = data;
2336 struct wined3d_resource *resource = op->resource;
2338 *op->hr = resource->resource_ops->resource_sub_resource_unmap(resource, op->sub_resource_idx);
2341 static HRESULT wined3d_cs_unmap(struct wined3d_device_context *context, struct wined3d_resource *resource,
2342 unsigned int sub_resource_idx)
2344 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2345 struct wined3d_cs_unmap *op;
2346 HRESULT hr;
2348 wined3d_not_from_cs(cs);
2350 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2351 op->opcode = WINED3D_CS_OP_UNMAP;
2352 op->resource = resource;
2353 op->sub_resource_idx = sub_resource_idx;
2354 op->hr = &hr;
2356 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_MAP);
2357 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_MAP);
2359 return hr;
2362 static void wined3d_cs_exec_blt_sub_resource(struct wined3d_cs *cs, const void *data)
2364 const struct wined3d_cs_blt_sub_resource *op = data;
2366 if (op->dst_resource->type == WINED3D_RTYPE_BUFFER)
2368 wined3d_buffer_copy(buffer_from_resource(op->dst_resource), op->dst_box.left,
2369 buffer_from_resource(op->src_resource), op->src_box.left,
2370 op->src_box.right - op->src_box.left);
2372 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_3D)
2374 struct wined3d_texture *src_texture, *dst_texture;
2375 unsigned int level, update_w, update_h, update_d;
2376 unsigned int row_pitch, slice_pitch;
2377 struct wined3d_context *context;
2378 struct wined3d_bo_address addr;
2380 if (op->flags & ~WINED3D_BLT_RAW)
2382 FIXME("Flags %#x not implemented for %s resources.\n",
2383 op->flags, debug_d3dresourcetype(op->dst_resource->type));
2384 goto error;
2387 if (!(op->flags & WINED3D_BLT_RAW) && op->src_resource->format != op->dst_resource->format)
2389 FIXME("Format conversion not implemented for %s resources.\n",
2390 debug_d3dresourcetype(op->dst_resource->type));
2391 goto error;
2394 update_w = op->dst_box.right - op->dst_box.left;
2395 update_h = op->dst_box.bottom - op->dst_box.top;
2396 update_d = op->dst_box.back - op->dst_box.front;
2397 if (op->src_box.right - op->src_box.left != update_w
2398 || op->src_box.bottom - op->src_box.top != update_h
2399 || op->src_box.back - op->src_box.front != update_d)
2401 FIXME("Stretching not implemented for %s resources.\n",
2402 debug_d3dresourcetype(op->dst_resource->type));
2403 goto error;
2406 dst_texture = texture_from_resource(op->dst_resource);
2407 src_texture = texture_from_resource(op->src_resource);
2409 context = context_acquire(cs->c.device, NULL, 0);
2411 if (!wined3d_texture_load_location(src_texture, op->src_sub_resource_idx,
2412 context, src_texture->resource.map_binding))
2414 ERR("Failed to load source sub-resource into %s.\n",
2415 wined3d_debug_location(src_texture->resource.map_binding));
2416 context_release(context);
2417 goto error;
2420 level = op->dst_sub_resource_idx % dst_texture->level_count;
2421 if (update_w == wined3d_texture_get_level_width(dst_texture, level)
2422 && update_h == wined3d_texture_get_level_height(dst_texture, level)
2423 && update_d == wined3d_texture_get_level_depth(dst_texture, level))
2425 wined3d_texture_prepare_location(dst_texture, op->dst_sub_resource_idx,
2426 context, WINED3D_LOCATION_TEXTURE_RGB);
2428 else if (!wined3d_texture_load_location(dst_texture, op->dst_sub_resource_idx,
2429 context, WINED3D_LOCATION_TEXTURE_RGB))
2431 ERR("Failed to load destination sub-resource.\n");
2432 context_release(context);
2433 goto error;
2436 wined3d_texture_get_memory(src_texture, op->src_sub_resource_idx, &addr, src_texture->resource.map_binding);
2437 wined3d_texture_get_pitch(src_texture, op->src_sub_resource_idx % src_texture->level_count,
2438 &row_pitch, &slice_pitch);
2440 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&addr),
2441 dst_texture->resource.format, &op->src_box, row_pitch, slice_pitch, dst_texture,
2442 op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
2443 op->dst_box.left, op->dst_box.top, op->dst_box.front);
2444 wined3d_texture_validate_location(dst_texture, op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2445 wined3d_texture_invalidate_location(dst_texture, op->dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2447 context_release(context);
2449 else
2451 if (FAILED(texture2d_blt(texture_from_resource(op->dst_resource), op->dst_sub_resource_idx,
2452 &op->dst_box, texture_from_resource(op->src_resource), op->src_sub_resource_idx,
2453 &op->src_box, op->flags, &op->fx, op->filter)))
2454 FIXME("Blit failed.\n");
2457 error:
2458 if (op->src_resource)
2459 wined3d_resource_release(op->src_resource);
2460 wined3d_resource_release(op->dst_resource);
2463 void wined3d_device_context_emit_blt_sub_resource(struct wined3d_device_context *context,
2464 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, const struct wined3d_box *dst_box,
2465 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx, const struct wined3d_box *src_box,
2466 unsigned int flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2468 struct wined3d_cs_blt_sub_resource *op;
2470 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2471 op->opcode = WINED3D_CS_OP_BLT_SUB_RESOURCE;
2472 op->dst_resource = dst_resource;
2473 op->dst_sub_resource_idx = dst_sub_resource_idx;
2474 op->dst_box = *dst_box;
2475 op->src_resource = src_resource;
2476 op->src_sub_resource_idx = src_sub_resource_idx;
2477 op->src_box = *src_box;
2478 op->flags = flags;
2479 if (fx)
2480 op->fx = *fx;
2481 else
2482 memset(&op->fx, 0, sizeof(op->fx));
2483 op->filter = filter;
2485 wined3d_device_context_acquire_resource(context, dst_resource);
2486 if (src_resource)
2487 wined3d_device_context_acquire_resource(context, src_resource);
2489 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2490 if (flags & WINED3D_BLT_SYNCHRONOUS)
2491 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_DEFAULT);
2494 static void wined3d_cs_exec_update_sub_resource(struct wined3d_cs *cs, const void *data)
2496 const struct wined3d_cs_update_sub_resource *op = data;
2497 struct wined3d_resource *resource = op->resource;
2498 const struct wined3d_box *box = &op->box;
2499 unsigned int width, height, depth, level;
2500 struct wined3d_const_bo_address addr;
2501 struct wined3d_context *context;
2502 struct wined3d_texture *texture;
2503 struct wined3d_box src_box;
2505 context = context_acquire(cs->c.device, NULL, 0);
2507 if (resource->type == WINED3D_RTYPE_BUFFER)
2509 struct wined3d_buffer *buffer = buffer_from_resource(resource);
2511 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
2513 ERR("Failed to load buffer location.\n");
2514 goto done;
2517 wined3d_buffer_upload_data(buffer, context, box, op->data.data);
2518 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
2519 goto done;
2522 texture = wined3d_texture_from_resource(resource);
2524 level = op->sub_resource_idx % texture->level_count;
2525 width = wined3d_texture_get_level_width(texture, level);
2526 height = wined3d_texture_get_level_height(texture, level);
2527 depth = wined3d_texture_get_level_depth(texture, level);
2529 addr.buffer_object = 0;
2530 addr.addr = op->data.data;
2532 /* Only load the sub-resource for partial updates. */
2533 if (!box->left && !box->top && !box->front
2534 && box->right == width && box->bottom == height && box->back == depth)
2535 wined3d_texture_prepare_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2536 else
2537 wined3d_texture_load_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2539 wined3d_box_set(&src_box, 0, 0, box->right - box->left, box->bottom - box->top, 0, box->back - box->front);
2540 texture->texture_ops->texture_upload_data(context, &addr, texture->resource.format, &src_box,
2541 op->data.row_pitch, op->data.slice_pitch, texture, op->sub_resource_idx,
2542 WINED3D_LOCATION_TEXTURE_RGB, box->left, box->top, box->front);
2544 wined3d_texture_validate_location(texture, op->sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2545 wined3d_texture_invalidate_location(texture, op->sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2547 done:
2548 context_release(context);
2550 wined3d_resource_release(resource);
2553 static void wined3d_cs_update_sub_resource(struct wined3d_device_context *context,
2554 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
2555 const void *data, unsigned int row_pitch, unsigned int slice_pitch)
2557 struct wined3d_cs_update_sub_resource *op;
2559 wined3d_resource_wait_idle(resource);
2561 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2562 op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2563 op->resource = resource;
2564 op->sub_resource_idx = sub_resource_idx;
2565 op->box = *box;
2566 op->data.row_pitch = row_pitch;
2567 op->data.slice_pitch = slice_pitch;
2568 op->data.data = data;
2570 wined3d_device_context_acquire_resource(context, resource);
2572 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_MAP);
2573 /* The data pointer may go away, so we need to wait until it is read.
2574 * Copying the data may be faster if it's small. */
2575 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_MAP);
2578 static void wined3d_cs_exec_add_dirty_texture_region(struct wined3d_cs *cs, const void *data)
2580 const struct wined3d_cs_add_dirty_texture_region *op = data;
2581 struct wined3d_texture *texture = op->texture;
2582 unsigned int sub_resource_idx, i;
2583 struct wined3d_context *context;
2585 context = context_acquire(cs->c.device, NULL, 0);
2586 sub_resource_idx = op->layer * texture->level_count;
2587 for (i = 0; i < texture->level_count; ++i, ++sub_resource_idx)
2589 if (wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
2590 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2591 else
2592 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
2594 context_release(context);
2596 wined3d_resource_release(&texture->resource);
2599 void wined3d_cs_emit_add_dirty_texture_region(struct wined3d_cs *cs,
2600 struct wined3d_texture *texture, unsigned int layer)
2602 struct wined3d_cs_add_dirty_texture_region *op;
2604 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2605 op->opcode = WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION;
2606 op->texture = texture;
2607 op->layer = layer;
2609 wined3d_resource_acquire(&texture->resource);
2611 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2614 static void wined3d_cs_exec_clear_unordered_access_view(struct wined3d_cs *cs, const void *data)
2616 const struct wined3d_cs_clear_unordered_access_view *op = data;
2617 struct wined3d_unordered_access_view *view = op->view;
2618 struct wined3d_context *context;
2620 context = context_acquire(cs->c.device, NULL, 0);
2621 cs->c.device->adapter->adapter_ops->adapter_clear_uav(context, view, &op->clear_value);
2622 context_release(context);
2624 wined3d_resource_release(view->resource);
2627 void wined3d_device_context_emit_clear_uav_uint(struct wined3d_device_context *context,
2628 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
2630 struct wined3d_cs_clear_unordered_access_view *op;
2632 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2633 op->opcode = WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW;
2634 op->view = view;
2635 op->clear_value = *clear_value;
2637 wined3d_device_context_acquire_resource(context, view->resource);
2639 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2642 static void wined3d_cs_exec_copy_uav_counter(struct wined3d_cs *cs, const void *data)
2644 const struct wined3d_cs_copy_uav_counter *op = data;
2645 struct wined3d_unordered_access_view *view = op->view;
2646 struct wined3d_context *context;
2648 context = context_acquire(cs->c.device, NULL, 0);
2649 wined3d_unordered_access_view_copy_counter(view, op->buffer, op->offset, context);
2650 context_release(context);
2652 wined3d_resource_release(&op->buffer->resource);
2655 void wined3d_device_context_emit_copy_uav_counter(struct wined3d_device_context *context,
2656 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
2658 struct wined3d_cs_copy_uav_counter *op;
2660 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2661 op->opcode = WINED3D_CS_OP_COPY_UAV_COUNTER;
2662 op->buffer = dst_buffer;
2663 op->offset = offset;
2664 op->view = uav;
2666 wined3d_device_context_acquire_resource(context, &dst_buffer->resource);
2668 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2671 static void wined3d_cs_exec_generate_mipmaps(struct wined3d_cs *cs, const void *data)
2673 const struct wined3d_cs_generate_mipmaps *op = data;
2674 struct wined3d_shader_resource_view *view = op->view;
2675 struct wined3d_context *context;
2677 context = context_acquire(cs->c.device, NULL, 0);
2678 cs->c.device->adapter->adapter_ops->adapter_generate_mipmap(context, view);
2679 context_release(context);
2681 wined3d_resource_release(view->resource);
2684 void wined3d_device_context_emit_generate_mipmaps(struct wined3d_device_context *context,
2685 struct wined3d_shader_resource_view *view)
2687 struct wined3d_cs_generate_mipmaps *op;
2689 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2690 op->opcode = WINED3D_CS_OP_GENERATE_MIPMAPS;
2691 op->view = view;
2693 wined3d_device_context_acquire_resource(context, view->resource);
2695 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2698 static void wined3d_cs_emit_stop(struct wined3d_cs *cs)
2700 struct wined3d_cs_stop *op;
2702 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2703 op->opcode = WINED3D_CS_OP_STOP;
2705 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2706 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2709 static void wined3d_cs_acquire_resource(struct wined3d_device_context *context, struct wined3d_resource *resource)
2711 wined3d_resource_acquire(resource);
2714 static void (* const wined3d_cs_op_handlers[])(struct wined3d_cs *cs, const void *data) =
2716 /* WINED3D_CS_OP_NOP */ wined3d_cs_exec_nop,
2717 /* WINED3D_CS_OP_PRESENT */ wined3d_cs_exec_present,
2718 /* WINED3D_CS_OP_CLEAR */ wined3d_cs_exec_clear,
2719 /* WINED3D_CS_OP_DISPATCH */ wined3d_cs_exec_dispatch,
2720 /* WINED3D_CS_OP_DRAW */ wined3d_cs_exec_draw,
2721 /* WINED3D_CS_OP_FLUSH */ wined3d_cs_exec_flush,
2722 /* WINED3D_CS_OP_SET_PREDICATION */ wined3d_cs_exec_set_predication,
2723 /* WINED3D_CS_OP_SET_VIEWPORTS */ wined3d_cs_exec_set_viewports,
2724 /* WINED3D_CS_OP_SET_SCISSOR_RECTS */ wined3d_cs_exec_set_scissor_rects,
2725 /* WINED3D_CS_OP_SET_RENDERTARGET_VIEW */ wined3d_cs_exec_set_rendertarget_view,
2726 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW */ wined3d_cs_exec_set_depth_stencil_view,
2727 /* WINED3D_CS_OP_SET_VERTEX_DECLARATION */ wined3d_cs_exec_set_vertex_declaration,
2728 /* WINED3D_CS_OP_SET_STREAM_SOURCE */ wined3d_cs_exec_set_stream_source,
2729 /* WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ */ wined3d_cs_exec_set_stream_source_freq,
2730 /* WINED3D_CS_OP_SET_STREAM_OUTPUT */ wined3d_cs_exec_set_stream_output,
2731 /* WINED3D_CS_OP_SET_INDEX_BUFFER */ wined3d_cs_exec_set_index_buffer,
2732 /* WINED3D_CS_OP_SET_CONSTANT_BUFFER */ wined3d_cs_exec_set_constant_buffer,
2733 /* WINED3D_CS_OP_SET_TEXTURE */ wined3d_cs_exec_set_texture,
2734 /* WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW */ wined3d_cs_exec_set_shader_resource_view,
2735 /* WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_set_unordered_access_view,
2736 /* WINED3D_CS_OP_SET_SAMPLER */ wined3d_cs_exec_set_sampler,
2737 /* WINED3D_CS_OP_SET_SHADER */ wined3d_cs_exec_set_shader,
2738 /* WINED3D_CS_OP_SET_BLEND_STATE */ wined3d_cs_exec_set_blend_state,
2739 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE */ wined3d_cs_exec_set_depth_stencil_state,
2740 /* WINED3D_CS_OP_SET_RASTERIZER_STATE */ wined3d_cs_exec_set_rasterizer_state,
2741 /* WINED3D_CS_OP_SET_RENDER_STATE */ wined3d_cs_exec_set_render_state,
2742 /* WINED3D_CS_OP_SET_TEXTURE_STATE */ wined3d_cs_exec_set_texture_state,
2743 /* WINED3D_CS_OP_SET_SAMPLER_STATE */ wined3d_cs_exec_set_sampler_state,
2744 /* WINED3D_CS_OP_SET_TRANSFORM */ wined3d_cs_exec_set_transform,
2745 /* WINED3D_CS_OP_SET_CLIP_PLANE */ wined3d_cs_exec_set_clip_plane,
2746 /* WINED3D_CS_OP_SET_COLOR_KEY */ wined3d_cs_exec_set_color_key,
2747 /* WINED3D_CS_OP_SET_MATERIAL */ wined3d_cs_exec_set_material,
2748 /* WINED3D_CS_OP_SET_LIGHT */ wined3d_cs_exec_set_light,
2749 /* WINED3D_CS_OP_SET_LIGHT_ENABLE */ wined3d_cs_exec_set_light_enable,
2750 /* WINED3D_CS_OP_SET_FEATURE_LEVEL */ wined3d_cs_exec_set_feature_level,
2751 /* WINED3D_CS_OP_PUSH_CONSTANTS */ wined3d_cs_exec_push_constants,
2752 /* WINED3D_CS_OP_RESET_STATE */ wined3d_cs_exec_reset_state,
2753 /* WINED3D_CS_OP_CALLBACK */ wined3d_cs_exec_callback,
2754 /* WINED3D_CS_OP_QUERY_ISSUE */ wined3d_cs_exec_query_issue,
2755 /* WINED3D_CS_OP_PRELOAD_RESOURCE */ wined3d_cs_exec_preload_resource,
2756 /* WINED3D_CS_OP_UNLOAD_RESOURCE */ wined3d_cs_exec_unload_resource,
2757 /* WINED3D_CS_OP_MAP */ wined3d_cs_exec_map,
2758 /* WINED3D_CS_OP_UNMAP */ wined3d_cs_exec_unmap,
2759 /* WINED3D_CS_OP_BLT_SUB_RESOURCE */ wined3d_cs_exec_blt_sub_resource,
2760 /* WINED3D_CS_OP_UPDATE_SUB_RESOURCE */ wined3d_cs_exec_update_sub_resource,
2761 /* WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION */ wined3d_cs_exec_add_dirty_texture_region,
2762 /* WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_clear_unordered_access_view,
2763 /* WINED3D_CS_OP_COPY_UAV_COUNTER */ wined3d_cs_exec_copy_uav_counter,
2764 /* WINED3D_CS_OP_GENERATE_MIPMAPS */ wined3d_cs_exec_generate_mipmaps,
2767 static void *wined3d_cs_st_require_space(struct wined3d_device_context *context,
2768 size_t size, enum wined3d_cs_queue_id queue_id)
2770 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2772 if (size > (cs->data_size - cs->end))
2774 size_t new_size;
2775 void *new_data;
2777 new_size = max(size, cs->data_size * 2);
2778 if (!cs->end)
2779 new_data = heap_realloc(cs->data, new_size);
2780 else
2781 new_data = heap_alloc(new_size);
2782 if (!new_data)
2783 return NULL;
2785 cs->data_size = new_size;
2786 cs->start = cs->end = 0;
2787 cs->data = new_data;
2790 cs->end += size;
2792 return (BYTE *)cs->data + cs->start;
2795 static void wined3d_cs_st_submit(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
2797 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2798 enum wined3d_cs_op opcode;
2799 size_t start;
2800 BYTE *data;
2802 data = cs->data;
2803 start = cs->start;
2804 cs->start = cs->end;
2806 opcode = *(const enum wined3d_cs_op *)&data[start];
2807 if (opcode >= WINED3D_CS_OP_STOP)
2808 ERR("Invalid opcode %#x.\n", opcode);
2809 else
2810 wined3d_cs_op_handlers[opcode](cs, &data[start]);
2812 if (cs->data == data)
2813 cs->start = cs->end = start;
2814 else if (!start)
2815 heap_free(data);
2818 static void wined3d_cs_st_finish(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
2822 static const struct wined3d_device_context_ops wined3d_cs_st_ops =
2824 wined3d_cs_st_require_space,
2825 wined3d_cs_st_submit,
2826 wined3d_cs_st_finish,
2827 wined3d_cs_st_push_constants,
2828 wined3d_cs_map,
2829 wined3d_cs_unmap,
2830 wined3d_cs_update_sub_resource,
2831 wined3d_cs_issue_query,
2832 wined3d_cs_flush,
2833 wined3d_cs_acquire_resource,
2836 static BOOL wined3d_cs_queue_is_empty(const struct wined3d_cs *cs, const struct wined3d_cs_queue *queue)
2838 wined3d_from_cs(cs);
2839 return *(volatile LONG *)&queue->head == queue->tail;
2842 static void wined3d_cs_queue_submit(struct wined3d_cs_queue *queue, struct wined3d_cs *cs)
2844 struct wined3d_cs_packet *packet;
2845 size_t packet_size;
2847 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2848 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2849 InterlockedExchange(&queue->head, (queue->head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1));
2851 if (InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2852 SetEvent(cs->event);
2855 static void wined3d_cs_mt_submit(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
2857 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2859 if (cs->thread_id == GetCurrentThreadId())
2860 return wined3d_cs_st_submit(context, queue_id);
2862 wined3d_cs_queue_submit(&cs->queue[queue_id], cs);
2865 static void *wined3d_cs_queue_require_space(struct wined3d_cs_queue *queue, size_t size, struct wined3d_cs *cs)
2867 size_t queue_size = ARRAY_SIZE(queue->data);
2868 size_t header_size, packet_size, remaining;
2869 struct wined3d_cs_packet *packet;
2871 header_size = FIELD_OFFSET(struct wined3d_cs_packet, data[0]);
2872 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[size]);
2873 packet_size = (packet_size + header_size - 1) & ~(header_size - 1);
2874 size = packet_size - header_size;
2875 if (packet_size >= WINED3D_CS_QUEUE_SIZE)
2877 ERR("Packet size %lu >= queue size %u.\n",
2878 (unsigned long)packet_size, WINED3D_CS_QUEUE_SIZE);
2879 return NULL;
2882 remaining = queue_size - queue->head;
2883 if (remaining < packet_size)
2885 size_t nop_size = remaining - header_size;
2886 struct wined3d_cs_nop *nop;
2888 TRACE("Inserting a nop for %lu + %lu bytes.\n",
2889 (unsigned long)header_size, (unsigned long)nop_size);
2891 nop = wined3d_cs_queue_require_space(queue, nop_size, cs);
2892 if (nop_size)
2893 nop->opcode = WINED3D_CS_OP_NOP;
2895 wined3d_cs_queue_submit(queue, cs);
2896 assert(!queue->head);
2899 for (;;)
2901 LONG tail = *(volatile LONG *)&queue->tail;
2902 LONG head = queue->head;
2903 LONG new_pos;
2905 /* Empty. */
2906 if (head == tail)
2907 break;
2908 new_pos = (head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1);
2909 /* Head ahead of tail. We checked the remaining size above, so we only
2910 * need to make sure we don't make head equal to tail. */
2911 if (head > tail && (new_pos != tail))
2912 break;
2913 /* Tail ahead of head. Make sure the new head is before the tail as
2914 * well. Note that new_pos is 0 when it's at the end of the queue. */
2915 if (new_pos < tail && new_pos)
2916 break;
2918 TRACE("Waiting for free space. Head %u, tail %u, packet size %lu.\n",
2919 head, tail, (unsigned long)packet_size);
2922 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2923 packet->size = size;
2924 return packet->data;
2927 static void *wined3d_cs_mt_require_space(struct wined3d_device_context *context,
2928 size_t size, enum wined3d_cs_queue_id queue_id)
2930 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2932 if (cs->thread_id == GetCurrentThreadId())
2933 return wined3d_cs_st_require_space(context, size, queue_id);
2935 return wined3d_cs_queue_require_space(&cs->queue[queue_id], size, cs);
2938 static void wined3d_cs_mt_finish(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
2940 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2942 if (cs->thread_id == GetCurrentThreadId())
2943 return wined3d_cs_st_finish(context, queue_id);
2945 while (cs->queue[queue_id].head != *(volatile LONG *)&cs->queue[queue_id].tail)
2946 YieldProcessor();
2949 static const struct wined3d_device_context_ops wined3d_cs_mt_ops =
2951 wined3d_cs_mt_require_space,
2952 wined3d_cs_mt_submit,
2953 wined3d_cs_mt_finish,
2954 wined3d_cs_mt_push_constants,
2955 wined3d_cs_map,
2956 wined3d_cs_unmap,
2957 wined3d_cs_update_sub_resource,
2958 wined3d_cs_issue_query,
2959 wined3d_cs_flush,
2960 wined3d_cs_acquire_resource,
2963 static void poll_queries(struct wined3d_cs *cs)
2965 struct wined3d_query *query, *cursor;
2967 LIST_FOR_EACH_ENTRY_SAFE(query, cursor, &cs->query_poll_list, struct wined3d_query, poll_list_entry)
2969 if (!query->query_ops->query_poll(query, 0))
2970 continue;
2972 list_remove(&query->poll_list_entry);
2973 list_init(&query->poll_list_entry);
2974 InterlockedIncrement(&query->counter_retrieved);
2978 static void wined3d_cs_wait_event(struct wined3d_cs *cs)
2980 InterlockedExchange(&cs->waiting_for_event, TRUE);
2982 /* The main thread might have enqueued a command and blocked on it after
2983 * the CS thread decided to enter wined3d_cs_wait_event(), but before
2984 * "waiting_for_event" was set.
2986 * Likewise, we can race with the main thread when resetting
2987 * "waiting_for_event", in which case we would need to call
2988 * WaitForSingleObject() because the main thread called SetEvent(). */
2989 if (!(wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_DEFAULT])
2990 && wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_MAP]))
2991 && InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2992 return;
2994 WaitForSingleObject(cs->event, INFINITE);
2997 static void wined3d_cs_command_lock(const struct wined3d_cs *cs)
2999 if (cs->serialize_commands)
3000 EnterCriticalSection(&wined3d_command_cs);
3003 static void wined3d_cs_command_unlock(const struct wined3d_cs *cs)
3005 if (cs->serialize_commands)
3006 LeaveCriticalSection(&wined3d_command_cs);
3009 static DWORD WINAPI wined3d_cs_run(void *ctx)
3011 struct wined3d_cs_packet *packet;
3012 struct wined3d_cs_queue *queue;
3013 unsigned int spin_count = 0;
3014 struct wined3d_cs *cs = ctx;
3015 enum wined3d_cs_op opcode;
3016 HMODULE wined3d_module;
3017 unsigned int poll = 0;
3018 LONG tail;
3020 TRACE("Started.\n");
3022 /* Copy the module handle to a local variable to avoid racing with the
3023 * thread freeing "cs" before the FreeLibraryAndExitThread() call. */
3024 wined3d_module = cs->wined3d_module;
3026 list_init(&cs->query_poll_list);
3027 cs->thread_id = GetCurrentThreadId();
3028 for (;;)
3030 if (++poll == WINED3D_CS_QUERY_POLL_INTERVAL)
3032 wined3d_cs_command_lock(cs);
3033 poll_queries(cs);
3034 wined3d_cs_command_unlock(cs);
3035 poll = 0;
3038 queue = &cs->queue[WINED3D_CS_QUEUE_MAP];
3039 if (wined3d_cs_queue_is_empty(cs, queue))
3041 queue = &cs->queue[WINED3D_CS_QUEUE_DEFAULT];
3042 if (wined3d_cs_queue_is_empty(cs, queue))
3044 if (++spin_count >= WINED3D_CS_SPIN_COUNT && list_empty(&cs->query_poll_list))
3045 wined3d_cs_wait_event(cs);
3046 continue;
3049 spin_count = 0;
3051 tail = queue->tail;
3052 packet = (struct wined3d_cs_packet *)&queue->data[tail];
3053 if (packet->size)
3055 opcode = *(const enum wined3d_cs_op *)packet->data;
3057 TRACE("Executing %s.\n", debug_cs_op(opcode));
3058 if (opcode >= WINED3D_CS_OP_STOP)
3060 if (opcode > WINED3D_CS_OP_STOP)
3061 ERR("Invalid opcode %#x.\n", opcode);
3062 break;
3065 wined3d_cs_command_lock(cs);
3066 wined3d_cs_op_handlers[opcode](cs, packet->data);
3067 wined3d_cs_command_unlock(cs);
3068 TRACE("%s executed.\n", debug_cs_op(opcode));
3071 tail += FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
3072 tail &= (WINED3D_CS_QUEUE_SIZE - 1);
3073 InterlockedExchange(&queue->tail, tail);
3076 cs->queue[WINED3D_CS_QUEUE_MAP].tail = cs->queue[WINED3D_CS_QUEUE_MAP].head;
3077 cs->queue[WINED3D_CS_QUEUE_DEFAULT].tail = cs->queue[WINED3D_CS_QUEUE_DEFAULT].head;
3078 TRACE("Stopped.\n");
3079 FreeLibraryAndExitThread(wined3d_module, 0);
3082 struct wined3d_cs *wined3d_cs_create(struct wined3d_device *device,
3083 const enum wined3d_feature_level *levels, unsigned int level_count)
3085 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3086 struct wined3d_cs *cs;
3088 if (!(cs = heap_alloc_zero(sizeof(*cs))))
3089 return NULL;
3091 if (FAILED(wined3d_state_create(device, levels, level_count, &cs->c.state)))
3093 heap_free(cs);
3094 return NULL;
3097 cs->c.ops = &wined3d_cs_st_ops;
3098 cs->c.device = device;
3099 cs->serialize_commands = TRACE_ON(d3d_sync) || wined3d_settings.cs_multithreaded & WINED3D_CSMT_SERIALIZE;
3101 state_init(&cs->state, d3d_info, WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT, cs->c.state->feature_level);
3103 cs->data_size = WINED3D_INITIAL_CS_SIZE;
3104 if (!(cs->data = heap_alloc(cs->data_size)))
3105 goto fail;
3107 if (wined3d_settings.cs_multithreaded & WINED3D_CSMT_ENABLE
3108 && !RtlIsCriticalSectionLockedByThread(NtCurrentTeb()->Peb->LoaderLock))
3110 cs->c.ops = &wined3d_cs_mt_ops;
3112 if (!(cs->event = CreateEventW(NULL, FALSE, FALSE, NULL)))
3114 ERR("Failed to create command stream event.\n");
3115 heap_free(cs->data);
3116 goto fail;
3119 if (!(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
3120 (const WCHAR *)wined3d_cs_run, &cs->wined3d_module)))
3122 ERR("Failed to get wined3d module handle.\n");
3123 CloseHandle(cs->event);
3124 heap_free(cs->data);
3125 goto fail;
3128 if (!(cs->thread = CreateThread(NULL, 0, wined3d_cs_run, cs, 0, NULL)))
3130 ERR("Failed to create wined3d command stream thread.\n");
3131 FreeLibrary(cs->wined3d_module);
3132 CloseHandle(cs->event);
3133 heap_free(cs->data);
3134 goto fail;
3138 return cs;
3140 fail:
3141 wined3d_state_destroy(cs->c.state);
3142 state_cleanup(&cs->state);
3143 heap_free(cs);
3144 return NULL;
3147 void wined3d_cs_destroy(struct wined3d_cs *cs)
3149 if (cs->thread)
3151 wined3d_cs_emit_stop(cs);
3152 CloseHandle(cs->thread);
3153 if (!CloseHandle(cs->event))
3154 ERR("Closing event failed.\n");
3157 wined3d_state_destroy(cs->c.state);
3158 state_cleanup(&cs->state);
3159 heap_free(cs->data);
3160 heap_free(cs);