xref: /reactos/dll/directx/wine/wined3d/cs.c (revision 81cffd76)
1 /*
2  * Copyright 2013 Henri Verbeet for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 #include "config.h"
20 #include "wine/port.h"
21 #include "wined3d_private.h"
22 
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
24 
25 #define WINED3D_INITIAL_CS_SIZE 4096
26 
27 enum wined3d_cs_op
28 {
29     WINED3D_CS_OP_NOP,
30     WINED3D_CS_OP_PRESENT,
31     WINED3D_CS_OP_CLEAR,
32     WINED3D_CS_OP_DISPATCH,
33     WINED3D_CS_OP_DRAW,
34     WINED3D_CS_OP_FLUSH,
35     WINED3D_CS_OP_SET_PREDICATION,
36     WINED3D_CS_OP_SET_VIEWPORT,
37     WINED3D_CS_OP_SET_SCISSOR_RECT,
38     WINED3D_CS_OP_SET_RENDERTARGET_VIEW,
39     WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW,
40     WINED3D_CS_OP_SET_VERTEX_DECLARATION,
41     WINED3D_CS_OP_SET_STREAM_SOURCE,
42     WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ,
43     WINED3D_CS_OP_SET_STREAM_OUTPUT,
44     WINED3D_CS_OP_SET_INDEX_BUFFER,
45     WINED3D_CS_OP_SET_CONSTANT_BUFFER,
46     WINED3D_CS_OP_SET_TEXTURE,
47     WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW,
48     WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW,
49     WINED3D_CS_OP_SET_SAMPLER,
50     WINED3D_CS_OP_SET_SHADER,
51     WINED3D_CS_OP_SET_BLEND_STATE,
52     WINED3D_CS_OP_SET_RASTERIZER_STATE,
53     WINED3D_CS_OP_SET_RENDER_STATE,
54     WINED3D_CS_OP_SET_TEXTURE_STATE,
55     WINED3D_CS_OP_SET_SAMPLER_STATE,
56     WINED3D_CS_OP_SET_TRANSFORM,
57     WINED3D_CS_OP_SET_CLIP_PLANE,
58     WINED3D_CS_OP_SET_COLOR_KEY,
59     WINED3D_CS_OP_SET_MATERIAL,
60     WINED3D_CS_OP_SET_LIGHT,
61     WINED3D_CS_OP_SET_LIGHT_ENABLE,
62     WINED3D_CS_OP_PUSH_CONSTANTS,
63     WINED3D_CS_OP_RESET_STATE,
64     WINED3D_CS_OP_CALLBACK,
65     WINED3D_CS_OP_QUERY_ISSUE,
66     WINED3D_CS_OP_PRELOAD_RESOURCE,
67     WINED3D_CS_OP_UNLOAD_RESOURCE,
68     WINED3D_CS_OP_MAP,
69     WINED3D_CS_OP_UNMAP,
70     WINED3D_CS_OP_BLT_SUB_RESOURCE,
71     WINED3D_CS_OP_UPDATE_SUB_RESOURCE,
72     WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION,
73     WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW,
74     WINED3D_CS_OP_COPY_UAV_COUNTER,
75     WINED3D_CS_OP_GENERATE_MIPMAPS,
76     WINED3D_CS_OP_STOP,
77 };
78 
79 struct wined3d_cs_packet
80 {
81     size_t size;
82     BYTE data[1];
83 };
84 
85 struct wined3d_cs_nop
86 {
87     enum wined3d_cs_op opcode;
88 };
89 
90 struct wined3d_cs_present
91 {
92     enum wined3d_cs_op opcode;
93     HWND dst_window_override;
94     struct wined3d_swapchain *swapchain;
95     RECT src_rect;
96     RECT dst_rect;
97     DWORD swap_interval;
98     DWORD flags;
99 };
100 
101 struct wined3d_cs_clear
102 {
103     enum wined3d_cs_op opcode;
104     DWORD flags;
105     unsigned int rt_count;
106     struct wined3d_fb_state *fb;
107     RECT draw_rect;
108     struct wined3d_color color;
109     float depth;
110     DWORD stencil;
111     unsigned int rect_count;
112     RECT rects[1];
113 };
114 
115 struct wined3d_cs_dispatch
116 {
117     enum wined3d_cs_op opcode;
118     struct wined3d_dispatch_parameters parameters;
119 };
120 
121 struct wined3d_cs_draw
122 {
123     enum wined3d_cs_op opcode;
124     GLenum primitive_type;
125     GLint patch_vertex_count;
126     struct wined3d_draw_parameters parameters;
127 };
128 
129 struct wined3d_cs_flush
130 {
131     enum wined3d_cs_op opcode;
132 };
133 
134 struct wined3d_cs_set_predication
135 {
136     enum wined3d_cs_op opcode;
137     struct wined3d_query *predicate;
138     BOOL value;
139 };
140 
141 struct wined3d_cs_set_viewport
142 {
143     enum wined3d_cs_op opcode;
144     struct wined3d_viewport viewport;
145 };
146 
147 struct wined3d_cs_set_scissor_rect
148 {
149     enum wined3d_cs_op opcode;
150     RECT rect;
151 };
152 
153 struct wined3d_cs_set_rendertarget_view
154 {
155     enum wined3d_cs_op opcode;
156     unsigned int view_idx;
157     struct wined3d_rendertarget_view *view;
158 };
159 
160 struct wined3d_cs_set_depth_stencil_view
161 {
162     enum wined3d_cs_op opcode;
163     struct wined3d_rendertarget_view *view;
164 };
165 
166 struct wined3d_cs_set_vertex_declaration
167 {
168     enum wined3d_cs_op opcode;
169     struct wined3d_vertex_declaration *declaration;
170 };
171 
172 struct wined3d_cs_set_stream_source
173 {
174     enum wined3d_cs_op opcode;
175     UINT stream_idx;
176     struct wined3d_buffer *buffer;
177     UINT offset;
178     UINT stride;
179 };
180 
181 struct wined3d_cs_set_stream_source_freq
182 {
183     enum wined3d_cs_op opcode;
184     UINT stream_idx;
185     UINT frequency;
186     UINT flags;
187 };
188 
189 struct wined3d_cs_set_stream_output
190 {
191     enum wined3d_cs_op opcode;
192     UINT stream_idx;
193     struct wined3d_buffer *buffer;
194     UINT offset;
195 };
196 
197 struct wined3d_cs_set_index_buffer
198 {
199     enum wined3d_cs_op opcode;
200     struct wined3d_buffer *buffer;
201     enum wined3d_format_id format_id;
202     unsigned int offset;
203 };
204 
205 struct wined3d_cs_set_constant_buffer
206 {
207     enum wined3d_cs_op opcode;
208     enum wined3d_shader_type type;
209     UINT cb_idx;
210     struct wined3d_buffer *buffer;
211 };
212 
213 struct wined3d_cs_set_texture
214 {
215     enum wined3d_cs_op opcode;
216     UINT stage;
217     struct wined3d_texture *texture;
218 };
219 
220 struct wined3d_cs_set_color_key
221 {
222     enum wined3d_cs_op opcode;
223     struct wined3d_texture *texture;
224     WORD flags;
225     WORD set;
226     struct wined3d_color_key color_key;
227 };
228 
229 struct wined3d_cs_set_shader_resource_view
230 {
231     enum wined3d_cs_op opcode;
232     enum wined3d_shader_type type;
233     UINT view_idx;
234     struct wined3d_shader_resource_view *view;
235 };
236 
237 struct wined3d_cs_set_unordered_access_view
238 {
239     enum wined3d_cs_op opcode;
240     enum wined3d_pipeline pipeline;
241     unsigned int view_idx;
242     struct wined3d_unordered_access_view *view;
243     unsigned int initial_count;
244 };
245 
246 struct wined3d_cs_set_sampler
247 {
248     enum wined3d_cs_op opcode;
249     enum wined3d_shader_type type;
250     UINT sampler_idx;
251     struct wined3d_sampler *sampler;
252 };
253 
254 struct wined3d_cs_set_shader
255 {
256     enum wined3d_cs_op opcode;
257     enum wined3d_shader_type type;
258     struct wined3d_shader *shader;
259 };
260 
261 struct wined3d_cs_set_blend_state
262 {
263     enum wined3d_cs_op opcode;
264     struct wined3d_blend_state *state;
265 };
266 
267 struct wined3d_cs_set_rasterizer_state
268 {
269     enum wined3d_cs_op opcode;
270     struct wined3d_rasterizer_state *state;
271 };
272 
273 struct wined3d_cs_set_render_state
274 {
275     enum wined3d_cs_op opcode;
276     enum wined3d_render_state state;
277     DWORD value;
278 };
279 
280 struct wined3d_cs_set_texture_state
281 {
282     enum wined3d_cs_op opcode;
283     UINT stage;
284     enum wined3d_texture_stage_state state;
285     DWORD value;
286 };
287 
288 struct wined3d_cs_set_sampler_state
289 {
290     enum wined3d_cs_op opcode;
291     UINT sampler_idx;
292     enum wined3d_sampler_state state;
293     DWORD value;
294 };
295 
296 struct wined3d_cs_set_transform
297 {
298     enum wined3d_cs_op opcode;
299     enum wined3d_transform_state state;
300     struct wined3d_matrix matrix;
301 };
302 
303 struct wined3d_cs_set_clip_plane
304 {
305     enum wined3d_cs_op opcode;
306     UINT plane_idx;
307     struct wined3d_vec4 plane;
308 };
309 
310 struct wined3d_cs_set_material
311 {
312     enum wined3d_cs_op opcode;
313     struct wined3d_material material;
314 };
315 
316 struct wined3d_cs_set_light
317 {
318     enum wined3d_cs_op opcode;
319     struct wined3d_light_info light;
320 };
321 
322 struct wined3d_cs_set_light_enable
323 {
324     enum wined3d_cs_op opcode;
325     unsigned int idx;
326     BOOL enable;
327 };
328 
329 struct wined3d_cs_push_constants
330 {
331     enum wined3d_cs_op opcode;
332     enum wined3d_push_constants type;
333     unsigned int start_idx;
334     unsigned int count;
335     BYTE constants[1];
336 };
337 
338 struct wined3d_cs_reset_state
339 {
340     enum wined3d_cs_op opcode;
341 };
342 
343 struct wined3d_cs_callback
344 {
345     enum wined3d_cs_op opcode;
346     void (*callback)(void *object);
347     void *object;
348 };
349 
350 struct wined3d_cs_query_issue
351 {
352     enum wined3d_cs_op opcode;
353     struct wined3d_query *query;
354     DWORD flags;
355 };
356 
357 struct wined3d_cs_preload_resource
358 {
359     enum wined3d_cs_op opcode;
360     struct wined3d_resource *resource;
361 };
362 
363 struct wined3d_cs_unload_resource
364 {
365     enum wined3d_cs_op opcode;
366     struct wined3d_resource *resource;
367 };
368 
369 struct wined3d_cs_map
370 {
371     enum wined3d_cs_op opcode;
372     struct wined3d_resource *resource;
373     unsigned int sub_resource_idx;
374     struct wined3d_map_desc *map_desc;
375     const struct wined3d_box *box;
376     DWORD flags;
377     HRESULT *hr;
378 };
379 
380 struct wined3d_cs_unmap
381 {
382     enum wined3d_cs_op opcode;
383     struct wined3d_resource *resource;
384     unsigned int sub_resource_idx;
385     HRESULT *hr;
386 };
387 
388 struct wined3d_cs_blt_sub_resource
389 {
390     enum wined3d_cs_op opcode;
391     struct wined3d_resource *dst_resource;
392     unsigned int dst_sub_resource_idx;
393     struct wined3d_box dst_box;
394     struct wined3d_resource *src_resource;
395     unsigned int src_sub_resource_idx;
396     struct wined3d_box src_box;
397     DWORD flags;
398     struct wined3d_blt_fx fx;
399     enum wined3d_texture_filter_type filter;
400 };
401 
402 struct wined3d_cs_update_sub_resource
403 {
404     enum wined3d_cs_op opcode;
405     struct wined3d_resource *resource;
406     unsigned int sub_resource_idx;
407     struct wined3d_box box;
408     struct wined3d_sub_resource_data data;
409 #if defined(STAGING_CSMT)
410     BYTE copy_data[1];
411 #endif /* STAGING_CSMT */
412 };
413 
414 struct wined3d_cs_add_dirty_texture_region
415 {
416     enum wined3d_cs_op opcode;
417     struct wined3d_texture *texture;
418     unsigned int layer;
419 };
420 
421 struct wined3d_cs_clear_unordered_access_view
422 {
423     enum wined3d_cs_op opcode;
424     struct wined3d_unordered_access_view *view;
425     struct wined3d_uvec4 clear_value;
426 };
427 
428 struct wined3d_cs_copy_uav_counter
429 {
430     enum wined3d_cs_op opcode;
431     struct wined3d_buffer *buffer;
432     unsigned int offset;
433     struct wined3d_unordered_access_view *view;
434 };
435 
436 struct wined3d_cs_generate_mipmaps
437 {
438     enum wined3d_cs_op opcode;
439     struct wined3d_shader_resource_view *view;
440 };
441 
442 struct wined3d_cs_stop
443 {
444     enum wined3d_cs_op opcode;
445 };
446 
wined3d_cs_exec_nop(struct wined3d_cs * cs,const void * data)447 static void wined3d_cs_exec_nop(struct wined3d_cs *cs, const void *data)
448 {
449 }
450 
wined3d_cs_exec_present(struct wined3d_cs * cs,const void * data)451 static void wined3d_cs_exec_present(struct wined3d_cs *cs, const void *data)
452 {
453     const struct wined3d_cs_present *op = data;
454     struct wined3d_swapchain *swapchain;
455     unsigned int i;
456 
457     swapchain = op->swapchain;
458     wined3d_swapchain_set_window(swapchain, op->dst_window_override);
459 
460     if (op->swap_interval && swapchain->desc.swap_interval != op->swap_interval)
461     {
462         swapchain->desc.swap_interval = op->swap_interval;
463         swapchain_update_swap_interval(swapchain);
464     }
465 
466     swapchain->swapchain_ops->swapchain_present(swapchain, &op->src_rect, &op->dst_rect, op->flags);
467 
468     wined3d_resource_release(&swapchain->front_buffer->resource);
469     for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
470     {
471         wined3d_resource_release(&swapchain->back_buffers[i]->resource);
472     }
473 
474     InterlockedDecrement(&cs->pending_presents);
475 }
476 
wined3d_cs_emit_present(struct wined3d_cs * cs,struct wined3d_swapchain * swapchain,const RECT * src_rect,const RECT * dst_rect,HWND dst_window_override,DWORD swap_interval,DWORD flags)477 void wined3d_cs_emit_present(struct wined3d_cs *cs, struct wined3d_swapchain *swapchain,
478         const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
479         DWORD swap_interval, DWORD flags)
480 {
481     struct wined3d_cs_present *op;
482     unsigned int i;
483     LONG pending;
484 
485     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
486     op->opcode = WINED3D_CS_OP_PRESENT;
487     op->dst_window_override = dst_window_override;
488     op->swapchain = swapchain;
489     op->src_rect = *src_rect;
490     op->dst_rect = *dst_rect;
491     op->swap_interval = swap_interval;
492     op->flags = flags;
493 
494     pending = InterlockedIncrement(&cs->pending_presents);
495 
496     wined3d_resource_acquire(&swapchain->front_buffer->resource);
497     for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
498     {
499         wined3d_resource_acquire(&swapchain->back_buffers[i]->resource);
500     }
501 
502     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
503 
504     /* Limit input latency by limiting the number of presents that we can get
505      * ahead of the worker thread. We have a constant limit here, but
506      * IDXGIDevice1 allows tuning this. */
507     while (pending > 1)
508     {
509         wined3d_pause();
510         pending = InterlockedCompareExchange(&cs->pending_presents, 0, 0);
511     }
512 }
513 
wined3d_cs_exec_clear(struct wined3d_cs * cs,const void * data)514 static void wined3d_cs_exec_clear(struct wined3d_cs *cs, const void *data)
515 {
516     const struct wined3d_cs_clear *op = data;
517     struct wined3d_device *device;
518     unsigned int i;
519 
520     device = cs->device;
521     device->blitter->ops->blitter_clear(device->blitter, device, op->rt_count, op->fb,
522             op->rect_count, op->rects, &op->draw_rect, op->flags, &op->color, op->depth, op->stencil);
523 
524     if (op->flags & WINED3DCLEAR_TARGET)
525     {
526         for (i = 0; i < op->rt_count; ++i)
527         {
528             if (op->fb->render_targets[i])
529                 wined3d_resource_release(op->fb->render_targets[i]->resource);
530         }
531     }
532     if (op->flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
533         wined3d_resource_release(op->fb->depth_stencil->resource);
534 }
535 
wined3d_cs_emit_clear(struct wined3d_cs * cs,DWORD rect_count,const RECT * rects,DWORD flags,const struct wined3d_color * color,float depth,DWORD stencil)536 void wined3d_cs_emit_clear(struct wined3d_cs *cs, DWORD rect_count, const RECT *rects,
537         DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
538 {
539     unsigned int rt_count = cs->device->adapter->gl_info.limits.buffers;
540     const struct wined3d_state *state = &cs->device->state;
541     const struct wined3d_viewport *vp = &state->viewport;
542     struct wined3d_cs_clear *op;
543     unsigned int i;
544 
545     op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_clear, rects[rect_count]),
546             WINED3D_CS_QUEUE_DEFAULT);
547     op->opcode = WINED3D_CS_OP_CLEAR;
548     op->flags = flags;
549     op->rt_count = rt_count;
550     op->fb = &cs->fb;
551     SetRect(&op->draw_rect, vp->x, vp->y, vp->x + vp->width, vp->y + vp->height);
552     if (state->render_states[WINED3D_RS_SCISSORTESTENABLE])
553         IntersectRect(&op->draw_rect, &op->draw_rect, &state->scissor_rect);
554     op->color = *color;
555     op->depth = depth;
556     op->stencil = stencil;
557     op->rect_count = rect_count;
558     memcpy(op->rects, rects, sizeof(*rects) * rect_count);
559 
560     if (flags & WINED3DCLEAR_TARGET)
561     {
562         for (i = 0; i < rt_count; ++i)
563         {
564             if (state->fb->render_targets[i])
565                 wined3d_resource_acquire(state->fb->render_targets[i]->resource);
566         }
567     }
568     if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
569         wined3d_resource_acquire(state->fb->depth_stencil->resource);
570 
571     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
572 }
573 
wined3d_cs_emit_clear_rendertarget_view(struct wined3d_cs * cs,struct wined3d_rendertarget_view * view,const RECT * rect,DWORD flags,const struct wined3d_color * color,float depth,DWORD stencil)574 void wined3d_cs_emit_clear_rendertarget_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view,
575         const RECT *rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
576 {
577     struct wined3d_cs_clear *op;
578     size_t size;
579 
580     size = FIELD_OFFSET(struct wined3d_cs_clear, rects[1]) + sizeof(struct wined3d_fb_state);
581     op = cs->ops->require_space(cs, size, WINED3D_CS_QUEUE_DEFAULT);
582     op->fb = (void *)&op->rects[1];
583 
584     op->opcode = WINED3D_CS_OP_CLEAR;
585     op->flags = flags;
586     if (flags & WINED3DCLEAR_TARGET)
587     {
588         op->rt_count = 1;
589         op->fb->render_targets[0] = view;
590         op->fb->depth_stencil = NULL;
591         op->color = *color;
592     }
593     else
594     {
595         op->rt_count = 0;
596         op->fb->render_targets[0] = NULL;
597         op->fb->depth_stencil = view;
598         op->depth = depth;
599         op->stencil = stencil;
600     }
601     SetRect(&op->draw_rect, 0, 0, view->width, view->height);
602     op->rect_count = 1;
603     op->rects[0] = *rect;
604 
605     wined3d_resource_acquire(view->resource);
606 
607     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
608 }
609 
acquire_shader_resources(const struct wined3d_state * state,unsigned int shader_mask)610 static void acquire_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
611 {
612     struct wined3d_shader_sampler_map_entry *entry;
613     struct wined3d_shader_resource_view *view;
614     struct wined3d_shader *shader;
615     unsigned int i, j;
616 
617     for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
618     {
619         if (!(shader_mask & (1u << i)))
620             continue;
621 
622         if (!(shader = state->shader[i]))
623             continue;
624 
625         for (j = 0; j < WINED3D_MAX_CBS; ++j)
626         {
627             if (state->cb[i][j])
628                 wined3d_resource_acquire(&state->cb[i][j]->resource);
629         }
630 
631         for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
632         {
633             entry = &shader->reg_maps.sampler_map.entries[j];
634 
635             if (!(view = state->shader_resource_view[i][entry->resource_idx]))
636                 continue;
637 
638             wined3d_resource_acquire(view->resource);
639         }
640     }
641 }
642 
release_shader_resources(const struct wined3d_state * state,unsigned int shader_mask)643 static void release_shader_resources(const struct wined3d_state *state, unsigned int shader_mask)
644 {
645     struct wined3d_shader_sampler_map_entry *entry;
646     struct wined3d_shader_resource_view *view;
647     struct wined3d_shader *shader;
648     unsigned int i, j;
649 
650     for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
651     {
652         if (!(shader_mask & (1u << i)))
653             continue;
654 
655         if (!(shader = state->shader[i]))
656             continue;
657 
658         for (j = 0; j < WINED3D_MAX_CBS; ++j)
659         {
660             if (state->cb[i][j])
661                 wined3d_resource_release(&state->cb[i][j]->resource);
662         }
663 
664         for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
665         {
666             entry = &shader->reg_maps.sampler_map.entries[j];
667 
668             if (!(view = state->shader_resource_view[i][entry->resource_idx]))
669                 continue;
670 
671             wined3d_resource_release(view->resource);
672         }
673     }
674 }
675 
acquire_unordered_access_resources(const struct wined3d_shader * shader,struct wined3d_unordered_access_view * const * views)676 static void acquire_unordered_access_resources(const struct wined3d_shader *shader,
677         struct wined3d_unordered_access_view * const *views)
678 {
679     unsigned int i;
680 
681     if (!shader)
682         return;
683 
684     for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
685     {
686         if (!shader->reg_maps.uav_resource_info[i].type)
687             continue;
688 
689         if (!views[i])
690             continue;
691 
692         wined3d_resource_acquire(views[i]->resource);
693     }
694 }
695 
release_unordered_access_resources(const struct wined3d_shader * shader,struct wined3d_unordered_access_view * const * views)696 static void release_unordered_access_resources(const struct wined3d_shader *shader,
697         struct wined3d_unordered_access_view * const *views)
698 {
699     unsigned int i;
700 
701     if (!shader)
702         return;
703 
704     for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
705     {
706         if (!shader->reg_maps.uav_resource_info[i].type)
707             continue;
708 
709         if (!views[i])
710             continue;
711 
712         wined3d_resource_release(views[i]->resource);
713     }
714 }
715 
wined3d_cs_exec_dispatch(struct wined3d_cs * cs,const void * data)716 static void wined3d_cs_exec_dispatch(struct wined3d_cs *cs, const void *data)
717 {
718     const struct wined3d_cs_dispatch *op = data;
719     struct wined3d_state *state = &cs->state;
720 
721     dispatch_compute(cs->device, state, &op->parameters);
722 
723     if (op->parameters.indirect)
724         wined3d_resource_release(&op->parameters.u.indirect.buffer->resource);
725 
726     release_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
727     release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
728             state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
729 }
730 
acquire_compute_pipeline_resources(const struct wined3d_state * state)731 static void acquire_compute_pipeline_resources(const struct wined3d_state *state)
732 {
733     acquire_shader_resources(state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
734     acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_COMPUTE],
735             state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
736 }
737 
wined3d_cs_emit_dispatch(struct wined3d_cs * cs,unsigned int group_count_x,unsigned int group_count_y,unsigned int group_count_z)738 void wined3d_cs_emit_dispatch(struct wined3d_cs *cs,
739         unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
740 {
741     const struct wined3d_state *state = &cs->device->state;
742     struct wined3d_cs_dispatch *op;
743 
744     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
745     op->opcode = WINED3D_CS_OP_DISPATCH;
746     op->parameters.indirect = FALSE;
747     op->parameters.u.direct.group_count_x = group_count_x;
748     op->parameters.u.direct.group_count_y = group_count_y;
749     op->parameters.u.direct.group_count_z = group_count_z;
750 
751     acquire_compute_pipeline_resources(state);
752 
753     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
754 }
755 
wined3d_cs_emit_dispatch_indirect(struct wined3d_cs * cs,struct wined3d_buffer * buffer,unsigned int offset)756 void wined3d_cs_emit_dispatch_indirect(struct wined3d_cs *cs,
757         struct wined3d_buffer *buffer, unsigned int offset)
758 {
759     const struct wined3d_state *state = &cs->device->state;
760     struct wined3d_cs_dispatch *op;
761 
762     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
763     op->opcode = WINED3D_CS_OP_DISPATCH;
764     op->parameters.indirect = TRUE;
765     op->parameters.u.indirect.buffer = buffer;
766     op->parameters.u.indirect.offset = offset;
767 
768     acquire_compute_pipeline_resources(state);
769     wined3d_resource_acquire(&buffer->resource);
770 
771     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
772 }
773 
wined3d_cs_exec_draw(struct wined3d_cs * cs,const void * data)774 static void wined3d_cs_exec_draw(struct wined3d_cs *cs, const void *data)
775 {
776     const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
777     struct wined3d_state *state = &cs->state;
778     const struct wined3d_cs_draw *op = data;
779     int load_base_vertex_idx;
780     unsigned int i;
781 
782     /* ARB_draw_indirect always supports a base vertex offset. */
783     if (!op->parameters.indirect && !gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
784         load_base_vertex_idx = op->parameters.u.direct.base_vertex_idx;
785     else
786         load_base_vertex_idx = 0;
787 
788     if (state->load_base_vertex_index != load_base_vertex_idx)
789     {
790         state->load_base_vertex_index = load_base_vertex_idx;
791         device_invalidate_state(cs->device, STATE_BASEVERTEXINDEX);
792     }
793 
794     if (state->gl_primitive_type != op->primitive_type)
795     {
796         if (state->gl_primitive_type == GL_POINTS || op->primitive_type == GL_POINTS)
797             device_invalidate_state(cs->device, STATE_POINT_ENABLE);
798         state->gl_primitive_type = op->primitive_type;
799     }
800     state->gl_patch_vertices = op->patch_vertex_count;
801 
802     draw_primitive(cs->device, state, &op->parameters);
803 
804     if (op->parameters.indirect)
805     {
806         struct wined3d_buffer *buffer = op->parameters.u.indirect.buffer;
807         wined3d_resource_release(&buffer->resource);
808     }
809 
810     if (op->parameters.indexed)
811         wined3d_resource_release(&state->index_buffer->resource);
812     for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
813     {
814         if (state->streams[i].buffer)
815             wined3d_resource_release(&state->streams[i].buffer->resource);
816     }
817     for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
818     {
819         if (state->stream_output[i].buffer)
820             wined3d_resource_release(&state->stream_output[i].buffer->resource);
821     }
822     for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
823     {
824         if (state->textures[i])
825             wined3d_resource_release(&state->textures[i]->resource);
826     }
827     for (i = 0; i < gl_info->limits.buffers; ++i)
828     {
829         if (state->fb->render_targets[i])
830             wined3d_resource_release(state->fb->render_targets[i]->resource);
831     }
832     if (state->fb->depth_stencil)
833         wined3d_resource_release(state->fb->depth_stencil->resource);
834     release_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
835     release_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
836             state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
837 }
838 
acquire_graphics_pipeline_resources(const struct wined3d_state * state,BOOL indexed,const struct wined3d_gl_info * gl_info)839 static void acquire_graphics_pipeline_resources(const struct wined3d_state *state,
840         BOOL indexed, const struct wined3d_gl_info *gl_info)
841 {
842     unsigned int i;
843 
844     if (indexed)
845         wined3d_resource_acquire(&state->index_buffer->resource);
846     for (i = 0; i < ARRAY_SIZE(state->streams); ++i)
847     {
848         if (state->streams[i].buffer)
849             wined3d_resource_acquire(&state->streams[i].buffer->resource);
850     }
851     for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
852     {
853         if (state->stream_output[i].buffer)
854             wined3d_resource_acquire(&state->stream_output[i].buffer->resource);
855     }
856     for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
857     {
858         if (state->textures[i])
859             wined3d_resource_acquire(&state->textures[i]->resource);
860     }
861     for (i = 0; i < gl_info->limits.buffers; ++i)
862     {
863         if (state->fb->render_targets[i])
864             wined3d_resource_acquire(state->fb->render_targets[i]->resource);
865     }
866     if (state->fb->depth_stencil)
867         wined3d_resource_acquire(state->fb->depth_stencil->resource);
868     acquire_shader_resources(state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
869     acquire_unordered_access_resources(state->shader[WINED3D_SHADER_TYPE_PIXEL],
870             state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
871 }
872 
wined3d_cs_emit_draw(struct wined3d_cs * cs,GLenum primitive_type,unsigned int patch_vertex_count,int base_vertex_idx,unsigned int start_idx,unsigned int index_count,unsigned int start_instance,unsigned int instance_count,BOOL indexed)873 void wined3d_cs_emit_draw(struct wined3d_cs *cs, GLenum primitive_type, unsigned int patch_vertex_count,
874         int base_vertex_idx, unsigned int start_idx, unsigned int index_count,
875         unsigned int start_instance, unsigned int instance_count, BOOL indexed)
876 {
877     const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
878     const struct wined3d_state *state = &cs->device->state;
879     struct wined3d_cs_draw *op;
880 
881     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
882     op->opcode = WINED3D_CS_OP_DRAW;
883     op->primitive_type = primitive_type;
884     op->patch_vertex_count = patch_vertex_count;
885     op->parameters.indirect = FALSE;
886     op->parameters.u.direct.base_vertex_idx = base_vertex_idx;
887     op->parameters.u.direct.start_idx = start_idx;
888     op->parameters.u.direct.index_count = index_count;
889     op->parameters.u.direct.start_instance = start_instance;
890     op->parameters.u.direct.instance_count = instance_count;
891     op->parameters.indexed = indexed;
892 
893     acquire_graphics_pipeline_resources(state, indexed, gl_info);
894 
895     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
896 }
897 
wined3d_cs_emit_draw_indirect(struct wined3d_cs * cs,GLenum primitive_type,unsigned int patch_vertex_count,struct wined3d_buffer * buffer,unsigned int offset,BOOL indexed)898 void wined3d_cs_emit_draw_indirect(struct wined3d_cs *cs, GLenum primitive_type, unsigned int patch_vertex_count,
899         struct wined3d_buffer *buffer, unsigned int offset, BOOL indexed)
900 {
901     const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
902     const struct wined3d_state *state = &cs->device->state;
903     struct wined3d_cs_draw *op;
904 
905     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
906     op->opcode = WINED3D_CS_OP_DRAW;
907     op->primitive_type = primitive_type;
908     op->patch_vertex_count = patch_vertex_count;
909     op->parameters.indirect = TRUE;
910     op->parameters.u.indirect.buffer = buffer;
911     op->parameters.u.indirect.offset = offset;
912     op->parameters.indexed = indexed;
913 
914     acquire_graphics_pipeline_resources(state, indexed, gl_info);
915     wined3d_resource_acquire(&buffer->resource);
916 
917     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
918 }
919 
wined3d_cs_exec_flush(struct wined3d_cs * cs,const void * data)920 static void wined3d_cs_exec_flush(struct wined3d_cs *cs, const void *data)
921 {
922     struct wined3d_context *context;
923 
924     context = context_acquire(cs->device, NULL, 0);
925     if (context->valid)
926         context->gl_info->gl_ops.gl.p_glFlush();
927     context_release(context);
928 }
929 
wined3d_cs_emit_flush(struct wined3d_cs * cs)930 void wined3d_cs_emit_flush(struct wined3d_cs *cs)
931 {
932     struct wined3d_cs_flush *op;
933 
934     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
935     op->opcode = WINED3D_CS_OP_FLUSH;
936 
937     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
938     cs->queries_flushed = TRUE;
939 }
940 
wined3d_cs_exec_set_predication(struct wined3d_cs * cs,const void * data)941 static void wined3d_cs_exec_set_predication(struct wined3d_cs *cs, const void *data)
942 {
943     const struct wined3d_cs_set_predication *op = data;
944 
945     cs->state.predicate = op->predicate;
946     cs->state.predicate_value = op->value;
947 }
948 
wined3d_cs_emit_set_predication(struct wined3d_cs * cs,struct wined3d_query * predicate,BOOL value)949 void wined3d_cs_emit_set_predication(struct wined3d_cs *cs, struct wined3d_query *predicate, BOOL value)
950 {
951     struct wined3d_cs_set_predication *op;
952 
953     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
954     op->opcode = WINED3D_CS_OP_SET_PREDICATION;
955     op->predicate = predicate;
956     op->value = value;
957 
958     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
959 }
960 
wined3d_cs_exec_set_viewport(struct wined3d_cs * cs,const void * data)961 static void wined3d_cs_exec_set_viewport(struct wined3d_cs *cs, const void *data)
962 {
963     const struct wined3d_cs_set_viewport *op = data;
964 
965     cs->state.viewport = op->viewport;
966     device_invalidate_state(cs->device, STATE_VIEWPORT);
967 }
968 
wined3d_cs_emit_set_viewport(struct wined3d_cs * cs,const struct wined3d_viewport * viewport)969 void wined3d_cs_emit_set_viewport(struct wined3d_cs *cs, const struct wined3d_viewport *viewport)
970 {
971     struct wined3d_cs_set_viewport *op;
972 
973     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
974     op->opcode = WINED3D_CS_OP_SET_VIEWPORT;
975     op->viewport = *viewport;
976 
977     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
978 }
979 
wined3d_cs_exec_set_scissor_rect(struct wined3d_cs * cs,const void * data)980 static void wined3d_cs_exec_set_scissor_rect(struct wined3d_cs *cs, const void *data)
981 {
982     const struct wined3d_cs_set_scissor_rect *op = data;
983 
984     cs->state.scissor_rect = op->rect;
985     device_invalidate_state(cs->device, STATE_SCISSORRECT);
986 }
987 
wined3d_cs_emit_set_scissor_rect(struct wined3d_cs * cs,const RECT * rect)988 void wined3d_cs_emit_set_scissor_rect(struct wined3d_cs *cs, const RECT *rect)
989 {
990     struct wined3d_cs_set_scissor_rect *op;
991 
992     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
993     op->opcode = WINED3D_CS_OP_SET_SCISSOR_RECT;
994     op->rect = *rect;
995 
996     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
997 }
998 
wined3d_cs_exec_set_rendertarget_view(struct wined3d_cs * cs,const void * data)999 static void wined3d_cs_exec_set_rendertarget_view(struct wined3d_cs *cs, const void *data)
1000 {
1001     const struct wined3d_cs_set_rendertarget_view *op = data;
1002 
1003     cs->fb.render_targets[op->view_idx] = op->view;
1004     device_invalidate_state(cs->device, STATE_FRAMEBUFFER);
1005 }
1006 
wined3d_cs_emit_set_rendertarget_view(struct wined3d_cs * cs,unsigned int view_idx,struct wined3d_rendertarget_view * view)1007 void wined3d_cs_emit_set_rendertarget_view(struct wined3d_cs *cs, unsigned int view_idx,
1008         struct wined3d_rendertarget_view *view)
1009 {
1010     struct wined3d_cs_set_rendertarget_view *op;
1011 
1012     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1013     op->opcode = WINED3D_CS_OP_SET_RENDERTARGET_VIEW;
1014     op->view_idx = view_idx;
1015     op->view = view;
1016 
1017     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1018 }
1019 
wined3d_cs_exec_set_depth_stencil_view(struct wined3d_cs * cs,const void * data)1020 static void wined3d_cs_exec_set_depth_stencil_view(struct wined3d_cs *cs, const void *data)
1021 {
1022     const struct wined3d_cs_set_depth_stencil_view *op = data;
1023     struct wined3d_device *device = cs->device;
1024     struct wined3d_rendertarget_view *prev;
1025 
1026     if ((prev = cs->state.fb->depth_stencil))
1027     {
1028         struct wined3d_surface *prev_surface = wined3d_rendertarget_view_get_surface(prev);
1029 
1030         if (prev_surface && (device->swapchains[0]->desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
1031                 || prev_surface->container->flags & WINED3D_TEXTURE_DISCARD))
1032         {
1033             wined3d_texture_validate_location(prev_surface->container,
1034                     prev->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
1035         }
1036     }
1037 
1038     cs->fb.depth_stencil = op->view;
1039 
1040     if (!prev != !op->view)
1041     {
1042         /* Swapping NULL / non NULL depth stencil affects the depth and tests */
1043         device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
1044         device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
1045         device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
1046         device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
1047         device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIASCLAMP));
1048     }
1049     else if (prev && prev->format->depth_bias_scale != op->view->format->depth_bias_scale)
1050     {
1051         device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
1052     }
1053 
1054     device_invalidate_state(device, STATE_FRAMEBUFFER);
1055 }
1056 
wined3d_cs_emit_set_depth_stencil_view(struct wined3d_cs * cs,struct wined3d_rendertarget_view * view)1057 void wined3d_cs_emit_set_depth_stencil_view(struct wined3d_cs *cs, struct wined3d_rendertarget_view *view)
1058 {
1059     struct wined3d_cs_set_depth_stencil_view *op;
1060 
1061     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1062     op->opcode = WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW;
1063     op->view = view;
1064 
1065     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1066 }
1067 
wined3d_cs_exec_set_vertex_declaration(struct wined3d_cs * cs,const void * data)1068 static void wined3d_cs_exec_set_vertex_declaration(struct wined3d_cs *cs, const void *data)
1069 {
1070     const struct wined3d_cs_set_vertex_declaration *op = data;
1071 
1072     cs->state.vertex_declaration = op->declaration;
1073     device_invalidate_state(cs->device, STATE_VDECL);
1074 }
1075 
wined3d_cs_emit_set_vertex_declaration(struct wined3d_cs * cs,struct wined3d_vertex_declaration * declaration)1076 void wined3d_cs_emit_set_vertex_declaration(struct wined3d_cs *cs, struct wined3d_vertex_declaration *declaration)
1077 {
1078     struct wined3d_cs_set_vertex_declaration *op;
1079 
1080     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1081     op->opcode = WINED3D_CS_OP_SET_VERTEX_DECLARATION;
1082     op->declaration = declaration;
1083 
1084     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1085 }
1086 
wined3d_cs_exec_set_stream_source(struct wined3d_cs * cs,const void * data)1087 static void wined3d_cs_exec_set_stream_source(struct wined3d_cs *cs, const void *data)
1088 {
1089     const struct wined3d_cs_set_stream_source *op = data;
1090     struct wined3d_stream_state *stream;
1091     struct wined3d_buffer *prev;
1092 
1093     stream = &cs->state.streams[op->stream_idx];
1094     prev = stream->buffer;
1095     stream->buffer = op->buffer;
1096     stream->offset = op->offset;
1097     stream->stride = op->stride;
1098 
1099     if (op->buffer)
1100         InterlockedIncrement(&op->buffer->resource.bind_count);
1101     if (prev)
1102         InterlockedDecrement(&prev->resource.bind_count);
1103 
1104     device_invalidate_state(cs->device, STATE_STREAMSRC);
1105 }
1106 
wined3d_cs_emit_set_stream_source(struct wined3d_cs * cs,UINT stream_idx,struct wined3d_buffer * buffer,UINT offset,UINT stride)1107 void wined3d_cs_emit_set_stream_source(struct wined3d_cs *cs, UINT stream_idx,
1108         struct wined3d_buffer *buffer, UINT offset, UINT stride)
1109 {
1110     struct wined3d_cs_set_stream_source *op;
1111 
1112     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1113     op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE;
1114     op->stream_idx = stream_idx;
1115     op->buffer = buffer;
1116     op->offset = offset;
1117     op->stride = stride;
1118 
1119     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1120 }
1121 
wined3d_cs_exec_set_stream_source_freq(struct wined3d_cs * cs,const void * data)1122 static void wined3d_cs_exec_set_stream_source_freq(struct wined3d_cs *cs, const void *data)
1123 {
1124     const struct wined3d_cs_set_stream_source_freq *op = data;
1125     struct wined3d_stream_state *stream;
1126 
1127     stream = &cs->state.streams[op->stream_idx];
1128     stream->frequency = op->frequency;
1129     stream->flags = op->flags;
1130 
1131     device_invalidate_state(cs->device, STATE_STREAMSRC);
1132 }
1133 
wined3d_cs_emit_set_stream_source_freq(struct wined3d_cs * cs,UINT stream_idx,UINT frequency,UINT flags)1134 void wined3d_cs_emit_set_stream_source_freq(struct wined3d_cs *cs, UINT stream_idx, UINT frequency, UINT flags)
1135 {
1136     struct wined3d_cs_set_stream_source_freq *op;
1137 
1138     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1139     op->opcode = WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ;
1140     op->stream_idx = stream_idx;
1141     op->frequency = frequency;
1142     op->flags = flags;
1143 
1144     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1145 }
1146 
wined3d_cs_exec_set_stream_output(struct wined3d_cs * cs,const void * data)1147 static void wined3d_cs_exec_set_stream_output(struct wined3d_cs *cs, const void *data)
1148 {
1149     const struct wined3d_cs_set_stream_output *op = data;
1150     struct wined3d_stream_output *stream;
1151     struct wined3d_buffer *prev;
1152 
1153     stream = &cs->state.stream_output[op->stream_idx];
1154     prev = stream->buffer;
1155     stream->buffer = op->buffer;
1156     stream->offset = op->offset;
1157 
1158     if (op->buffer)
1159         InterlockedIncrement(&op->buffer->resource.bind_count);
1160     if (prev)
1161         InterlockedDecrement(&prev->resource.bind_count);
1162 
1163     device_invalidate_state(cs->device, STATE_STREAM_OUTPUT);
1164 }
1165 
wined3d_cs_emit_set_stream_output(struct wined3d_cs * cs,UINT stream_idx,struct wined3d_buffer * buffer,UINT offset)1166 void wined3d_cs_emit_set_stream_output(struct wined3d_cs *cs, UINT stream_idx,
1167         struct wined3d_buffer *buffer, UINT offset)
1168 {
1169     struct wined3d_cs_set_stream_output *op;
1170 
1171     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1172     op->opcode = WINED3D_CS_OP_SET_STREAM_OUTPUT;
1173     op->stream_idx = stream_idx;
1174     op->buffer = buffer;
1175     op->offset = offset;
1176 
1177     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1178 }
1179 
wined3d_cs_exec_set_index_buffer(struct wined3d_cs * cs,const void * data)1180 static void wined3d_cs_exec_set_index_buffer(struct wined3d_cs *cs, const void *data)
1181 {
1182     const struct wined3d_cs_set_index_buffer *op = data;
1183     struct wined3d_buffer *prev;
1184 
1185     prev = cs->state.index_buffer;
1186     cs->state.index_buffer = op->buffer;
1187     cs->state.index_format = op->format_id;
1188     cs->state.index_offset = op->offset;
1189 
1190     if (op->buffer)
1191         InterlockedIncrement(&op->buffer->resource.bind_count);
1192     if (prev)
1193         InterlockedDecrement(&prev->resource.bind_count);
1194 
1195     device_invalidate_state(cs->device, STATE_INDEXBUFFER);
1196 }
1197 
wined3d_cs_emit_set_index_buffer(struct wined3d_cs * cs,struct wined3d_buffer * buffer,enum wined3d_format_id format_id,unsigned int offset)1198 void wined3d_cs_emit_set_index_buffer(struct wined3d_cs *cs, struct wined3d_buffer *buffer,
1199         enum wined3d_format_id format_id, unsigned int offset)
1200 {
1201     struct wined3d_cs_set_index_buffer *op;
1202 
1203     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1204     op->opcode = WINED3D_CS_OP_SET_INDEX_BUFFER;
1205     op->buffer = buffer;
1206     op->format_id = format_id;
1207     op->offset = offset;
1208 
1209     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1210 }
1211 
wined3d_cs_exec_set_constant_buffer(struct wined3d_cs * cs,const void * data)1212 static void wined3d_cs_exec_set_constant_buffer(struct wined3d_cs *cs, const void *data)
1213 {
1214     const struct wined3d_cs_set_constant_buffer *op = data;
1215     struct wined3d_buffer *prev;
1216 
1217     prev = cs->state.cb[op->type][op->cb_idx];
1218     cs->state.cb[op->type][op->cb_idx] = op->buffer;
1219 
1220     if (op->buffer)
1221         InterlockedIncrement(&op->buffer->resource.bind_count);
1222     if (prev)
1223         InterlockedDecrement(&prev->resource.bind_count);
1224 
1225     device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(op->type));
1226 }
1227 
wined3d_cs_emit_set_constant_buffer(struct wined3d_cs * cs,enum wined3d_shader_type type,UINT cb_idx,struct wined3d_buffer * buffer)1228 void wined3d_cs_emit_set_constant_buffer(struct wined3d_cs *cs, enum wined3d_shader_type type,
1229         UINT cb_idx, struct wined3d_buffer *buffer)
1230 {
1231     struct wined3d_cs_set_constant_buffer *op;
1232 
1233     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1234     op->opcode = WINED3D_CS_OP_SET_CONSTANT_BUFFER;
1235     op->type = type;
1236     op->cb_idx = cb_idx;
1237     op->buffer = buffer;
1238 
1239     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1240 }
1241 
wined3d_cs_exec_set_texture(struct wined3d_cs * cs,const void * data)1242 static void wined3d_cs_exec_set_texture(struct wined3d_cs *cs, const void *data)
1243 {
1244     const struct wined3d_gl_info *gl_info = &cs->device->adapter->gl_info;
1245     const struct wined3d_d3d_info *d3d_info = &cs->device->adapter->d3d_info;
1246     const struct wined3d_cs_set_texture *op = data;
1247     struct wined3d_texture *prev;
1248     BOOL old_use_color_key = FALSE, new_use_color_key = FALSE;
1249 
1250     prev = cs->state.textures[op->stage];
1251     cs->state.textures[op->stage] = op->texture;
1252 
1253     if (op->texture)
1254     {
1255         const struct wined3d_format *new_format = op->texture->resource.format;
1256         const struct wined3d_format *old_format = prev ? prev->resource.format : NULL;
1257         unsigned int old_fmt_flags = prev ? prev->resource.format_flags : 0;
1258         unsigned int new_fmt_flags = op->texture->resource.format_flags;
1259 
1260         if (InterlockedIncrement(&op->texture->resource.bind_count) == 1)
1261             op->texture->sampler = op->stage;
1262 
1263         if (!prev || op->texture->target != prev->target
1264                 || (!is_same_fixup(new_format->color_fixup, old_format->color_fixup)
1265                 && !(can_use_texture_swizzle(gl_info, new_format) && can_use_texture_swizzle(gl_info, old_format)))
1266                 || (new_fmt_flags & WINED3DFMT_FLAG_SHADOW) != (old_fmt_flags & WINED3DFMT_FLAG_SHADOW))
1267             device_invalidate_state(cs->device, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
1268 
1269         if (!prev && op->stage < d3d_info->limits.ffp_blend_stages)
1270         {
1271             /* The source arguments for color and alpha ops have different
1272              * meanings when a NULL texture is bound, so the COLOR_OP and
1273              * ALPHA_OP have to be dirtified. */
1274             device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1275             device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1276         }
1277 
1278         if (!op->stage && op->texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1279             new_use_color_key = TRUE;
1280     }
1281 
1282     if (prev)
1283     {
1284         if (InterlockedDecrement(&prev->resource.bind_count) && prev->sampler == op->stage)
1285         {
1286             unsigned int i;
1287 
1288             /* Search for other stages the texture is bound to. Shouldn't
1289              * happen if applications bind textures to a single stage only. */
1290             TRACE("Searching for other stages the texture is bound to.\n");
1291             for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
1292             {
1293                 if (cs->state.textures[i] == prev)
1294                 {
1295                     TRACE("Texture is also bound to stage %u.\n", i);
1296                     prev->sampler = i;
1297                     break;
1298                 }
1299             }
1300         }
1301 
1302         if (!op->texture && op->stage < d3d_info->limits.ffp_blend_stages)
1303         {
1304             device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_COLOR_OP));
1305             device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, WINED3D_TSS_ALPHA_OP));
1306         }
1307 
1308         if (!op->stage && prev->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1309             old_use_color_key = TRUE;
1310     }
1311 
1312     device_invalidate_state(cs->device, STATE_SAMPLER(op->stage));
1313 
1314     if (new_use_color_key != old_use_color_key)
1315         device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1316 
1317     if (new_use_color_key)
1318         device_invalidate_state(cs->device, STATE_COLOR_KEY);
1319 }
1320 
wined3d_cs_emit_set_texture(struct wined3d_cs * cs,UINT stage,struct wined3d_texture * texture)1321 void wined3d_cs_emit_set_texture(struct wined3d_cs *cs, UINT stage, struct wined3d_texture *texture)
1322 {
1323     struct wined3d_cs_set_texture *op;
1324 
1325     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1326     op->opcode = WINED3D_CS_OP_SET_TEXTURE;
1327     op->stage = stage;
1328     op->texture = texture;
1329 
1330     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1331 }
1332 
wined3d_cs_exec_set_shader_resource_view(struct wined3d_cs * cs,const void * data)1333 static void wined3d_cs_exec_set_shader_resource_view(struct wined3d_cs *cs, const void *data)
1334 {
1335     const struct wined3d_cs_set_shader_resource_view *op = data;
1336     struct wined3d_shader_resource_view *prev;
1337 
1338     prev = cs->state.shader_resource_view[op->type][op->view_idx];
1339     cs->state.shader_resource_view[op->type][op->view_idx] = op->view;
1340 
1341     if (op->view)
1342         InterlockedIncrement(&op->view->resource->bind_count);
1343     if (prev)
1344         InterlockedDecrement(&prev->resource->bind_count);
1345 
1346     if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1347         device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1348     else
1349         device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1350 }
1351 
wined3d_cs_emit_set_shader_resource_view(struct wined3d_cs * cs,enum wined3d_shader_type type,UINT view_idx,struct wined3d_shader_resource_view * view)1352 void wined3d_cs_emit_set_shader_resource_view(struct wined3d_cs *cs, enum wined3d_shader_type type,
1353         UINT view_idx, struct wined3d_shader_resource_view *view)
1354 {
1355     struct wined3d_cs_set_shader_resource_view *op;
1356 
1357     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1358     op->opcode = WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW;
1359     op->type = type;
1360     op->view_idx = view_idx;
1361     op->view = view;
1362 
1363     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1364 }
1365 
wined3d_cs_exec_set_unordered_access_view(struct wined3d_cs * cs,const void * data)1366 static void wined3d_cs_exec_set_unordered_access_view(struct wined3d_cs *cs, const void *data)
1367 {
1368     const struct wined3d_cs_set_unordered_access_view *op = data;
1369     struct wined3d_unordered_access_view *prev;
1370 
1371     prev = cs->state.unordered_access_view[op->pipeline][op->view_idx];
1372     cs->state.unordered_access_view[op->pipeline][op->view_idx] = op->view;
1373 
1374     if (op->view)
1375         InterlockedIncrement(&op->view->resource->bind_count);
1376     if (prev)
1377         InterlockedDecrement(&prev->resource->bind_count);
1378 
1379     if (op->view && op->initial_count != ~0u)
1380         wined3d_unordered_access_view_set_counter(op->view, op->initial_count);
1381 
1382     device_invalidate_state(cs->device, STATE_UNORDERED_ACCESS_VIEW_BINDING(op->pipeline));
1383 }
1384 
wined3d_cs_emit_set_unordered_access_view(struct wined3d_cs * cs,enum wined3d_pipeline pipeline,unsigned int view_idx,struct wined3d_unordered_access_view * view,unsigned int initial_count)1385 void wined3d_cs_emit_set_unordered_access_view(struct wined3d_cs *cs, enum wined3d_pipeline pipeline,
1386         unsigned int view_idx, struct wined3d_unordered_access_view *view, unsigned int initial_count)
1387 {
1388     struct wined3d_cs_set_unordered_access_view *op;
1389 
1390     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1391     op->opcode = WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW;
1392     op->pipeline = pipeline;
1393     op->view_idx = view_idx;
1394     op->view = view;
1395     op->initial_count = initial_count;
1396 
1397     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1398 }
1399 
wined3d_cs_exec_set_sampler(struct wined3d_cs * cs,const void * data)1400 static void wined3d_cs_exec_set_sampler(struct wined3d_cs *cs, const void *data)
1401 {
1402     const struct wined3d_cs_set_sampler *op = data;
1403 
1404     cs->state.sampler[op->type][op->sampler_idx] = op->sampler;
1405     if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1406         device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1407     else
1408         device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1409 }
1410 
wined3d_cs_emit_set_sampler(struct wined3d_cs * cs,enum wined3d_shader_type type,UINT sampler_idx,struct wined3d_sampler * sampler)1411 void wined3d_cs_emit_set_sampler(struct wined3d_cs *cs, enum wined3d_shader_type type,
1412         UINT sampler_idx, struct wined3d_sampler *sampler)
1413 {
1414     struct wined3d_cs_set_sampler *op;
1415 
1416     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1417     op->opcode = WINED3D_CS_OP_SET_SAMPLER;
1418     op->type = type;
1419     op->sampler_idx = sampler_idx;
1420     op->sampler = sampler;
1421 
1422     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1423 }
1424 
wined3d_cs_exec_set_shader(struct wined3d_cs * cs,const void * data)1425 static void wined3d_cs_exec_set_shader(struct wined3d_cs *cs, const void *data)
1426 {
1427     const struct wined3d_cs_set_shader *op = data;
1428 
1429     cs->state.shader[op->type] = op->shader;
1430     device_invalidate_state(cs->device, STATE_SHADER(op->type));
1431     if (op->type != WINED3D_SHADER_TYPE_COMPUTE)
1432         device_invalidate_state(cs->device, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1433     else
1434         device_invalidate_state(cs->device, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1435 }
1436 
wined3d_cs_emit_set_shader(struct wined3d_cs * cs,enum wined3d_shader_type type,struct wined3d_shader * shader)1437 void wined3d_cs_emit_set_shader(struct wined3d_cs *cs, enum wined3d_shader_type type, struct wined3d_shader *shader)
1438 {
1439     struct wined3d_cs_set_shader *op;
1440 
1441     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1442     op->opcode = WINED3D_CS_OP_SET_SHADER;
1443     op->type = type;
1444     op->shader = shader;
1445 
1446     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1447 }
1448 
wined3d_cs_exec_set_blend_state(struct wined3d_cs * cs,const void * data)1449 static void wined3d_cs_exec_set_blend_state(struct wined3d_cs *cs, const void *data)
1450 {
1451     const struct wined3d_cs_set_blend_state *op = data;
1452 
1453     cs->state.blend_state = op->state;
1454     device_invalidate_state(cs->device, STATE_BLEND);
1455 }
1456 
wined3d_cs_emit_set_blend_state(struct wined3d_cs * cs,struct wined3d_blend_state * state)1457 void wined3d_cs_emit_set_blend_state(struct wined3d_cs *cs, struct wined3d_blend_state *state)
1458 {
1459     struct wined3d_cs_set_blend_state *op;
1460 
1461     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1462     op->opcode = WINED3D_CS_OP_SET_BLEND_STATE;
1463     op->state = state;
1464 
1465     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1466 }
1467 
wined3d_cs_exec_set_rasterizer_state(struct wined3d_cs * cs,const void * data)1468 static void wined3d_cs_exec_set_rasterizer_state(struct wined3d_cs *cs, const void *data)
1469 {
1470     const struct wined3d_cs_set_rasterizer_state *op = data;
1471 
1472     cs->state.rasterizer_state = op->state;
1473     device_invalidate_state(cs->device, STATE_FRONTFACE);
1474 }
1475 
wined3d_cs_emit_set_rasterizer_state(struct wined3d_cs * cs,struct wined3d_rasterizer_state * rasterizer_state)1476 void wined3d_cs_emit_set_rasterizer_state(struct wined3d_cs *cs,
1477         struct wined3d_rasterizer_state *rasterizer_state)
1478 {
1479     struct wined3d_cs_set_rasterizer_state *op;
1480 
1481     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1482     op->opcode = WINED3D_CS_OP_SET_RASTERIZER_STATE;
1483     op->state = rasterizer_state;
1484 
1485     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1486 }
1487 
wined3d_cs_exec_set_render_state(struct wined3d_cs * cs,const void * data)1488 static void wined3d_cs_exec_set_render_state(struct wined3d_cs *cs, const void *data)
1489 {
1490     const struct wined3d_cs_set_render_state *op = data;
1491 
1492     cs->state.render_states[op->state] = op->value;
1493     device_invalidate_state(cs->device, STATE_RENDER(op->state));
1494 }
1495 
wined3d_cs_emit_set_render_state(struct wined3d_cs * cs,enum wined3d_render_state state,DWORD value)1496 void wined3d_cs_emit_set_render_state(struct wined3d_cs *cs, enum wined3d_render_state state, DWORD value)
1497 {
1498     struct wined3d_cs_set_render_state *op;
1499 
1500     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1501     op->opcode = WINED3D_CS_OP_SET_RENDER_STATE;
1502     op->state = state;
1503     op->value = value;
1504 
1505     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1506 }
1507 
wined3d_cs_exec_set_texture_state(struct wined3d_cs * cs,const void * data)1508 static void wined3d_cs_exec_set_texture_state(struct wined3d_cs *cs, const void *data)
1509 {
1510     const struct wined3d_cs_set_texture_state *op = data;
1511 
1512     cs->state.texture_states[op->stage][op->state] = op->value;
1513     device_invalidate_state(cs->device, STATE_TEXTURESTAGE(op->stage, op->state));
1514 }
1515 
wined3d_cs_emit_set_texture_state(struct wined3d_cs * cs,UINT stage,enum wined3d_texture_stage_state state,DWORD value)1516 void wined3d_cs_emit_set_texture_state(struct wined3d_cs *cs, UINT stage,
1517         enum wined3d_texture_stage_state state, DWORD value)
1518 {
1519     struct wined3d_cs_set_texture_state *op;
1520 
1521     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1522     op->opcode = WINED3D_CS_OP_SET_TEXTURE_STATE;
1523     op->stage = stage;
1524     op->state = state;
1525     op->value = value;
1526 
1527     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1528 }
1529 
wined3d_cs_exec_set_sampler_state(struct wined3d_cs * cs,const void * data)1530 static void wined3d_cs_exec_set_sampler_state(struct wined3d_cs *cs, const void *data)
1531 {
1532     const struct wined3d_cs_set_sampler_state *op = data;
1533 
1534     cs->state.sampler_states[op->sampler_idx][op->state] = op->value;
1535     device_invalidate_state(cs->device, STATE_SAMPLER(op->sampler_idx));
1536 }
1537 
wined3d_cs_emit_set_sampler_state(struct wined3d_cs * cs,UINT sampler_idx,enum wined3d_sampler_state state,DWORD value)1538 void wined3d_cs_emit_set_sampler_state(struct wined3d_cs *cs, UINT sampler_idx,
1539         enum wined3d_sampler_state state, DWORD value)
1540 {
1541     struct wined3d_cs_set_sampler_state *op;
1542 
1543     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1544     op->opcode = WINED3D_CS_OP_SET_SAMPLER_STATE;
1545     op->sampler_idx = sampler_idx;
1546     op->state = state;
1547     op->value = value;
1548 
1549     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1550 }
1551 
wined3d_cs_exec_set_transform(struct wined3d_cs * cs,const void * data)1552 static void wined3d_cs_exec_set_transform(struct wined3d_cs *cs, const void *data)
1553 {
1554     const struct wined3d_cs_set_transform *op = data;
1555 
1556     cs->state.transforms[op->state] = op->matrix;
1557     if (op->state < WINED3D_TS_WORLD_MATRIX(cs->device->adapter->d3d_info.limits.ffp_vertex_blend_matrices))
1558         device_invalidate_state(cs->device, STATE_TRANSFORM(op->state));
1559 }
1560 
wined3d_cs_emit_set_transform(struct wined3d_cs * cs,enum wined3d_transform_state state,const struct wined3d_matrix * matrix)1561 void wined3d_cs_emit_set_transform(struct wined3d_cs *cs, enum wined3d_transform_state state,
1562         const struct wined3d_matrix *matrix)
1563 {
1564     struct wined3d_cs_set_transform *op;
1565 
1566     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1567     op->opcode = WINED3D_CS_OP_SET_TRANSFORM;
1568     op->state = state;
1569     op->matrix = *matrix;
1570 
1571     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1572 }
1573 
wined3d_cs_exec_set_clip_plane(struct wined3d_cs * cs,const void * data)1574 static void wined3d_cs_exec_set_clip_plane(struct wined3d_cs *cs, const void *data)
1575 {
1576     const struct wined3d_cs_set_clip_plane *op = data;
1577 
1578     cs->state.clip_planes[op->plane_idx] = op->plane;
1579     device_invalidate_state(cs->device, STATE_CLIPPLANE(op->plane_idx));
1580 }
1581 
wined3d_cs_emit_set_clip_plane(struct wined3d_cs * cs,UINT plane_idx,const struct wined3d_vec4 * plane)1582 void wined3d_cs_emit_set_clip_plane(struct wined3d_cs *cs, UINT plane_idx, const struct wined3d_vec4 *plane)
1583 {
1584     struct wined3d_cs_set_clip_plane *op;
1585 
1586     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1587     op->opcode = WINED3D_CS_OP_SET_CLIP_PLANE;
1588     op->plane_idx = plane_idx;
1589     op->plane = *plane;
1590 
1591     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1592 }
1593 
wined3d_cs_exec_set_color_key(struct wined3d_cs * cs,const void * data)1594 static void wined3d_cs_exec_set_color_key(struct wined3d_cs *cs, const void *data)
1595 {
1596     const struct wined3d_cs_set_color_key *op = data;
1597     struct wined3d_texture *texture = op->texture;
1598 
1599     if (op->set)
1600     {
1601         switch (op->flags)
1602         {
1603             case WINED3D_CKEY_DST_BLT:
1604                 texture->async.dst_blt_color_key = op->color_key;
1605                 texture->async.color_key_flags |= WINED3D_CKEY_DST_BLT;
1606                 break;
1607 
1608             case WINED3D_CKEY_DST_OVERLAY:
1609                 texture->async.dst_overlay_color_key = op->color_key;
1610                 texture->async.color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
1611                 break;
1612 
1613             case WINED3D_CKEY_SRC_BLT:
1614                 if (texture == cs->state.textures[0])
1615                 {
1616                     device_invalidate_state(cs->device, STATE_COLOR_KEY);
1617                     if (!(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1618                         device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1619                 }
1620 
1621                 texture->async.src_blt_color_key = op->color_key;
1622                 texture->async.color_key_flags |= WINED3D_CKEY_SRC_BLT;
1623                 break;
1624 
1625             case WINED3D_CKEY_SRC_OVERLAY:
1626                 texture->async.src_overlay_color_key = op->color_key;
1627                 texture->async.color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
1628                 break;
1629         }
1630     }
1631     else
1632     {
1633         switch (op->flags)
1634         {
1635             case WINED3D_CKEY_DST_BLT:
1636                 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_BLT;
1637                 break;
1638 
1639             case WINED3D_CKEY_DST_OVERLAY:
1640                 texture->async.color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
1641                 break;
1642 
1643             case WINED3D_CKEY_SRC_BLT:
1644                 if (texture == cs->state.textures[0] && texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1645                     device_invalidate_state(cs->device, STATE_RENDER(WINED3D_RS_COLORKEYENABLE));
1646 
1647                 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
1648                 break;
1649 
1650             case WINED3D_CKEY_SRC_OVERLAY:
1651                 texture->async.color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
1652                 break;
1653         }
1654     }
1655 }
1656 
wined3d_cs_emit_set_color_key(struct wined3d_cs * cs,struct wined3d_texture * texture,WORD flags,const struct wined3d_color_key * color_key)1657 void wined3d_cs_emit_set_color_key(struct wined3d_cs *cs, struct wined3d_texture *texture,
1658         WORD flags, const struct wined3d_color_key *color_key)
1659 {
1660     struct wined3d_cs_set_color_key *op;
1661 
1662     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1663     op->opcode = WINED3D_CS_OP_SET_COLOR_KEY;
1664     op->texture = texture;
1665     op->flags = flags;
1666     if (color_key)
1667     {
1668         op->color_key = *color_key;
1669         op->set = 1;
1670     }
1671     else
1672         op->set = 0;
1673 
1674     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1675 }
1676 
wined3d_cs_exec_set_material(struct wined3d_cs * cs,const void * data)1677 static void wined3d_cs_exec_set_material(struct wined3d_cs *cs, const void *data)
1678 {
1679     const struct wined3d_cs_set_material *op = data;
1680 
1681     cs->state.material = op->material;
1682     device_invalidate_state(cs->device, STATE_MATERIAL);
1683 }
1684 
wined3d_cs_emit_set_material(struct wined3d_cs * cs,const struct wined3d_material * material)1685 void wined3d_cs_emit_set_material(struct wined3d_cs *cs, const struct wined3d_material *material)
1686 {
1687     struct wined3d_cs_set_material *op;
1688 
1689     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1690     op->opcode = WINED3D_CS_OP_SET_MATERIAL;
1691     op->material = *material;
1692 
1693     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1694 }
1695 
wined3d_cs_exec_set_light(struct wined3d_cs * cs,const void * data)1696 static void wined3d_cs_exec_set_light(struct wined3d_cs *cs, const void *data)
1697 {
1698     const struct wined3d_cs_set_light *op = data;
1699     struct wined3d_light_info *light_info;
1700     unsigned int light_idx, hash_idx;
1701 
1702     light_idx = op->light.OriginalIndex;
1703 
1704     if (!(light_info = wined3d_state_get_light(&cs->state, light_idx)))
1705     {
1706         TRACE("Adding new light.\n");
1707         if (!(light_info = heap_alloc_zero(sizeof(*light_info))))
1708         {
1709             ERR("Failed to allocate light info.\n");
1710             return;
1711         }
1712 
1713         hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1714         list_add_head(&cs->state.light_map[hash_idx], &light_info->entry);
1715         light_info->glIndex = -1;
1716         light_info->OriginalIndex = light_idx;
1717     }
1718 
1719     if (light_info->glIndex != -1)
1720     {
1721         if (light_info->OriginalParms.type != op->light.OriginalParms.type)
1722             device_invalidate_state(cs->device, STATE_LIGHT_TYPE);
1723         device_invalidate_state(cs->device, STATE_ACTIVELIGHT(light_info->glIndex));
1724     }
1725 
1726     light_info->OriginalParms = op->light.OriginalParms;
1727     light_info->position = op->light.position;
1728     light_info->direction = op->light.direction;
1729     light_info->exponent = op->light.exponent;
1730     light_info->cutoff = op->light.cutoff;
1731 }
1732 
wined3d_cs_emit_set_light(struct wined3d_cs * cs,const struct wined3d_light_info * light)1733 void wined3d_cs_emit_set_light(struct wined3d_cs *cs, const struct wined3d_light_info *light)
1734 {
1735     struct wined3d_cs_set_light *op;
1736 
1737     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1738     op->opcode = WINED3D_CS_OP_SET_LIGHT;
1739     op->light = *light;
1740 
1741     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1742 }
1743 
wined3d_cs_exec_set_light_enable(struct wined3d_cs * cs,const void * data)1744 static void wined3d_cs_exec_set_light_enable(struct wined3d_cs *cs, const void *data)
1745 {
1746     const struct wined3d_cs_set_light_enable *op = data;
1747     struct wined3d_device *device = cs->device;
1748     struct wined3d_light_info *light_info;
1749     int prev_idx;
1750 
1751     if (!(light_info = wined3d_state_get_light(&cs->state, op->idx)))
1752     {
1753         ERR("Light doesn't exist.\n");
1754         return;
1755     }
1756 
1757     prev_idx = light_info->glIndex;
1758     wined3d_state_enable_light(&cs->state, &device->adapter->d3d_info, light_info, op->enable);
1759     if (light_info->glIndex != prev_idx)
1760     {
1761         device_invalidate_state(device, STATE_LIGHT_TYPE);
1762         device_invalidate_state(device, STATE_ACTIVELIGHT(op->enable ? light_info->glIndex : prev_idx));
1763     }
1764 }
1765 
wined3d_cs_emit_set_light_enable(struct wined3d_cs * cs,unsigned int idx,BOOL enable)1766 void wined3d_cs_emit_set_light_enable(struct wined3d_cs *cs, unsigned int idx, BOOL enable)
1767 {
1768     struct wined3d_cs_set_light_enable *op;
1769 
1770     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1771     op->opcode = WINED3D_CS_OP_SET_LIGHT_ENABLE;
1772     op->idx = idx;
1773     op->enable = enable;
1774 
1775     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1776 }
1777 
1778 static const struct
1779 {
1780     size_t offset;
1781     size_t size;
1782     DWORD mask;
1783 }
1784 wined3d_cs_push_constant_info[] =
1785 {
1786     /* WINED3D_PUSH_CONSTANTS_VS_F */
1787     {FIELD_OFFSET(struct wined3d_state, vs_consts_f), sizeof(struct wined3d_vec4),  WINED3D_SHADER_CONST_VS_F},
1788     /* WINED3D_PUSH_CONSTANTS_PS_F */
1789     {FIELD_OFFSET(struct wined3d_state, ps_consts_f), sizeof(struct wined3d_vec4),  WINED3D_SHADER_CONST_PS_F},
1790     /* WINED3D_PUSH_CONSTANTS_VS_I */
1791     {FIELD_OFFSET(struct wined3d_state, vs_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_VS_I},
1792     /* WINED3D_PUSH_CONSTANTS_PS_I */
1793     {FIELD_OFFSET(struct wined3d_state, ps_consts_i), sizeof(struct wined3d_ivec4), WINED3D_SHADER_CONST_PS_I},
1794     /* WINED3D_PUSH_CONSTANTS_VS_B */
1795     {FIELD_OFFSET(struct wined3d_state, vs_consts_b), sizeof(BOOL),                 WINED3D_SHADER_CONST_VS_B},
1796     /* WINED3D_PUSH_CONSTANTS_PS_B */
1797     {FIELD_OFFSET(struct wined3d_state, ps_consts_b), sizeof(BOOL),                 WINED3D_SHADER_CONST_PS_B},
1798 };
1799 
wined3d_cs_st_push_constants(struct wined3d_cs * cs,enum wined3d_push_constants p,unsigned int start_idx,unsigned int count,const void * constants)1800 static void wined3d_cs_st_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1801         unsigned int start_idx, unsigned int count, const void *constants)
1802 {
1803     struct wined3d_device *device = cs->device;
1804     unsigned int context_count;
1805     unsigned int i;
1806     size_t offset;
1807 
1808     if (p == WINED3D_PUSH_CONSTANTS_VS_F)
1809         device->shader_backend->shader_update_float_vertex_constants(device, start_idx, count);
1810     else if (p == WINED3D_PUSH_CONSTANTS_PS_F)
1811         device->shader_backend->shader_update_float_pixel_constants(device, start_idx, count);
1812 
1813     offset = wined3d_cs_push_constant_info[p].offset + start_idx * wined3d_cs_push_constant_info[p].size;
1814     memcpy((BYTE *)&cs->state + offset, constants, count * wined3d_cs_push_constant_info[p].size);
1815     for (i = 0, context_count = device->context_count; i < context_count; ++i)
1816     {
1817         device->contexts[i]->constant_update_mask |= wined3d_cs_push_constant_info[p].mask;
1818     }
1819 }
1820 
wined3d_cs_exec_push_constants(struct wined3d_cs * cs,const void * data)1821 static void wined3d_cs_exec_push_constants(struct wined3d_cs *cs, const void *data)
1822 {
1823     const struct wined3d_cs_push_constants *op = data;
1824 
1825     wined3d_cs_st_push_constants(cs, op->type, op->start_idx, op->count, op->constants);
1826 }
1827 
wined3d_cs_mt_push_constants(struct wined3d_cs * cs,enum wined3d_push_constants p,unsigned int start_idx,unsigned int count,const void * constants)1828 static void wined3d_cs_mt_push_constants(struct wined3d_cs *cs, enum wined3d_push_constants p,
1829         unsigned int start_idx, unsigned int count, const void *constants)
1830 {
1831     struct wined3d_cs_push_constants *op;
1832     size_t size;
1833 
1834     size = count * wined3d_cs_push_constant_info[p].size;
1835     op = cs->ops->require_space(cs, FIELD_OFFSET(struct wined3d_cs_push_constants, constants[size]),
1836             WINED3D_CS_QUEUE_DEFAULT);
1837     op->opcode = WINED3D_CS_OP_PUSH_CONSTANTS;
1838     op->type = p;
1839     op->start_idx = start_idx;
1840     op->count = count;
1841     memcpy(op->constants, constants, size);
1842 
1843     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1844 }
1845 
wined3d_cs_exec_reset_state(struct wined3d_cs * cs,const void * data)1846 static void wined3d_cs_exec_reset_state(struct wined3d_cs *cs, const void *data)
1847 {
1848     struct wined3d_adapter *adapter = cs->device->adapter;
1849 
1850     state_cleanup(&cs->state);
1851     memset(&cs->state, 0, sizeof(cs->state));
1852     state_init(&cs->state, &cs->fb, &adapter->gl_info, &adapter->d3d_info,
1853             WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
1854 }
1855 
wined3d_cs_emit_reset_state(struct wined3d_cs * cs)1856 void wined3d_cs_emit_reset_state(struct wined3d_cs *cs)
1857 {
1858     struct wined3d_cs_reset_state *op;
1859 
1860     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1861     op->opcode = WINED3D_CS_OP_RESET_STATE;
1862 
1863     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1864 }
1865 
wined3d_cs_exec_callback(struct wined3d_cs * cs,const void * data)1866 static void wined3d_cs_exec_callback(struct wined3d_cs *cs, const void *data)
1867 {
1868     const struct wined3d_cs_callback *op = data;
1869 
1870     op->callback(op->object);
1871 }
1872 
wined3d_cs_emit_callback(struct wined3d_cs * cs,void (* callback)(void * object),void * object)1873 static void wined3d_cs_emit_callback(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1874 {
1875     struct wined3d_cs_callback *op;
1876 
1877     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1878     op->opcode = WINED3D_CS_OP_CALLBACK;
1879     op->callback = callback;
1880     op->object = object;
1881 
1882     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1883 }
1884 
wined3d_cs_destroy_object(struct wined3d_cs * cs,void (* callback)(void * object),void * object)1885 void wined3d_cs_destroy_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1886 {
1887     wined3d_cs_emit_callback(cs, callback, object);
1888 }
1889 
wined3d_cs_init_object(struct wined3d_cs * cs,void (* callback)(void * object),void * object)1890 void wined3d_cs_init_object(struct wined3d_cs *cs, void (*callback)(void *object), void *object)
1891 {
1892     wined3d_cs_emit_callback(cs, callback, object);
1893 }
1894 
wined3d_cs_exec_query_issue(struct wined3d_cs * cs,const void * data)1895 static void wined3d_cs_exec_query_issue(struct wined3d_cs *cs, const void *data)
1896 {
1897     const struct wined3d_cs_query_issue *op = data;
1898     struct wined3d_query *query = op->query;
1899     BOOL poll;
1900 
1901     poll = query->query_ops->query_issue(query, op->flags);
1902 
1903     if (!cs->thread)
1904         return;
1905 
1906     if (poll && list_empty(&query->poll_list_entry))
1907     {
1908         list_add_tail(&cs->query_poll_list, &query->poll_list_entry);
1909         return;
1910     }
1911 
1912     /* This can happen if occlusion queries are restarted. This discards the
1913      * old result, since polling it could result in a GL error. */
1914     if ((op->flags & WINED3DISSUE_BEGIN) && !poll && !list_empty(&query->poll_list_entry))
1915     {
1916         list_remove(&query->poll_list_entry);
1917         list_init(&query->poll_list_entry);
1918         InterlockedIncrement(&query->counter_retrieved);
1919         return;
1920     }
1921 
1922     /* This can happen when an occlusion query is ended without being started,
1923      * in which case we don't want to poll, but still have to counter-balance
1924      * the increment of the main counter.
1925      *
1926      * This can also happen if an event query is re-issued before the first
1927      * fence was reached. In this case the query is already in the list and
1928      * the poll function will check the new fence. We have to counter-balance
1929      * the discarded increment. */
1930     if (op->flags & WINED3DISSUE_END)
1931         InterlockedIncrement(&query->counter_retrieved);
1932 }
1933 
wined3d_cs_emit_query_issue(struct wined3d_cs * cs,struct wined3d_query * query,DWORD flags)1934 void wined3d_cs_emit_query_issue(struct wined3d_cs *cs, struct wined3d_query *query, DWORD flags)
1935 {
1936     struct wined3d_cs_query_issue *op;
1937 
1938     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1939     op->opcode = WINED3D_CS_OP_QUERY_ISSUE;
1940     op->query = query;
1941     op->flags = flags;
1942 
1943     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1944     cs->queries_flushed = FALSE;
1945 }
1946 
wined3d_cs_exec_preload_resource(struct wined3d_cs * cs,const void * data)1947 static void wined3d_cs_exec_preload_resource(struct wined3d_cs *cs, const void *data)
1948 {
1949     const struct wined3d_cs_preload_resource *op = data;
1950     struct wined3d_resource *resource = op->resource;
1951 
1952     resource->resource_ops->resource_preload(resource);
1953     wined3d_resource_release(resource);
1954 }
1955 
wined3d_cs_emit_preload_resource(struct wined3d_cs * cs,struct wined3d_resource * resource)1956 void wined3d_cs_emit_preload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
1957 {
1958     struct wined3d_cs_preload_resource *op;
1959 
1960     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1961     op->opcode = WINED3D_CS_OP_PRELOAD_RESOURCE;
1962     op->resource = resource;
1963 
1964     wined3d_resource_acquire(resource);
1965 
1966     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1967 }
1968 
wined3d_cs_exec_unload_resource(struct wined3d_cs * cs,const void * data)1969 static void wined3d_cs_exec_unload_resource(struct wined3d_cs *cs, const void *data)
1970 {
1971     const struct wined3d_cs_unload_resource *op = data;
1972     struct wined3d_resource *resource = op->resource;
1973 
1974     resource->resource_ops->resource_unload(resource);
1975     wined3d_resource_release(resource);
1976 }
1977 
wined3d_cs_emit_unload_resource(struct wined3d_cs * cs,struct wined3d_resource * resource)1978 void wined3d_cs_emit_unload_resource(struct wined3d_cs *cs, struct wined3d_resource *resource)
1979 {
1980     struct wined3d_cs_unload_resource *op;
1981 
1982     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
1983     op->opcode = WINED3D_CS_OP_UNLOAD_RESOURCE;
1984     op->resource = resource;
1985 
1986     wined3d_resource_acquire(resource);
1987 
1988     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
1989 }
1990 
wined3d_cs_exec_map(struct wined3d_cs * cs,const void * data)1991 static void wined3d_cs_exec_map(struct wined3d_cs *cs, const void *data)
1992 {
1993     const struct wined3d_cs_map *op = data;
1994     struct wined3d_resource *resource = op->resource;
1995 
1996     *op->hr = resource->resource_ops->resource_sub_resource_map(resource,
1997             op->sub_resource_idx, op->map_desc, op->box, op->flags);
1998 }
1999 
wined3d_cs_map(struct wined3d_cs * cs,struct wined3d_resource * resource,unsigned int sub_resource_idx,struct wined3d_map_desc * map_desc,const struct wined3d_box * box,unsigned int flags)2000 HRESULT wined3d_cs_map(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx,
2001         struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
2002 {
2003     struct wined3d_cs_map *op;
2004     HRESULT hr;
2005 
2006     /* Mapping resources from the worker thread isn't an issue by itself, but
2007      * increasing the map count would be visible to applications. */
2008     wined3d_not_from_cs(cs);
2009 
2010     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2011     op->opcode = WINED3D_CS_OP_MAP;
2012     op->resource = resource;
2013     op->sub_resource_idx = sub_resource_idx;
2014     op->map_desc = map_desc;
2015     op->box = box;
2016     op->flags = flags;
2017     op->hr = &hr;
2018 
2019     cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2020     cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2021 
2022     return hr;
2023 }
2024 
wined3d_cs_exec_unmap(struct wined3d_cs * cs,const void * data)2025 static void wined3d_cs_exec_unmap(struct wined3d_cs *cs, const void *data)
2026 {
2027     const struct wined3d_cs_unmap *op = data;
2028     struct wined3d_resource *resource = op->resource;
2029 
2030     *op->hr = resource->resource_ops->resource_sub_resource_unmap(resource, op->sub_resource_idx);
2031 }
2032 
wined3d_cs_unmap(struct wined3d_cs * cs,struct wined3d_resource * resource,unsigned int sub_resource_idx)2033 HRESULT wined3d_cs_unmap(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx)
2034 {
2035     struct wined3d_cs_unmap *op;
2036     HRESULT hr;
2037 
2038     wined3d_not_from_cs(cs);
2039 
2040     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2041     op->opcode = WINED3D_CS_OP_UNMAP;
2042     op->resource = resource;
2043     op->sub_resource_idx = sub_resource_idx;
2044     op->hr = &hr;
2045 
2046     cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2047     cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2048 
2049     return hr;
2050 }
2051 
wined3d_cs_exec_blt_sub_resource(struct wined3d_cs * cs,const void * data)2052 static void wined3d_cs_exec_blt_sub_resource(struct wined3d_cs *cs, const void *data)
2053 {
2054     const struct wined3d_cs_blt_sub_resource *op = data;
2055 
2056     if (op->dst_resource->type == WINED3D_RTYPE_BUFFER)
2057     {
2058         wined3d_buffer_copy(buffer_from_resource(op->dst_resource), op->dst_box.left,
2059                 buffer_from_resource(op->src_resource), op->src_box.left,
2060                 op->src_box.right - op->src_box.left);
2061     }
2062     else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_2D)
2063     {
2064         struct wined3d_surface *dst_surface, *src_surface;
2065         struct wined3d_texture *dst_texture, *src_texture;
2066         RECT dst_rect, src_rect;
2067 
2068         dst_texture = texture_from_resource(op->dst_resource);
2069         src_texture = texture_from_resource(op->src_resource);
2070         dst_surface = dst_texture->sub_resources[op->dst_sub_resource_idx].u.surface;
2071         src_surface = src_texture->sub_resources[op->src_sub_resource_idx].u.surface;
2072         SetRect(&dst_rect, op->dst_box.left, op->dst_box.top, op->dst_box.right, op->dst_box.bottom);
2073         SetRect(&src_rect, op->src_box.left, op->src_box.top, op->src_box.right, op->src_box.bottom);
2074 
2075         if (FAILED(wined3d_surface_blt(dst_surface, &dst_rect, src_surface,
2076                 &src_rect, op->flags, &op->fx, op->filter)))
2077             FIXME("Blit failed.\n");
2078     }
2079     else if (op->dst_resource->type == WINED3D_RTYPE_TEXTURE_3D)
2080     {
2081         struct wined3d_texture *src_texture, *dst_texture;
2082         unsigned int level, update_w, update_h, update_d;
2083         unsigned int row_pitch, slice_pitch;
2084         struct wined3d_context *context;
2085         struct wined3d_bo_address addr;
2086 
2087         if (op->flags & ~WINED3D_BLT_RAW)
2088         {
2089             FIXME("Flags %#x not implemented for %s resources.\n",
2090                     op->flags, debug_d3dresourcetype(op->dst_resource->type));
2091             goto error;
2092         }
2093 
2094         if (!(op->flags & WINED3D_BLT_RAW) && op->src_resource->format != op->dst_resource->format)
2095         {
2096             FIXME("Format conversion not implemented for %s resources.\n",
2097                     debug_d3dresourcetype(op->dst_resource->type));
2098             goto error;
2099         }
2100 
2101         update_w = op->dst_box.right - op->dst_box.left;
2102         update_h = op->dst_box.bottom - op->dst_box.top;
2103         update_d = op->dst_box.back - op->dst_box.front;
2104         if (op->src_box.right - op->src_box.left != update_w
2105                 || op->src_box.bottom - op->src_box.top != update_h
2106                 || op->src_box.back - op->src_box.front != update_d)
2107         {
2108             FIXME("Stretching not implemented for %s resources.\n",
2109                     debug_d3dresourcetype(op->dst_resource->type));
2110             goto error;
2111         }
2112 
2113         if (op->src_box.left || op->src_box.top || op->src_box.front)
2114         {
2115             FIXME("Source box %s not supported for %s resources.\n",
2116                     debug_box(&op->src_box), debug_d3dresourcetype(op->dst_resource->type));
2117             goto error;
2118         }
2119 
2120         dst_texture = texture_from_resource(op->dst_resource);
2121         src_texture = texture_from_resource(op->src_resource);
2122 
2123         context = context_acquire(cs->device, NULL, 0);
2124 
2125         if (!wined3d_texture_load_location(src_texture, op->src_sub_resource_idx,
2126                 context, src_texture->resource.map_binding))
2127         {
2128             ERR("Failed to load source sub-resource into %s.\n",
2129                     wined3d_debug_location(src_texture->resource.map_binding));
2130             context_release(context);
2131             goto error;
2132         }
2133 
2134         level = op->dst_sub_resource_idx % dst_texture->level_count;
2135         if (update_w == wined3d_texture_get_level_width(dst_texture, level)
2136                 && update_h == wined3d_texture_get_level_height(dst_texture, level)
2137                 && update_d == wined3d_texture_get_level_depth(dst_texture, level))
2138         {
2139             wined3d_texture_prepare_texture(dst_texture, context, FALSE);
2140         }
2141         else if (!wined3d_texture_load_location(dst_texture, op->dst_sub_resource_idx,
2142                 context, WINED3D_LOCATION_TEXTURE_RGB))
2143         {
2144             ERR("Failed to load destination sub-resource.\n");
2145             context_release(context);
2146             goto error;
2147         }
2148 
2149         wined3d_texture_get_memory(src_texture, op->src_sub_resource_idx, &addr, src_texture->resource.map_binding);
2150         wined3d_texture_get_pitch(src_texture, op->src_sub_resource_idx % src_texture->level_count,
2151                 &row_pitch, &slice_pitch);
2152 
2153         wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
2154         wined3d_texture_upload_data(dst_texture, op->dst_sub_resource_idx, context, &op->dst_box,
2155                 wined3d_const_bo_address(&addr), row_pitch, slice_pitch);
2156         wined3d_texture_validate_location(dst_texture, op->dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2157         wined3d_texture_invalidate_location(dst_texture, op->dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2158 
2159         context_release(context);
2160     }
2161     else
2162     {
2163         FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(op->dst_resource->type));
2164     }
2165 
2166 error:
2167     if (op->src_resource)
2168         wined3d_resource_release(op->src_resource);
2169     wined3d_resource_release(op->dst_resource);
2170 }
2171 
wined3d_cs_emit_blt_sub_resource(struct wined3d_cs * cs,struct wined3d_resource * dst_resource,unsigned int dst_sub_resource_idx,const struct wined3d_box * dst_box,struct wined3d_resource * src_resource,unsigned int src_sub_resource_idx,const struct wined3d_box * src_box,DWORD flags,const struct wined3d_blt_fx * fx,enum wined3d_texture_filter_type filter)2172 void wined3d_cs_emit_blt_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *dst_resource,
2173         unsigned int dst_sub_resource_idx, const struct wined3d_box *dst_box, struct wined3d_resource *src_resource,
2174         unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, DWORD flags,
2175         const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2176 {
2177     struct wined3d_cs_blt_sub_resource *op;
2178 
2179     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2180     op->opcode = WINED3D_CS_OP_BLT_SUB_RESOURCE;
2181     op->dst_resource = dst_resource;
2182     op->dst_sub_resource_idx = dst_sub_resource_idx;
2183     op->dst_box = *dst_box;
2184     op->src_resource = src_resource;
2185     op->src_sub_resource_idx = src_sub_resource_idx;
2186     op->src_box = *src_box;
2187     op->flags = flags;
2188     if (fx)
2189         op->fx = *fx;
2190     else
2191         memset(&op->fx, 0, sizeof(op->fx));
2192     op->filter = filter;
2193 
2194     wined3d_resource_acquire(dst_resource);
2195     if (src_resource)
2196         wined3d_resource_acquire(src_resource);
2197 
2198     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2199     if (flags & WINED3D_BLT_SYNCHRONOUS)
2200         cs->ops->finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2201 }
2202 
wined3d_cs_exec_update_sub_resource(struct wined3d_cs * cs,const void * data)2203 static void wined3d_cs_exec_update_sub_resource(struct wined3d_cs *cs, const void *data)
2204 {
2205     const struct wined3d_cs_update_sub_resource *op = data;
2206     struct wined3d_resource *resource = op->resource;
2207     const struct wined3d_box *box = &op->box;
2208     unsigned int width, height, depth, level;
2209     struct wined3d_const_bo_address addr;
2210     struct wined3d_context *context;
2211     struct wined3d_texture *texture;
2212 
2213     context = context_acquire(cs->device, NULL, 0);
2214 
2215     if (resource->type == WINED3D_RTYPE_BUFFER)
2216     {
2217         struct wined3d_buffer *buffer = buffer_from_resource(resource);
2218 
2219         if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
2220         {
2221             ERR("Failed to load buffer location.\n");
2222             goto done;
2223         }
2224 
2225         wined3d_buffer_upload_data(buffer, context, box, op->data.data);
2226         wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
2227         goto done;
2228     }
2229 
2230     texture = wined3d_texture_from_resource(resource);
2231 
2232     level = op->sub_resource_idx % texture->level_count;
2233     width = wined3d_texture_get_level_width(texture, level);
2234     height = wined3d_texture_get_level_height(texture, level);
2235     depth = wined3d_texture_get_level_depth(texture, level);
2236 
2237     addr.buffer_object = 0;
2238     addr.addr = op->data.data;
2239 
2240     /* Only load the sub-resource for partial updates. */
2241     if (!box->left && !box->top && !box->front
2242             && box->right == width && box->bottom == height && box->back == depth)
2243         wined3d_texture_prepare_texture(texture, context, FALSE);
2244     else
2245         wined3d_texture_load_location(texture, op->sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2246     wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2247 
2248     wined3d_texture_upload_data(texture, op->sub_resource_idx, context,
2249             box, &addr, op->data.row_pitch, op->data.slice_pitch);
2250 
2251     wined3d_texture_validate_location(texture, op->sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
2252     wined3d_texture_invalidate_location(texture, op->sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
2253 
2254 done:
2255     context_release(context);
2256 
2257     wined3d_resource_release(resource);
2258 }
2259 
wined3d_cs_emit_update_sub_resource(struct wined3d_cs * cs,struct wined3d_resource * resource,unsigned int sub_resource_idx,const struct wined3d_box * box,const void * data,unsigned int row_pitch,unsigned int slice_pitch)2260 void wined3d_cs_emit_update_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *resource,
2261         unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
2262         unsigned int slice_pitch)
2263 {
2264     struct wined3d_cs_update_sub_resource *op;
2265 #if defined(STAGING_CSMT)
2266     size_t data_size, size;
2267 
2268     if (resource->type != WINED3D_RTYPE_BUFFER && resource->format_flags & WINED3DFMT_FLAG_BLOCKS)
2269         goto no_async;
2270 
2271     data_size = 0;
2272     switch (resource->type)
2273     {
2274         case WINED3D_RTYPE_TEXTURE_3D:
2275             data_size += (box->back - box->front - 1) * slice_pitch;
2276             /* fall-through */
2277         case WINED3D_RTYPE_TEXTURE_2D:
2278             data_size += (box->bottom - box->top - 1) * row_pitch;
2279             /* fall-through */
2280         case WINED3D_RTYPE_TEXTURE_1D:
2281             data_size += (box->right - box->left) * resource->format->byte_count;
2282             break;
2283         case WINED3D_RTYPE_BUFFER:
2284             data_size = box->right - box->left;
2285             break;
2286         case WINED3D_RTYPE_NONE:
2287             return;
2288     }
2289 
2290     size = FIELD_OFFSET(struct wined3d_cs_update_sub_resource, copy_data[data_size]);
2291     if (!cs->ops->check_space(cs, size, WINED3D_CS_QUEUE_DEFAULT))
2292         goto no_async;
2293 
2294     op = cs->ops->require_space(cs, size, WINED3D_CS_QUEUE_DEFAULT);
2295     op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2296     op->resource = resource;
2297     op->sub_resource_idx = sub_resource_idx;
2298     op->box = *box;
2299     op->data.row_pitch = row_pitch;
2300     op->data.slice_pitch = slice_pitch;
2301     op->data.data = op->copy_data;
2302     memcpy(op->copy_data, data, data_size);
2303 
2304     wined3d_resource_acquire(resource);
2305 
2306     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2307     return;
2308 
2309 no_async:
2310     wined3d_resource_wait_idle(resource);
2311 #endif /* STAGING_CSMT */
2312 
2313     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_MAP);
2314     op->opcode = WINED3D_CS_OP_UPDATE_SUB_RESOURCE;
2315     op->resource = resource;
2316     op->sub_resource_idx = sub_resource_idx;
2317     op->box = *box;
2318     op->data.row_pitch = row_pitch;
2319     op->data.slice_pitch = slice_pitch;
2320     op->data.data = data;
2321 
2322     wined3d_resource_acquire(resource);
2323 
2324     cs->ops->submit(cs, WINED3D_CS_QUEUE_MAP);
2325 #if !defined(STAGING_CSMT)
2326     /* The data pointer may go away, so we need to wait until it is read.
2327      * Copying the data may be faster if it's small. */
2328 #endif /* STAGING_CSMT */
2329     cs->ops->finish(cs, WINED3D_CS_QUEUE_MAP);
2330 }
2331 
wined3d_cs_exec_add_dirty_texture_region(struct wined3d_cs * cs,const void * data)2332 static void wined3d_cs_exec_add_dirty_texture_region(struct wined3d_cs *cs, const void *data)
2333 {
2334     const struct wined3d_cs_add_dirty_texture_region *op = data;
2335     struct wined3d_texture *texture = op->texture;
2336     unsigned int sub_resource_idx, i;
2337     struct wined3d_context *context;
2338 
2339     context = context_acquire(cs->device, NULL, 0);
2340     sub_resource_idx = op->layer * texture->level_count;
2341     for (i = 0; i < texture->level_count; ++i, ++sub_resource_idx)
2342     {
2343         if (wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
2344             wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2345         else
2346             ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
2347     }
2348     context_release(context);
2349 
2350     wined3d_resource_release(&texture->resource);
2351 }
2352 
wined3d_cs_emit_add_dirty_texture_region(struct wined3d_cs * cs,struct wined3d_texture * texture,unsigned int layer)2353 void wined3d_cs_emit_add_dirty_texture_region(struct wined3d_cs *cs,
2354         struct wined3d_texture *texture, unsigned int layer)
2355 {
2356     struct wined3d_cs_add_dirty_texture_region *op;
2357 
2358     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2359     op->opcode = WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION;
2360     op->texture = texture;
2361     op->layer = layer;
2362 
2363     wined3d_resource_acquire(&texture->resource);
2364 
2365     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2366 }
2367 
wined3d_cs_exec_clear_unordered_access_view(struct wined3d_cs * cs,const void * data)2368 static void wined3d_cs_exec_clear_unordered_access_view(struct wined3d_cs *cs, const void *data)
2369 {
2370     const struct wined3d_cs_clear_unordered_access_view *op = data;
2371     struct wined3d_unordered_access_view *view = op->view;
2372     struct wined3d_context *context;
2373 
2374     context = context_acquire(cs->device, NULL, 0);
2375     wined3d_unordered_access_view_clear_uint(view, &op->clear_value, context);
2376     context_release(context);
2377 
2378     wined3d_resource_release(view->resource);
2379 }
2380 
wined3d_cs_emit_clear_unordered_access_view_uint(struct wined3d_cs * cs,struct wined3d_unordered_access_view * view,const struct wined3d_uvec4 * clear_value)2381 void wined3d_cs_emit_clear_unordered_access_view_uint(struct wined3d_cs *cs,
2382         struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
2383 {
2384     struct wined3d_cs_clear_unordered_access_view *op;
2385 
2386     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2387     op->opcode = WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW;
2388     op->view = view;
2389     op->clear_value = *clear_value;
2390 
2391     wined3d_resource_acquire(view->resource);
2392 
2393     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2394 }
2395 
wined3d_cs_exec_copy_uav_counter(struct wined3d_cs * cs,const void * data)2396 static void wined3d_cs_exec_copy_uav_counter(struct wined3d_cs *cs, const void *data)
2397 {
2398     const struct wined3d_cs_copy_uav_counter *op = data;
2399     struct wined3d_unordered_access_view *view = op->view;
2400     struct wined3d_context *context;
2401 
2402     context = context_acquire(cs->device, NULL, 0);
2403     wined3d_unordered_access_view_copy_counter(view, op->buffer, op->offset, context);
2404     context_release(context);
2405 
2406     wined3d_resource_release(&op->buffer->resource);
2407     wined3d_resource_release(view->resource);
2408 }
2409 
wined3d_cs_emit_copy_uav_counter(struct wined3d_cs * cs,struct wined3d_buffer * dst_buffer,unsigned int offset,struct wined3d_unordered_access_view * uav)2410 void wined3d_cs_emit_copy_uav_counter(struct wined3d_cs *cs, struct wined3d_buffer *dst_buffer,
2411         unsigned int offset, struct wined3d_unordered_access_view *uav)
2412 {
2413     struct wined3d_cs_copy_uav_counter *op;
2414 
2415     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2416     op->opcode = WINED3D_CS_OP_COPY_UAV_COUNTER;
2417     op->buffer = dst_buffer;
2418     op->offset = offset;
2419     op->view = uav;
2420 
2421     wined3d_resource_acquire(&dst_buffer->resource);
2422     wined3d_resource_acquire(uav->resource);
2423 
2424     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2425 }
2426 
wined3d_cs_exec_generate_mipmaps(struct wined3d_cs * cs,const void * data)2427 static void wined3d_cs_exec_generate_mipmaps(struct wined3d_cs *cs, const void *data)
2428 {
2429     const struct wined3d_cs_generate_mipmaps *op = data;
2430     struct wined3d_shader_resource_view *view = op->view;
2431 
2432     shader_resource_view_generate_mipmaps(view);
2433     wined3d_resource_release(view->resource);
2434 }
2435 
wined3d_cs_emit_generate_mipmaps(struct wined3d_cs * cs,struct wined3d_shader_resource_view * view)2436 void wined3d_cs_emit_generate_mipmaps(struct wined3d_cs *cs, struct wined3d_shader_resource_view *view)
2437 {
2438     struct wined3d_cs_generate_mipmaps *op;
2439 
2440     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2441     op->opcode = WINED3D_CS_OP_GENERATE_MIPMAPS;
2442     op->view = view;
2443 
2444     wined3d_resource_acquire(view->resource);
2445 
2446     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2447 }
2448 
wined3d_cs_emit_stop(struct wined3d_cs * cs)2449 static void wined3d_cs_emit_stop(struct wined3d_cs *cs)
2450 {
2451     struct wined3d_cs_stop *op;
2452 
2453     op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
2454     op->opcode = WINED3D_CS_OP_STOP;
2455 
2456     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
2457     cs->ops->finish(cs, WINED3D_CS_QUEUE_DEFAULT);
2458 }
2459 
2460 static void (* const wined3d_cs_op_handlers[])(struct wined3d_cs *cs, const void *data) =
2461 {
2462     /* WINED3D_CS_OP_NOP                         */ wined3d_cs_exec_nop,
2463     /* WINED3D_CS_OP_PRESENT                     */ wined3d_cs_exec_present,
2464     /* WINED3D_CS_OP_CLEAR                       */ wined3d_cs_exec_clear,
2465     /* WINED3D_CS_OP_DISPATCH                    */ wined3d_cs_exec_dispatch,
2466     /* WINED3D_CS_OP_DRAW                        */ wined3d_cs_exec_draw,
2467     /* WINED3D_CS_OP_FLUSH                       */ wined3d_cs_exec_flush,
2468     /* WINED3D_CS_OP_SET_PREDICATION             */ wined3d_cs_exec_set_predication,
2469     /* WINED3D_CS_OP_SET_VIEWPORT                */ wined3d_cs_exec_set_viewport,
2470     /* WINED3D_CS_OP_SET_SCISSOR_RECT            */ wined3d_cs_exec_set_scissor_rect,
2471     /* WINED3D_CS_OP_SET_RENDERTARGET_VIEW       */ wined3d_cs_exec_set_rendertarget_view,
2472     /* WINED3D_CS_OP_SET_DEPTH_STENCIL_VIEW      */ wined3d_cs_exec_set_depth_stencil_view,
2473     /* WINED3D_CS_OP_SET_VERTEX_DECLARATION      */ wined3d_cs_exec_set_vertex_declaration,
2474     /* WINED3D_CS_OP_SET_STREAM_SOURCE           */ wined3d_cs_exec_set_stream_source,
2475     /* WINED3D_CS_OP_SET_STREAM_SOURCE_FREQ      */ wined3d_cs_exec_set_stream_source_freq,
2476     /* WINED3D_CS_OP_SET_STREAM_OUTPUT           */ wined3d_cs_exec_set_stream_output,
2477     /* WINED3D_CS_OP_SET_INDEX_BUFFER            */ wined3d_cs_exec_set_index_buffer,
2478     /* WINED3D_CS_OP_SET_CONSTANT_BUFFER         */ wined3d_cs_exec_set_constant_buffer,
2479     /* WINED3D_CS_OP_SET_TEXTURE                 */ wined3d_cs_exec_set_texture,
2480     /* WINED3D_CS_OP_SET_SHADER_RESOURCE_VIEW    */ wined3d_cs_exec_set_shader_resource_view,
2481     /* WINED3D_CS_OP_SET_UNORDERED_ACCESS_VIEW   */ wined3d_cs_exec_set_unordered_access_view,
2482     /* WINED3D_CS_OP_SET_SAMPLER                 */ wined3d_cs_exec_set_sampler,
2483     /* WINED3D_CS_OP_SET_SHADER                  */ wined3d_cs_exec_set_shader,
2484     /* WINED3D_CS_OP_SET_BLEND_STATE             */ wined3d_cs_exec_set_blend_state,
2485     /* WINED3D_CS_OP_SET_RASTERIZER_STATE        */ wined3d_cs_exec_set_rasterizer_state,
2486     /* WINED3D_CS_OP_SET_RENDER_STATE            */ wined3d_cs_exec_set_render_state,
2487     /* WINED3D_CS_OP_SET_TEXTURE_STATE           */ wined3d_cs_exec_set_texture_state,
2488     /* WINED3D_CS_OP_SET_SAMPLER_STATE           */ wined3d_cs_exec_set_sampler_state,
2489     /* WINED3D_CS_OP_SET_TRANSFORM               */ wined3d_cs_exec_set_transform,
2490     /* WINED3D_CS_OP_SET_CLIP_PLANE              */ wined3d_cs_exec_set_clip_plane,
2491     /* WINED3D_CS_OP_SET_COLOR_KEY               */ wined3d_cs_exec_set_color_key,
2492     /* WINED3D_CS_OP_SET_MATERIAL                */ wined3d_cs_exec_set_material,
2493     /* WINED3D_CS_OP_SET_LIGHT                   */ wined3d_cs_exec_set_light,
2494     /* WINED3D_CS_OP_SET_LIGHT_ENABLE            */ wined3d_cs_exec_set_light_enable,
2495     /* WINED3D_CS_OP_PUSH_CONSTANTS              */ wined3d_cs_exec_push_constants,
2496     /* WINED3D_CS_OP_RESET_STATE                 */ wined3d_cs_exec_reset_state,
2497     /* WINED3D_CS_OP_CALLBACK                    */ wined3d_cs_exec_callback,
2498     /* WINED3D_CS_OP_QUERY_ISSUE                 */ wined3d_cs_exec_query_issue,
2499     /* WINED3D_CS_OP_PRELOAD_RESOURCE            */ wined3d_cs_exec_preload_resource,
2500     /* WINED3D_CS_OP_UNLOAD_RESOURCE             */ wined3d_cs_exec_unload_resource,
2501     /* WINED3D_CS_OP_MAP                         */ wined3d_cs_exec_map,
2502     /* WINED3D_CS_OP_UNMAP                       */ wined3d_cs_exec_unmap,
2503     /* WINED3D_CS_OP_BLT_SUB_RESOURCE            */ wined3d_cs_exec_blt_sub_resource,
2504     /* WINED3D_CS_OP_UPDATE_SUB_RESOURCE         */ wined3d_cs_exec_update_sub_resource,
2505     /* WINED3D_CS_OP_ADD_DIRTY_TEXTURE_REGION    */ wined3d_cs_exec_add_dirty_texture_region,
2506     /* WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_clear_unordered_access_view,
2507     /* WINED3D_CS_OP_COPY_UAV_COUNTER            */ wined3d_cs_exec_copy_uav_counter,
2508     /* WINED3D_CS_OP_GENERATE_MIPMAPS            */ wined3d_cs_exec_generate_mipmaps,
2509 };
2510 
2511 #if defined(STAGING_CSMT)
wined3d_cs_st_check_space(struct wined3d_cs * cs,size_t size,enum wined3d_cs_queue_id queue_id)2512 static BOOL wined3d_cs_st_check_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2513 {
2514     return TRUE;
2515 }
2516 
2517 #endif /* STAGING_CSMT */
wined3d_cs_st_require_space(struct wined3d_cs * cs,size_t size,enum wined3d_cs_queue_id queue_id)2518 static void *wined3d_cs_st_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2519 {
2520     if (size > (cs->data_size - cs->end))
2521     {
2522         size_t new_size;
2523         void *new_data;
2524 
2525         new_size = max(size, cs->data_size * 2);
2526         if (!cs->end)
2527             new_data = heap_realloc(cs->data, new_size);
2528         else
2529             new_data = heap_alloc(new_size);
2530         if (!new_data)
2531             return NULL;
2532 
2533         cs->data_size = new_size;
2534         cs->start = cs->end = 0;
2535         cs->data = new_data;
2536     }
2537 
2538     cs->end += size;
2539 
2540     return (BYTE *)cs->data + cs->start;
2541 }
2542 
wined3d_cs_st_submit(struct wined3d_cs * cs,enum wined3d_cs_queue_id queue_id)2543 static void wined3d_cs_st_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2544 {
2545     enum wined3d_cs_op opcode;
2546     size_t start;
2547     BYTE *data;
2548 
2549     data = cs->data;
2550     start = cs->start;
2551     cs->start = cs->end;
2552 
2553     opcode = *(const enum wined3d_cs_op *)&data[start];
2554     if (opcode >= WINED3D_CS_OP_STOP)
2555         ERR("Invalid opcode %#x.\n", opcode);
2556     else
2557         wined3d_cs_op_handlers[opcode](cs, &data[start]);
2558 
2559     if (cs->data == data)
2560         cs->start = cs->end = start;
2561     else if (!start)
2562         heap_free(data);
2563 }
2564 
wined3d_cs_st_finish(struct wined3d_cs * cs,enum wined3d_cs_queue_id queue_id)2565 static void wined3d_cs_st_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2566 {
2567 }
2568 
2569 static const struct wined3d_cs_ops wined3d_cs_st_ops =
2570 {
2571 #if defined(STAGING_CSMT)
2572     wined3d_cs_st_check_space,
2573 #endif /* STAGING_CSMT */
2574     wined3d_cs_st_require_space,
2575     wined3d_cs_st_submit,
2576     wined3d_cs_st_finish,
2577     wined3d_cs_st_push_constants,
2578 };
2579 
wined3d_cs_queue_is_empty(const struct wined3d_cs * cs,const struct wined3d_cs_queue * queue)2580 static BOOL wined3d_cs_queue_is_empty(const struct wined3d_cs *cs, const struct wined3d_cs_queue *queue)
2581 {
2582     wined3d_from_cs(cs);
2583     return *(volatile LONG *)&queue->head == queue->tail;
2584 }
2585 
wined3d_cs_queue_submit(struct wined3d_cs_queue * queue,struct wined3d_cs * cs)2586 static void wined3d_cs_queue_submit(struct wined3d_cs_queue *queue, struct wined3d_cs *cs)
2587 {
2588     struct wined3d_cs_packet *packet;
2589     size_t packet_size;
2590 
2591     packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2592     packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2593     InterlockedExchange(&queue->head, (queue->head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1));
2594 
2595     if (InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2596         SetEvent(cs->event);
2597 }
2598 
wined3d_cs_mt_submit(struct wined3d_cs * cs,enum wined3d_cs_queue_id queue_id)2599 static void wined3d_cs_mt_submit(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2600 {
2601     if (cs->thread_id == GetCurrentThreadId())
2602     {
2603         wined3d_cs_st_submit(cs, queue_id);
2604         return;
2605     }
2606 
2607     wined3d_cs_queue_submit(&cs->queue[queue_id], cs);
2608 }
2609 
2610 #if defined(STAGING_CSMT)
wined3d_cs_queue_check_space(struct wined3d_cs_queue * queue,size_t size)2611 static BOOL wined3d_cs_queue_check_space(struct wined3d_cs_queue *queue, size_t size)
2612 {
2613     size_t queue_size = ARRAY_SIZE(queue->data);
2614     size_t header_size, packet_size, remaining;
2615 
2616     header_size = FIELD_OFFSET(struct wined3d_cs_packet, data[0]);
2617     size = (size + header_size - 1) & ~(header_size - 1);
2618     packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[size]);
2619 
2620     remaining = queue_size - queue->head;
2621     return (remaining >= packet_size);
2622 }
2623 
2624 #endif /* STAGING_CSMT */
wined3d_cs_queue_require_space(struct wined3d_cs_queue * queue,size_t size,struct wined3d_cs * cs)2625 static void *wined3d_cs_queue_require_space(struct wined3d_cs_queue *queue, size_t size, struct wined3d_cs *cs)
2626 {
2627     size_t queue_size = ARRAY_SIZE(queue->data);
2628     size_t header_size, packet_size, remaining;
2629     struct wined3d_cs_packet *packet;
2630 
2631     header_size = FIELD_OFFSET(struct wined3d_cs_packet, data[0]);
2632     size = (size + header_size - 1) & ~(header_size - 1);
2633     packet_size = FIELD_OFFSET(struct wined3d_cs_packet, data[size]);
2634     if (packet_size >= WINED3D_CS_QUEUE_SIZE)
2635     {
2636         ERR("Packet size %lu >= queue size %u.\n",
2637                 (unsigned long)packet_size, WINED3D_CS_QUEUE_SIZE);
2638         return NULL;
2639     }
2640 
2641     remaining = queue_size - queue->head;
2642     if (remaining < packet_size)
2643     {
2644         size_t nop_size = remaining - header_size;
2645         struct wined3d_cs_nop *nop;
2646 
2647         TRACE("Inserting a nop for %lu + %lu bytes.\n",
2648                 (unsigned long)header_size, (unsigned long)nop_size);
2649 
2650         nop = wined3d_cs_queue_require_space(queue, nop_size, cs);
2651         if (nop_size)
2652             nop->opcode = WINED3D_CS_OP_NOP;
2653 
2654         wined3d_cs_queue_submit(queue, cs);
2655         assert(!queue->head);
2656     }
2657 
2658     for (;;)
2659     {
2660         LONG tail = *(volatile LONG *)&queue->tail;
2661         LONG head = queue->head;
2662         LONG new_pos;
2663 
2664         /* Empty. */
2665         if (head == tail)
2666             break;
2667         new_pos = (head + packet_size) & (WINED3D_CS_QUEUE_SIZE - 1);
2668         /* Head ahead of tail. We checked the remaining size above, so we only
2669          * need to make sure we don't make head equal to tail. */
2670         if (head > tail && (new_pos != tail))
2671             break;
2672         /* Tail ahead of head. Make sure the new head is before the tail as
2673          * well. Note that new_pos is 0 when it's at the end of the queue. */
2674         if (new_pos < tail && new_pos)
2675             break;
2676 
2677         TRACE("Waiting for free space. Head %u, tail %u, packet size %lu.\n",
2678                 head, tail, (unsigned long)packet_size);
2679     }
2680 
2681     packet = (struct wined3d_cs_packet *)&queue->data[queue->head];
2682     packet->size = size;
2683     return packet->data;
2684 }
2685 
2686 #if defined(STAGING_CSMT)
wined3d_cs_mt_check_space(struct wined3d_cs * cs,size_t size,enum wined3d_cs_queue_id queue_id)2687 static BOOL wined3d_cs_mt_check_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2688 {
2689     if (cs->thread_id == GetCurrentThreadId())
2690         return wined3d_cs_st_check_space(cs, size, queue_id);
2691 
2692     return wined3d_cs_queue_check_space(&cs->queue[queue_id], size);
2693 }
2694 
2695 #endif /* STAGING_CSMT */
wined3d_cs_mt_require_space(struct wined3d_cs * cs,size_t size,enum wined3d_cs_queue_id queue_id)2696 static void *wined3d_cs_mt_require_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
2697 {
2698     if (cs->thread_id == GetCurrentThreadId())
2699         return wined3d_cs_st_require_space(cs, size, queue_id);
2700 
2701     return wined3d_cs_queue_require_space(&cs->queue[queue_id], size, cs);
2702 }
2703 
wined3d_cs_mt_finish(struct wined3d_cs * cs,enum wined3d_cs_queue_id queue_id)2704 static void wined3d_cs_mt_finish(struct wined3d_cs *cs, enum wined3d_cs_queue_id queue_id)
2705 {
2706     if (cs->thread_id == GetCurrentThreadId())
2707     {
2708         wined3d_cs_st_finish(cs, queue_id);
2709         return;
2710     }
2711 
2712     while (cs->queue[queue_id].head != *(volatile LONG *)&cs->queue[queue_id].tail)
2713         wined3d_pause();
2714 }
2715 
2716 static const struct wined3d_cs_ops wined3d_cs_mt_ops =
2717 {
2718 #if defined(STAGING_CSMT)
2719     wined3d_cs_mt_check_space,
2720 #endif /* STAGING_CSMT */
2721     wined3d_cs_mt_require_space,
2722     wined3d_cs_mt_submit,
2723     wined3d_cs_mt_finish,
2724     wined3d_cs_mt_push_constants,
2725 };
2726 
poll_queries(struct wined3d_cs * cs)2727 static void poll_queries(struct wined3d_cs *cs)
2728 {
2729     struct wined3d_query *query, *cursor;
2730 
2731     LIST_FOR_EACH_ENTRY_SAFE(query, cursor, &cs->query_poll_list, struct wined3d_query, poll_list_entry)
2732     {
2733         if (!query->query_ops->query_poll(query, 0))
2734             continue;
2735 
2736         list_remove(&query->poll_list_entry);
2737         list_init(&query->poll_list_entry);
2738         InterlockedIncrement(&query->counter_retrieved);
2739     }
2740 }
2741 
wined3d_cs_wait_event(struct wined3d_cs * cs)2742 static void wined3d_cs_wait_event(struct wined3d_cs *cs)
2743 {
2744     InterlockedExchange(&cs->waiting_for_event, TRUE);
2745 
2746     /* The main thread might have enqueued a command and blocked on it after
2747      * the CS thread decided to enter wined3d_cs_wait_event(), but before
2748      * "waiting_for_event" was set.
2749      *
2750      * Likewise, we can race with the main thread when resetting
2751      * "waiting_for_event", in which case we would need to call
2752      * WaitForSingleObject() because the main thread called SetEvent(). */
2753     if (!(wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_DEFAULT])
2754             && wined3d_cs_queue_is_empty(cs, &cs->queue[WINED3D_CS_QUEUE_MAP]))
2755             && InterlockedCompareExchange(&cs->waiting_for_event, FALSE, TRUE))
2756         return;
2757 
2758     WaitForSingleObject(cs->event, INFINITE);
2759 }
2760 
wined3d_cs_run(void * ctx)2761 static DWORD WINAPI wined3d_cs_run(void *ctx)
2762 {
2763     struct wined3d_cs_packet *packet;
2764     struct wined3d_cs_queue *queue;
2765     unsigned int spin_count = 0;
2766     struct wined3d_cs *cs = ctx;
2767     enum wined3d_cs_op opcode;
2768     HMODULE wined3d_module;
2769     unsigned int poll = 0;
2770     LONG tail;
2771 
2772     TRACE("Started.\n");
2773 
2774     /* Copy the module handle to a local variable to avoid racing with the
2775      * thread freeing "cs" before the FreeLibraryAndExitThread() call. */
2776     wined3d_module = cs->wined3d_module;
2777 
2778     list_init(&cs->query_poll_list);
2779     cs->thread_id = GetCurrentThreadId();
2780     for (;;)
2781     {
2782         if (++poll == WINED3D_CS_QUERY_POLL_INTERVAL)
2783         {
2784             poll_queries(cs);
2785             poll = 0;
2786         }
2787 
2788         queue = &cs->queue[WINED3D_CS_QUEUE_MAP];
2789         if (wined3d_cs_queue_is_empty(cs, queue))
2790         {
2791             queue = &cs->queue[WINED3D_CS_QUEUE_DEFAULT];
2792             if (wined3d_cs_queue_is_empty(cs, queue))
2793             {
2794                 if (++spin_count >= WINED3D_CS_SPIN_COUNT && list_empty(&cs->query_poll_list))
2795                     wined3d_cs_wait_event(cs);
2796                 continue;
2797             }
2798         }
2799         spin_count = 0;
2800 
2801         tail = queue->tail;
2802         packet = (struct wined3d_cs_packet *)&queue->data[tail];
2803         if (packet->size)
2804         {
2805             opcode = *(const enum wined3d_cs_op *)packet->data;
2806 
2807             if (opcode >= WINED3D_CS_OP_STOP)
2808             {
2809                 if (opcode > WINED3D_CS_OP_STOP)
2810                     ERR("Invalid opcode %#x.\n", opcode);
2811                 break;
2812             }
2813 
2814             wined3d_cs_op_handlers[opcode](cs, packet->data);
2815         }
2816 
2817         tail += FIELD_OFFSET(struct wined3d_cs_packet, data[packet->size]);
2818         tail &= (WINED3D_CS_QUEUE_SIZE - 1);
2819         InterlockedExchange(&queue->tail, tail);
2820     }
2821 
2822     cs->queue[WINED3D_CS_QUEUE_MAP].tail = cs->queue[WINED3D_CS_QUEUE_MAP].head;
2823     cs->queue[WINED3D_CS_QUEUE_DEFAULT].tail = cs->queue[WINED3D_CS_QUEUE_DEFAULT].head;
2824     TRACE("Stopped.\n");
2825     FreeLibraryAndExitThread(wined3d_module, 0);
2826 }
2827 
wined3d_cs_create(struct wined3d_device * device)2828 struct wined3d_cs *wined3d_cs_create(struct wined3d_device *device)
2829 {
2830     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2831     struct wined3d_cs *cs;
2832 
2833     if (!(cs = heap_alloc_zero(sizeof(*cs))))
2834         return NULL;
2835 
2836     cs->ops = &wined3d_cs_st_ops;
2837     cs->device = device;
2838 
2839     state_init(&cs->state, &cs->fb, gl_info, &device->adapter->d3d_info,
2840             WINED3D_STATE_NO_REF | WINED3D_STATE_INIT_DEFAULT);
2841 
2842     cs->data_size = WINED3D_INITIAL_CS_SIZE;
2843     if (!(cs->data = heap_alloc(cs->data_size)))
2844         goto fail;
2845 
2846     if (wined3d_settings.cs_multithreaded
2847             && !RtlIsCriticalSectionLockedByThread(NtCurrentTeb()->Peb->LoaderLock))
2848     {
2849         cs->ops = &wined3d_cs_mt_ops;
2850 
2851         if (!(cs->event = CreateEventW(NULL, FALSE, FALSE, NULL)))
2852         {
2853             ERR("Failed to create command stream event.\n");
2854             heap_free(cs->data);
2855             goto fail;
2856         }
2857 
2858         if (!(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2859                 (const WCHAR *)wined3d_cs_run, &cs->wined3d_module)))
2860         {
2861             ERR("Failed to get wined3d module handle.\n");
2862             CloseHandle(cs->event);
2863             heap_free(cs->data);
2864             goto fail;
2865         }
2866 
2867         if (!(cs->thread = CreateThread(NULL, 0, wined3d_cs_run, cs, 0, NULL)))
2868         {
2869             ERR("Failed to create wined3d command stream thread.\n");
2870             FreeLibrary(cs->wined3d_module);
2871             CloseHandle(cs->event);
2872             heap_free(cs->data);
2873             goto fail;
2874         }
2875     }
2876 
2877     return cs;
2878 
2879 fail:
2880     state_cleanup(&cs->state);
2881     heap_free(cs);
2882     return NULL;
2883 }
2884 
wined3d_cs_destroy(struct wined3d_cs * cs)2885 void wined3d_cs_destroy(struct wined3d_cs *cs)
2886 {
2887     if (cs->thread)
2888     {
2889         wined3d_cs_emit_stop(cs);
2890         CloseHandle(cs->thread);
2891         if (!CloseHandle(cs->event))
2892             ERR("Closing event failed.\n");
2893     }
2894 
2895     state_cleanup(&cs->state);
2896     heap_free(cs->data);
2897     heap_free(cs);
2898 }
2899