rasapi32: Update spec file.
[wine/zf.git] / dlls / wined3d / cs.c
blob32ea900e27d8de98238165f30a9490ae8835437c
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 const char *debug_cs_op(enum wined3d_cs_op op)
485 switch (op)
487 #define WINED3D_TO_STR(type) case type: return #type
488 WINED3D_TO_STR(WINED3D_CS_OP_NOP);
489 WINED3D_TO_STR(WINED3D_CS_OP_PRESENT);
490 WINED3D_TO_STR(WINED3D_CS_OP_CLEAR);
491 WINED3D_TO_STR(WINED3D_CS_OP_DISPATCH);
492 WINED3D_TO_STR(WINED3D_CS_OP_DRAW);
493 WINED3D_TO_STR(WINED3D_CS_OP_FLUSH);
494 WINED3D_TO_STR(WINED3D_CS_OP_SET_PREDICATION);
495 WINED3D_TO_STR(WINED3D_CS_OP_SET_VIEWPORTS);
496 WINED3D_TO_STR(WINED3D_CS_OP_SET_SCISSOR_RECTS);
497 WINED3D_TO_STR(WINED3D_CS_OP_SET_RENDERTARGET_VIEW);
498 WINED3D_TO_STR(WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW);
499 WINED3D_TO_STR(WINED3D_CS_OP_SET_VERTEX_DECLARATION);
500 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_SOURCE);
501 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ);
502 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_OUTPUT);
503 WINED3D_TO_STR(WINED3D_CS_OP_SET_INDEX_BUFFER);
504 WINED3D_TO_STR(WINED3D_CS_OP_SET_CONSTANT_BUFFER);
505 WINED3D_TO_STR(WINED3D_CS_OP_SET_TEXTURE);
506 WINED3D_TO_STR(WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW);
507 WINED3D_TO_STR(WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW);
508 WINED3D_TO_STR(WINED3D_CS_OP_SET_SAMPLER);
509 WINED3D_TO_STR(WINED3D_CS_OP_SET_SHADER);
510 WINED3D_TO_STR(WINED3D_CS_OP_SET_BLEND_STATE);
511 WINED3D_TO_STR(WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE);
512 WINED3D_TO_STR(WINED3D_CS_OP_SET_RASTERIZER_STATE);
513 WINED3D_TO_STR(WINED3D_CS_OP_SET_RENDER_STATE);
514 WINED3D_TO_STR(WINED3D_CS_OP_SET_TEXTURE_STATE);
515 WINED3D_TO_STR(WINED3D_CS_OP_SET_SAMPLER_STATE);
516 WINED3D_TO_STR(WINED3D_CS_OP_SET_TRANSFORM);
517 WINED3D_TO_STR(WINED3D_CS_OP_SET_CLIP_PLANE);
518 WINED3D_TO_STR(WINED3D_CS_OP_SET_COLOR_KEY);
519 WINED3D_TO_STR(WINED3D_CS_OP_SET_MATERIAL);
520 WINED3D_TO_STR(WINED3D_CS_OP_SET_LIGHT);
521 WINED3D_TO_STR(WINED3D_CS_OP_SET_LIGHT_ENABLE);
522 WINED3D_TO_STR(WINED3D_CS_OP_SET_FEATURE_LEVEL);
523 WINED3D_TO_STR(WINED3D_CS_OP_PUSH_CONSTANTS);
524 WINED3D_TO_STR(WINED3D_CS_OP_RESET_STATE);
525 WINED3D_TO_STR(WINED3D_CS_OP_CALLBACK);
526 WINED3D_TO_STR(WINED3D_CS_OP_QUERY_ISSUE);
527 WINED3D_TO_STR(WINED3D_CS_OP_PRELOAD_RESOURCE);
528 WINED3D_TO_STR(WINED3D_CS_OP_UNLOAD_RESOURCE);
529 WINED3D_TO_STR(WINED3D_CS_OP_MAP);
530 WINED3D_TO_STR(WINED3D_CS_OP_UNMAP);
531 WINED3D_TO_STR(WINED3D_CS_OP_BLT_SUB_RESOURCE);
532 WINED3D_TO_STR(WINED3D_CS_OP_UPDATE_SUB_RESOURCE);
533 WINED3D_TO_STR(WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION);
534 WINED3D_TO_STR(WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW);
535 WINED3D_TO_STR(WINED3D_CS_OP_COPY_UAV_COUNTER);
536 WINED3D_TO_STR(WINED3D_CS_OP_GENERATE_MIPMAPS);
537 WINED3D_TO_STR(WINED3D_CS_OP_STOP);
538 #undef WINED3D_TO_STR
540 return wine_dbg_sprintf("UNKNOWN_OP(%#x)", op);
543 static void wined3d_cs_exec_nop(struct wined3d_cs *cs, const void *data)
547 static void wined3d_cs_exec_present(struct wined3d_cs *cs, const void *data)
549 struct wined3d_texture *logo_texture, *cursor_texture, *back_buffer;
550 struct wined3d_rendertarget_view *dsv = cs->state.fb.depth_stencil;
551 const struct wined3d_cs_present *op = data;
552 const struct wined3d_swapchain_desc *desc;
553 struct wined3d_swapchain *swapchain;
554 unsigned int i;
556 swapchain = op->swapchain;
557 desc = &swapchain->state.desc;
558 back_buffer = swapchain->back_buffers[0];
559 wined3d_swapchain_set_window(swapchain, op->dst_window_override);
561 if ((logo_texture = swapchain->device->logo_texture))
563 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
565 /* Blit the logo into the upper left corner of the back-buffer. */
566 wined3d_device_context_blt(&cs->c, back_buffer, 0, &rect, logo_texture, 0,
567 &rect, WINED3D_BLT_SRC_CKEY, NULL, WINED3D_TEXF_POINT);
570 if ((cursor_texture = swapchain->device->cursor_texture)
571 && swapchain->device->bCursorVisible && !swapchain->device->hardwareCursor)
573 RECT dst_rect =
575 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
576 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
577 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
578 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
580 RECT src_rect =
582 0, 0, cursor_texture->resource.width, cursor_texture->resource.height
584 const RECT clip_rect = {0, 0, back_buffer->resource.width, back_buffer->resource.height};
586 TRACE("Rendering the software cursor.\n");
588 if (desc->windowed)
589 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
590 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
591 wined3d_device_context_blt(&cs->c, back_buffer, 0, &dst_rect, cursor_texture, 0,
592 &src_rect, WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
595 swapchain->swapchain_ops->swapchain_present(swapchain, &op->src_rect, &op->dst_rect, op->swap_interval, op->flags);
597 /* Discard buffers if the swap effect allows it. */
598 back_buffer = swapchain->back_buffers[desc->backbuffer_count - 1];
599 if (desc->swap_effect == WINED3D_SWAP_EFFECT_DISCARD || desc->swap_effect == WINED3D_SWAP_EFFECT_FLIP_DISCARD)
600 wined3d_texture_validate_location(back_buffer, 0, WINED3D_LOCATION_DISCARDED);
602 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
604 struct wined3d_texture *ds = texture_from_resource(dsv->resource);
606 if ((desc->flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL || ds->flags & WINED3D_TEXTURE_DISCARD))
607 wined3d_rendertarget_view_validate_location(dsv, WINED3D_LOCATION_DISCARDED);
610 if (TRACE_ON(fps))
612 DWORD time = GetTickCount();
613 ++swapchain->frames;
615 /* every 1.5 seconds */
616 if (time - swapchain->prev_time > 1500)
618 TRACE_(fps)("%p @ approx %.2ffps\n",
619 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
620 swapchain->prev_time = time;
621 swapchain->frames = 0;
625 wined3d_resource_release(&swapchain->front_buffer->resource);
626 for (i = 0; i < desc->backbuffer_count; ++i)
628 wined3d_resource_release(&swapchain->back_buffers[i]->resource);
631 InterlockedDecrement(&cs->pending_presents);
634 void wined3d_cs_emit_present(struct wined3d_cs *cs, struct wined3d_swapchain *swapchain,
635 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
636 unsigned int swap_interval, DWORD flags)
638 struct wined3d_cs_present *op;
639 unsigned int i;
640 LONG pending;
642 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
643 op->opcode = WINED3D_CS_OP_PRESENT;
644 op->dst_window_override = dst_window_override;
645 op->swapchain = swapchain;
646 op->src_rect = *src_rect;
647 op->dst_rect = *dst_rect;
648 op->swap_interval = swap_interval;
649 op->flags = flags;
651 pending = InterlockedIncrement(&cs->pending_presents);
653 wined3d_resource_acquire(&swapchain->front_buffer->resource);
654 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
656 wined3d_resource_acquire(&swapchain->back_buffers[i]->resource);
659 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
661 /* Limit input latency by limiting the number of presents that we can get
662 * ahead of the worker thread. */
663 while (pending >= swapchain->max_frame_latency)
665 YieldProcessor();
666 pending = InterlockedCompareExchange(&cs->pending_presents, 0, 0);
670 static void wined3d_cs_exec_clear(struct wined3d_cs *cs, const void *data)
672 struct wined3d_device *device = cs->c.device;
673 const struct wined3d_cs_clear *op = data;
674 unsigned int i;
676 device->blitter->ops->blitter_clear(device->blitter, device, op->rt_count, op->fb,
677 op->rect_count, op->rects, &op->draw_rect, op->flags, &op->color, op->depth, op->stencil);
679 if (op->flags & WINED3DCLEAR_TARGET)
681 for (i = 0; i < op->rt_count; ++i)
683 if (op->fb->render_targets[i])
684 wined3d_resource_release(op->fb->render_targets[i]->resource);
687 if (op->flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
688 wined3d_resource_release(op->fb->depth_stencil->resource);
691 void wined3d_cs_emit_clear(struct wined3d_cs *cs, DWORD rect_count, const RECT *rects,
692 DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
694 const struct wined3d_state *state = cs->c.state;
695 const struct wined3d_viewport *vp = &state->viewports[0];
696 struct wined3d_rendertarget_view *view;
697 struct wined3d_cs_clear *op;
698 unsigned int rt_count, i;
700 rt_count = flags & WINED3DCLEAR_TARGET ? cs->c.device->adapter->d3d_info.limits.max_rt_count : 0;
702 op = wined3d_device_context_require_space(&cs->c, FIELD_OFFSET(struct wined3d_cs_clear, rects[rect_count]),
703 WINED3D_CS_QUEUE_DEFAULT);
704 op->opcode = WINED3D_CS_OP_CLEAR;
705 op->flags = flags & (WINED3DCLEAR_TARGET | WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
706 op->rt_count = rt_count;
707 op->fb = &cs->state.fb;
708 SetRect(&op->draw_rect, vp->x, vp->y, vp->x + vp->width, vp->y + vp->height);
709 if (state->rasterizer_state && state->rasterizer_state->desc.scissor)
710 IntersectRect(&op->draw_rect, &op->draw_rect, &state->scissor_rects[0]);
711 op->color = *color;
712 op->depth = depth;
713 op->stencil = stencil;
714 op->rect_count = rect_count;
715 memcpy(op->rects, rects, sizeof(*rects) * rect_count);
717 for (i = 0; i < rt_count; ++i)
719 if ((view = state->fb.render_targets[i]))
720 wined3d_resource_acquire(view->resource);
722 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
724 view = state->fb.depth_stencil;
725 wined3d_resource_acquire(view->resource);
728 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
731 void wined3d_device_context_emit_clear_rendertarget_view(struct wined3d_device_context *context,
732 struct wined3d_rendertarget_view *view, const RECT *rect, unsigned int flags,
733 const struct wined3d_color *color, float depth, unsigned int stencil)
735 struct wined3d_cs_clear *op;
736 size_t size;
738 size = FIELD_OFFSET(struct wined3d_cs_clear, rects[1]) + sizeof(struct wined3d_fb_state);
739 op = wined3d_device_context_require_space(context, size, WINED3D_CS_QUEUE_DEFAULT);
740 op->fb = (void *)&op->rects[1];
742 op->opcode = WINED3D_CS_OP_CLEAR;
743 op->flags = flags & (WINED3DCLEAR_TARGET | WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
744 if (flags & WINED3DCLEAR_TARGET)
746 op->rt_count = 1;
747 op->fb->render_targets[0] = view;
748 op->fb->depth_stencil = NULL;
749 op->color = *color;
751 else
753 op->rt_count = 0;
754 op->fb->render_targets[0] = NULL;
755 op->fb->depth_stencil = view;
756 op->depth = depth;
757 op->stencil = stencil;
759 SetRect(&op->draw_rect, 0, 0, view->width, view->height);
760 op->rect_count = 1;
761 op->rects[0] = *rect;
763 wined3d_resource_acquire(view->resource);
765 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
766 if (flags & WINED3DCLEAR_SYNCHRONOUS)
767 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_DEFAULT);
770 static void acquire_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
772 struct wined3d_shader_sampler_map_entry *entry;
773 struct wined3d_shader_resource_view *view;
774 struct wined3d_shader *shader;
775 unsigned int i, j;
777 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
779 if (!(shader_mask & (1u << i)))
780 continue;
782 if (!(shader = state->shader[i]))
783 continue;
785 for (j = 0; j < WINED3D_MAX_CBS; ++j)
787 if (state->cb[i][j])
788 wined3d_resource_acquire(&state->cb[i][j]->resource);
791 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
793 entry = &shader->reg_maps.sampler_map.entries[j];
795 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
796 continue;
798 wined3d_resource_acquire(view->resource);
803 static void release_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
805 struct wined3d_shader_sampler_map_entry *entry;
806 struct wined3d_shader_resource_view *view;
807 struct wined3d_shader *shader;
808 unsigned int i, j;
810 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
812 if (!(shader_mask & (1u << i)))
813 continue;
815 if (!(shader = state->shader[i]))
816 continue;
818 for (j = 0; j < WINED3D_MAX_CBS; ++j)
820 if (state->cb[i][j])
821 wined3d_resource_release(&state->cb[i][j]->resource);
824 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
826 entry = &shader->reg_maps.sampler_map.entries[j];
828 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
829 continue;
831 wined3d_resource_release(view->resource);
836 static void acquire_unordered_access_resources(const struct wined3d_shader *shader,
837 struct wined3d_unordered_access_view * const *views)
839 unsigned int i;
841 if (!shader)
842 return;
844 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
846 if (!shader->reg_maps.uav_resource_info[i].type)
847 continue;
849 if (!views[i])
850 continue;
852 wined3d_resource_acquire(views[i]->resource);
856 static void release_unordered_access_resources(const struct wined3d_shader *shader,
857 struct wined3d_unordered_access_view * const *views)
859 unsigned int i;
861 if (!shader)
862 return;
864 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
866 if (!shader->reg_maps.uav_resource_info[i].type)
867 continue;
869 if (!views[i])
870 continue;
872 wined3d_resource_release(views[i]->resource);
876 static void wined3d_cs_exec_dispatch(struct wined3d_cs *cs, const void *data)
878 const struct wined3d_cs_dispatch *op = data;
879 struct wined3d_state *state = &cs->state;
881 if (!state->shader[WINED3D_SHADER_TYPE_COMPUTE])
882 WARN("No compute shader bound, skipping dispatch.\n");
883 else
884 cs->c.device->adapter->adapter_ops->adapter_dispatch_compute(cs->c.device, state, &op->parameters);
886 if (op->parameters.indirect)
887 wined3d_resource_release(&op->parameters.u.indirect.buffer->resource);
889 release_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
890 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
891 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
894 static void acquire_compute_pipeline_resources(const struct wined3d_state *state)
896 acquire_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
897 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
898 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
901 void CDECL wined3d_device_context_dispatch(struct wined3d_device_context *context,
902 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
904 struct wined3d_cs_dispatch *op;
906 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
907 op->opcode = WINED3D_CS_OP_DISPATCH;
908 op->parameters.indirect = FALSE;
909 op->parameters.u.direct.group_count_x = group_count_x;
910 op->parameters.u.direct.group_count_y = group_count_y;
911 op->parameters.u.direct.group_count_z = group_count_z;
913 acquire_compute_pipeline_resources(context->state);
915 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
918 void CDECL wined3d_device_context_dispatch_indirect(struct wined3d_device_context *context,
919 struct wined3d_buffer *buffer, unsigned int offset)
921 struct wined3d_cs_dispatch *op;
923 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
924 op->opcode = WINED3D_CS_OP_DISPATCH;
925 op->parameters.indirect = TRUE;
926 op->parameters.u.indirect.buffer = buffer;
927 op->parameters.u.indirect.offset = offset;
929 acquire_compute_pipeline_resources(context->state);
930 wined3d_resource_acquire(&buffer->resource);
932 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
935 static void wined3d_cs_exec_draw(struct wined3d_cs *cs, const void *data)
937 const struct wined3d_d3d_info *d3d_info = &cs->c.device->adapter->d3d_info;
938 const struct wined3d_shader *geometry_shader;
939 struct wined3d_device *device = cs->c.device;
940 int base_vertex_idx, load_base_vertex_idx;
941 struct wined3d_state *state = &cs->state;
942 const struct wined3d_cs_draw *op = data;
943 unsigned int i;
945 base_vertex_idx = 0;
946 if (!op->parameters.indirect)
948 const struct wined3d_direct_draw_parameters *direct = &op->parameters.u.direct;
950 if (op->parameters.indexed && d3d_info->draw_base_vertex_offset)
951 base_vertex_idx = direct->base_vertex_idx;
952 else if (!op->parameters.indexed)
953 base_vertex_idx = direct->start_idx;
956 /* ARB_draw_indirect always supports a base vertex offset. */
957 if (!op->parameters.indirect && !d3d_info->draw_base_vertex_offset)
958 load_base_vertex_idx = op->parameters.u.direct.base_vertex_idx;
959 else
960 load_base_vertex_idx = 0;
962 if (state->base_vertex_index != base_vertex_idx)
964 state->base_vertex_index = base_vertex_idx;
965 for (i = 0; i < device->context_count; ++i)
966 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_BASE_VERTEX_ID;
969 if (state->load_base_vertex_index != load_base_vertex_idx)
971 state->load_base_vertex_index = load_base_vertex_idx;
972 device_invalidate_state(cs->c.device, STATE_BASEVERTEXINDEX);
975 if (state->primitive_type != op->primitive_type)
977 if ((geometry_shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]) && !geometry_shader->function)
978 device_invalidate_state(cs->c.device, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY));
979 if (state->primitive_type == WINED3D_PT_POINTLIST || op->primitive_type == WINED3D_PT_POINTLIST)
980 device_invalidate_state(cs->c.device, STATE_POINT_ENABLE);
981 state->primitive_type = op->primitive_type;
983 state->patch_vertex_count = op->patch_vertex_count;
985 cs->c.device->adapter->adapter_ops->adapter_draw_primitive(cs->c.device, state, &op->parameters);
987 if (op->parameters.indirect)
989 struct wined3d_buffer *buffer = op->parameters.u.indirect.buffer;
990 wined3d_resource_release(&buffer->resource);
993 if (op->parameters.indexed)
994 wined3d_resource_release(&state->index_buffer->resource);
995 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
997 if (state->streams[i].buffer)
998 wined3d_resource_release(&state->streams[i].buffer->resource);
1000 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
1002 if (state->stream_output[i].buffer)
1003 wined3d_resource_release(&state->stream_output[i].buffer->resource);
1005 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
1007 if (state->textures[i])
1008 wined3d_resource_release(&state->textures[i]->resource);
1010 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
1012 if (state->fb.render_targets[i])
1013 wined3d_resource_release(state->fb.render_targets[i]->resource);
1015 if (state->fb.depth_stencil)
1016 wined3d_resource_release(state->fb.depth_stencil->resource);
1017 release_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
1018 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
1019 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
1022 static void acquire_graphics_pipeline_resources(const struct wined3d_state *state,
1023 BOOL indexed, const struct wined3d_d3d_info *d3d_info)
1025 unsigned int i;
1027 if (indexed)
1028 wined3d_resource_acquire(&state->index_buffer->resource);
1029 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
1031 if (state->streams[i].buffer)
1032 wined3d_resource_acquire(&state->streams[i].buffer->resource);
1034 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
1036 if (state->stream_output[i].buffer)
1037 wined3d_resource_acquire(&state->stream_output[i].buffer->resource);
1039 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
1041 if (state->textures[i])
1042 wined3d_resource_acquire(&state->textures[i]->resource);
1044 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
1046 if (state->fb.render_targets[i])
1047 wined3d_resource_acquire(state->fb.render_targets[i]->resource);
1049 if (state->fb.depth_stencil)
1050 wined3d_resource_acquire(state->fb.depth_stencil->resource);
1051 acquire_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
1052 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
1053 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
1056 void wined3d_device_context_emit_draw(struct wined3d_device_context *context,
1057 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count, int base_vertex_idx,
1058 unsigned int start_idx, unsigned int index_count, unsigned int start_instance, unsigned int instance_count,
1059 bool indexed)
1061 const struct wined3d_d3d_info *d3d_info = &context->device->adapter->d3d_info;
1062 const struct wined3d_state *state = context->state;
1063 struct wined3d_cs_draw *op;
1065 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1066 op->opcode = WINED3D_CS_OP_DRAW;
1067 op->primitive_type = primitive_type;
1068 op->patch_vertex_count = patch_vertex_count;
1069 op->parameters.indirect = FALSE;
1070 op->parameters.u.direct.base_vertex_idx = base_vertex_idx;
1071 op->parameters.u.direct.start_idx = start_idx;
1072 op->parameters.u.direct.index_count = index_count;
1073 op->parameters.u.direct.start_instance = start_instance;
1074 op->parameters.u.direct.instance_count = instance_count;
1075 op->parameters.indexed = indexed;
1077 acquire_graphics_pipeline_resources(state, indexed, d3d_info);
1079 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1082 void CDECL wined3d_device_context_draw_indirect(struct wined3d_device_context *context,
1083 struct wined3d_buffer *buffer, unsigned int offset, bool indexed)
1085 const struct wined3d_d3d_info *d3d_info = &context->device->adapter->d3d_info;
1086 const struct wined3d_state *state = context->state;
1087 struct wined3d_cs_draw *op;
1089 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1090 op->opcode = WINED3D_CS_OP_DRAW;
1091 op->primitive_type = state->primitive_type;
1092 op->patch_vertex_count = state->patch_vertex_count;
1093 op->parameters.indirect = TRUE;
1094 op->parameters.u.indirect.buffer = buffer;
1095 op->parameters.u.indirect.offset = offset;
1096 op->parameters.indexed = indexed;
1098 acquire_graphics_pipeline_resources(state, indexed, d3d_info);
1099 wined3d_resource_acquire(&buffer->resource);
1101 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1104 static void wined3d_cs_exec_flush(struct wined3d_cs *cs, const void *data)
1106 struct wined3d_context *context;
1108 context = context_acquire(cs->c.device, NULL, 0);
1109 cs->c.device->adapter->adapter_ops->adapter_flush_context(context);
1110 context_release(context);
1113 void wined3d_cs_emit_flush(struct wined3d_cs *cs)
1115 struct wined3d_cs_flush *op;
1117 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1118 op->opcode = WINED3D_CS_OP_FLUSH;
1120 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1121 cs->queries_flushed = TRUE;
1124 static void wined3d_cs_exec_set_predication(struct wined3d_cs *cs, const void *data)
1126 const struct wined3d_cs_set_predication *op = data;
1128 cs->state.predicate = op->predicate;
1129 cs->state.predicate_value = op->value;
1132 void wined3d_device_context_emit_set_predication(struct wined3d_device_context *context,
1133 struct wined3d_query *predicate, BOOL value)
1135 struct wined3d_cs_set_predication *op;
1137 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1138 op->opcode = WINED3D_CS_OP_SET_PREDICATION;
1139 op->predicate = predicate;
1140 op->value = value;
1142 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1145 static void wined3d_cs_exec_set_viewports(struct wined3d_cs *cs, const void *data)
1147 const struct wined3d_cs_set_viewports *op = data;
1149 if (op->viewport_count)
1150 memcpy(cs->state.viewports, op->viewports, op->viewport_count * sizeof(*op->viewports));
1151 else
1152 memset(cs->state.viewports, 0, sizeof(*cs->state.viewports));
1153 cs->state.viewport_count = op->viewport_count;
1154 device_invalidate_state(cs->c.device, STATE_VIEWPORT);
1157 void wined3d_device_context_emit_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
1158 const struct wined3d_viewport *viewports)
1160 struct wined3d_cs_set_viewports *op;
1162 op = wined3d_device_context_require_space(context,
1163 FIELD_OFFSET(struct wined3d_cs_set_viewports,viewports[viewport_count]), WINED3D_CS_QUEUE_DEFAULT);
1164 op->opcode = WINED3D_CS_OP_SET_VIEWPORTS;
1165 memcpy(op->viewports, viewports, viewport_count * sizeof(*viewports));
1166 op->viewport_count = viewport_count;
1168 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1171 static void wined3d_cs_exec_set_scissor_rects(struct wined3d_cs *cs, const void *data)
1173 const struct wined3d_cs_set_scissor_rects *op = data;
1175 if (op->rect_count)
1176 memcpy(cs->state.scissor_rects, op->rects, op->rect_count * sizeof(*op->rects));
1177 else
1178 SetRectEmpty(cs->state.scissor_rects);
1179 cs->state.scissor_rect_count = op->rect_count;
1180 device_invalidate_state(cs->c.device, STATE_SCISSORRECT);
1183 void wined3d_device_context_emit_set_scissor_rects(struct wined3d_device_context *context,
1184 unsigned int rect_count, const RECT *rects)
1186 struct wined3d_cs_set_scissor_rects *op;
1188 op = wined3d_device_context_require_space(context, FIELD_OFFSET(struct wined3d_cs_set_scissor_rects, rects[rect_count]),
1189 WINED3D_CS_QUEUE_DEFAULT);
1190 op->opcode = WINED3D_CS_OP_SET_SCISSOR_RECTS;
1191 memcpy(op->rects, rects, rect_count * sizeof(*rects));
1192 op->rect_count = rect_count;
1194 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1197 static void wined3d_cs_exec_set_rendertarget_view(struct wined3d_cs *cs, const void *data)
1199 const struct wined3d_cs_set_rendertarget_view *op = data;
1200 bool prev_alpha_swizzle, curr_alpha_swizzle;
1201 struct wined3d_rendertarget_view *prev;
1202 bool prev_srgb_write, curr_srgb_write;
1203 struct wined3d_device *device;
1205 device = cs->c.device;
1206 prev = cs->state.fb.render_targets[op->view_idx];
1207 cs->state.fb.render_targets[op->view_idx] = op->view;
1208 device_invalidate_state(device, STATE_FRAMEBUFFER);
1210 prev_alpha_swizzle = prev && prev->format->id == WINED3DFMT_A8_UNORM;
1211 curr_alpha_swizzle = op->view && op->view->format->id == WINED3DFMT_A8_UNORM;
1212 if (prev_alpha_swizzle != curr_alpha_swizzle)
1213 device_invalidate_state(device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1215 if (!(device->adapter->d3d_info.wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1216 || cs->state.render_states[WINED3D_RS_SRGBWRITEENABLE])
1218 prev_srgb_write = prev && prev->format_flags & WINED3DFMT_FLAG_SRGB_WRITE;
1219 curr_srgb_write = op->view && op->view->format_flags & WINED3DFMT_FLAG_SRGB_WRITE;
1220 if (prev_srgb_write != curr_srgb_write)
1221 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
1225 void wined3d_device_context_emit_set_rendertarget_view(struct wined3d_device_context *context, unsigned int view_idx,
1226 struct wined3d_rendertarget_view *view)
1228 struct wined3d_cs_set_rendertarget_view *op;
1230 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1231 op->opcode = WINED3D_CS_OP_SET_RENDERTARGET_VIEW;
1232 op->view_idx = view_idx;
1233 op->view = view;
1235 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1238 static void wined3d_cs_exec_set_depth_stencil_view(struct wined3d_cs *cs, const void *data)
1240 const struct wined3d_cs_set_depth_stencil_view *op = data;
1241 struct wined3d_device *device = cs->c.device;
1242 struct wined3d_rendertarget_view *prev;
1244 if ((prev = cs->state.fb.depth_stencil) && prev->resource->type != WINED3D_RTYPE_BUFFER)
1246 struct wined3d_texture *prev_texture = texture_from_resource(prev->resource);
1248 if (device->swapchains[0]->state.desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
1249 || prev_texture->flags & WINED3D_TEXTURE_DISCARD)
1250 wined3d_texture_validate_location(prev_texture,
1251 prev->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
1254 cs->state.fb.depth_stencil = op->view;
1256 if (!prev != !op->view)
1258 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
1259 device_invalidate_state(device, STATE_DEPTH_STENCIL);
1260 device_invalidate_state(device, STATE_STENCIL_REF);
1261 device_invalidate_state(device, STATE_RASTERIZER);
1263 else if (prev)
1265 if (prev->format->depth_bias_scale != op->view->format->depth_bias_scale)
1266 device_invalidate_state(device, STATE_RASTERIZER);
1267 if (prev->format->stencil_size != op->view->format->stencil_size)
1268 device_invalidate_state(device, STATE_STENCIL_REF);
1271 device_invalidate_state(device, STATE_FRAMEBUFFER);
1274 void wined3d_device_context_emit_set_depth_stencil_view(struct wined3d_device_context *context,
1275 struct wined3d_rendertarget_view *view)
1277 struct wined3d_cs_set_depth_stencil_view *op;
1279 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1280 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW;
1281 op->view = view;
1283 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1286 static void wined3d_cs_exec_set_vertex_declaration(struct wined3d_cs *cs, const void *data)
1288 const struct wined3d_cs_set_vertex_declaration *op = data;
1290 cs->state.vertex_declaration = op->declaration;
1291 device_invalidate_state(cs->c.device, STATE_VDECL);
1294 void wined3d_device_context_emit_set_vertex_declaration(struct wined3d_device_context *context,
1295 struct wined3d_vertex_declaration *declaration)
1297 struct wined3d_cs_set_vertex_declaration *op;
1299 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1300 op->opcode = WINED3D_CS_OP_SET_VERTEX_DECLARATION;
1301 op->declaration = declaration;
1303 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1306 static void wined3d_cs_exec_set_stream_source(struct wined3d_cs *cs, const void *data)
1308 const struct wined3d_cs_set_stream_source *op = data;
1309 struct wined3d_stream_state *stream;
1310 struct wined3d_buffer *prev;
1312 stream = &cs->state.streams[op->stream_idx];
1313 prev = stream->buffer;
1314 stream->buffer = op->buffer;
1315 stream->offset = op->offset;
1316 stream->stride = op->stride;
1318 if (op->buffer)
1319 InterlockedIncrement(&op->buffer->resource.bind_count);
1320 if (prev)
1321 InterlockedDecrement(&prev->resource.bind_count);
1323 device_invalidate_state(cs->c.device, STATE_STREAMSRC);
1326 void wined3d_device_context_emit_set_stream_source(struct wined3d_device_context *context, unsigned int stream_idx,
1327 struct wined3d_buffer *buffer, unsigned int offset, unsigned int stride)
1329 struct wined3d_cs_set_stream_source *op;
1331 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1332 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE;
1333 op->stream_idx = stream_idx;
1334 op->buffer = buffer;
1335 op->offset = offset;
1336 op->stride = stride;
1338 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1341 static void wined3d_cs_exec_set_stream_source_freq(struct wined3d_cs *cs, const void *data)
1343 const struct wined3d_cs_set_stream_source_freq *op = data;
1344 struct wined3d_stream_state *stream;
1346 stream = &cs->state.streams[op->stream_idx];
1347 stream->frequency = op->frequency;
1348 stream->flags = op->flags;
1350 device_invalidate_state(cs->c.device, STATE_STREAMSRC);
1353 void wined3d_cs_emit_set_stream_source_freq(struct wined3d_cs *cs, UINT stream_idx, UINT frequency, UINT flags)
1355 struct wined3d_cs_set_stream_source_freq *op;
1357 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1358 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ;
1359 op->stream_idx = stream_idx;
1360 op->frequency = frequency;
1361 op->flags = flags;
1363 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1366 static void wined3d_cs_exec_set_stream_output(struct wined3d_cs *cs, const void *data)
1368 const struct wined3d_cs_set_stream_output *op = data;
1369 struct wined3d_stream_output *stream;
1370 struct wined3d_buffer *prev;
1372 stream = &cs->state.stream_output[op->stream_idx];
1373 prev = stream->buffer;
1374 stream->buffer = op->buffer;
1375 stream->offset = op->offset;
1377 if (op->buffer)
1378 InterlockedIncrement(&op->buffer->resource.bind_count);
1379 if (prev)
1380 InterlockedDecrement(&prev->resource.bind_count);
1382 device_invalidate_state(cs->c.device, STATE_STREAM_OUTPUT);
1385 void wined3d_device_context_emit_set_stream_output(struct wined3d_device_context *context, unsigned int stream_idx,
1386 struct wined3d_buffer *buffer, unsigned int offset)
1388 struct wined3d_cs_set_stream_output *op;
1390 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1391 op->opcode = WINED3D_CS_OP_SET_STREAM_OUTPUT;
1392 op->stream_idx = stream_idx;
1393 op->buffer = buffer;
1394 op->offset = offset;
1396 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1399 static void wined3d_cs_exec_set_index_buffer(struct wined3d_cs *cs, const void *data)
1401 const struct wined3d_cs_set_index_buffer *op = data;
1402 struct wined3d_buffer *prev;
1404 prev = cs->state.index_buffer;
1405 cs->state.index_buffer = op->buffer;
1406 cs->state.index_format = op->format_id;
1407 cs->state.index_offset = op->offset;
1409 if (op->buffer)
1410 InterlockedIncrement(&op->buffer->resource.bind_count);
1411 if (prev)
1412 InterlockedDecrement(&prev->resource.bind_count);
1414 device_invalidate_state(cs->c.device, STATE_INDEXBUFFER);
1417 void wined3d_device_context_emit_set_index_buffer(struct wined3d_device_context *context, struct wined3d_buffer *buffer,
1418 enum wined3d_format_id format_id, unsigned int offset)
1420 struct wined3d_cs_set_index_buffer *op;
1422 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1423 op->opcode = WINED3D_CS_OP_SET_INDEX_BUFFER;
1424 op->buffer = buffer;
1425 op->format_id = format_id;
1426 op->offset = offset;
1428 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1431 static void wined3d_cs_exec_set_constant_buffer(struct wined3d_cs *cs, const void *data)
1433 const struct wined3d_cs_set_constant_buffer *op = data;
1434 struct wined3d_buffer *prev;
1436 prev = cs->state.cb[op->type][op->cb_idx];
1437 cs->state.cb[op->type][op->cb_idx] = op->buffer;
1439 if (op->buffer)
1440 InterlockedIncrement(&op->buffer->resource.bind_count);
1441 if (prev)
1442 InterlockedDecrement(&prev->resource.bind_count);
1444 device_invalidate_state(cs->c.device, STATE_CONSTANT_BUFFER(op->type));
1447 void wined3d_device_context_emit_set_constant_buffer(struct wined3d_device_context *context,
1448 enum wined3d_shader_type type, UINT cb_idx, struct wined3d_buffer *buffer)
1450 struct wined3d_cs_set_constant_buffer *op;
1452 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1453 op->opcode = WINED3D_CS_OP_SET_CONSTANT_BUFFER;
1454 op->type = type;
1455 op->cb_idx = cb_idx;
1456 op->buffer = buffer;
1458 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1461 static void wined3d_cs_exec_set_texture(struct wined3d_cs *cs, const void *data)
1463 const struct wined3d_d3d_info *d3d_info = &cs->c.device->adapter->d3d_info;
1464 const struct wined3d_cs_set_texture *op = data;
1465 struct wined3d_texture *prev;
1466 BOOL old_use_color_key = FALSE, new_use_color_key = FALSE;
1468 prev = cs->state.textures[op->stage];
1469 cs->state.textures[op->stage] = op->texture;
1471 if (op->texture)
1473 const struct wined3d_format *new_format = op->texture->resource.format;
1474 const struct wined3d_format *old_format = prev ? prev->resource.format : NULL;
1475 unsigned int old_fmt_flags = prev ? prev->resource.format_flags : 0;
1476 unsigned int new_fmt_flags = op->texture->resource.format_flags;
1478 if (InterlockedIncrement(&op->texture->resource.bind_count) == 1)
1479 op->texture->sampler = op->stage;
1481 if (!prev || wined3d_texture_gl(op->texture)->target != wined3d_texture_gl(prev)->target
1482 || (!is_same_fixup(new_format->color_fixup, old_format->color_fixup)
1483 && !(can_use_texture_swizzle(d3d_info, new_format) && can_use_texture_swizzle(d3d_info, old_format)))
1484 || (new_fmt_flags & WINED3DFMT_FLAG_SHADOW) != (old_fmt_flags & WINED3DFMT_FLAG_SHADOW))
1485 device_invalidate_state(cs->c.device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1487 if (!prev && op->stage < d3d_info->limits.ffp_blend_stages)
1489 /* The source arguments for color and alpha ops have different
1490 * meanings when a NULL texture is bound, so the COLOR_OP and
1491 * ALPHA_OP have to be dirtified. */
1492 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1493 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1496 if (!op->stage && op->texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1497 new_use_color_key = TRUE;
1500 if (prev)
1502 if (InterlockedDecrement(&prev->resource.bind_count) && prev->sampler == op->stage)
1504 unsigned int i;
1506 /* Search for other stages the texture is bound to. Shouldn't
1507 * happen if applications bind textures to a single stage only. */
1508 TRACE("Searching for other stages the texture is bound to.\n");
1509 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1511 if (cs->state.textures[i] == prev)
1513 TRACE("Texture is also bound to stage %u.\n", i);
1514 prev->sampler = i;
1515 break;
1520 if (!op->texture && op->stage < d3d_info->limits.ffp_blend_stages)
1522 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1523 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1526 if (!op->stage && prev->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1527 old_use_color_key = TRUE;
1530 device_invalidate_state(cs->c.device, STATE_SAMPLER(op->stage));
1532 if (new_use_color_key != old_use_color_key)
1533 device_invalidate_state(cs->c.device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1535 if (new_use_color_key)
1536 device_invalidate_state(cs->c.device, STATE_COLOR_KEY);
1539 void wined3d_cs_emit_set_texture(struct wined3d_cs *cs, UINT stage, struct wined3d_texture *texture)
1541 struct wined3d_cs_set_texture *op;
1543 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1544 op->opcode = WINED3D_CS_OP_SET_TEXTURE;
1545 op->stage = stage;
1546 op->texture = texture;
1548 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1551 static void wined3d_cs_exec_set_shader_resource_view(struct wined3d_cs *cs, const void *data)
1553 const struct wined3d_cs_set_shader_resource_view *op = data;
1554 struct wined3d_shader_resource_view *prev;
1556 prev = cs->state.shader_resource_view[op->type][op->view_idx];
1557 cs->state.shader_resource_view[op->type][op->view_idx] = op->view;
1559 if (op->view)
1560 InterlockedIncrement(&op->view->resource->bind_count);
1561 if (prev)
1562 InterlockedDecrement(&prev->resource->bind_count);
1564 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1565 device_invalidate_state(cs->c.device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1566 else
1567 device_invalidate_state(cs->c.device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1570 void wined3d_device_context_emit_set_shader_resource_view(struct wined3d_device_context *context,
1571 enum wined3d_shader_type type, unsigned int view_idx, struct wined3d_shader_resource_view *view)
1573 struct wined3d_cs_set_shader_resource_view *op;
1575 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1576 op->opcode = WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW;
1577 op->type = type;
1578 op->view_idx = view_idx;
1579 op->view = view;
1581 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1584 static void wined3d_cs_exec_set_unordered_access_view(struct wined3d_cs *cs, const void *data)
1586 const struct wined3d_cs_set_unordered_access_view *op = data;
1587 struct wined3d_unordered_access_view *prev;
1589 prev = cs->state.unordered_access_view[op->pipeline][op->view_idx];
1590 cs->state.unordered_access_view[op->pipeline][op->view_idx] = op->view;
1592 if (op->view)
1593 InterlockedIncrement(&op->view->resource->bind_count);
1594 if (prev)
1595 InterlockedDecrement(&prev->resource->bind_count);
1597 if (op->view && op->initial_count != ~0u)
1598 wined3d_unordered_access_view_set_counter(op->view, op->initial_count);
1600 device_invalidate_state(cs->c.device, STATE_UNORDERED_ACCESS_VIEW_BINDING(op->pipeline));
1603 void wined3d_device_context_emit_set_unordered_access_view(struct wined3d_device_context *context,
1604 enum wined3d_pipeline pipeline, unsigned int view_idx, struct wined3d_unordered_access_view *view,
1605 unsigned int initial_count)
1607 struct wined3d_cs_set_unordered_access_view *op;
1609 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1610 op->opcode = WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW;
1611 op->pipeline = pipeline;
1612 op->view_idx = view_idx;
1613 op->view = view;
1614 op->initial_count = initial_count;
1616 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1619 static void wined3d_cs_exec_set_sampler(struct wined3d_cs *cs, const void *data)
1621 const struct wined3d_cs_set_sampler *op = data;
1623 cs->state.sampler[op->type][op->sampler_idx] = op->sampler;
1624 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1625 device_invalidate_state(cs->c.device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1626 else
1627 device_invalidate_state(cs->c.device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1630 void wined3d_device_context_emit_set_sampler(struct wined3d_device_context *context, enum wined3d_shader_type type,
1631 unsigned int sampler_idx, struct wined3d_sampler *sampler)
1633 struct wined3d_cs_set_sampler *op;
1635 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1636 op->opcode = WINED3D_CS_OP_SET_SAMPLER;
1637 op->type = type;
1638 op->sampler_idx = sampler_idx;
1639 op->sampler = sampler;
1641 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1644 static void wined3d_cs_exec_set_shader(struct wined3d_cs *cs, const void *data)
1646 const struct wined3d_cs_set_shader *op = data;
1648 cs->state.shader[op->type] = op->shader;
1649 device_invalidate_state(cs->c.device, STATE_SHADER(op->type));
1650 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1651 device_invalidate_state(cs->c.device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1652 else
1653 device_invalidate_state(cs->c.device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1656 void wined3d_device_context_emit_set_shader(struct wined3d_device_context *context,
1657 enum wined3d_shader_type type, struct wined3d_shader *shader)
1659 struct wined3d_cs_set_shader *op;
1661 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1662 op->opcode = WINED3D_CS_OP_SET_SHADER;
1663 op->type = type;
1664 op->shader = shader;
1666 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1669 static void wined3d_cs_exec_set_blend_state(struct wined3d_cs *cs, const void *data)
1671 const struct wined3d_cs_set_blend_state *op = data;
1672 struct wined3d_state *state = &cs->state;
1674 if (state->blend_state != op->state)
1676 state->blend_state = op->state;
1677 device_invalidate_state(cs->c.device, STATE_BLEND);
1679 state->blend_factor = op->factor;
1680 device_invalidate_state(cs->c.device, STATE_BLEND_FACTOR);
1681 state->sample_mask = op->sample_mask;
1682 device_invalidate_state(cs->c.device, STATE_SAMPLE_MASK);
1685 void wined3d_device_context_emit_set_blend_state(struct wined3d_device_context *context,
1686 struct wined3d_blend_state *state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
1688 struct wined3d_cs_set_blend_state *op;
1690 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1691 op->opcode = WINED3D_CS_OP_SET_BLEND_STATE;
1692 op->state = state;
1693 op->factor = *blend_factor;
1694 op->sample_mask = sample_mask;
1696 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1699 static void wined3d_cs_exec_set_depth_stencil_state(struct wined3d_cs *cs, const void *data)
1701 const struct wined3d_cs_set_depth_stencil_state *op = data;
1702 struct wined3d_state *state = &cs->state;
1704 if (state->depth_stencil_state != op->state)
1706 state->depth_stencil_state = op->state;
1707 device_invalidate_state(cs->c.device, STATE_DEPTH_STENCIL);
1709 state->stencil_ref = op->stencil_ref;
1710 device_invalidate_state(cs->c.device, STATE_STENCIL_REF);
1713 void wined3d_device_context_emit_set_depth_stencil_state(struct wined3d_device_context *context,
1714 struct wined3d_depth_stencil_state *state, unsigned int stencil_ref)
1716 struct wined3d_cs_set_depth_stencil_state *op;
1718 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1719 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE;
1720 op->state = state;
1721 op->stencil_ref = stencil_ref;
1723 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1726 static void wined3d_cs_exec_set_rasterizer_state(struct wined3d_cs *cs, const void *data)
1728 const struct wined3d_cs_set_rasterizer_state *op = data;
1730 cs->state.rasterizer_state = op->state;
1731 device_invalidate_state(cs->c.device, STATE_RASTERIZER);
1734 void wined3d_device_context_emit_set_rasterizer_state(struct wined3d_device_context *context,
1735 struct wined3d_rasterizer_state *rasterizer_state)
1737 struct wined3d_cs_set_rasterizer_state *op;
1739 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1740 op->opcode = WINED3D_CS_OP_SET_RASTERIZER_STATE;
1741 op->state = rasterizer_state;
1743 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
1746 static void wined3d_cs_exec_set_render_state(struct wined3d_cs *cs, const void *data)
1748 const struct wined3d_cs_set_render_state *op = data;
1750 cs->state.render_states[op->state] = op->value;
1751 device_invalidate_state(cs->c.device, STATE_RENDER(op->state));
1754 void wined3d_cs_emit_set_render_state(struct wined3d_cs *cs, enum wined3d_render_state state, DWORD value)
1756 struct wined3d_cs_set_render_state *op;
1758 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1759 op->opcode = WINED3D_CS_OP_SET_RENDER_STATE;
1760 op->state = state;
1761 op->value = value;
1763 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1766 static void wined3d_cs_exec_set_texture_state(struct wined3d_cs *cs, const void *data)
1768 const struct wined3d_cs_set_texture_state *op = data;
1770 cs->state.texture_states[op->stage][op->state] = op->value;
1771 device_invalidate_state(cs->c.device, STATE_TEXTURESTAGE(op->stage, op->state));
1774 void wined3d_cs_emit_set_texture_state(struct wined3d_cs *cs, UINT stage,
1775 enum wined3d_texture_stage_state state, DWORD value)
1777 struct wined3d_cs_set_texture_state *op;
1779 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1780 op->opcode = WINED3D_CS_OP_SET_TEXTURE_STATE;
1781 op->stage = stage;
1782 op->state = state;
1783 op->value = value;
1785 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1788 static void wined3d_cs_exec_set_sampler_state(struct wined3d_cs *cs, const void *data)
1790 const struct wined3d_cs_set_sampler_state *op = data;
1792 cs->state.sampler_states[op->sampler_idx][op->state] = op->value;
1793 device_invalidate_state(cs->c.device, STATE_SAMPLER(op->sampler_idx));
1796 void wined3d_cs_emit_set_sampler_state(struct wined3d_cs *cs, UINT sampler_idx,
1797 enum wined3d_sampler_state state, DWORD value)
1799 struct wined3d_cs_set_sampler_state *op;
1801 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1802 op->opcode = WINED3D_CS_OP_SET_SAMPLER_STATE;
1803 op->sampler_idx = sampler_idx;
1804 op->state = state;
1805 op->value = value;
1807 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1810 static void wined3d_cs_exec_set_transform(struct wined3d_cs *cs, const void *data)
1812 const struct wined3d_cs_set_transform *op = data;
1814 cs->state.transforms[op->state] = op->matrix;
1815 if (op->state < WINED3D_TS_WORLD_MATRIX(cs->c.device->adapter->d3d_info.limits.ffp_vertex_blend_matrices))
1816 device_invalidate_state(cs->c.device, STATE_TRANSFORM(op->state));
1819 void wined3d_cs_emit_set_transform(struct wined3d_cs *cs, enum wined3d_transform_state state,
1820 const struct wined3d_matrix *matrix)
1822 struct wined3d_cs_set_transform *op;
1824 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1825 op->opcode = WINED3D_CS_OP_SET_TRANSFORM;
1826 op->state = state;
1827 op->matrix = *matrix;
1829 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1832 static void wined3d_cs_exec_set_clip_plane(struct wined3d_cs *cs, const void *data)
1834 const struct wined3d_cs_set_clip_plane *op = data;
1836 cs->state.clip_planes[op->plane_idx] = op->plane;
1837 device_invalidate_state(cs->c.device, STATE_CLIPPLANE(op->plane_idx));
1840 void wined3d_cs_emit_set_clip_plane(struct wined3d_cs *cs, UINT plane_idx, const struct wined3d_vec4 *plane)
1842 struct wined3d_cs_set_clip_plane *op;
1844 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1845 op->opcode = WINED3D_CS_OP_SET_CLIP_PLANE;
1846 op->plane_idx = plane_idx;
1847 op->plane = *plane;
1849 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1852 static void wined3d_cs_exec_set_color_key(struct wined3d_cs *cs, const void *data)
1854 const struct wined3d_cs_set_color_key *op = data;
1855 struct wined3d_texture *texture = op->texture;
1857 if (op->set)
1859 switch (op->flags)
1861 case WINED3D_CKEY_DST_BLT:
1862 texture->async.dst_blt_color_key = op->color_key;
1863 texture->async.color_key_flags |= WINED3D_CKEY_DST_BLT;
1864 break;
1866 case WINED3D_CKEY_DST_OVERLAY:
1867 texture->async.dst_overlay_color_key = op->color_key;
1868 texture->async.color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
1869 break;
1871 case WINED3D_CKEY_SRC_BLT:
1872 if (texture == cs->state.textures[0])
1874 device_invalidate_state(cs->c.device, STATE_COLOR_KEY);
1875 if (!(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1876 device_invalidate_state(cs->c.device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1879 texture->async.src_blt_color_key = op->color_key;
1880 texture->async.color_key_flags |= WINED3D_CKEY_SRC_BLT;
1881 break;
1883 case WINED3D_CKEY_SRC_OVERLAY:
1884 texture->async.src_overlay_color_key = op->color_key;
1885 texture->async.color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
1886 break;
1889 else
1891 switch (op->flags)
1893 case WINED3D_CKEY_DST_BLT:
1894 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_BLT;
1895 break;
1897 case WINED3D_CKEY_DST_OVERLAY:
1898 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
1899 break;
1901 case WINED3D_CKEY_SRC_BLT:
1902 if (texture == cs->state.textures[0] && texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1903 device_invalidate_state(cs->c.device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1905 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
1906 break;
1908 case WINED3D_CKEY_SRC_OVERLAY:
1909 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
1910 break;
1915 void wined3d_cs_emit_set_color_key(struct wined3d_cs *cs, struct wined3d_texture *texture,
1916 WORD flags, const struct wined3d_color_key *color_key)
1918 struct wined3d_cs_set_color_key *op;
1920 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1921 op->opcode = WINED3D_CS_OP_SET_COLOR_KEY;
1922 op->texture = texture;
1923 op->flags = flags;
1924 if (color_key)
1926 op->color_key = *color_key;
1927 op->set = 1;
1929 else
1930 op->set = 0;
1932 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1935 static void wined3d_cs_exec_set_material(struct wined3d_cs *cs, const void *data)
1937 const struct wined3d_cs_set_material *op = data;
1939 cs->state.material = op->material;
1940 device_invalidate_state(cs->c.device, STATE_MATERIAL);
1943 void wined3d_cs_emit_set_material(struct wined3d_cs *cs, const struct wined3d_material *material)
1945 struct wined3d_cs_set_material *op;
1947 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1948 op->opcode = WINED3D_CS_OP_SET_MATERIAL;
1949 op->material = *material;
1951 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
1954 static void wined3d_cs_exec_set_light(struct wined3d_cs *cs, const void *data)
1956 const struct wined3d_cs_set_light *op = data;
1957 struct wined3d_light_info *light_info;
1958 unsigned int light_idx, hash_idx;
1960 light_idx = op->light.OriginalIndex;
1962 if (!(light_info = wined3d_light_state_get_light(&cs->state.light_state, light_idx)))
1964 TRACE("Adding new light.\n");
1965 if (!(light_info = heap_alloc_zero(sizeof(*light_info))))
1967 ERR("Failed to allocate light info.\n");
1968 return;
1971 hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1972 list_add_head(&cs->state.light_state.light_map[hash_idx], &light_info->entry);
1973 light_info->glIndex = -1;
1974 light_info->OriginalIndex = light_idx;
1977 if (light_info->glIndex != -1)
1979 if (light_info->OriginalParms.type != op->light.OriginalParms.type)
1980 device_invalidate_state(cs->c.device, STATE_LIGHT_TYPE);
1981 device_invalidate_state(cs->c.device, STATE_ACTIVELIGHT(light_info->glIndex));
1984 light_info->OriginalParms = op->light.OriginalParms;
1985 light_info->position = op->light.position;
1986 light_info->direction = op->light.direction;
1987 light_info->exponent = op->light.exponent;
1988 light_info->cutoff = op->light.cutoff;
1991 void wined3d_cs_emit_set_light(struct wined3d_cs *cs, const struct wined3d_light_info *light)
1993 struct wined3d_cs_set_light *op;
1995 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1996 op->opcode = WINED3D_CS_OP_SET_LIGHT;
1997 op->light = *light;
1999 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2002 static void wined3d_cs_exec_set_light_enable(struct wined3d_cs *cs, const void *data)
2004 const struct wined3d_cs_set_light_enable *op = data;
2005 struct wined3d_device *device = cs->c.device;
2006 struct wined3d_light_info *light_info;
2007 int prev_idx;
2009 if (!(light_info = wined3d_light_state_get_light(&cs->state.light_state, op->idx)))
2011 ERR("Light doesn't exist.\n");
2012 return;
2015 prev_idx = light_info->glIndex;
2016 wined3d_light_state_enable_light(&cs->state.light_state, &device->adapter->d3d_info, light_info, op->enable);
2017 if (light_info->glIndex != prev_idx)
2019 device_invalidate_state(device, STATE_LIGHT_TYPE);
2020 device_invalidate_state(device, STATE_ACTIVELIGHT(op->enable ? light_info->glIndex : prev_idx));
2024 void wined3d_cs_emit_set_light_enable(struct wined3d_cs *cs, unsigned int idx, BOOL enable)
2026 struct wined3d_cs_set_light_enable *op;
2028 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2029 op->opcode = WINED3D_CS_OP_SET_LIGHT_ENABLE;
2030 op->idx = idx;
2031 op->enable = enable;
2033 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2036 static void wined3d_cs_exec_set_feature_level(struct wined3d_cs *cs, const void *data)
2038 const struct wined3d_cs_set_feature_level *op = data;
2040 cs->state.feature_level = op->level;
2043 void wined3d_cs_emit_set_feature_level(struct wined3d_cs *cs, enum wined3d_feature_level level)
2045 struct wined3d_cs_set_feature_level *op;
2047 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2048 op->opcode = WINED3D_CS_OP_SET_FEATURE_LEVEL;
2049 op->level = level;
2051 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2054 static const struct
2056 size_t offset;
2057 size_t size;
2058 DWORD mask;
2060 wined3d_cs_push_constant_info[] =
2062 /* WINED3D_PUSH_CONSTANTS_VS_F */
2063 {FIELD_OFFSET(struct wined3d_state, vs_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_VS_F},
2064 /* WINED3D_PUSH_CONSTANTS_PS_F */
2065 {FIELD_OFFSET(struct wined3d_state, ps_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_PS_F},
2066 /* WINED3D_PUSH_CONSTANTS_VS_I */
2067 {FIELD_OFFSET(struct wined3d_state, vs_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_VS_I},
2068 /* WINED3D_PUSH_CONSTANTS_PS_I */
2069 {FIELD_OFFSET(struct wined3d_state, ps_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_PS_I},
2070 /* WINED3D_PUSH_CONSTANTS_VS_B */
2071 {FIELD_OFFSET(struct wined3d_state, vs_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_VS_B},
2072 /* WINED3D_PUSH_CONSTANTS_PS_B */
2073 {FIELD_OFFSET(struct wined3d_state, ps_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_PS_B},
2076 static struct wined3d_cs *wined3d_cs_from_context(struct wined3d_device_context *context)
2078 return CONTAINING_RECORD(context, struct wined3d_cs, c);
2081 static void wined3d_cs_st_push_constants(struct wined3d_device_context *context, enum wined3d_push_constants p,
2082 unsigned int start_idx, unsigned int count, const void *constants)
2084 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2085 struct wined3d_device *device = cs->c.device;
2086 unsigned int context_count;
2087 unsigned int i;
2088 size_t offset;
2090 if (p == WINED3D_PUSH_CONSTANTS_VS_F)
2091 device->shader_backend->shader_update_float_vertex_constants(device, start_idx, count);
2092 else if (p == WINED3D_PUSH_CONSTANTS_PS_F)
2093 device->shader_backend->shader_update_float_pixel_constants(device, start_idx, count);
2095 offset = wined3d_cs_push_constant_info[p].offset + start_idx * wined3d_cs_push_constant_info[p].size;
2096 memcpy((BYTE *)&cs->state + offset, constants, count * wined3d_cs_push_constant_info[p].size);
2097 for (i = 0, context_count = device->context_count; i < context_count; ++i)
2099 device->contexts[i]->constant_update_mask |= wined3d_cs_push_constant_info[p].mask;
2103 static void wined3d_cs_exec_push_constants(struct wined3d_cs *cs, const void *data)
2105 const struct wined3d_cs_push_constants *op = data;
2107 wined3d_cs_st_push_constants(&cs->c, op->type, op->start_idx, op->count, op->constants);
2110 static void wined3d_cs_mt_push_constants(struct wined3d_device_context *context, enum wined3d_push_constants p,
2111 unsigned int start_idx, unsigned int count, const void *constants)
2113 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2114 struct wined3d_cs_push_constants *op;
2115 size_t size;
2117 size = count * wined3d_cs_push_constant_info[p].size;
2118 op = wined3d_device_context_require_space(&cs->c, FIELD_OFFSET(struct wined3d_cs_push_constants, constants[size]),
2119 WINED3D_CS_QUEUE_DEFAULT);
2120 op->opcode = WINED3D_CS_OP_PUSH_CONSTANTS;
2121 op->type = p;
2122 op->start_idx = start_idx;
2123 op->count = count;
2124 memcpy(op->constants, constants, size);
2126 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2129 static void wined3d_cs_exec_reset_state(struct wined3d_cs *cs, const void *data)
2131 state_cleanup(&cs->state);
2132 wined3d_state_reset(&cs->state, &cs->c.device->adapter->d3d_info);
2135 void wined3d_cs_emit_reset_state(struct wined3d_cs *cs)
2137 struct wined3d_cs_reset_state *op;
2139 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2140 op->opcode = WINED3D_CS_OP_RESET_STATE;
2142 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2145 static void wined3d_cs_exec_callback(struct wined3d_cs *cs, const void *data)
2147 const struct wined3d_cs_callback *op = data;
2149 op->callback(op->object);
2152 static void wined3d_cs_emit_callback(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2154 struct wined3d_cs_callback *op;
2156 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2157 op->opcode = WINED3D_CS_OP_CALLBACK;
2158 op->callback = callback;
2159 op->object = object;
2161 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2164 void wined3d_cs_destroy_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2166 wined3d_cs_emit_callback(cs, callback, object);
2169 void wined3d_cs_init_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2171 wined3d_cs_emit_callback(cs, callback, object);
2174 static void wined3d_cs_exec_query_issue(struct wined3d_cs *cs, const void *data)
2176 const struct wined3d_cs_query_issue *op = data;
2177 struct wined3d_query *query = op->query;
2178 BOOL poll;
2180 poll = query->query_ops->query_issue(query, op->flags);
2182 if (!cs->thread)
2183 return;
2185 if (poll && list_empty(&query->poll_list_entry))
2187 if (query->buffer_object)
2188 InterlockedIncrement(&query->counter_retrieved);
2189 else
2190 list_add_tail(&cs->query_poll_list, &query->poll_list_entry);
2191 return;
2194 /* This can happen if occlusion queries are restarted. This discards the
2195 * old result, since polling it could result in a GL error. */
2196 if ((op->flags & WINED3DISSUE_BEGIN) && !poll && !list_empty(&query->poll_list_entry))
2198 list_remove(&query->poll_list_entry);
2199 list_init(&query->poll_list_entry);
2200 InterlockedIncrement(&query->counter_retrieved);
2201 return;
2204 /* This can happen when an occlusion query is ended without being started,
2205 * in which case we don't want to poll, but still have to counter-balance
2206 * the increment of the main counter.
2208 * This can also happen if an event query is re-issued before the first
2209 * fence was reached. In this case the query is already in the list and
2210 * the poll function will check the new fence. We have to counter-balance
2211 * the discarded increment. */
2212 if (op->flags & WINED3DISSUE_END)
2213 InterlockedIncrement(&query->counter_retrieved);
2216 void wined3d_cs_emit_query_issue(struct wined3d_cs *cs, struct wined3d_query *query, DWORD flags)
2218 struct wined3d_cs_query_issue *op;
2220 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2221 op->opcode = WINED3D_CS_OP_QUERY_ISSUE;
2222 op->query = query;
2223 op->flags = flags;
2225 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2226 cs->queries_flushed = FALSE;
2229 static void wined3d_cs_exec_preload_resource(struct wined3d_cs *cs, const void *data)
2231 const struct wined3d_cs_preload_resource *op = data;
2232 struct wined3d_resource *resource = op->resource;
2234 resource->resource_ops->resource_preload(resource);
2235 wined3d_resource_release(resource);
2238 void wined3d_cs_emit_preload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
2240 struct wined3d_cs_preload_resource *op;
2242 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2243 op->opcode = WINED3D_CS_OP_PRELOAD_RESOURCE;
2244 op->resource = resource;
2246 wined3d_resource_acquire(resource);
2248 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2251 static void wined3d_cs_exec_unload_resource(struct wined3d_cs *cs, const void *data)
2253 const struct wined3d_cs_unload_resource *op = data;
2254 struct wined3d_resource *resource = op->resource;
2256 resource->resource_ops->resource_unload(resource);
2257 wined3d_resource_release(resource);
2260 void wined3d_cs_emit_unload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
2262 struct wined3d_cs_unload_resource *op;
2264 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2265 op->opcode = WINED3D_CS_OP_UNLOAD_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_map(struct wined3d_cs *cs, const void *data)
2275 const struct wined3d_cs_map *op = data;
2276 struct wined3d_resource *resource = op->resource;
2278 *op->hr = resource->resource_ops->resource_sub_resource_map(resource,
2279 op->sub_resource_idx, op->map_desc, op->box, op->flags);
2282 HRESULT wined3d_cs_map(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx,
2283 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
2285 struct wined3d_cs_map *op;
2286 HRESULT hr;
2288 /* Mapping resources from the worker thread isn't an issue by itself, but
2289 * increasing the map count would be visible to applications. */
2290 wined3d_not_from_cs(cs);
2292 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2293 op->opcode = WINED3D_CS_OP_MAP;
2294 op->resource = resource;
2295 op->sub_resource_idx = sub_resource_idx;
2296 op->map_desc = map_desc;
2297 op->box = box;
2298 op->flags = flags;
2299 op->hr = &hr;
2301 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_MAP);
2302 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_MAP);
2304 return hr;
2307 static void wined3d_cs_exec_unmap(struct wined3d_cs *cs, const void *data)
2309 const struct wined3d_cs_unmap *op = data;
2310 struct wined3d_resource *resource = op->resource;
2312 *op->hr = resource->resource_ops->resource_sub_resource_unmap(resource, op->sub_resource_idx);
2315 HRESULT wined3d_cs_unmap(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx)
2317 struct wined3d_cs_unmap *op;
2318 HRESULT hr;
2320 wined3d_not_from_cs(cs);
2322 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2323 op->opcode = WINED3D_CS_OP_UNMAP;
2324 op->resource = resource;
2325 op->sub_resource_idx = sub_resource_idx;
2326 op->hr = &hr;
2328 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_MAP);
2329 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_MAP);
2331 return hr;
2334 static void wined3d_cs_exec_blt_sub_resource(struct wined3d_cs *cs, const void *data)
2336 const struct wined3d_cs_blt_sub_resource *op = data;
2338 if (op->dst_resource->type == WINED3D_RTYPE_BUFFER)
2340 wined3d_buffer_copy(buffer_from_resource(op->dst_resource), op->dst_box.left,
2341 buffer_from_resource(op->src_resource), op->src_box.left,
2342 op->src_box.right - op->src_box.left);
2344 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_3D)
2346 struct wined3d_texture *src_texture, *dst_texture;
2347 unsigned int level, update_w, update_h, update_d;
2348 unsigned int row_pitch, slice_pitch;
2349 struct wined3d_context *context;
2350 struct wined3d_bo_address addr;
2352 if (op->flags & ~WINED3D_BLT_RAW)
2354 FIXME("Flags %#x not implemented for %s resources.\n",
2355 op->flags, debug_d3dresourcetype(op->dst_resource->type));
2356 goto error;
2359 if (!(op->flags & WINED3D_BLT_RAW) && op->src_resource->format != op->dst_resource->format)
2361 FIXME("Format conversion not implemented for %s resources.\n",
2362 debug_d3dresourcetype(op->dst_resource->type));
2363 goto error;
2366 update_w = op->dst_box.right - op->dst_box.left;
2367 update_h = op->dst_box.bottom - op->dst_box.top;
2368 update_d = op->dst_box.back - op->dst_box.front;
2369 if (op->src_box.right - op->src_box.left != update_w
2370 || op->src_box.bottom - op->src_box.top != update_h
2371 || op->src_box.back - op->src_box.front != update_d)
2373 FIXME("Stretching not implemented for %s resources.\n",
2374 debug_d3dresourcetype(op->dst_resource->type));
2375 goto error;
2378 dst_texture = texture_from_resource(op->dst_resource);
2379 src_texture = texture_from_resource(op->src_resource);
2381 context = context_acquire(cs->c.device, NULL, 0);
2383 if (!wined3d_texture_load_location(src_texture, op->src_sub_resource_idx,
2384 context, src_texture->resource.map_binding))
2386 ERR("Failed to load source sub-resource into %s.\n",
2387 wined3d_debug_location(src_texture->resource.map_binding));
2388 context_release(context);
2389 goto error;
2392 level = op->dst_sub_resource_idx % dst_texture->level_count;
2393 if (update_w == wined3d_texture_get_level_width(dst_texture, level)
2394 && update_h == wined3d_texture_get_level_height(dst_texture, level)
2395 && update_d == wined3d_texture_get_level_depth(dst_texture, level))
2397 wined3d_texture_prepare_location(dst_texture, op->dst_sub_resource_idx,
2398 context, WINED3D_LOCATION_TEXTURE_RGB);
2400 else if (!wined3d_texture_load_location(dst_texture, op->dst_sub_resource_idx,
2401 context, WINED3D_LOCATION_TEXTURE_RGB))
2403 ERR("Failed to load destination sub-resource.\n");
2404 context_release(context);
2405 goto error;
2408 wined3d_texture_get_memory(src_texture, op->src_sub_resource_idx, &addr, src_texture->resource.map_binding);
2409 wined3d_texture_get_pitch(src_texture, op->src_sub_resource_idx % src_texture->level_count,
2410 &row_pitch, &slice_pitch);
2412 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&addr),
2413 dst_texture->resource.format, &op->src_box, row_pitch, slice_pitch, dst_texture,
2414 op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
2415 op->dst_box.left, op->dst_box.top, op->dst_box.front);
2416 wined3d_texture_validate_location(dst_texture, op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2417 wined3d_texture_invalidate_location(dst_texture, op->dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2419 context_release(context);
2421 else
2423 if (FAILED(texture2d_blt(texture_from_resource(op->dst_resource), op->dst_sub_resource_idx,
2424 &op->dst_box, texture_from_resource(op->src_resource), op->src_sub_resource_idx,
2425 &op->src_box, op->flags, &op->fx, op->filter)))
2426 FIXME("Blit failed.\n");
2429 error:
2430 if (op->src_resource)
2431 wined3d_resource_release(op->src_resource);
2432 wined3d_resource_release(op->dst_resource);
2435 void wined3d_device_context_emit_blt_sub_resource(struct wined3d_device_context *context,
2436 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, const struct wined3d_box *dst_box,
2437 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx, const struct wined3d_box *src_box,
2438 unsigned int flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2440 struct wined3d_cs_blt_sub_resource *op;
2442 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2443 op->opcode = WINED3D_CS_OP_BLT_SUB_RESOURCE;
2444 op->dst_resource = dst_resource;
2445 op->dst_sub_resource_idx = dst_sub_resource_idx;
2446 op->dst_box = *dst_box;
2447 op->src_resource = src_resource;
2448 op->src_sub_resource_idx = src_sub_resource_idx;
2449 op->src_box = *src_box;
2450 op->flags = flags;
2451 if (fx)
2452 op->fx = *fx;
2453 else
2454 memset(&op->fx, 0, sizeof(op->fx));
2455 op->filter = filter;
2457 wined3d_resource_acquire(dst_resource);
2458 if (src_resource)
2459 wined3d_resource_acquire(src_resource);
2461 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2462 if (flags & WINED3D_BLT_SYNCHRONOUS)
2463 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_DEFAULT);
2466 static void wined3d_cs_exec_update_sub_resource(struct wined3d_cs *cs, const void *data)
2468 const struct wined3d_cs_update_sub_resource *op = data;
2469 struct wined3d_resource *resource = op->resource;
2470 const struct wined3d_box *box = &op->box;
2471 unsigned int width, height, depth, level;
2472 struct wined3d_const_bo_address addr;
2473 struct wined3d_context *context;
2474 struct wined3d_texture *texture;
2475 struct wined3d_box src_box;
2477 context = context_acquire(cs->c.device, NULL, 0);
2479 if (resource->type == WINED3D_RTYPE_BUFFER)
2481 struct wined3d_buffer *buffer = buffer_from_resource(resource);
2483 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
2485 ERR("Failed to load buffer location.\n");
2486 goto done;
2489 wined3d_buffer_upload_data(buffer, context, box, op->data.data);
2490 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
2491 goto done;
2494 texture = wined3d_texture_from_resource(resource);
2496 level = op->sub_resource_idx % texture->level_count;
2497 width = wined3d_texture_get_level_width(texture, level);
2498 height = wined3d_texture_get_level_height(texture, level);
2499 depth = wined3d_texture_get_level_depth(texture, level);
2501 addr.buffer_object = 0;
2502 addr.addr = op->data.data;
2504 /* Only load the sub-resource for partial updates. */
2505 if (!box->left && !box->top && !box->front
2506 && box->right == width && box->bottom == height && box->back == depth)
2507 wined3d_texture_prepare_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2508 else
2509 wined3d_texture_load_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2511 wined3d_box_set(&src_box, 0, 0, box->right - box->left, box->bottom - box->top, 0, box->back - box->front);
2512 texture->texture_ops->texture_upload_data(context, &addr, texture->resource.format, &src_box,
2513 op->data.row_pitch, op->data.slice_pitch, texture, op->sub_resource_idx,
2514 WINED3D_LOCATION_TEXTURE_RGB, box->left, box->top, box->front);
2516 wined3d_texture_validate_location(texture, op->sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2517 wined3d_texture_invalidate_location(texture, op->sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2519 done:
2520 context_release(context);
2522 wined3d_resource_release(resource);
2525 void wined3d_device_context_emit_update_sub_resource(struct wined3d_device_context *context,
2526 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
2527 const void *data, unsigned int row_pitch, unsigned int slice_pitch)
2529 struct wined3d_cs_update_sub_resource *op;
2531 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2532 op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2533 op->resource = resource;
2534 op->sub_resource_idx = sub_resource_idx;
2535 op->box = *box;
2536 op->data.row_pitch = row_pitch;
2537 op->data.slice_pitch = slice_pitch;
2538 op->data.data = data;
2540 wined3d_resource_acquire(resource);
2542 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_MAP);
2543 /* The data pointer may go away, so we need to wait until it is read.
2544 * Copying the data may be faster if it's small. */
2545 wined3d_device_context_finish(context, WINED3D_CS_QUEUE_MAP);
2548 static void wined3d_cs_exec_add_dirty_texture_region(struct wined3d_cs *cs, const void *data)
2550 const struct wined3d_cs_add_dirty_texture_region *op = data;
2551 struct wined3d_texture *texture = op->texture;
2552 unsigned int sub_resource_idx, i;
2553 struct wined3d_context *context;
2555 context = context_acquire(cs->c.device, NULL, 0);
2556 sub_resource_idx = op->layer * texture->level_count;
2557 for (i = 0; i < texture->level_count; ++i, ++sub_resource_idx)
2559 if (wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
2560 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2561 else
2562 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
2564 context_release(context);
2566 wined3d_resource_release(&texture->resource);
2569 void wined3d_cs_emit_add_dirty_texture_region(struct wined3d_cs *cs,
2570 struct wined3d_texture *texture, unsigned int layer)
2572 struct wined3d_cs_add_dirty_texture_region *op;
2574 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2575 op->opcode = WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION;
2576 op->texture = texture;
2577 op->layer = layer;
2579 wined3d_resource_acquire(&texture->resource);
2581 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2584 static void wined3d_cs_exec_clear_unordered_access_view(struct wined3d_cs *cs, const void *data)
2586 const struct wined3d_cs_clear_unordered_access_view *op = data;
2587 struct wined3d_unordered_access_view *view = op->view;
2588 struct wined3d_context *context;
2590 context = context_acquire(cs->c.device, NULL, 0);
2591 cs->c.device->adapter->adapter_ops->adapter_clear_uav(context, view, &op->clear_value);
2592 context_release(context);
2594 wined3d_resource_release(view->resource);
2597 void wined3d_device_context_emit_clear_uav_uint(struct wined3d_device_context *context,
2598 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
2600 struct wined3d_cs_clear_unordered_access_view *op;
2602 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2603 op->opcode = WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW;
2604 op->view = view;
2605 op->clear_value = *clear_value;
2607 wined3d_resource_acquire(view->resource);
2609 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2612 static void wined3d_cs_exec_copy_uav_counter(struct wined3d_cs *cs, const void *data)
2614 const struct wined3d_cs_copy_uav_counter *op = data;
2615 struct wined3d_unordered_access_view *view = op->view;
2616 struct wined3d_context *context;
2618 context = context_acquire(cs->c.device, NULL, 0);
2619 wined3d_unordered_access_view_copy_counter(view, op->buffer, op->offset, context);
2620 context_release(context);
2622 wined3d_resource_release(&op->buffer->resource);
2625 void wined3d_device_context_emit_copy_uav_counter(struct wined3d_device_context *context,
2626 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
2628 struct wined3d_cs_copy_uav_counter *op;
2630 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2631 op->opcode = WINED3D_CS_OP_COPY_UAV_COUNTER;
2632 op->buffer = dst_buffer;
2633 op->offset = offset;
2634 op->view = uav;
2636 wined3d_resource_acquire(&dst_buffer->resource);
2638 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2641 static void wined3d_cs_exec_generate_mipmaps(struct wined3d_cs *cs, const void *data)
2643 const struct wined3d_cs_generate_mipmaps *op = data;
2644 struct wined3d_shader_resource_view *view = op->view;
2645 struct wined3d_context *context;
2647 context = context_acquire(cs->c.device, NULL, 0);
2648 cs->c.device->adapter->adapter_ops->adapter_generate_mipmap(context, view);
2649 context_release(context);
2651 wined3d_resource_release(view->resource);
2654 void wined3d_device_context_emit_generate_mipmaps(struct wined3d_device_context *context,
2655 struct wined3d_shader_resource_view *view)
2657 struct wined3d_cs_generate_mipmaps *op;
2659 op = wined3d_device_context_require_space(context, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2660 op->opcode = WINED3D_CS_OP_GENERATE_MIPMAPS;
2661 op->view = view;
2663 wined3d_resource_acquire(view->resource);
2665 wined3d_device_context_submit(context, WINED3D_CS_QUEUE_DEFAULT);
2668 static void wined3d_cs_emit_stop(struct wined3d_cs *cs)
2670 struct wined3d_cs_stop *op;
2672 op = wined3d_device_context_require_space(&cs->c, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2673 op->opcode = WINED3D_CS_OP_STOP;
2675 wined3d_device_context_submit(&cs->c, WINED3D_CS_QUEUE_DEFAULT);
2676 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2679 static void (* const wined3d_cs_op_handlers[])(struct wined3d_cs *cs, const void *data) =
2681 /* WINED3D_CS_OP_NOP */ wined3d_cs_exec_nop,
2682 /* WINED3D_CS_OP_PRESENT */ wined3d_cs_exec_present,
2683 /* WINED3D_CS_OP_CLEAR */ wined3d_cs_exec_clear,
2684 /* WINED3D_CS_OP_DISPATCH */ wined3d_cs_exec_dispatch,
2685 /* WINED3D_CS_OP_DRAW */ wined3d_cs_exec_draw,
2686 /* WINED3D_CS_OP_FLUSH */ wined3d_cs_exec_flush,
2687 /* WINED3D_CS_OP_SET_PREDICATION */ wined3d_cs_exec_set_predication,
2688 /* WINED3D_CS_OP_SET_VIEWPORTS */ wined3d_cs_exec_set_viewports,
2689 /* WINED3D_CS_OP_SET_SCISSOR_RECTS */ wined3d_cs_exec_set_scissor_rects,
2690 /* WINED3D_CS_OP_SET_RENDERTARGET_VIEW */ wined3d_cs_exec_set_rendertarget_view,
2691 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW */ wined3d_cs_exec_set_depth_stencil_view,
2692 /* WINED3D_CS_OP_SET_VERTEX_DECLARATION */ wined3d_cs_exec_set_vertex_declaration,
2693 /* WINED3D_CS_OP_SET_STREAM_SOURCE */ wined3d_cs_exec_set_stream_source,
2694 /* WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ */ wined3d_cs_exec_set_stream_source_freq,
2695 /* WINED3D_CS_OP_SET_STREAM_OUTPUT */ wined3d_cs_exec_set_stream_output,
2696 /* WINED3D_CS_OP_SET_INDEX_BUFFER */ wined3d_cs_exec_set_index_buffer,
2697 /* WINED3D_CS_OP_SET_CONSTANT_BUFFER */ wined3d_cs_exec_set_constant_buffer,
2698 /* WINED3D_CS_OP_SET_TEXTURE */ wined3d_cs_exec_set_texture,
2699 /* WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW */ wined3d_cs_exec_set_shader_resource_view,
2700 /* WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_set_unordered_access_view,
2701 /* WINED3D_CS_OP_SET_SAMPLER */ wined3d_cs_exec_set_sampler,
2702 /* WINED3D_CS_OP_SET_SHADER */ wined3d_cs_exec_set_shader,
2703 /* WINED3D_CS_OP_SET_BLEND_STATE */ wined3d_cs_exec_set_blend_state,
2704 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_STATE */ wined3d_cs_exec_set_depth_stencil_state,
2705 /* WINED3D_CS_OP_SET_RASTERIZER_STATE */ wined3d_cs_exec_set_rasterizer_state,
2706 /* WINED3D_CS_OP_SET_RENDER_STATE */ wined3d_cs_exec_set_render_state,
2707 /* WINED3D_CS_OP_SET_TEXTURE_STATE */ wined3d_cs_exec_set_texture_state,
2708 /* WINED3D_CS_OP_SET_SAMPLER_STATE */ wined3d_cs_exec_set_sampler_state,
2709 /* WINED3D_CS_OP_SET_TRANSFORM */ wined3d_cs_exec_set_transform,
2710 /* WINED3D_CS_OP_SET_CLIP_PLANE */ wined3d_cs_exec_set_clip_plane,
2711 /* WINED3D_CS_OP_SET_COLOR_KEY */ wined3d_cs_exec_set_color_key,
2712 /* WINED3D_CS_OP_SET_MATERIAL */ wined3d_cs_exec_set_material,
2713 /* WINED3D_CS_OP_SET_LIGHT */ wined3d_cs_exec_set_light,
2714 /* WINED3D_CS_OP_SET_LIGHT_ENABLE */ wined3d_cs_exec_set_light_enable,
2715 /* WINED3D_CS_OP_SET_FEATURE_LEVEL */ wined3d_cs_exec_set_feature_level,
2716 /* WINED3D_CS_OP_PUSH_CONSTANTS */ wined3d_cs_exec_push_constants,
2717 /* WINED3D_CS_OP_RESET_STATE */ wined3d_cs_exec_reset_state,
2718 /* WINED3D_CS_OP_CALLBACK */ wined3d_cs_exec_callback,
2719 /* WINED3D_CS_OP_QUERY_ISSUE */ wined3d_cs_exec_query_issue,
2720 /* WINED3D_CS_OP_PRELOAD_RESOURCE */ wined3d_cs_exec_preload_resource,
2721 /* WINED3D_CS_OP_UNLOAD_RESOURCE */ wined3d_cs_exec_unload_resource,
2722 /* WINED3D_CS_OP_MAP */ wined3d_cs_exec_map,
2723 /* WINED3D_CS_OP_UNMAP */ wined3d_cs_exec_unmap,
2724 /* WINED3D_CS_OP_BLT_SUB_RESOURCE */ wined3d_cs_exec_blt_sub_resource,
2725 /* WINED3D_CS_OP_UPDATE_SUB_RESOURCE */ wined3d_cs_exec_update_sub_resource,
2726 /* WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION */ wined3d_cs_exec_add_dirty_texture_region,
2727 /* WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_clear_unordered_access_view,
2728 /* WINED3D_CS_OP_COPY_UAV_COUNTER */ wined3d_cs_exec_copy_uav_counter,
2729 /* WINED3D_CS_OP_GENERATE_MIPMAPS */ wined3d_cs_exec_generate_mipmaps,
2732 static void *wined3d_cs_st_require_space(struct wined3d_device_context *context,
2733 size_t size, enum wined3d_cs_queue_id queue_id)
2735 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2737 if (size > (cs->data_size - cs->end))
2739 size_t new_size;
2740 void *new_data;
2742 new_size = max(size, cs->data_size * 2);
2743 if (!cs->end)
2744 new_data = heap_realloc(cs->data, new_size);
2745 else
2746 new_data = heap_alloc(new_size);
2747 if (!new_data)
2748 return NULL;
2750 cs->data_size = new_size;
2751 cs->start = cs->end = 0;
2752 cs->data = new_data;
2755 cs->end += size;
2757 return (BYTE *)cs->data + cs->start;
2760 static void wined3d_cs_st_submit(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
2762 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2763 enum wined3d_cs_op opcode;
2764 size_t start;
2765 BYTE *data;
2767 data = cs->data;
2768 start = cs->start;
2769 cs->start = cs->end;
2771 opcode = *(const enum wined3d_cs_op *)&data[start];
2772 if (opcode >= WINED3D_CS_OP_STOP)
2773 ERR("Invalid opcode %#x.\n", opcode);
2774 else
2775 wined3d_cs_op_handlers[opcode](cs, &data[start]);
2777 if (cs->data == data)
2778 cs->start = cs->end = start;
2779 else if (!start)
2780 heap_free(data);
2783 static void wined3d_cs_st_finish(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
2787 static const struct wined3d_device_context_ops wined3d_cs_st_ops =
2789 wined3d_cs_st_require_space,
2790 wined3d_cs_st_submit,
2791 wined3d_cs_st_finish,
2792 wined3d_cs_st_push_constants,
2795 static BOOL wined3d_cs_queue_is_empty(const struct wined3d_cs *cs, const struct wined3d_cs_queue *queue)
2797 wined3d_from_cs(cs);
2798 return *(volatile LONG *)&queue->head == queue->tail;
2801 static void wined3d_cs_queue_submit(struct wined3d_cs_queue *queue, struct wined3d_cs *cs)
2803 struct wined3d_cs_packet *packet;
2804 size_t packet_size;
2806 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2807 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2808 InterlockedExchange(&queue->head, (queue->head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1));
2810 if (InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2811 SetEvent(cs->event);
2814 static void wined3d_cs_mt_submit(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
2816 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2818 if (cs->thread_id == GetCurrentThreadId())
2819 return wined3d_cs_st_submit(context, queue_id);
2821 wined3d_cs_queue_submit(&cs->queue[queue_id], cs);
2824 static void *wined3d_cs_queue_require_space(struct wined3d_cs_queue *queue, size_t size, struct wined3d_cs *cs)
2826 size_t queue_size = ARRAY_SIZE(queue->data);
2827 size_t header_size, packet_size, remaining;
2828 struct wined3d_cs_packet *packet;
2830 header_size = FIELD_OFFSET(struct wined3d_cs_packet, data[0]);
2831 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[size]);
2832 packet_size = (packet_size + header_size - 1) & ~(header_size - 1);
2833 size = packet_size - header_size;
2834 if (packet_size >= WINED3D_CS_QUEUE_SIZE)
2836 ERR("Packet size %lu >= queue size %u.\n",
2837 (unsigned long)packet_size, WINED3D_CS_QUEUE_SIZE);
2838 return NULL;
2841 remaining = queue_size - queue->head;
2842 if (remaining < packet_size)
2844 size_t nop_size = remaining - header_size;
2845 struct wined3d_cs_nop *nop;
2847 TRACE("Inserting a nop for %lu + %lu bytes.\n",
2848 (unsigned long)header_size, (unsigned long)nop_size);
2850 nop = wined3d_cs_queue_require_space(queue, nop_size, cs);
2851 if (nop_size)
2852 nop->opcode = WINED3D_CS_OP_NOP;
2854 wined3d_cs_queue_submit(queue, cs);
2855 assert(!queue->head);
2858 for (;;)
2860 LONG tail = *(volatile LONG *)&queue->tail;
2861 LONG head = queue->head;
2862 LONG new_pos;
2864 /* Empty. */
2865 if (head == tail)
2866 break;
2867 new_pos = (head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1);
2868 /* Head ahead of tail. We checked the remaining size above, so we only
2869 * need to make sure we don't make head equal to tail. */
2870 if (head > tail && (new_pos != tail))
2871 break;
2872 /* Tail ahead of head. Make sure the new head is before the tail as
2873 * well. Note that new_pos is 0 when it's at the end of the queue. */
2874 if (new_pos < tail && new_pos)
2875 break;
2877 TRACE("Waiting for free space. Head %u, tail %u, packet size %lu.\n",
2878 head, tail, (unsigned long)packet_size);
2881 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2882 packet->size = size;
2883 return packet->data;
2886 static void *wined3d_cs_mt_require_space(struct wined3d_device_context *context,
2887 size_t size, enum wined3d_cs_queue_id queue_id)
2889 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2891 if (cs->thread_id == GetCurrentThreadId())
2892 return wined3d_cs_st_require_space(context, size, queue_id);
2894 return wined3d_cs_queue_require_space(&cs->queue[queue_id], size, cs);
2897 static void wined3d_cs_mt_finish(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id)
2899 struct wined3d_cs *cs = wined3d_cs_from_context(context);
2901 if (cs->thread_id == GetCurrentThreadId())
2902 return wined3d_cs_st_finish(context, queue_id);
2904 while (cs->queue[queue_id].head != *(volatile LONG *)&cs->queue[queue_id].tail)
2905 YieldProcessor();
2908 static const struct wined3d_device_context_ops wined3d_cs_mt_ops =
2910 wined3d_cs_mt_require_space,
2911 wined3d_cs_mt_submit,
2912 wined3d_cs_mt_finish,
2913 wined3d_cs_mt_push_constants,
2916 static void poll_queries(struct wined3d_cs *cs)
2918 struct wined3d_query *query, *cursor;
2920 LIST_FOR_EACH_ENTRY_SAFE(query, cursor, &cs->query_poll_list, struct wined3d_query, poll_list_entry)
2922 if (!query->query_ops->query_poll(query, 0))
2923 continue;
2925 list_remove(&query->poll_list_entry);
2926 list_init(&query->poll_list_entry);
2927 InterlockedIncrement(&query->counter_retrieved);
2931 static void wined3d_cs_wait_event(struct wined3d_cs *cs)
2933 InterlockedExchange(&cs->waiting_for_event, TRUE);
2935 /* The main thread might have enqueued a command and blocked on it after
2936 * the CS thread decided to enter wined3d_cs_wait_event(), but before
2937 * "waiting_for_event" was set.
2939 * Likewise, we can race with the main thread when resetting
2940 * "waiting_for_event", in which case we would need to call
2941 * WaitForSingleObject() because the main thread called SetEvent(). */
2942 if (!(wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_DEFAULT])
2943 && wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_MAP]))
2944 && InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2945 return;
2947 WaitForSingleObject(cs->event, INFINITE);
2950 static void wined3d_cs_command_lock(const struct wined3d_cs *cs)
2952 if (cs->serialize_commands)
2953 EnterCriticalSection(&wined3d_command_cs);
2956 static void wined3d_cs_command_unlock(const struct wined3d_cs *cs)
2958 if (cs->serialize_commands)
2959 LeaveCriticalSection(&wined3d_command_cs);
2962 static DWORD WINAPI wined3d_cs_run(void *ctx)
2964 struct wined3d_cs_packet *packet;
2965 struct wined3d_cs_queue *queue;
2966 unsigned int spin_count = 0;
2967 struct wined3d_cs *cs = ctx;
2968 enum wined3d_cs_op opcode;
2969 HMODULE wined3d_module;
2970 unsigned int poll = 0;
2971 LONG tail;
2973 TRACE("Started.\n");
2975 /* Copy the module handle to a local variable to avoid racing with the
2976 * thread freeing "cs" before the FreeLibraryAndExitThread() call. */
2977 wined3d_module = cs->wined3d_module;
2979 list_init(&cs->query_poll_list);
2980 cs->thread_id = GetCurrentThreadId();
2981 for (;;)
2983 if (++poll == WINED3D_CS_QUERY_POLL_INTERVAL)
2985 wined3d_cs_command_lock(cs);
2986 poll_queries(cs);
2987 wined3d_cs_command_unlock(cs);
2988 poll = 0;
2991 queue = &cs->queue[WINED3D_CS_QUEUE_MAP];
2992 if (wined3d_cs_queue_is_empty(cs, queue))
2994 queue = &cs->queue[WINED3D_CS_QUEUE_DEFAULT];
2995 if (wined3d_cs_queue_is_empty(cs, queue))
2997 if (++spin_count >= WINED3D_CS_SPIN_COUNT && list_empty(&cs->query_poll_list))
2998 wined3d_cs_wait_event(cs);
2999 continue;
3002 spin_count = 0;
3004 tail = queue->tail;
3005 packet = (struct wined3d_cs_packet *)&queue->data[tail];
3006 if (packet->size)
3008 opcode = *(const enum wined3d_cs_op *)packet->data;
3010 TRACE("Executing %s.\n", debug_cs_op(opcode));
3011 if (opcode >= WINED3D_CS_OP_STOP)
3013 if (opcode > WINED3D_CS_OP_STOP)
3014 ERR("Invalid opcode %#x.\n", opcode);
3015 break;
3018 wined3d_cs_command_lock(cs);
3019 wined3d_cs_op_handlers[opcode](cs, packet->data);
3020 wined3d_cs_command_unlock(cs);
3021 TRACE("%s executed.\n", debug_cs_op(opcode));
3024 tail += FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
3025 tail &= (WINED3D_CS_QUEUE_SIZE - 1);
3026 InterlockedExchange(&queue->tail, tail);
3029 cs->queue[WINED3D_CS_QUEUE_MAP].tail = cs->queue[WINED3D_CS_QUEUE_MAP].head;
3030 cs->queue[WINED3D_CS_QUEUE_DEFAULT].tail = cs->queue[WINED3D_CS_QUEUE_DEFAULT].head;
3031 TRACE("Stopped.\n");
3032 FreeLibraryAndExitThread(wined3d_module, 0);
3035 struct wined3d_cs *wined3d_cs_create(struct wined3d_device *device,
3036 const enum wined3d_feature_level *levels, unsigned int level_count)
3038 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3039 struct wined3d_cs *cs;
3041 if (!(cs = heap_alloc_zero(sizeof(*cs))))
3042 return NULL;
3044 if (FAILED(wined3d_state_create(device, levels, level_count, &cs->c.state)))
3046 heap_free(cs);
3047 return NULL;
3050 cs->c.ops = &wined3d_cs_st_ops;
3051 cs->c.device = device;
3052 cs->serialize_commands = TRACE_ON(d3d_sync) || wined3d_settings.cs_multithreaded & WINED3D_CSMT_SERIALIZE;
3054 state_init(&cs->state, d3d_info, WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT, cs->c.state->feature_level);
3056 cs->data_size = WINED3D_INITIAL_CS_SIZE;
3057 if (!(cs->data = heap_alloc(cs->data_size)))
3058 goto fail;
3060 if (wined3d_settings.cs_multithreaded & WINED3D_CSMT_ENABLE
3061 && !RtlIsCriticalSectionLockedByThread(NtCurrentTeb()->Peb->LoaderLock))
3063 cs->c.ops = &wined3d_cs_mt_ops;
3065 if (!(cs->event = CreateEventW(NULL, FALSE, FALSE, NULL)))
3067 ERR("Failed to create command stream event.\n");
3068 heap_free(cs->data);
3069 goto fail;
3072 if (!(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
3073 (const WCHAR *)wined3d_cs_run, &cs->wined3d_module)))
3075 ERR("Failed to get wined3d module handle.\n");
3076 CloseHandle(cs->event);
3077 heap_free(cs->data);
3078 goto fail;
3081 if (!(cs->thread = CreateThread(NULL, 0, wined3d_cs_run, cs, 0, NULL)))
3083 ERR("Failed to create wined3d command stream thread.\n");
3084 FreeLibrary(cs->wined3d_module);
3085 CloseHandle(cs->event);
3086 heap_free(cs->data);
3087 goto fail;
3091 return cs;
3093 fail:
3094 wined3d_state_destroy(cs->c.state);
3095 state_cleanup(&cs->state);
3096 heap_free(cs);
3097 return NULL;
3100 void wined3d_cs_destroy(struct wined3d_cs *cs)
3102 if (cs->thread)
3104 wined3d_cs_emit_stop(cs);
3105 CloseHandle(cs->thread);
3106 if (!CloseHandle(cs->event))
3107 ERR("Closing event failed.\n");
3110 wined3d_state_destroy(cs->c.state);
3111 state_cleanup(&cs->state);
3112 heap_free(cs->data);
3113 heap_free(cs);