winebuild: Add a -syscall entry point flag
[wine.git] / dlls / wined3d / cs.c
blobb87003c53b8315b4e46bf5acfb0d8fb186df62b5
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(fps);
26 #define WINED3D_INITIAL_CS_SIZE 4096
28 enum wined3d_cs_op
30 WINED3D_CS_OP_NOP,
31 WINED3D_CS_OP_PRESENT,
32 WINED3D_CS_OP_CLEAR,
33 WINED3D_CS_OP_DISPATCH,
34 WINED3D_CS_OP_DRAW,
35 WINED3D_CS_OP_FLUSH,
36 WINED3D_CS_OP_SET_PREDICATION,
37 WINED3D_CS_OP_SET_VIEWPORTS,
38 WINED3D_CS_OP_SET_SCISSOR_RECTS,
39 WINED3D_CS_OP_SET_RENDERTARGET_VIEW,
40 WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW,
41 WINED3D_CS_OP_SET_VERTEX_DECLARATION,
42 WINED3D_CS_OP_SET_STREAM_SOURCE,
43 WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ,
44 WINED3D_CS_OP_SET_STREAM_OUTPUT,
45 WINED3D_CS_OP_SET_INDEX_BUFFER,
46 WINED3D_CS_OP_SET_CONSTANT_BUFFER,
47 WINED3D_CS_OP_SET_TEXTURE,
48 WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW,
49 WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW,
50 WINED3D_CS_OP_SET_SAMPLER,
51 WINED3D_CS_OP_SET_SHADER,
52 WINED3D_CS_OP_SET_BLEND_STATE,
53 WINED3D_CS_OP_SET_RASTERIZER_STATE,
54 WINED3D_CS_OP_SET_RENDER_STATE,
55 WINED3D_CS_OP_SET_TEXTURE_STATE,
56 WINED3D_CS_OP_SET_SAMPLER_STATE,
57 WINED3D_CS_OP_SET_TRANSFORM,
58 WINED3D_CS_OP_SET_CLIP_PLANE,
59 WINED3D_CS_OP_SET_COLOR_KEY,
60 WINED3D_CS_OP_SET_MATERIAL,
61 WINED3D_CS_OP_SET_LIGHT,
62 WINED3D_CS_OP_SET_LIGHT_ENABLE,
63 WINED3D_CS_OP_PUSH_CONSTANTS,
64 WINED3D_CS_OP_RESET_STATE,
65 WINED3D_CS_OP_CALLBACK,
66 WINED3D_CS_OP_QUERY_ISSUE,
67 WINED3D_CS_OP_PRELOAD_RESOURCE,
68 WINED3D_CS_OP_UNLOAD_RESOURCE,
69 WINED3D_CS_OP_MAP,
70 WINED3D_CS_OP_UNMAP,
71 WINED3D_CS_OP_BLT_SUB_RESOURCE,
72 WINED3D_CS_OP_UPDATE_SUB_RESOURCE,
73 WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION,
74 WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW,
75 WINED3D_CS_OP_COPY_UAV_COUNTER,
76 WINED3D_CS_OP_GENERATE_MIPMAPS,
77 WINED3D_CS_OP_STOP,
80 struct wined3d_cs_packet
82 size_t size;
83 BYTE data[1];
86 struct wined3d_cs_nop
88 enum wined3d_cs_op opcode;
91 struct wined3d_cs_present
93 enum wined3d_cs_op opcode;
94 HWND dst_window_override;
95 struct wined3d_swapchain *swapchain;
96 RECT src_rect;
97 RECT dst_rect;
98 unsigned int swap_interval;
99 DWORD flags;
102 struct wined3d_cs_clear
104 enum wined3d_cs_op opcode;
105 DWORD flags;
106 unsigned int rt_count;
107 struct wined3d_fb_state *fb;
108 RECT draw_rect;
109 struct wined3d_color color;
110 float depth;
111 DWORD stencil;
112 unsigned int rect_count;
113 RECT rects[1];
116 struct wined3d_cs_dispatch
118 enum wined3d_cs_op opcode;
119 struct wined3d_dispatch_parameters parameters;
122 struct wined3d_cs_draw
124 enum wined3d_cs_op opcode;
125 enum wined3d_primitive_type primitive_type;
126 GLint patch_vertex_count;
127 struct wined3d_draw_parameters parameters;
130 struct wined3d_cs_flush
132 enum wined3d_cs_op opcode;
135 struct wined3d_cs_set_predication
137 enum wined3d_cs_op opcode;
138 struct wined3d_query *predicate;
139 BOOL value;
142 struct wined3d_cs_set_viewports
144 enum wined3d_cs_op opcode;
145 unsigned int viewport_count;
146 struct wined3d_viewport viewports[1];
149 struct wined3d_cs_set_scissor_rects
151 enum wined3d_cs_op opcode;
152 unsigned int rect_count;
153 RECT rects[1];
156 struct wined3d_cs_set_rendertarget_view
158 enum wined3d_cs_op opcode;
159 unsigned int view_idx;
160 struct wined3d_rendertarget_view *view;
163 struct wined3d_cs_set_depth_stencil_view
165 enum wined3d_cs_op opcode;
166 struct wined3d_rendertarget_view *view;
169 struct wined3d_cs_set_vertex_declaration
171 enum wined3d_cs_op opcode;
172 struct wined3d_vertex_declaration *declaration;
175 struct wined3d_cs_set_stream_source
177 enum wined3d_cs_op opcode;
178 UINT stream_idx;
179 struct wined3d_buffer *buffer;
180 UINT offset;
181 UINT stride;
184 struct wined3d_cs_set_stream_source_freq
186 enum wined3d_cs_op opcode;
187 UINT stream_idx;
188 UINT frequency;
189 UINT flags;
192 struct wined3d_cs_set_stream_output
194 enum wined3d_cs_op opcode;
195 UINT stream_idx;
196 struct wined3d_buffer *buffer;
197 UINT offset;
200 struct wined3d_cs_set_index_buffer
202 enum wined3d_cs_op opcode;
203 struct wined3d_buffer *buffer;
204 enum wined3d_format_id format_id;
205 unsigned int offset;
208 struct wined3d_cs_set_constant_buffer
210 enum wined3d_cs_op opcode;
211 enum wined3d_shader_type type;
212 UINT cb_idx;
213 struct wined3d_buffer *buffer;
216 struct wined3d_cs_set_texture
218 enum wined3d_cs_op opcode;
219 UINT stage;
220 struct wined3d_texture *texture;
223 struct wined3d_cs_set_color_key
225 enum wined3d_cs_op opcode;
226 struct wined3d_texture *texture;
227 WORD flags;
228 WORD set;
229 struct wined3d_color_key color_key;
232 struct wined3d_cs_set_shader_resource_view
234 enum wined3d_cs_op opcode;
235 enum wined3d_shader_type type;
236 UINT view_idx;
237 struct wined3d_shader_resource_view *view;
240 struct wined3d_cs_set_unordered_access_view
242 enum wined3d_cs_op opcode;
243 enum wined3d_pipeline pipeline;
244 unsigned int view_idx;
245 struct wined3d_unordered_access_view *view;
246 unsigned int initial_count;
249 struct wined3d_cs_set_sampler
251 enum wined3d_cs_op opcode;
252 enum wined3d_shader_type type;
253 UINT sampler_idx;
254 struct wined3d_sampler *sampler;
257 struct wined3d_cs_set_shader
259 enum wined3d_cs_op opcode;
260 enum wined3d_shader_type type;
261 struct wined3d_shader *shader;
264 struct wined3d_cs_set_blend_state
266 enum wined3d_cs_op opcode;
267 struct wined3d_blend_state *state;
268 struct wined3d_color factor;
271 struct wined3d_cs_set_rasterizer_state
273 enum wined3d_cs_op opcode;
274 struct wined3d_rasterizer_state *state;
277 struct wined3d_cs_set_render_state
279 enum wined3d_cs_op opcode;
280 enum wined3d_render_state state;
281 DWORD value;
284 struct wined3d_cs_set_texture_state
286 enum wined3d_cs_op opcode;
287 UINT stage;
288 enum wined3d_texture_stage_state state;
289 DWORD value;
292 struct wined3d_cs_set_sampler_state
294 enum wined3d_cs_op opcode;
295 UINT sampler_idx;
296 enum wined3d_sampler_state state;
297 DWORD value;
300 struct wined3d_cs_set_transform
302 enum wined3d_cs_op opcode;
303 enum wined3d_transform_state state;
304 struct wined3d_matrix matrix;
307 struct wined3d_cs_set_clip_plane
309 enum wined3d_cs_op opcode;
310 UINT plane_idx;
311 struct wined3d_vec4 plane;
314 struct wined3d_cs_set_material
316 enum wined3d_cs_op opcode;
317 struct wined3d_material material;
320 struct wined3d_cs_set_light
322 enum wined3d_cs_op opcode;
323 struct wined3d_light_info light;
326 struct wined3d_cs_set_light_enable
328 enum wined3d_cs_op opcode;
329 unsigned int idx;
330 BOOL enable;
333 struct wined3d_cs_push_constants
335 enum wined3d_cs_op opcode;
336 enum wined3d_push_constants type;
337 unsigned int start_idx;
338 unsigned int count;
339 BYTE constants[1];
342 struct wined3d_cs_reset_state
344 enum wined3d_cs_op opcode;
347 struct wined3d_cs_callback
349 enum wined3d_cs_op opcode;
350 void (*callback)(void *object);
351 void *object;
354 struct wined3d_cs_query_issue
356 enum wined3d_cs_op opcode;
357 struct wined3d_query *query;
358 DWORD flags;
361 struct wined3d_cs_preload_resource
363 enum wined3d_cs_op opcode;
364 struct wined3d_resource *resource;
367 struct wined3d_cs_unload_resource
369 enum wined3d_cs_op opcode;
370 struct wined3d_resource *resource;
373 struct wined3d_cs_map
375 enum wined3d_cs_op opcode;
376 struct wined3d_resource *resource;
377 unsigned int sub_resource_idx;
378 struct wined3d_map_desc *map_desc;
379 const struct wined3d_box *box;
380 DWORD flags;
381 HRESULT *hr;
384 struct wined3d_cs_unmap
386 enum wined3d_cs_op opcode;
387 struct wined3d_resource *resource;
388 unsigned int sub_resource_idx;
389 HRESULT *hr;
392 struct wined3d_cs_blt_sub_resource
394 enum wined3d_cs_op opcode;
395 struct wined3d_resource *dst_resource;
396 unsigned int dst_sub_resource_idx;
397 struct wined3d_box dst_box;
398 struct wined3d_resource *src_resource;
399 unsigned int src_sub_resource_idx;
400 struct wined3d_box src_box;
401 DWORD flags;
402 struct wined3d_blt_fx fx;
403 enum wined3d_texture_filter_type filter;
406 struct wined3d_cs_update_sub_resource
408 enum wined3d_cs_op opcode;
409 struct wined3d_resource *resource;
410 unsigned int sub_resource_idx;
411 struct wined3d_box box;
412 struct wined3d_sub_resource_data data;
415 struct wined3d_cs_add_dirty_texture_region
417 enum wined3d_cs_op opcode;
418 struct wined3d_texture *texture;
419 unsigned int layer;
422 struct wined3d_cs_clear_unordered_access_view
424 enum wined3d_cs_op opcode;
425 struct wined3d_unordered_access_view *view;
426 struct wined3d_uvec4 clear_value;
429 struct wined3d_cs_copy_uav_counter
431 enum wined3d_cs_op opcode;
432 struct wined3d_buffer *buffer;
433 unsigned int offset;
434 struct wined3d_unordered_access_view *view;
437 struct wined3d_cs_generate_mipmaps
439 enum wined3d_cs_op opcode;
440 struct wined3d_shader_resource_view *view;
443 struct wined3d_cs_stop
445 enum wined3d_cs_op opcode;
448 static inline void *wined3d_cs_require_space(struct wined3d_cs *cs,
449 size_t size, enum wined3d_cs_queue_id queue_id)
451 return cs->ops->require_space(cs, size, queue_id);
454 static inline void wined3d_cs_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
456 cs->ops->submit(cs, queue_id);
459 static const char *debug_cs_op(enum wined3d_cs_op op)
461 switch (op)
463 #define WINED3D_TO_STR(type) case type: return #type
464 WINED3D_TO_STR(WINED3D_CS_OP_NOP);
465 WINED3D_TO_STR(WINED3D_CS_OP_PRESENT);
466 WINED3D_TO_STR(WINED3D_CS_OP_CLEAR);
467 WINED3D_TO_STR(WINED3D_CS_OP_DISPATCH);
468 WINED3D_TO_STR(WINED3D_CS_OP_DRAW);
469 WINED3D_TO_STR(WINED3D_CS_OP_FLUSH);
470 WINED3D_TO_STR(WINED3D_CS_OP_SET_PREDICATION);
471 WINED3D_TO_STR(WINED3D_CS_OP_SET_VIEWPORTS);
472 WINED3D_TO_STR(WINED3D_CS_OP_SET_SCISSOR_RECTS);
473 WINED3D_TO_STR(WINED3D_CS_OP_SET_RENDERTARGET_VIEW);
474 WINED3D_TO_STR(WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW);
475 WINED3D_TO_STR(WINED3D_CS_OP_SET_VERTEX_DECLARATION);
476 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_SOURCE);
477 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ);
478 WINED3D_TO_STR(WINED3D_CS_OP_SET_STREAM_OUTPUT);
479 WINED3D_TO_STR(WINED3D_CS_OP_SET_INDEX_BUFFER);
480 WINED3D_TO_STR(WINED3D_CS_OP_SET_CONSTANT_BUFFER);
481 WINED3D_TO_STR(WINED3D_CS_OP_SET_TEXTURE);
482 WINED3D_TO_STR(WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW);
483 WINED3D_TO_STR(WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW);
484 WINED3D_TO_STR(WINED3D_CS_OP_SET_SAMPLER);
485 WINED3D_TO_STR(WINED3D_CS_OP_SET_SHADER);
486 WINED3D_TO_STR(WINED3D_CS_OP_SET_BLEND_STATE);
487 WINED3D_TO_STR(WINED3D_CS_OP_SET_RASTERIZER_STATE);
488 WINED3D_TO_STR(WINED3D_CS_OP_SET_RENDER_STATE);
489 WINED3D_TO_STR(WINED3D_CS_OP_SET_TEXTURE_STATE);
490 WINED3D_TO_STR(WINED3D_CS_OP_SET_SAMPLER_STATE);
491 WINED3D_TO_STR(WINED3D_CS_OP_SET_TRANSFORM);
492 WINED3D_TO_STR(WINED3D_CS_OP_SET_CLIP_PLANE);
493 WINED3D_TO_STR(WINED3D_CS_OP_SET_COLOR_KEY);
494 WINED3D_TO_STR(WINED3D_CS_OP_SET_MATERIAL);
495 WINED3D_TO_STR(WINED3D_CS_OP_SET_LIGHT);
496 WINED3D_TO_STR(WINED3D_CS_OP_SET_LIGHT_ENABLE);
497 WINED3D_TO_STR(WINED3D_CS_OP_PUSH_CONSTANTS);
498 WINED3D_TO_STR(WINED3D_CS_OP_RESET_STATE);
499 WINED3D_TO_STR(WINED3D_CS_OP_CALLBACK);
500 WINED3D_TO_STR(WINED3D_CS_OP_QUERY_ISSUE);
501 WINED3D_TO_STR(WINED3D_CS_OP_PRELOAD_RESOURCE);
502 WINED3D_TO_STR(WINED3D_CS_OP_UNLOAD_RESOURCE);
503 WINED3D_TO_STR(WINED3D_CS_OP_MAP);
504 WINED3D_TO_STR(WINED3D_CS_OP_UNMAP);
505 WINED3D_TO_STR(WINED3D_CS_OP_BLT_SUB_RESOURCE);
506 WINED3D_TO_STR(WINED3D_CS_OP_UPDATE_SUB_RESOURCE);
507 WINED3D_TO_STR(WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION);
508 WINED3D_TO_STR(WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW);
509 WINED3D_TO_STR(WINED3D_CS_OP_COPY_UAV_COUNTER);
510 WINED3D_TO_STR(WINED3D_CS_OP_GENERATE_MIPMAPS);
511 WINED3D_TO_STR(WINED3D_CS_OP_STOP);
512 #undef WINED3D_TO_STR
513 default:
514 return wine_dbg_sprintf("UNKNOWN_OP(%#x)", op);
518 static void wined3d_cs_exec_nop(struct wined3d_cs *cs, const void *data)
522 static void wined3d_cs_exec_present(struct wined3d_cs *cs, const void *data)
524 struct wined3d_texture *logo_texture, *cursor_texture, *back_buffer;
525 struct wined3d_rendertarget_view *dsv = cs->state.fb.depth_stencil;
526 const struct wined3d_cs_present *op = data;
527 const struct wined3d_swapchain_desc *desc;
528 struct wined3d_swapchain *swapchain;
529 unsigned int i;
531 swapchain = op->swapchain;
532 desc = &swapchain->state.desc;
533 back_buffer = swapchain->back_buffers[0];
534 wined3d_swapchain_set_window(swapchain, op->dst_window_override);
536 if ((logo_texture = swapchain->device->logo_texture))
538 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
540 /* Blit the logo into the upper left corner of the back-buffer. */
541 wined3d_texture_blt(back_buffer, 0, &rect, logo_texture, 0,
542 &rect, WINED3D_BLT_SRC_CKEY, NULL, WINED3D_TEXF_POINT);
545 if ((cursor_texture = swapchain->device->cursor_texture)
546 && swapchain->device->bCursorVisible && !swapchain->device->hardwareCursor)
548 RECT dst_rect =
550 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
551 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
552 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
553 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
555 RECT src_rect =
557 0, 0, cursor_texture->resource.width, cursor_texture->resource.height
559 const RECT clip_rect = {0, 0, back_buffer->resource.width, back_buffer->resource.height};
561 TRACE("Rendering the software cursor.\n");
563 if (desc->windowed)
564 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
565 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
566 wined3d_texture_blt(back_buffer, 0, &dst_rect, cursor_texture, 0,
567 &src_rect, WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
570 swapchain->swapchain_ops->swapchain_present(swapchain, &op->src_rect, &op->dst_rect, op->swap_interval, op->flags);
572 /* Discard buffers if the swap effect allows it. */
573 back_buffer = swapchain->back_buffers[desc->backbuffer_count - 1];
574 if (desc->swap_effect == WINED3D_SWAP_EFFECT_DISCARD || desc->swap_effect == WINED3D_SWAP_EFFECT_FLIP_DISCARD)
575 wined3d_texture_validate_location(back_buffer, 0, WINED3D_LOCATION_DISCARDED);
577 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
579 struct wined3d_texture *ds = texture_from_resource(dsv->resource);
581 if ((desc->flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL || ds->flags & WINED3D_TEXTURE_DISCARD))
582 wined3d_rendertarget_view_validate_location(dsv, WINED3D_LOCATION_DISCARDED);
585 if (TRACE_ON(fps))
587 DWORD time = GetTickCount();
588 ++swapchain->frames;
590 /* every 1.5 seconds */
591 if (time - swapchain->prev_time > 1500)
593 TRACE_(fps)("%p @ approx %.2ffps\n",
594 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
595 swapchain->prev_time = time;
596 swapchain->frames = 0;
600 wined3d_resource_release(&swapchain->front_buffer->resource);
601 for (i = 0; i < desc->backbuffer_count; ++i)
603 wined3d_resource_release(&swapchain->back_buffers[i]->resource);
606 InterlockedDecrement(&cs->pending_presents);
609 void wined3d_cs_emit_present(struct wined3d_cs *cs, struct wined3d_swapchain *swapchain,
610 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
611 unsigned int swap_interval, DWORD flags)
613 struct wined3d_cs_present *op;
614 unsigned int i;
615 LONG pending;
617 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
618 op->opcode = WINED3D_CS_OP_PRESENT;
619 op->dst_window_override = dst_window_override;
620 op->swapchain = swapchain;
621 op->src_rect = *src_rect;
622 op->dst_rect = *dst_rect;
623 op->swap_interval = swap_interval;
624 op->flags = flags;
626 pending = InterlockedIncrement(&cs->pending_presents);
628 wined3d_resource_acquire(&swapchain->front_buffer->resource);
629 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
631 wined3d_resource_acquire(&swapchain->back_buffers[i]->resource);
634 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
636 /* Limit input latency by limiting the number of presents that we can get
637 * ahead of the worker thread. */
638 while (pending >= swapchain->max_frame_latency)
640 wined3d_pause();
641 pending = InterlockedCompareExchange(&cs->pending_presents, 0, 0);
645 static void wined3d_cs_exec_clear(struct wined3d_cs *cs, const void *data)
647 const struct wined3d_cs_clear *op = data;
648 struct wined3d_device *device;
649 unsigned int i;
651 device = cs->device;
652 device->blitter->ops->blitter_clear(device->blitter, device, op->rt_count, op->fb,
653 op->rect_count, op->rects, &op->draw_rect, op->flags, &op->color, op->depth, op->stencil);
655 if (op->flags & WINED3DCLEAR_TARGET)
657 for (i = 0; i < op->rt_count; ++i)
659 if (op->fb->render_targets[i])
660 wined3d_resource_release(op->fb->render_targets[i]->resource);
663 if (op->flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
664 wined3d_resource_release(op->fb->depth_stencil->resource);
667 void wined3d_cs_emit_clear(struct wined3d_cs *cs, DWORD rect_count, const RECT *rects,
668 DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
670 const struct wined3d_state *state = &cs->device->state;
671 const struct wined3d_viewport *vp = &state->viewports[0];
672 struct wined3d_rendertarget_view *view;
673 struct wined3d_cs_clear *op;
674 unsigned int rt_count, i;
676 rt_count = flags & WINED3DCLEAR_TARGET ? cs->device->adapter->d3d_info.limits.max_rt_count : 0;
678 op = wined3d_cs_require_space(cs, FIELD_OFFSET(struct wined3d_cs_clear, rects[rect_count]),
679 WINED3D_CS_QUEUE_DEFAULT);
680 op->opcode = WINED3D_CS_OP_CLEAR;
681 op->flags = flags & (WINED3DCLEAR_TARGET | WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
682 op->rt_count = rt_count;
683 op->fb = &cs->state.fb;
684 SetRect(&op->draw_rect, vp->x, vp->y, vp->x + vp->width, vp->y + vp->height);
685 if (state->rasterizer_state && state->rasterizer_state->desc.scissor)
686 IntersectRect(&op->draw_rect, &op->draw_rect, &state->scissor_rects[0]);
687 op->color = *color;
688 op->depth = depth;
689 op->stencil = stencil;
690 op->rect_count = rect_count;
691 memcpy(op->rects, rects, sizeof(*rects) * rect_count);
693 for (i = 0; i < rt_count; ++i)
695 if ((view = state->fb.render_targets[i]))
696 wined3d_resource_acquire(view->resource);
698 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
700 view = state->fb.depth_stencil;
701 wined3d_resource_acquire(view->resource);
704 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
707 void wined3d_cs_emit_clear_rendertarget_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view,
708 const RECT *rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
710 struct wined3d_cs_clear *op;
711 size_t size;
713 size = FIELD_OFFSET(struct wined3d_cs_clear, rects[1]) + sizeof(struct wined3d_fb_state);
714 op = wined3d_cs_require_space(cs, size, WINED3D_CS_QUEUE_DEFAULT);
715 op->fb = (void *)&op->rects[1];
717 op->opcode = WINED3D_CS_OP_CLEAR;
718 op->flags = flags & (WINED3DCLEAR_TARGET | WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
719 if (flags & WINED3DCLEAR_TARGET)
721 op->rt_count = 1;
722 op->fb->render_targets[0] = view;
723 op->fb->depth_stencil = NULL;
724 op->color = *color;
726 else
728 op->rt_count = 0;
729 op->fb->render_targets[0] = NULL;
730 op->fb->depth_stencil = view;
731 op->depth = depth;
732 op->stencil = stencil;
734 SetRect(&op->draw_rect, 0, 0, view->width, view->height);
735 op->rect_count = 1;
736 op->rects[0] = *rect;
738 wined3d_resource_acquire(view->resource);
740 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
741 if (flags & WINED3DCLEAR_SYNCHRONOUS)
742 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_DEFAULT);
745 static void acquire_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
747 struct wined3d_shader_sampler_map_entry *entry;
748 struct wined3d_shader_resource_view *view;
749 struct wined3d_shader *shader;
750 unsigned int i, j;
752 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
754 if (!(shader_mask & (1u << i)))
755 continue;
757 if (!(shader = state->shader[i]))
758 continue;
760 for (j = 0; j < WINED3D_MAX_CBS; ++j)
762 if (state->cb[i][j])
763 wined3d_resource_acquire(&state->cb[i][j]->resource);
766 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
768 entry = &shader->reg_maps.sampler_map.entries[j];
770 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
771 continue;
773 wined3d_resource_acquire(view->resource);
778 static void release_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
780 struct wined3d_shader_sampler_map_entry *entry;
781 struct wined3d_shader_resource_view *view;
782 struct wined3d_shader *shader;
783 unsigned int i, j;
785 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
787 if (!(shader_mask & (1u << i)))
788 continue;
790 if (!(shader = state->shader[i]))
791 continue;
793 for (j = 0; j < WINED3D_MAX_CBS; ++j)
795 if (state->cb[i][j])
796 wined3d_resource_release(&state->cb[i][j]->resource);
799 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
801 entry = &shader->reg_maps.sampler_map.entries[j];
803 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
804 continue;
806 wined3d_resource_release(view->resource);
811 static void acquire_unordered_access_resources(const struct wined3d_shader *shader,
812 struct wined3d_unordered_access_view * const *views)
814 unsigned int i;
816 if (!shader)
817 return;
819 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
821 if (!shader->reg_maps.uav_resource_info[i].type)
822 continue;
824 if (!views[i])
825 continue;
827 wined3d_resource_acquire(views[i]->resource);
831 static void release_unordered_access_resources(const struct wined3d_shader *shader,
832 struct wined3d_unordered_access_view * const *views)
834 unsigned int i;
836 if (!shader)
837 return;
839 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
841 if (!shader->reg_maps.uav_resource_info[i].type)
842 continue;
844 if (!views[i])
845 continue;
847 wined3d_resource_release(views[i]->resource);
851 static void wined3d_cs_exec_dispatch(struct wined3d_cs *cs, const void *data)
853 const struct wined3d_cs_dispatch *op = data;
854 struct wined3d_state *state = &cs->state;
856 if (!state->shader[WINED3D_SHADER_TYPE_COMPUTE])
857 WARN("No compute shader bound, skipping dispatch.\n");
858 else
859 cs->device->adapter->adapter_ops->adapter_dispatch_compute(cs->device, state, &op->parameters);
861 if (op->parameters.indirect)
862 wined3d_resource_release(&op->parameters.u.indirect.buffer->resource);
864 release_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
865 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
866 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
869 static void acquire_compute_pipeline_resources(const struct wined3d_state *state)
871 acquire_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
872 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
873 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
876 void wined3d_cs_emit_dispatch(struct wined3d_cs *cs,
877 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
879 const struct wined3d_state *state = &cs->device->state;
880 struct wined3d_cs_dispatch *op;
882 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
883 op->opcode = WINED3D_CS_OP_DISPATCH;
884 op->parameters.indirect = FALSE;
885 op->parameters.u.direct.group_count_x = group_count_x;
886 op->parameters.u.direct.group_count_y = group_count_y;
887 op->parameters.u.direct.group_count_z = group_count_z;
889 acquire_compute_pipeline_resources(state);
891 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
894 void wined3d_cs_emit_dispatch_indirect(struct wined3d_cs *cs,
895 struct wined3d_buffer *buffer, unsigned int offset)
897 const struct wined3d_state *state = &cs->device->state;
898 struct wined3d_cs_dispatch *op;
900 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
901 op->opcode = WINED3D_CS_OP_DISPATCH;
902 op->parameters.indirect = TRUE;
903 op->parameters.u.indirect.buffer = buffer;
904 op->parameters.u.indirect.offset = offset;
906 acquire_compute_pipeline_resources(state);
907 wined3d_resource_acquire(&buffer->resource);
909 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
912 static void wined3d_cs_exec_draw(struct wined3d_cs *cs, const void *data)
914 const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
915 const struct wined3d_shader *geometry_shader;
916 struct wined3d_device *device = cs->device;
917 int base_vertex_idx, load_base_vertex_idx;
918 struct wined3d_state *state = &cs->state;
919 const struct wined3d_cs_draw *op = data;
920 unsigned int i;
922 base_vertex_idx = 0;
923 if (!op->parameters.indirect)
925 const struct wined3d_direct_draw_parameters *direct = &op->parameters.u.direct;
927 if (op->parameters.indexed && d3d_info->draw_base_vertex_offset)
928 base_vertex_idx = direct->base_vertex_idx;
929 else if (!op->parameters.indexed)
930 base_vertex_idx = direct->start_idx;
933 /* ARB_draw_indirect always supports a base vertex offset. */
934 if (!op->parameters.indirect && !d3d_info->draw_base_vertex_offset)
935 load_base_vertex_idx = op->parameters.u.direct.base_vertex_idx;
936 else
937 load_base_vertex_idx = 0;
939 if (state->base_vertex_index != base_vertex_idx)
941 state->base_vertex_index = base_vertex_idx;
942 for (i = 0; i < device->context_count; ++i)
943 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_BASE_VERTEX_ID;
946 if (state->load_base_vertex_index != load_base_vertex_idx)
948 state->load_base_vertex_index = load_base_vertex_idx;
949 device_invalidate_state(cs->device, STATE_BASEVERTEXINDEX);
952 if (state->primitive_type != op->primitive_type)
954 if ((geometry_shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]) && !geometry_shader->function)
955 device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY));
956 if (state->primitive_type == WINED3D_PT_POINTLIST || op->primitive_type == WINED3D_PT_POINTLIST)
957 device_invalidate_state(cs->device, STATE_POINT_ENABLE);
958 state->primitive_type = op->primitive_type;
960 state->patch_vertex_count = op->patch_vertex_count;
962 cs->device->adapter->adapter_ops->adapter_draw_primitive(cs->device, state, &op->parameters);
964 if (op->parameters.indirect)
966 struct wined3d_buffer *buffer = op->parameters.u.indirect.buffer;
967 wined3d_resource_release(&buffer->resource);
970 if (op->parameters.indexed)
971 wined3d_resource_release(&state->index_buffer->resource);
972 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
974 if (state->streams[i].buffer)
975 wined3d_resource_release(&state->streams[i].buffer->resource);
977 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
979 if (state->stream_output[i].buffer)
980 wined3d_resource_release(&state->stream_output[i].buffer->resource);
982 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
984 if (state->textures[i])
985 wined3d_resource_release(&state->textures[i]->resource);
987 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
989 if (state->fb.render_targets[i])
990 wined3d_resource_release(state->fb.render_targets[i]->resource);
992 if (state->fb.depth_stencil)
993 wined3d_resource_release(state->fb.depth_stencil->resource);
994 release_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
995 release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
996 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
999 static void acquire_graphics_pipeline_resources(const struct wined3d_state *state,
1000 BOOL indexed, const struct wined3d_d3d_info *d3d_info)
1002 unsigned int i;
1004 if (indexed)
1005 wined3d_resource_acquire(&state->index_buffer->resource);
1006 for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
1008 if (state->streams[i].buffer)
1009 wined3d_resource_acquire(&state->streams[i].buffer->resource);
1011 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
1013 if (state->stream_output[i].buffer)
1014 wined3d_resource_acquire(&state->stream_output[i].buffer->resource);
1016 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
1018 if (state->textures[i])
1019 wined3d_resource_acquire(&state->textures[i]->resource);
1021 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
1023 if (state->fb.render_targets[i])
1024 wined3d_resource_acquire(state->fb.render_targets[i]->resource);
1026 if (state->fb.depth_stencil)
1027 wined3d_resource_acquire(state->fb.depth_stencil->resource);
1028 acquire_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
1029 acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
1030 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
1033 void wined3d_cs_emit_draw(struct wined3d_cs *cs, enum wined3d_primitive_type primitive_type,
1034 unsigned int patch_vertex_count, int base_vertex_idx, unsigned int start_idx,
1035 unsigned int index_count, unsigned int start_instance, unsigned int instance_count, bool indexed)
1037 const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
1038 const struct wined3d_state *state = &cs->device->state;
1039 struct wined3d_cs_draw *op;
1041 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1042 op->opcode = WINED3D_CS_OP_DRAW;
1043 op->primitive_type = primitive_type;
1044 op->patch_vertex_count = patch_vertex_count;
1045 op->parameters.indirect = FALSE;
1046 op->parameters.u.direct.base_vertex_idx = base_vertex_idx;
1047 op->parameters.u.direct.start_idx = start_idx;
1048 op->parameters.u.direct.index_count = index_count;
1049 op->parameters.u.direct.start_instance = start_instance;
1050 op->parameters.u.direct.instance_count = instance_count;
1051 op->parameters.indexed = indexed;
1053 acquire_graphics_pipeline_resources(state, indexed, d3d_info);
1055 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1058 void wined3d_cs_emit_draw_indirect(struct wined3d_cs *cs, enum wined3d_primitive_type primitive_type,
1059 unsigned int patch_vertex_count, struct wined3d_buffer *buffer, unsigned int offset, bool indexed)
1061 const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
1062 const struct wined3d_state *state = &cs->device->state;
1063 struct wined3d_cs_draw *op;
1065 op = wined3d_cs_require_space(cs, 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 = TRUE;
1070 op->parameters.u.indirect.buffer = buffer;
1071 op->parameters.u.indirect.offset = offset;
1072 op->parameters.indexed = indexed;
1074 acquire_graphics_pipeline_resources(state, indexed, d3d_info);
1075 wined3d_resource_acquire(&buffer->resource);
1077 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1080 static void wined3d_cs_exec_flush(struct wined3d_cs *cs, const void *data)
1082 struct wined3d_context *context;
1084 context = context_acquire(cs->device, NULL, 0);
1085 cs->device->adapter->adapter_ops->adapter_flush_context(context);
1086 context_release(context);
1089 void wined3d_cs_emit_flush(struct wined3d_cs *cs)
1091 struct wined3d_cs_flush *op;
1093 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1094 op->opcode = WINED3D_CS_OP_FLUSH;
1096 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1097 cs->queries_flushed = TRUE;
1100 static void wined3d_cs_exec_set_predication(struct wined3d_cs *cs, const void *data)
1102 const struct wined3d_cs_set_predication *op = data;
1104 cs->state.predicate = op->predicate;
1105 cs->state.predicate_value = op->value;
1108 void wined3d_cs_emit_set_predication(struct wined3d_cs *cs, struct wined3d_query *predicate, BOOL value)
1110 struct wined3d_cs_set_predication *op;
1112 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1113 op->opcode = WINED3D_CS_OP_SET_PREDICATION;
1114 op->predicate = predicate;
1115 op->value = value;
1117 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1120 static void wined3d_cs_exec_set_viewports(struct wined3d_cs *cs, const void *data)
1122 const struct wined3d_cs_set_viewports *op = data;
1124 if (op->viewport_count)
1125 memcpy(cs->state.viewports, op->viewports, op->viewport_count * sizeof(*op->viewports));
1126 else
1127 memset(cs->state.viewports, 0, sizeof(*cs->state.viewports));
1128 cs->state.viewport_count = op->viewport_count;
1129 device_invalidate_state(cs->device, STATE_VIEWPORT);
1132 void wined3d_cs_emit_set_viewports(struct wined3d_cs *cs, unsigned int viewport_count,
1133 const struct wined3d_viewport *viewports)
1135 struct wined3d_cs_set_viewports *op;
1137 op = wined3d_cs_require_space(cs, FIELD_OFFSET(struct wined3d_cs_set_viewports, viewports[viewport_count]),
1138 WINED3D_CS_QUEUE_DEFAULT);
1139 op->opcode = WINED3D_CS_OP_SET_VIEWPORTS;
1140 memcpy(op->viewports, viewports, viewport_count * sizeof(*viewports));
1141 op->viewport_count = viewport_count;
1143 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1146 static void wined3d_cs_exec_set_scissor_rects(struct wined3d_cs *cs, const void *data)
1148 const struct wined3d_cs_set_scissor_rects *op = data;
1150 if (op->rect_count)
1151 memcpy(cs->state.scissor_rects, op->rects, op->rect_count * sizeof(*op->rects));
1152 else
1153 SetRectEmpty(cs->state.scissor_rects);
1154 cs->state.scissor_rect_count = op->rect_count;
1155 device_invalidate_state(cs->device, STATE_SCISSORRECT);
1158 void wined3d_cs_emit_set_scissor_rects(struct wined3d_cs *cs, unsigned int rect_count, const RECT *rects)
1160 struct wined3d_cs_set_scissor_rects *op;
1162 op = wined3d_cs_require_space(cs, FIELD_OFFSET(struct wined3d_cs_set_scissor_rects, rects[rect_count]),
1163 WINED3D_CS_QUEUE_DEFAULT);
1164 op->opcode = WINED3D_CS_OP_SET_SCISSOR_RECTS;
1165 memcpy(op->rects, rects, rect_count * sizeof(*rects));
1166 op->rect_count = rect_count;
1168 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1171 static void wined3d_cs_exec_set_rendertarget_view(struct wined3d_cs *cs, const void *data)
1173 const struct wined3d_cs_set_rendertarget_view *op = data;
1174 BOOL prev_alpha_swizzle, curr_alpha_swizzle;
1175 struct wined3d_rendertarget_view *prev;
1177 prev = cs->state.fb.render_targets[op->view_idx];
1178 cs->state.fb.render_targets[op->view_idx] = op->view;
1179 device_invalidate_state(cs->device, STATE_FRAMEBUFFER);
1181 prev_alpha_swizzle = prev && prev->format->id == WINED3DFMT_A8_UNORM;
1182 curr_alpha_swizzle = op->view && op->view->format->id == WINED3DFMT_A8_UNORM;
1183 if (prev_alpha_swizzle != curr_alpha_swizzle)
1184 device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1187 void wined3d_cs_emit_set_rendertarget_view(struct wined3d_cs *cs, unsigned int view_idx,
1188 struct wined3d_rendertarget_view *view)
1190 struct wined3d_cs_set_rendertarget_view *op;
1192 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1193 op->opcode = WINED3D_CS_OP_SET_RENDERTARGET_VIEW;
1194 op->view_idx = view_idx;
1195 op->view = view;
1197 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1200 static void wined3d_cs_exec_set_depth_stencil_view(struct wined3d_cs *cs, const void *data)
1202 const struct wined3d_cs_set_depth_stencil_view *op = data;
1203 struct wined3d_device *device = cs->device;
1204 struct wined3d_rendertarget_view *prev;
1206 if ((prev = cs->state.fb.depth_stencil) && prev->resource->type != WINED3D_RTYPE_BUFFER)
1208 struct wined3d_texture *prev_texture = texture_from_resource(prev->resource);
1210 if (device->swapchains[0]->state.desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
1211 || prev_texture->flags & WINED3D_TEXTURE_DISCARD)
1212 wined3d_texture_validate_location(prev_texture,
1213 prev->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
1216 cs->state.fb.depth_stencil = op->view;
1218 if (!prev != !op->view)
1220 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
1221 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
1222 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
1223 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
1224 device_invalidate_state(device, STATE_RASTERIZER);
1226 else if (prev)
1228 if (prev->format->depth_bias_scale != op->view->format->depth_bias_scale)
1229 device_invalidate_state(device, STATE_RASTERIZER);
1230 if (prev->format->stencil_size != op->view->format->stencil_size)
1231 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILREF));
1234 device_invalidate_state(device, STATE_FRAMEBUFFER);
1237 void wined3d_cs_emit_set_depth_stencil_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view)
1239 struct wined3d_cs_set_depth_stencil_view *op;
1241 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1242 op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW;
1243 op->view = view;
1245 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1248 static void wined3d_cs_exec_set_vertex_declaration(struct wined3d_cs *cs, const void *data)
1250 const struct wined3d_cs_set_vertex_declaration *op = data;
1252 cs->state.vertex_declaration = op->declaration;
1253 device_invalidate_state(cs->device, STATE_VDECL);
1256 void wined3d_cs_emit_set_vertex_declaration(struct wined3d_cs *cs, struct wined3d_vertex_declaration *declaration)
1258 struct wined3d_cs_set_vertex_declaration *op;
1260 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1261 op->opcode = WINED3D_CS_OP_SET_VERTEX_DECLARATION;
1262 op->declaration = declaration;
1264 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1267 static void wined3d_cs_exec_set_stream_source(struct wined3d_cs *cs, const void *data)
1269 const struct wined3d_cs_set_stream_source *op = data;
1270 struct wined3d_stream_state *stream;
1271 struct wined3d_buffer *prev;
1273 stream = &cs->state.streams[op->stream_idx];
1274 prev = stream->buffer;
1275 stream->buffer = op->buffer;
1276 stream->offset = op->offset;
1277 stream->stride = op->stride;
1279 if (op->buffer)
1280 InterlockedIncrement(&op->buffer->resource.bind_count);
1281 if (prev)
1282 InterlockedDecrement(&prev->resource.bind_count);
1284 device_invalidate_state(cs->device, STATE_STREAMSRC);
1287 void wined3d_cs_emit_set_stream_source(struct wined3d_cs *cs, UINT stream_idx,
1288 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1290 struct wined3d_cs_set_stream_source *op;
1292 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1293 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE;
1294 op->stream_idx = stream_idx;
1295 op->buffer = buffer;
1296 op->offset = offset;
1297 op->stride = stride;
1299 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1302 static void wined3d_cs_exec_set_stream_source_freq(struct wined3d_cs *cs, const void *data)
1304 const struct wined3d_cs_set_stream_source_freq *op = data;
1305 struct wined3d_stream_state *stream;
1307 stream = &cs->state.streams[op->stream_idx];
1308 stream->frequency = op->frequency;
1309 stream->flags = op->flags;
1311 device_invalidate_state(cs->device, STATE_STREAMSRC);
1314 void wined3d_cs_emit_set_stream_source_freq(struct wined3d_cs *cs, UINT stream_idx, UINT frequency, UINT flags)
1316 struct wined3d_cs_set_stream_source_freq *op;
1318 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1319 op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ;
1320 op->stream_idx = stream_idx;
1321 op->frequency = frequency;
1322 op->flags = flags;
1324 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1327 static void wined3d_cs_exec_set_stream_output(struct wined3d_cs *cs, const void *data)
1329 const struct wined3d_cs_set_stream_output *op = data;
1330 struct wined3d_stream_output *stream;
1331 struct wined3d_buffer *prev;
1333 stream = &cs->state.stream_output[op->stream_idx];
1334 prev = stream->buffer;
1335 stream->buffer = op->buffer;
1336 stream->offset = op->offset;
1338 if (op->buffer)
1339 InterlockedIncrement(&op->buffer->resource.bind_count);
1340 if (prev)
1341 InterlockedDecrement(&prev->resource.bind_count);
1343 device_invalidate_state(cs->device, STATE_STREAM_OUTPUT);
1346 void wined3d_cs_emit_set_stream_output(struct wined3d_cs *cs, UINT stream_idx,
1347 struct wined3d_buffer *buffer, UINT offset)
1349 struct wined3d_cs_set_stream_output *op;
1351 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1352 op->opcode = WINED3D_CS_OP_SET_STREAM_OUTPUT;
1353 op->stream_idx = stream_idx;
1354 op->buffer = buffer;
1355 op->offset = offset;
1357 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1360 static void wined3d_cs_exec_set_index_buffer(struct wined3d_cs *cs, const void *data)
1362 const struct wined3d_cs_set_index_buffer *op = data;
1363 struct wined3d_buffer *prev;
1365 prev = cs->state.index_buffer;
1366 cs->state.index_buffer = op->buffer;
1367 cs->state.index_format = op->format_id;
1368 cs->state.index_offset = op->offset;
1370 if (op->buffer)
1371 InterlockedIncrement(&op->buffer->resource.bind_count);
1372 if (prev)
1373 InterlockedDecrement(&prev->resource.bind_count);
1375 device_invalidate_state(cs->device, STATE_INDEXBUFFER);
1378 void wined3d_cs_emit_set_index_buffer(struct wined3d_cs *cs, struct wined3d_buffer *buffer,
1379 enum wined3d_format_id format_id, unsigned int offset)
1381 struct wined3d_cs_set_index_buffer *op;
1383 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1384 op->opcode = WINED3D_CS_OP_SET_INDEX_BUFFER;
1385 op->buffer = buffer;
1386 op->format_id = format_id;
1387 op->offset = offset;
1389 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1392 static void wined3d_cs_exec_set_constant_buffer(struct wined3d_cs *cs, const void *data)
1394 const struct wined3d_cs_set_constant_buffer *op = data;
1395 struct wined3d_buffer *prev;
1397 prev = cs->state.cb[op->type][op->cb_idx];
1398 cs->state.cb[op->type][op->cb_idx] = op->buffer;
1400 if (op->buffer)
1401 InterlockedIncrement(&op->buffer->resource.bind_count);
1402 if (prev)
1403 InterlockedDecrement(&prev->resource.bind_count);
1405 device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(op->type));
1408 void wined3d_cs_emit_set_constant_buffer(struct wined3d_cs *cs, enum wined3d_shader_type type,
1409 UINT cb_idx, struct wined3d_buffer *buffer)
1411 struct wined3d_cs_set_constant_buffer *op;
1413 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1414 op->opcode = WINED3D_CS_OP_SET_CONSTANT_BUFFER;
1415 op->type = type;
1416 op->cb_idx = cb_idx;
1417 op->buffer = buffer;
1419 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1422 static void wined3d_cs_exec_set_texture(struct wined3d_cs *cs, const void *data)
1424 const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
1425 const struct wined3d_cs_set_texture *op = data;
1426 struct wined3d_texture *prev;
1427 BOOL old_use_color_key = FALSE, new_use_color_key = FALSE;
1429 prev = cs->state.textures[op->stage];
1430 cs->state.textures[op->stage] = op->texture;
1432 if (op->texture)
1434 const struct wined3d_format *new_format = op->texture->resource.format;
1435 const struct wined3d_format *old_format = prev ? prev->resource.format : NULL;
1436 unsigned int old_fmt_flags = prev ? prev->resource.format_flags : 0;
1437 unsigned int new_fmt_flags = op->texture->resource.format_flags;
1439 if (InterlockedIncrement(&op->texture->resource.bind_count) == 1)
1440 op->texture->sampler = op->stage;
1442 if (!prev || wined3d_texture_gl(op->texture)->target != wined3d_texture_gl(prev)->target
1443 || (!is_same_fixup(new_format->color_fixup, old_format->color_fixup)
1444 && !(can_use_texture_swizzle(d3d_info, new_format) && can_use_texture_swizzle(d3d_info, old_format)))
1445 || (new_fmt_flags & WINED3DFMT_FLAG_SHADOW) != (old_fmt_flags & WINED3DFMT_FLAG_SHADOW))
1446 device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1448 if (!prev && op->stage < d3d_info->limits.ffp_blend_stages)
1450 /* The source arguments for color and alpha ops have different
1451 * meanings when a NULL texture is bound, so the COLOR_OP and
1452 * ALPHA_OP have to be dirtified. */
1453 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1454 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1457 if (!op->stage && op->texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1458 new_use_color_key = TRUE;
1461 if (prev)
1463 if (InterlockedDecrement(&prev->resource.bind_count) && prev->sampler == op->stage)
1465 unsigned int i;
1467 /* Search for other stages the texture is bound to. Shouldn't
1468 * happen if applications bind textures to a single stage only. */
1469 TRACE("Searching for other stages the texture is bound to.\n");
1470 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1472 if (cs->state.textures[i] == prev)
1474 TRACE("Texture is also bound to stage %u.\n", i);
1475 prev->sampler = i;
1476 break;
1481 if (!op->texture && op->stage < d3d_info->limits.ffp_blend_stages)
1483 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1484 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1487 if (!op->stage && prev->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1488 old_use_color_key = TRUE;
1491 device_invalidate_state(cs->device, STATE_SAMPLER(op->stage));
1493 if (new_use_color_key != old_use_color_key)
1494 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1496 if (new_use_color_key)
1497 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1500 void wined3d_cs_emit_set_texture(struct wined3d_cs *cs, UINT stage, struct wined3d_texture *texture)
1502 struct wined3d_cs_set_texture *op;
1504 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1505 op->opcode = WINED3D_CS_OP_SET_TEXTURE;
1506 op->stage = stage;
1507 op->texture = texture;
1509 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1512 static void wined3d_cs_exec_set_shader_resource_view(struct wined3d_cs *cs, const void *data)
1514 const struct wined3d_cs_set_shader_resource_view *op = data;
1515 struct wined3d_shader_resource_view *prev;
1517 prev = cs->state.shader_resource_view[op->type][op->view_idx];
1518 cs->state.shader_resource_view[op->type][op->view_idx] = op->view;
1520 if (op->view)
1521 InterlockedIncrement(&op->view->resource->bind_count);
1522 if (prev)
1523 InterlockedDecrement(&prev->resource->bind_count);
1525 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1526 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1527 else
1528 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1531 void wined3d_cs_emit_set_shader_resource_view(struct wined3d_cs *cs, enum wined3d_shader_type type,
1532 UINT view_idx, struct wined3d_shader_resource_view *view)
1534 struct wined3d_cs_set_shader_resource_view *op;
1536 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1537 op->opcode = WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW;
1538 op->type = type;
1539 op->view_idx = view_idx;
1540 op->view = view;
1542 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1545 static void wined3d_cs_exec_set_unordered_access_view(struct wined3d_cs *cs, const void *data)
1547 const struct wined3d_cs_set_unordered_access_view *op = data;
1548 struct wined3d_unordered_access_view *prev;
1550 prev = cs->state.unordered_access_view[op->pipeline][op->view_idx];
1551 cs->state.unordered_access_view[op->pipeline][op->view_idx] = op->view;
1553 if (op->view)
1554 InterlockedIncrement(&op->view->resource->bind_count);
1555 if (prev)
1556 InterlockedDecrement(&prev->resource->bind_count);
1558 if (op->view && op->initial_count != ~0u)
1559 wined3d_unordered_access_view_set_counter(op->view, op->initial_count);
1561 device_invalidate_state(cs->device, STATE_UNORDERED_ACCESS_VIEW_BINDING(op->pipeline));
1564 void wined3d_cs_emit_set_unordered_access_view(struct wined3d_cs *cs, enum wined3d_pipeline pipeline,
1565 unsigned int view_idx, struct wined3d_unordered_access_view *view, unsigned int initial_count)
1567 struct wined3d_cs_set_unordered_access_view *op;
1569 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1570 op->opcode = WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW;
1571 op->pipeline = pipeline;
1572 op->view_idx = view_idx;
1573 op->view = view;
1574 op->initial_count = initial_count;
1576 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1579 static void wined3d_cs_exec_set_sampler(struct wined3d_cs *cs, const void *data)
1581 const struct wined3d_cs_set_sampler *op = data;
1583 cs->state.sampler[op->type][op->sampler_idx] = op->sampler;
1584 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1585 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1586 else
1587 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1590 void wined3d_cs_emit_set_sampler(struct wined3d_cs *cs, enum wined3d_shader_type type,
1591 UINT sampler_idx, struct wined3d_sampler *sampler)
1593 struct wined3d_cs_set_sampler *op;
1595 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1596 op->opcode = WINED3D_CS_OP_SET_SAMPLER;
1597 op->type = type;
1598 op->sampler_idx = sampler_idx;
1599 op->sampler = sampler;
1601 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1604 static void wined3d_cs_exec_set_shader(struct wined3d_cs *cs, const void *data)
1606 const struct wined3d_cs_set_shader *op = data;
1608 cs->state.shader[op->type] = op->shader;
1609 device_invalidate_state(cs->device, STATE_SHADER(op->type));
1610 if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1611 device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1612 else
1613 device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1616 void wined3d_cs_emit_set_shader(struct wined3d_cs *cs, enum wined3d_shader_type type, struct wined3d_shader *shader)
1618 struct wined3d_cs_set_shader *op;
1620 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1621 op->opcode = WINED3D_CS_OP_SET_SHADER;
1622 op->type = type;
1623 op->shader = shader;
1625 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1628 static void wined3d_cs_exec_set_blend_state(struct wined3d_cs *cs, const void *data)
1630 const struct wined3d_cs_set_blend_state *op = data;
1631 struct wined3d_state *state = &cs->state;
1633 if (state->blend_state != op->state)
1635 state->blend_state = op->state;
1636 device_invalidate_state(cs->device, STATE_BLEND);
1638 state->blend_factor = op->factor;
1639 device_invalidate_state(cs->device, STATE_BLEND_FACTOR);
1642 void wined3d_cs_emit_set_blend_state(struct wined3d_cs *cs, struct wined3d_blend_state *state,
1643 const struct wined3d_color *blend_factor)
1645 struct wined3d_cs_set_blend_state *op;
1647 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1648 op->opcode = WINED3D_CS_OP_SET_BLEND_STATE;
1649 op->state = state;
1650 op->factor = *blend_factor;
1652 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1655 static void wined3d_cs_exec_set_rasterizer_state(struct wined3d_cs *cs, const void *data)
1657 const struct wined3d_cs_set_rasterizer_state *op = data;
1659 cs->state.rasterizer_state = op->state;
1660 device_invalidate_state(cs->device, STATE_RASTERIZER);
1663 void wined3d_cs_emit_set_rasterizer_state(struct wined3d_cs *cs,
1664 struct wined3d_rasterizer_state *rasterizer_state)
1666 struct wined3d_cs_set_rasterizer_state *op;
1668 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1669 op->opcode = WINED3D_CS_OP_SET_RASTERIZER_STATE;
1670 op->state = rasterizer_state;
1672 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1675 static void wined3d_cs_exec_set_render_state(struct wined3d_cs *cs, const void *data)
1677 const struct wined3d_cs_set_render_state *op = data;
1679 cs->state.render_states[op->state] = op->value;
1680 device_invalidate_state(cs->device, STATE_RENDER(op->state));
1683 void wined3d_cs_emit_set_render_state(struct wined3d_cs *cs, enum wined3d_render_state state, DWORD value)
1685 struct wined3d_cs_set_render_state *op;
1687 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1688 op->opcode = WINED3D_CS_OP_SET_RENDER_STATE;
1689 op->state = state;
1690 op->value = value;
1692 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1695 static void wined3d_cs_exec_set_texture_state(struct wined3d_cs *cs, const void *data)
1697 const struct wined3d_cs_set_texture_state *op = data;
1699 cs->state.texture_states[op->stage][op->state] = op->value;
1700 device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, op->state));
1703 void wined3d_cs_emit_set_texture_state(struct wined3d_cs *cs, UINT stage,
1704 enum wined3d_texture_stage_state state, DWORD value)
1706 struct wined3d_cs_set_texture_state *op;
1708 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1709 op->opcode = WINED3D_CS_OP_SET_TEXTURE_STATE;
1710 op->stage = stage;
1711 op->state = state;
1712 op->value = value;
1714 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1717 static void wined3d_cs_exec_set_sampler_state(struct wined3d_cs *cs, const void *data)
1719 const struct wined3d_cs_set_sampler_state *op = data;
1721 cs->state.sampler_states[op->sampler_idx][op->state] = op->value;
1722 device_invalidate_state(cs->device, STATE_SAMPLER(op->sampler_idx));
1725 void wined3d_cs_emit_set_sampler_state(struct wined3d_cs *cs, UINT sampler_idx,
1726 enum wined3d_sampler_state state, DWORD value)
1728 struct wined3d_cs_set_sampler_state *op;
1730 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1731 op->opcode = WINED3D_CS_OP_SET_SAMPLER_STATE;
1732 op->sampler_idx = sampler_idx;
1733 op->state = state;
1734 op->value = value;
1736 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1739 static void wined3d_cs_exec_set_transform(struct wined3d_cs *cs, const void *data)
1741 const struct wined3d_cs_set_transform *op = data;
1743 cs->state.transforms[op->state] = op->matrix;
1744 if (op->state < WINED3D_TS_WORLD_MATRIX(cs->device->adapter->d3d_info.limits.ffp_vertex_blend_matrices))
1745 device_invalidate_state(cs->device, STATE_TRANSFORM(op->state));
1748 void wined3d_cs_emit_set_transform(struct wined3d_cs *cs, enum wined3d_transform_state state,
1749 const struct wined3d_matrix *matrix)
1751 struct wined3d_cs_set_transform *op;
1753 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1754 op->opcode = WINED3D_CS_OP_SET_TRANSFORM;
1755 op->state = state;
1756 op->matrix = *matrix;
1758 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1761 static void wined3d_cs_exec_set_clip_plane(struct wined3d_cs *cs, const void *data)
1763 const struct wined3d_cs_set_clip_plane *op = data;
1765 cs->state.clip_planes[op->plane_idx] = op->plane;
1766 device_invalidate_state(cs->device, STATE_CLIPPLANE(op->plane_idx));
1769 void wined3d_cs_emit_set_clip_plane(struct wined3d_cs *cs, UINT plane_idx, const struct wined3d_vec4 *plane)
1771 struct wined3d_cs_set_clip_plane *op;
1773 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1774 op->opcode = WINED3D_CS_OP_SET_CLIP_PLANE;
1775 op->plane_idx = plane_idx;
1776 op->plane = *plane;
1778 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1781 static void wined3d_cs_exec_set_color_key(struct wined3d_cs *cs, const void *data)
1783 const struct wined3d_cs_set_color_key *op = data;
1784 struct wined3d_texture *texture = op->texture;
1786 if (op->set)
1788 switch (op->flags)
1790 case WINED3D_CKEY_DST_BLT:
1791 texture->async.dst_blt_color_key = op->color_key;
1792 texture->async.color_key_flags |= WINED3D_CKEY_DST_BLT;
1793 break;
1795 case WINED3D_CKEY_DST_OVERLAY:
1796 texture->async.dst_overlay_color_key = op->color_key;
1797 texture->async.color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
1798 break;
1800 case WINED3D_CKEY_SRC_BLT:
1801 if (texture == cs->state.textures[0])
1803 device_invalidate_state(cs->device, STATE_COLOR_KEY);
1804 if (!(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1805 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1808 texture->async.src_blt_color_key = op->color_key;
1809 texture->async.color_key_flags |= WINED3D_CKEY_SRC_BLT;
1810 break;
1812 case WINED3D_CKEY_SRC_OVERLAY:
1813 texture->async.src_overlay_color_key = op->color_key;
1814 texture->async.color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
1815 break;
1818 else
1820 switch (op->flags)
1822 case WINED3D_CKEY_DST_BLT:
1823 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_BLT;
1824 break;
1826 case WINED3D_CKEY_DST_OVERLAY:
1827 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
1828 break;
1830 case WINED3D_CKEY_SRC_BLT:
1831 if (texture == cs->state.textures[0] && texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1832 device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1834 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
1835 break;
1837 case WINED3D_CKEY_SRC_OVERLAY:
1838 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
1839 break;
1844 void wined3d_cs_emit_set_color_key(struct wined3d_cs *cs, struct wined3d_texture *texture,
1845 WORD flags, const struct wined3d_color_key *color_key)
1847 struct wined3d_cs_set_color_key *op;
1849 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1850 op->opcode = WINED3D_CS_OP_SET_COLOR_KEY;
1851 op->texture = texture;
1852 op->flags = flags;
1853 if (color_key)
1855 op->color_key = *color_key;
1856 op->set = 1;
1858 else
1859 op->set = 0;
1861 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1864 static void wined3d_cs_exec_set_material(struct wined3d_cs *cs, const void *data)
1866 const struct wined3d_cs_set_material *op = data;
1868 cs->state.material = op->material;
1869 device_invalidate_state(cs->device, STATE_MATERIAL);
1872 void wined3d_cs_emit_set_material(struct wined3d_cs *cs, const struct wined3d_material *material)
1874 struct wined3d_cs_set_material *op;
1876 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1877 op->opcode = WINED3D_CS_OP_SET_MATERIAL;
1878 op->material = *material;
1880 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1883 static void wined3d_cs_exec_set_light(struct wined3d_cs *cs, const void *data)
1885 const struct wined3d_cs_set_light *op = data;
1886 struct wined3d_light_info *light_info;
1887 unsigned int light_idx, hash_idx;
1889 light_idx = op->light.OriginalIndex;
1891 if (!(light_info = wined3d_light_state_get_light(&cs->state.light_state, light_idx)))
1893 TRACE("Adding new light.\n");
1894 if (!(light_info = heap_alloc_zero(sizeof(*light_info))))
1896 ERR("Failed to allocate light info.\n");
1897 return;
1900 hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1901 list_add_head(&cs->state.light_state.light_map[hash_idx], &light_info->entry);
1902 light_info->glIndex = -1;
1903 light_info->OriginalIndex = light_idx;
1906 if (light_info->glIndex != -1)
1908 if (light_info->OriginalParms.type != op->light.OriginalParms.type)
1909 device_invalidate_state(cs->device, STATE_LIGHT_TYPE);
1910 device_invalidate_state(cs->device, STATE_ACTIVELIGHT(light_info->glIndex));
1913 light_info->OriginalParms = op->light.OriginalParms;
1914 light_info->position = op->light.position;
1915 light_info->direction = op->light.direction;
1916 light_info->exponent = op->light.exponent;
1917 light_info->cutoff = op->light.cutoff;
1920 void wined3d_cs_emit_set_light(struct wined3d_cs *cs, const struct wined3d_light_info *light)
1922 struct wined3d_cs_set_light *op;
1924 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1925 op->opcode = WINED3D_CS_OP_SET_LIGHT;
1926 op->light = *light;
1928 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1931 static void wined3d_cs_exec_set_light_enable(struct wined3d_cs *cs, const void *data)
1933 const struct wined3d_cs_set_light_enable *op = data;
1934 struct wined3d_device *device = cs->device;
1935 struct wined3d_light_info *light_info;
1936 int prev_idx;
1938 if (!(light_info = wined3d_light_state_get_light(&cs->state.light_state, op->idx)))
1940 ERR("Light doesn't exist.\n");
1941 return;
1944 prev_idx = light_info->glIndex;
1945 wined3d_light_state_enable_light(&cs->state.light_state, &device->adapter->d3d_info, light_info, op->enable);
1946 if (light_info->glIndex != prev_idx)
1948 device_invalidate_state(device, STATE_LIGHT_TYPE);
1949 device_invalidate_state(device, STATE_ACTIVELIGHT(op->enable ? light_info->glIndex : prev_idx));
1953 void wined3d_cs_emit_set_light_enable(struct wined3d_cs *cs, unsigned int idx, BOOL enable)
1955 struct wined3d_cs_set_light_enable *op;
1957 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1958 op->opcode = WINED3D_CS_OP_SET_LIGHT_ENABLE;
1959 op->idx = idx;
1960 op->enable = enable;
1962 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1965 static const struct
1967 size_t offset;
1968 size_t size;
1969 DWORD mask;
1971 wined3d_cs_push_constant_info[] =
1973 /* WINED3D_PUSH_CONSTANTS_VS_F */
1974 {FIELD_OFFSET(struct wined3d_state, vs_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_VS_F},
1975 /* WINED3D_PUSH_CONSTANTS_PS_F */
1976 {FIELD_OFFSET(struct wined3d_state, ps_consts_f), sizeof(struct wined3d_vec4), WINED3D_SHADER_CONST_PS_F},
1977 /* WINED3D_PUSH_CONSTANTS_VS_I */
1978 {FIELD_OFFSET(struct wined3d_state, vs_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_VS_I},
1979 /* WINED3D_PUSH_CONSTANTS_PS_I */
1980 {FIELD_OFFSET(struct wined3d_state, ps_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_PS_I},
1981 /* WINED3D_PUSH_CONSTANTS_VS_B */
1982 {FIELD_OFFSET(struct wined3d_state, vs_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_VS_B},
1983 /* WINED3D_PUSH_CONSTANTS_PS_B */
1984 {FIELD_OFFSET(struct wined3d_state, ps_consts_b), sizeof(BOOL), WINED3D_SHADER_CONST_PS_B},
1987 static void wined3d_cs_st_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1988 unsigned int start_idx, unsigned int count, const void *constants)
1990 struct wined3d_device *device = cs->device;
1991 unsigned int context_count;
1992 unsigned int i;
1993 size_t offset;
1995 if (p == WINED3D_PUSH_CONSTANTS_VS_F)
1996 device->shader_backend->shader_update_float_vertex_constants(device, start_idx, count);
1997 else if (p == WINED3D_PUSH_CONSTANTS_PS_F)
1998 device->shader_backend->shader_update_float_pixel_constants(device, start_idx, count);
2000 offset = wined3d_cs_push_constant_info[p].offset + start_idx * wined3d_cs_push_constant_info[p].size;
2001 memcpy((BYTE *)&cs->state + offset, constants, count * wined3d_cs_push_constant_info[p].size);
2002 for (i = 0, context_count = device->context_count; i < context_count; ++i)
2004 device->contexts[i]->constant_update_mask |= wined3d_cs_push_constant_info[p].mask;
2008 static void wined3d_cs_exec_push_constants(struct wined3d_cs *cs, const void *data)
2010 const struct wined3d_cs_push_constants *op = data;
2012 wined3d_cs_st_push_constants(cs, op->type, op->start_idx, op->count, op->constants);
2015 static void wined3d_cs_mt_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
2016 unsigned int start_idx, unsigned int count, const void *constants)
2018 struct wined3d_cs_push_constants *op;
2019 size_t size;
2021 size = count * wined3d_cs_push_constant_info[p].size;
2022 op = wined3d_cs_require_space(cs, FIELD_OFFSET(struct wined3d_cs_push_constants, constants[size]),
2023 WINED3D_CS_QUEUE_DEFAULT);
2024 op->opcode = WINED3D_CS_OP_PUSH_CONSTANTS;
2025 op->type = p;
2026 op->start_idx = start_idx;
2027 op->count = count;
2028 memcpy(op->constants, constants, size);
2030 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2033 static void wined3d_cs_exec_reset_state(struct wined3d_cs *cs, const void *data)
2035 struct wined3d_adapter *adapter = cs->device->adapter;
2037 state_cleanup(&cs->state);
2038 memset(&cs->state, 0, sizeof(cs->state));
2039 state_init(&cs->state, &adapter->d3d_info, WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
2042 void wined3d_cs_emit_reset_state(struct wined3d_cs *cs)
2044 struct wined3d_cs_reset_state *op;
2046 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2047 op->opcode = WINED3D_CS_OP_RESET_STATE;
2049 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2052 static void wined3d_cs_exec_callback(struct wined3d_cs *cs, const void *data)
2054 const struct wined3d_cs_callback *op = data;
2056 op->callback(op->object);
2059 static void wined3d_cs_emit_callback(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2061 struct wined3d_cs_callback *op;
2063 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2064 op->opcode = WINED3D_CS_OP_CALLBACK;
2065 op->callback = callback;
2066 op->object = object;
2068 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2071 void wined3d_cs_destroy_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2073 wined3d_cs_emit_callback(cs, callback, object);
2076 void wined3d_cs_init_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
2078 wined3d_cs_emit_callback(cs, callback, object);
2081 static void wined3d_cs_exec_query_issue(struct wined3d_cs *cs, const void *data)
2083 const struct wined3d_cs_query_issue *op = data;
2084 struct wined3d_query *query = op->query;
2085 BOOL poll;
2087 poll = query->query_ops->query_issue(query, op->flags);
2089 if (!cs->thread)
2090 return;
2092 if (poll && list_empty(&query->poll_list_entry))
2094 if (query->buffer_object)
2095 InterlockedIncrement(&query->counter_retrieved);
2096 else
2097 list_add_tail(&cs->query_poll_list, &query->poll_list_entry);
2098 return;
2101 /* This can happen if occlusion queries are restarted. This discards the
2102 * old result, since polling it could result in a GL error. */
2103 if ((op->flags & WINED3DISSUE_BEGIN) && !poll && !list_empty(&query->poll_list_entry))
2105 list_remove(&query->poll_list_entry);
2106 list_init(&query->poll_list_entry);
2107 InterlockedIncrement(&query->counter_retrieved);
2108 return;
2111 /* This can happen when an occlusion query is ended without being started,
2112 * in which case we don't want to poll, but still have to counter-balance
2113 * the increment of the main counter.
2115 * This can also happen if an event query is re-issued before the first
2116 * fence was reached. In this case the query is already in the list and
2117 * the poll function will check the new fence. We have to counter-balance
2118 * the discarded increment. */
2119 if (op->flags & WINED3DISSUE_END)
2120 InterlockedIncrement(&query->counter_retrieved);
2123 void wined3d_cs_emit_query_issue(struct wined3d_cs *cs, struct wined3d_query *query, DWORD flags)
2125 struct wined3d_cs_query_issue *op;
2127 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2128 op->opcode = WINED3D_CS_OP_QUERY_ISSUE;
2129 op->query = query;
2130 op->flags = flags;
2132 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2133 cs->queries_flushed = FALSE;
2136 static void wined3d_cs_exec_preload_resource(struct wined3d_cs *cs, const void *data)
2138 const struct wined3d_cs_preload_resource *op = data;
2139 struct wined3d_resource *resource = op->resource;
2141 resource->resource_ops->resource_preload(resource);
2142 wined3d_resource_release(resource);
2145 void wined3d_cs_emit_preload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
2147 struct wined3d_cs_preload_resource *op;
2149 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2150 op->opcode = WINED3D_CS_OP_PRELOAD_RESOURCE;
2151 op->resource = resource;
2153 wined3d_resource_acquire(resource);
2155 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2158 static void wined3d_cs_exec_unload_resource(struct wined3d_cs *cs, const void *data)
2160 const struct wined3d_cs_unload_resource *op = data;
2161 struct wined3d_resource *resource = op->resource;
2163 resource->resource_ops->resource_unload(resource);
2164 wined3d_resource_release(resource);
2167 void wined3d_cs_emit_unload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
2169 struct wined3d_cs_unload_resource *op;
2171 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2172 op->opcode = WINED3D_CS_OP_UNLOAD_RESOURCE;
2173 op->resource = resource;
2175 wined3d_resource_acquire(resource);
2177 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2180 static void wined3d_cs_exec_map(struct wined3d_cs *cs, const void *data)
2182 const struct wined3d_cs_map *op = data;
2183 struct wined3d_resource *resource = op->resource;
2185 *op->hr = resource->resource_ops->resource_sub_resource_map(resource,
2186 op->sub_resource_idx, op->map_desc, op->box, op->flags);
2189 HRESULT wined3d_cs_map(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx,
2190 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
2192 struct wined3d_cs_map *op;
2193 HRESULT hr;
2195 /* Mapping resources from the worker thread isn't an issue by itself, but
2196 * increasing the map count would be visible to applications. */
2197 wined3d_not_from_cs(cs);
2199 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2200 op->opcode = WINED3D_CS_OP_MAP;
2201 op->resource = resource;
2202 op->sub_resource_idx = sub_resource_idx;
2203 op->map_desc = map_desc;
2204 op->box = box;
2205 op->flags = flags;
2206 op->hr = &hr;
2208 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_MAP);
2209 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_MAP);
2211 return hr;
2214 static void wined3d_cs_exec_unmap(struct wined3d_cs *cs, const void *data)
2216 const struct wined3d_cs_unmap *op = data;
2217 struct wined3d_resource *resource = op->resource;
2219 *op->hr = resource->resource_ops->resource_sub_resource_unmap(resource, op->sub_resource_idx);
2222 HRESULT wined3d_cs_unmap(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx)
2224 struct wined3d_cs_unmap *op;
2225 HRESULT hr;
2227 wined3d_not_from_cs(cs);
2229 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2230 op->opcode = WINED3D_CS_OP_UNMAP;
2231 op->resource = resource;
2232 op->sub_resource_idx = sub_resource_idx;
2233 op->hr = &hr;
2235 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_MAP);
2236 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_MAP);
2238 return hr;
2241 static void wined3d_cs_exec_blt_sub_resource(struct wined3d_cs *cs, const void *data)
2243 const struct wined3d_cs_blt_sub_resource *op = data;
2245 if (op->dst_resource->type == WINED3D_RTYPE_BUFFER)
2247 wined3d_buffer_copy(buffer_from_resource(op->dst_resource), op->dst_box.left,
2248 buffer_from_resource(op->src_resource), op->src_box.left,
2249 op->src_box.right - op->src_box.left);
2251 else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_3D)
2253 struct wined3d_texture *src_texture, *dst_texture;
2254 unsigned int level, update_w, update_h, update_d;
2255 unsigned int row_pitch, slice_pitch;
2256 struct wined3d_context *context;
2257 struct wined3d_bo_address addr;
2259 if (op->flags & ~WINED3D_BLT_RAW)
2261 FIXME("Flags %#x not implemented for %s resources.\n",
2262 op->flags, debug_d3dresourcetype(op->dst_resource->type));
2263 goto error;
2266 if (!(op->flags & WINED3D_BLT_RAW) && op->src_resource->format != op->dst_resource->format)
2268 FIXME("Format conversion not implemented for %s resources.\n",
2269 debug_d3dresourcetype(op->dst_resource->type));
2270 goto error;
2273 update_w = op->dst_box.right - op->dst_box.left;
2274 update_h = op->dst_box.bottom - op->dst_box.top;
2275 update_d = op->dst_box.back - op->dst_box.front;
2276 if (op->src_box.right - op->src_box.left != update_w
2277 || op->src_box.bottom - op->src_box.top != update_h
2278 || op->src_box.back - op->src_box.front != update_d)
2280 FIXME("Stretching not implemented for %s resources.\n",
2281 debug_d3dresourcetype(op->dst_resource->type));
2282 goto error;
2285 dst_texture = texture_from_resource(op->dst_resource);
2286 src_texture = texture_from_resource(op->src_resource);
2288 context = context_acquire(cs->device, NULL, 0);
2290 if (!wined3d_texture_load_location(src_texture, op->src_sub_resource_idx,
2291 context, src_texture->resource.map_binding))
2293 ERR("Failed to load source sub-resource into %s.\n",
2294 wined3d_debug_location(src_texture->resource.map_binding));
2295 context_release(context);
2296 goto error;
2299 level = op->dst_sub_resource_idx % dst_texture->level_count;
2300 if (update_w == wined3d_texture_get_level_width(dst_texture, level)
2301 && update_h == wined3d_texture_get_level_height(dst_texture, level)
2302 && update_d == wined3d_texture_get_level_depth(dst_texture, level))
2304 wined3d_texture_prepare_location(dst_texture, op->dst_sub_resource_idx,
2305 context, WINED3D_LOCATION_TEXTURE_RGB);
2307 else if (!wined3d_texture_load_location(dst_texture, op->dst_sub_resource_idx,
2308 context, WINED3D_LOCATION_TEXTURE_RGB))
2310 ERR("Failed to load destination sub-resource.\n");
2311 context_release(context);
2312 goto error;
2315 wined3d_texture_get_memory(src_texture, op->src_sub_resource_idx, &addr, src_texture->resource.map_binding);
2316 wined3d_texture_get_pitch(src_texture, op->src_sub_resource_idx % src_texture->level_count,
2317 &row_pitch, &slice_pitch);
2319 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&addr),
2320 dst_texture->resource.format, &op->src_box, row_pitch, slice_pitch, dst_texture,
2321 op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
2322 op->dst_box.left, op->dst_box.top, op->dst_box.front);
2323 wined3d_texture_validate_location(dst_texture, op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2324 wined3d_texture_invalidate_location(dst_texture, op->dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2326 context_release(context);
2328 else
2330 if (FAILED(texture2d_blt(texture_from_resource(op->dst_resource), op->dst_sub_resource_idx,
2331 &op->dst_box, texture_from_resource(op->src_resource), op->src_sub_resource_idx,
2332 &op->src_box, op->flags, &op->fx, op->filter)))
2333 FIXME("Blit failed.\n");
2336 error:
2337 if (op->src_resource)
2338 wined3d_resource_release(op->src_resource);
2339 wined3d_resource_release(op->dst_resource);
2342 void wined3d_cs_emit_blt_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *dst_resource,
2343 unsigned int dst_sub_resource_idx, const struct wined3d_box *dst_box, struct wined3d_resource *src_resource,
2344 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, DWORD flags,
2345 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2347 struct wined3d_cs_blt_sub_resource *op;
2349 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2350 op->opcode = WINED3D_CS_OP_BLT_SUB_RESOURCE;
2351 op->dst_resource = dst_resource;
2352 op->dst_sub_resource_idx = dst_sub_resource_idx;
2353 op->dst_box = *dst_box;
2354 op->src_resource = src_resource;
2355 op->src_sub_resource_idx = src_sub_resource_idx;
2356 op->src_box = *src_box;
2357 op->flags = flags;
2358 if (fx)
2359 op->fx = *fx;
2360 else
2361 memset(&op->fx, 0, sizeof(op->fx));
2362 op->filter = filter;
2364 wined3d_resource_acquire(dst_resource);
2365 if (src_resource)
2366 wined3d_resource_acquire(src_resource);
2368 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2369 if (flags & WINED3D_BLT_SYNCHRONOUS)
2370 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2373 static void wined3d_cs_exec_update_sub_resource(struct wined3d_cs *cs, const void *data)
2375 const struct wined3d_cs_update_sub_resource *op = data;
2376 struct wined3d_resource *resource = op->resource;
2377 const struct wined3d_box *box = &op->box;
2378 unsigned int width, height, depth, level;
2379 struct wined3d_const_bo_address addr;
2380 struct wined3d_context *context;
2381 struct wined3d_texture *texture;
2382 struct wined3d_box src_box;
2384 context = context_acquire(cs->device, NULL, 0);
2386 if (resource->type == WINED3D_RTYPE_BUFFER)
2388 struct wined3d_buffer *buffer = buffer_from_resource(resource);
2390 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
2392 ERR("Failed to load buffer location.\n");
2393 goto done;
2396 wined3d_buffer_upload_data(buffer, context, box, op->data.data);
2397 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
2398 goto done;
2401 texture = wined3d_texture_from_resource(resource);
2403 level = op->sub_resource_idx % texture->level_count;
2404 width = wined3d_texture_get_level_width(texture, level);
2405 height = wined3d_texture_get_level_height(texture, level);
2406 depth = wined3d_texture_get_level_depth(texture, level);
2408 addr.buffer_object = 0;
2409 addr.addr = op->data.data;
2411 /* Only load the sub-resource for partial updates. */
2412 if (!box->left && !box->top && !box->front
2413 && box->right == width && box->bottom == height && box->back == depth)
2414 wined3d_texture_prepare_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2415 else
2416 wined3d_texture_load_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2418 wined3d_box_set(&src_box, 0, 0, box->right - box->left, box->bottom - box->top, 0, box->back - box->front);
2419 texture->texture_ops->texture_upload_data(context, &addr, texture->resource.format, &src_box,
2420 op->data.row_pitch, op->data.slice_pitch, texture, op->sub_resource_idx,
2421 WINED3D_LOCATION_TEXTURE_RGB, box->left, box->top, box->front);
2423 wined3d_texture_validate_location(texture, op->sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2424 wined3d_texture_invalidate_location(texture, op->sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2426 done:
2427 context_release(context);
2429 wined3d_resource_release(resource);
2432 void wined3d_cs_emit_update_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *resource,
2433 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
2434 unsigned int slice_pitch)
2436 struct wined3d_cs_update_sub_resource *op;
2438 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2439 op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2440 op->resource = resource;
2441 op->sub_resource_idx = sub_resource_idx;
2442 op->box = *box;
2443 op->data.row_pitch = row_pitch;
2444 op->data.slice_pitch = slice_pitch;
2445 op->data.data = data;
2447 wined3d_resource_acquire(resource);
2449 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_MAP);
2450 /* The data pointer may go away, so we need to wait until it is read.
2451 * Copying the data may be faster if it's small. */
2452 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_MAP);
2455 static void wined3d_cs_exec_add_dirty_texture_region(struct wined3d_cs *cs, const void *data)
2457 const struct wined3d_cs_add_dirty_texture_region *op = data;
2458 struct wined3d_texture *texture = op->texture;
2459 unsigned int sub_resource_idx, i;
2460 struct wined3d_context *context;
2462 context = context_acquire(cs->device, NULL, 0);
2463 sub_resource_idx = op->layer * texture->level_count;
2464 for (i = 0; i < texture->level_count; ++i, ++sub_resource_idx)
2466 if (wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
2467 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2468 else
2469 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
2471 context_release(context);
2473 wined3d_resource_release(&texture->resource);
2476 void wined3d_cs_emit_add_dirty_texture_region(struct wined3d_cs *cs,
2477 struct wined3d_texture *texture, unsigned int layer)
2479 struct wined3d_cs_add_dirty_texture_region *op;
2481 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2482 op->opcode = WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION;
2483 op->texture = texture;
2484 op->layer = layer;
2486 wined3d_resource_acquire(&texture->resource);
2488 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2491 static void wined3d_cs_exec_clear_unordered_access_view(struct wined3d_cs *cs, const void *data)
2493 const struct wined3d_cs_clear_unordered_access_view *op = data;
2494 struct wined3d_unordered_access_view *view = op->view;
2495 struct wined3d_context *context;
2497 context = context_acquire(cs->device, NULL, 0);
2498 cs->device->adapter->adapter_ops->adapter_clear_uav(context, view, &op->clear_value);
2499 context_release(context);
2501 wined3d_resource_release(view->resource);
2504 void wined3d_cs_emit_clear_unordered_access_view_uint(struct wined3d_cs *cs,
2505 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
2507 struct wined3d_cs_clear_unordered_access_view *op;
2509 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2510 op->opcode = WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW;
2511 op->view = view;
2512 op->clear_value = *clear_value;
2514 wined3d_resource_acquire(view->resource);
2516 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2519 static void wined3d_cs_exec_copy_uav_counter(struct wined3d_cs *cs, const void *data)
2521 const struct wined3d_cs_copy_uav_counter *op = data;
2522 struct wined3d_unordered_access_view *view = op->view;
2523 struct wined3d_context *context;
2525 context = context_acquire(cs->device, NULL, 0);
2526 wined3d_unordered_access_view_copy_counter(view, op->buffer, op->offset, context);
2527 context_release(context);
2529 wined3d_resource_release(&op->buffer->resource);
2532 void wined3d_cs_emit_copy_uav_counter(struct wined3d_cs *cs, struct wined3d_buffer *dst_buffer,
2533 unsigned int offset, struct wined3d_unordered_access_view *uav)
2535 struct wined3d_cs_copy_uav_counter *op;
2537 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2538 op->opcode = WINED3D_CS_OP_COPY_UAV_COUNTER;
2539 op->buffer = dst_buffer;
2540 op->offset = offset;
2541 op->view = uav;
2543 wined3d_resource_acquire(&dst_buffer->resource);
2545 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2548 static void wined3d_cs_exec_generate_mipmaps(struct wined3d_cs *cs, const void *data)
2550 const struct wined3d_cs_generate_mipmaps *op = data;
2551 struct wined3d_shader_resource_view *view = op->view;
2553 shader_resource_view_generate_mipmaps(view);
2554 wined3d_resource_release(view->resource);
2557 void wined3d_cs_emit_generate_mipmaps(struct wined3d_cs *cs, struct wined3d_shader_resource_view *view)
2559 struct wined3d_cs_generate_mipmaps *op;
2561 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2562 op->opcode = WINED3D_CS_OP_GENERATE_MIPMAPS;
2563 op->view = view;
2565 wined3d_resource_acquire(view->resource);
2567 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2570 static void wined3d_cs_emit_stop(struct wined3d_cs *cs)
2572 struct wined3d_cs_stop *op;
2574 op = wined3d_cs_require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2575 op->opcode = WINED3D_CS_OP_STOP;
2577 wined3d_cs_submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2578 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2581 static void (* const wined3d_cs_op_handlers[])(struct wined3d_cs *cs, const void *data) =
2583 /* WINED3D_CS_OP_NOP */ wined3d_cs_exec_nop,
2584 /* WINED3D_CS_OP_PRESENT */ wined3d_cs_exec_present,
2585 /* WINED3D_CS_OP_CLEAR */ wined3d_cs_exec_clear,
2586 /* WINED3D_CS_OP_DISPATCH */ wined3d_cs_exec_dispatch,
2587 /* WINED3D_CS_OP_DRAW */ wined3d_cs_exec_draw,
2588 /* WINED3D_CS_OP_FLUSH */ wined3d_cs_exec_flush,
2589 /* WINED3D_CS_OP_SET_PREDICATION */ wined3d_cs_exec_set_predication,
2590 /* WINED3D_CS_OP_SET_VIEWPORTS */ wined3d_cs_exec_set_viewports,
2591 /* WINED3D_CS_OP_SET_SCISSOR_RECTS */ wined3d_cs_exec_set_scissor_rects,
2592 /* WINED3D_CS_OP_SET_RENDERTARGET_VIEW */ wined3d_cs_exec_set_rendertarget_view,
2593 /* WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW */ wined3d_cs_exec_set_depth_stencil_view,
2594 /* WINED3D_CS_OP_SET_VERTEX_DECLARATION */ wined3d_cs_exec_set_vertex_declaration,
2595 /* WINED3D_CS_OP_SET_STREAM_SOURCE */ wined3d_cs_exec_set_stream_source,
2596 /* WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ */ wined3d_cs_exec_set_stream_source_freq,
2597 /* WINED3D_CS_OP_SET_STREAM_OUTPUT */ wined3d_cs_exec_set_stream_output,
2598 /* WINED3D_CS_OP_SET_INDEX_BUFFER */ wined3d_cs_exec_set_index_buffer,
2599 /* WINED3D_CS_OP_SET_CONSTANT_BUFFER */ wined3d_cs_exec_set_constant_buffer,
2600 /* WINED3D_CS_OP_SET_TEXTURE */ wined3d_cs_exec_set_texture,
2601 /* WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW */ wined3d_cs_exec_set_shader_resource_view,
2602 /* WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_set_unordered_access_view,
2603 /* WINED3D_CS_OP_SET_SAMPLER */ wined3d_cs_exec_set_sampler,
2604 /* WINED3D_CS_OP_SET_SHADER */ wined3d_cs_exec_set_shader,
2605 /* WINED3D_CS_OP_SET_BLEND_STATE */ wined3d_cs_exec_set_blend_state,
2606 /* WINED3D_CS_OP_SET_RASTERIZER_STATE */ wined3d_cs_exec_set_rasterizer_state,
2607 /* WINED3D_CS_OP_SET_RENDER_STATE */ wined3d_cs_exec_set_render_state,
2608 /* WINED3D_CS_OP_SET_TEXTURE_STATE */ wined3d_cs_exec_set_texture_state,
2609 /* WINED3D_CS_OP_SET_SAMPLER_STATE */ wined3d_cs_exec_set_sampler_state,
2610 /* WINED3D_CS_OP_SET_TRANSFORM */ wined3d_cs_exec_set_transform,
2611 /* WINED3D_CS_OP_SET_CLIP_PLANE */ wined3d_cs_exec_set_clip_plane,
2612 /* WINED3D_CS_OP_SET_COLOR_KEY */ wined3d_cs_exec_set_color_key,
2613 /* WINED3D_CS_OP_SET_MATERIAL */ wined3d_cs_exec_set_material,
2614 /* WINED3D_CS_OP_SET_LIGHT */ wined3d_cs_exec_set_light,
2615 /* WINED3D_CS_OP_SET_LIGHT_ENABLE */ wined3d_cs_exec_set_light_enable,
2616 /* WINED3D_CS_OP_PUSH_CONSTANTS */ wined3d_cs_exec_push_constants,
2617 /* WINED3D_CS_OP_RESET_STATE */ wined3d_cs_exec_reset_state,
2618 /* WINED3D_CS_OP_CALLBACK */ wined3d_cs_exec_callback,
2619 /* WINED3D_CS_OP_QUERY_ISSUE */ wined3d_cs_exec_query_issue,
2620 /* WINED3D_CS_OP_PRELOAD_RESOURCE */ wined3d_cs_exec_preload_resource,
2621 /* WINED3D_CS_OP_UNLOAD_RESOURCE */ wined3d_cs_exec_unload_resource,
2622 /* WINED3D_CS_OP_MAP */ wined3d_cs_exec_map,
2623 /* WINED3D_CS_OP_UNMAP */ wined3d_cs_exec_unmap,
2624 /* WINED3D_CS_OP_BLT_SUB_RESOURCE */ wined3d_cs_exec_blt_sub_resource,
2625 /* WINED3D_CS_OP_UPDATE_SUB_RESOURCE */ wined3d_cs_exec_update_sub_resource,
2626 /* WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION */ wined3d_cs_exec_add_dirty_texture_region,
2627 /* WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_clear_unordered_access_view,
2628 /* WINED3D_CS_OP_COPY_UAV_COUNTER */ wined3d_cs_exec_copy_uav_counter,
2629 /* WINED3D_CS_OP_GENERATE_MIPMAPS */ wined3d_cs_exec_generate_mipmaps,
2632 static void *wined3d_cs_st_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2634 if (size > (cs->data_size - cs->end))
2636 size_t new_size;
2637 void *new_data;
2639 new_size = max(size, cs->data_size * 2);
2640 if (!cs->end)
2641 new_data = heap_realloc(cs->data, new_size);
2642 else
2643 new_data = heap_alloc(new_size);
2644 if (!new_data)
2645 return NULL;
2647 cs->data_size = new_size;
2648 cs->start = cs->end = 0;
2649 cs->data = new_data;
2652 cs->end += size;
2654 return (BYTE *)cs->data + cs->start;
2657 static void wined3d_cs_st_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2659 enum wined3d_cs_op opcode;
2660 size_t start;
2661 BYTE *data;
2663 data = cs->data;
2664 start = cs->start;
2665 cs->start = cs->end;
2667 opcode = *(const enum wined3d_cs_op *)&data[start];
2668 if (opcode >= WINED3D_CS_OP_STOP)
2669 ERR("Invalid opcode %#x.\n", opcode);
2670 else
2671 wined3d_cs_op_handlers[opcode](cs, &data[start]);
2673 if (cs->data == data)
2674 cs->start = cs->end = start;
2675 else if (!start)
2676 heap_free(data);
2679 static void wined3d_cs_st_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2683 static const struct wined3d_cs_ops wined3d_cs_st_ops =
2685 wined3d_cs_st_require_space,
2686 wined3d_cs_st_submit,
2687 wined3d_cs_st_finish,
2688 wined3d_cs_st_push_constants,
2691 static BOOL wined3d_cs_queue_is_empty(const struct wined3d_cs *cs, const struct wined3d_cs_queue *queue)
2693 wined3d_from_cs(cs);
2694 return *(volatile LONG *)&queue->head == queue->tail;
2697 static void wined3d_cs_queue_submit(struct wined3d_cs_queue *queue, struct wined3d_cs *cs)
2699 struct wined3d_cs_packet *packet;
2700 size_t packet_size;
2702 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2703 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2704 InterlockedExchange(&queue->head, (queue->head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1));
2706 if (InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2707 SetEvent(cs->event);
2710 static void wined3d_cs_mt_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2712 if (cs->thread_id == GetCurrentThreadId())
2713 return wined3d_cs_st_submit(cs, queue_id);
2715 wined3d_cs_queue_submit(&cs->queue[queue_id], cs);
2718 static void *wined3d_cs_queue_require_space(struct wined3d_cs_queue *queue, size_t size, struct wined3d_cs *cs)
2720 size_t queue_size = ARRAY_SIZE(queue->data);
2721 size_t header_size, packet_size, remaining;
2722 struct wined3d_cs_packet *packet;
2724 header_size = FIELD_OFFSET(struct wined3d_cs_packet, data[0]);
2725 packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[size]);
2726 packet_size = (packet_size + header_size - 1) & ~(header_size - 1);
2727 size = packet_size - header_size;
2728 if (packet_size >= WINED3D_CS_QUEUE_SIZE)
2730 ERR("Packet size %lu >= queue size %u.\n",
2731 (unsigned long)packet_size, WINED3D_CS_QUEUE_SIZE);
2732 return NULL;
2735 remaining = queue_size - queue->head;
2736 if (remaining < packet_size)
2738 size_t nop_size = remaining - header_size;
2739 struct wined3d_cs_nop *nop;
2741 TRACE("Inserting a nop for %lu + %lu bytes.\n",
2742 (unsigned long)header_size, (unsigned long)nop_size);
2744 nop = wined3d_cs_queue_require_space(queue, nop_size, cs);
2745 if (nop_size)
2746 nop->opcode = WINED3D_CS_OP_NOP;
2748 wined3d_cs_queue_submit(queue, cs);
2749 assert(!queue->head);
2752 for (;;)
2754 LONG tail = *(volatile LONG *)&queue->tail;
2755 LONG head = queue->head;
2756 LONG new_pos;
2758 /* Empty. */
2759 if (head == tail)
2760 break;
2761 new_pos = (head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1);
2762 /* Head ahead of tail. We checked the remaining size above, so we only
2763 * need to make sure we don't make head equal to tail. */
2764 if (head > tail && (new_pos != tail))
2765 break;
2766 /* Tail ahead of head. Make sure the new head is before the tail as
2767 * well. Note that new_pos is 0 when it's at the end of the queue. */
2768 if (new_pos < tail && new_pos)
2769 break;
2771 TRACE("Waiting for free space. Head %u, tail %u, packet size %lu.\n",
2772 head, tail, (unsigned long)packet_size);
2775 packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2776 packet->size = size;
2777 return packet->data;
2780 static void *wined3d_cs_mt_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2782 if (cs->thread_id == GetCurrentThreadId())
2783 return wined3d_cs_st_require_space(cs, size, queue_id);
2785 return wined3d_cs_queue_require_space(&cs->queue[queue_id], size, cs);
2788 static void wined3d_cs_mt_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2790 if (cs->thread_id == GetCurrentThreadId())
2791 return wined3d_cs_st_finish(cs, queue_id);
2793 while (cs->queue[queue_id].head != *(volatile LONG *)&cs->queue[queue_id].tail)
2794 wined3d_pause();
2797 static const struct wined3d_cs_ops wined3d_cs_mt_ops =
2799 wined3d_cs_mt_require_space,
2800 wined3d_cs_mt_submit,
2801 wined3d_cs_mt_finish,
2802 wined3d_cs_mt_push_constants,
2805 static void poll_queries(struct wined3d_cs *cs)
2807 struct wined3d_query *query, *cursor;
2809 LIST_FOR_EACH_ENTRY_SAFE(query, cursor, &cs->query_poll_list, struct wined3d_query, poll_list_entry)
2811 if (!query->query_ops->query_poll(query, 0))
2812 continue;
2814 list_remove(&query->poll_list_entry);
2815 list_init(&query->poll_list_entry);
2816 InterlockedIncrement(&query->counter_retrieved);
2820 static void wined3d_cs_wait_event(struct wined3d_cs *cs)
2822 InterlockedExchange(&cs->waiting_for_event, TRUE);
2824 /* The main thread might have enqueued a command and blocked on it after
2825 * the CS thread decided to enter wined3d_cs_wait_event(), but before
2826 * "waiting_for_event" was set.
2828 * Likewise, we can race with the main thread when resetting
2829 * "waiting_for_event", in which case we would need to call
2830 * WaitForSingleObject() because the main thread called SetEvent(). */
2831 if (!(wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_DEFAULT])
2832 && wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_MAP]))
2833 && InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2834 return;
2836 WaitForSingleObject(cs->event, INFINITE);
2839 static DWORD WINAPI wined3d_cs_run(void *ctx)
2841 struct wined3d_cs_packet *packet;
2842 struct wined3d_cs_queue *queue;
2843 unsigned int spin_count = 0;
2844 struct wined3d_cs *cs = ctx;
2845 enum wined3d_cs_op opcode;
2846 HMODULE wined3d_module;
2847 unsigned int poll = 0;
2848 LONG tail;
2850 TRACE("Started.\n");
2852 /* Copy the module handle to a local variable to avoid racing with the
2853 * thread freeing "cs" before the FreeLibraryAndExitThread() call. */
2854 wined3d_module = cs->wined3d_module;
2856 list_init(&cs->query_poll_list);
2857 cs->thread_id = GetCurrentThreadId();
2858 for (;;)
2860 if (++poll == WINED3D_CS_QUERY_POLL_INTERVAL)
2862 poll_queries(cs);
2863 poll = 0;
2866 queue = &cs->queue[WINED3D_CS_QUEUE_MAP];
2867 if (wined3d_cs_queue_is_empty(cs, queue))
2869 queue = &cs->queue[WINED3D_CS_QUEUE_DEFAULT];
2870 if (wined3d_cs_queue_is_empty(cs, queue))
2872 if (++spin_count >= WINED3D_CS_SPIN_COUNT && list_empty(&cs->query_poll_list))
2873 wined3d_cs_wait_event(cs);
2874 continue;
2877 spin_count = 0;
2879 tail = queue->tail;
2880 packet = (struct wined3d_cs_packet *)&queue->data[tail];
2881 if (packet->size)
2883 opcode = *(const enum wined3d_cs_op *)packet->data;
2885 TRACE("Executing %s.\n", debug_cs_op(opcode));
2886 if (opcode >= WINED3D_CS_OP_STOP)
2888 if (opcode > WINED3D_CS_OP_STOP)
2889 ERR("Invalid opcode %#x.\n", opcode);
2890 break;
2893 wined3d_cs_op_handlers[opcode](cs, packet->data);
2894 TRACE("%s executed.\n", debug_cs_op(opcode));
2897 tail += FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2898 tail &= (WINED3D_CS_QUEUE_SIZE - 1);
2899 InterlockedExchange(&queue->tail, tail);
2902 cs->queue[WINED3D_CS_QUEUE_MAP].tail = cs->queue[WINED3D_CS_QUEUE_MAP].head;
2903 cs->queue[WINED3D_CS_QUEUE_DEFAULT].tail = cs->queue[WINED3D_CS_QUEUE_DEFAULT].head;
2904 TRACE("Stopped.\n");
2905 FreeLibraryAndExitThread(wined3d_module, 0);
2908 struct wined3d_cs *wined3d_cs_create(struct wined3d_device *device)
2910 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2911 struct wined3d_cs *cs;
2913 if (!(cs = heap_alloc_zero(sizeof(*cs))))
2914 return NULL;
2916 cs->ops = &wined3d_cs_st_ops;
2917 cs->device = device;
2919 state_init(&cs->state, d3d_info, WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
2921 cs->data_size = WINED3D_INITIAL_CS_SIZE;
2922 if (!(cs->data = heap_alloc(cs->data_size)))
2923 goto fail;
2925 if (wined3d_settings.cs_multithreaded
2926 && !RtlIsCriticalSectionLockedByThread(NtCurrentTeb()->Peb->LoaderLock))
2928 cs->ops = &wined3d_cs_mt_ops;
2930 if (!(cs->event = CreateEventW(NULL, FALSE, FALSE, NULL)))
2932 ERR("Failed to create command stream event.\n");
2933 heap_free(cs->data);
2934 goto fail;
2937 if (!(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2938 (const WCHAR *)wined3d_cs_run, &cs->wined3d_module)))
2940 ERR("Failed to get wined3d module handle.\n");
2941 CloseHandle(cs->event);
2942 heap_free(cs->data);
2943 goto fail;
2946 if (!(cs->thread = CreateThread(NULL, 0, wined3d_cs_run, cs, 0, NULL)))
2948 ERR("Failed to create wined3d command stream thread.\n");
2949 FreeLibrary(cs->wined3d_module);
2950 CloseHandle(cs->event);
2951 heap_free(cs->data);
2952 goto fail;
2956 return cs;
2958 fail:
2959 state_cleanup(&cs->state);
2960 heap_free(cs);
2961 return NULL;
2964 void wined3d_cs_destroy(struct wined3d_cs *cs)
2966 if (cs->thread)
2968 wined3d_cs_emit_stop(cs);
2969 CloseHandle(cs->thread);
2970 if (!CloseHandle(cs->event))
2971 ERR("Closing event failed.\n");
2974 state_cleanup(&cs->state);
2975 heap_free(cs->data);
2976 heap_free(cs);