c9901209189be662b5b0aa5de2fc29b8726953bf
[mesa.git] / src / gallium / state_trackers / nine / nine_state.c
1 /*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 * Copyright 2013 Christoph Bumiller
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24 #define NINE_STATE
25
26 #include "device9.h"
27 #include "swapchain9.h"
28 #include "basetexture9.h"
29 #include "buffer9.h"
30 #include "indexbuffer9.h"
31 #include "surface9.h"
32 #include "vertexbuffer9.h"
33 #include "vertexdeclaration9.h"
34 #include "vertexshader9.h"
35 #include "pixelshader9.h"
36 #include "nine_pipe.h"
37 #include "nine_ff.h"
38 #include "nine_limits.h"
39 #include "pipe/p_context.h"
40 #include "pipe/p_state.h"
41 #include "cso_cache/cso_context.h"
42 #include "util/u_atomic.h"
43 #include "util/u_upload_mgr.h"
44 #include "util/u_math.h"
45 #include "util/u_box.h"
46 #include "util/u_simple_shaders.h"
47 #include "util/u_gen_mipmap.h"
48
49 /* CSMT headers */
50 #include "nine_queue.h"
51 #include "nine_csmt_helper.h"
52 #include "os/os_thread.h"
53
54 #define DBG_CHANNEL DBG_DEVICE
55
56 /* Nine CSMT */
57
58 struct csmt_instruction {
59 int (* func)(struct NineDevice9 *This, struct csmt_instruction *instr);
60 };
61
62 struct csmt_context {
63 thrd_t worker;
64 struct nine_queue_pool* pool;
65 BOOL terminate;
66 cnd_t event_processed;
67 mtx_t mutex_processed;
68 struct NineDevice9 *device;
69 BOOL processed;
70 BOOL toPause;
71 BOOL hasPaused;
72 mtx_t thread_running;
73 mtx_t thread_resume;
74 };
75
76 /* Wait for instruction to be processed.
77 * Caller has to ensure that only one thread waits at time.
78 */
79 static void
80 nine_csmt_wait_processed(struct csmt_context *ctx)
81 {
82 mtx_lock(&ctx->mutex_processed);
83 while (!p_atomic_read(&ctx->processed)) {
84 cnd_wait(&ctx->event_processed, &ctx->mutex_processed);
85 }
86 mtx_unlock(&ctx->mutex_processed);
87 }
88
89 /* CSMT worker thread */
90 static
91 int
92 nine_csmt_worker(void *arg)
93 {
94 struct csmt_context *ctx = arg;
95 struct csmt_instruction *instr;
96 DBG("CSMT worker spawned\n");
97
98 u_thread_setname("CSMT-Worker");
99
100 while (1) {
101 nine_queue_wait_flush(ctx->pool);
102 mtx_lock(&ctx->thread_running);
103
104 /* Get instruction. NULL on empty cmdbuf. */
105 while (!p_atomic_read(&ctx->terminate) &&
106 (instr = (struct csmt_instruction *)nine_queue_get(ctx->pool))) {
107
108 /* decode */
109 if (instr->func(ctx->device, instr)) {
110 mtx_lock(&ctx->mutex_processed);
111 p_atomic_set(&ctx->processed, TRUE);
112 cnd_signal(&ctx->event_processed);
113 mtx_unlock(&ctx->mutex_processed);
114 }
115 if (p_atomic_read(&ctx->toPause)) {
116 mtx_unlock(&ctx->thread_running);
117 /* will wait here the thread can be resumed */
118 mtx_lock(&ctx->thread_resume);
119 mtx_lock(&ctx->thread_running);
120 mtx_unlock(&ctx->thread_resume);
121 }
122 }
123
124 mtx_unlock(&ctx->thread_running);
125 if (p_atomic_read(&ctx->terminate)) {
126 mtx_lock(&ctx->mutex_processed);
127 p_atomic_set(&ctx->processed, TRUE);
128 cnd_signal(&ctx->event_processed);
129 mtx_unlock(&ctx->mutex_processed);
130 break;
131 }
132 }
133
134 DBG("CSMT worker destroyed\n");
135 return 0;
136 }
137
138 /* Create a CSMT context.
139 * Spawns a worker thread.
140 */
141 struct csmt_context *
142 nine_csmt_create( struct NineDevice9 *This )
143 {
144 struct csmt_context *ctx;
145
146 ctx = CALLOC_STRUCT(csmt_context);
147 if (!ctx)
148 return NULL;
149
150 ctx->pool = nine_queue_create();
151 if (!ctx->pool) {
152 FREE(ctx);
153 return NULL;
154 }
155 cnd_init(&ctx->event_processed);
156 (void) mtx_init(&ctx->mutex_processed, mtx_plain);
157 (void) mtx_init(&ctx->thread_running, mtx_plain);
158 (void) mtx_init(&ctx->thread_resume, mtx_plain);
159
160 #if DEBUG
161 u_thread_setname("Main thread");
162 #endif
163
164 ctx->device = This;
165
166 ctx->worker = u_thread_create(nine_csmt_worker, ctx);
167 if (!ctx->worker) {
168 nine_queue_delete(ctx->pool);
169 FREE(ctx);
170 return NULL;
171 }
172
173 DBG("Returning context %p\n", ctx);
174
175 return ctx;
176 }
177
178 static int
179 nop_func( struct NineDevice9 *This, struct csmt_instruction *instr )
180 {
181 (void) This;
182 (void) instr;
183
184 return 1;
185 }
186
187 /* Push nop instruction and flush the queue.
188 * Waits for the worker to complete. */
189 void
190 nine_csmt_process( struct NineDevice9 *device )
191 {
192 struct csmt_instruction* instr;
193 struct csmt_context *ctx = device->csmt_ctx;
194
195 if (!device->csmt_active)
196 return;
197
198 if (nine_queue_isempty(ctx->pool))
199 return;
200
201 DBG("device=%p\n", device);
202
203 /* NOP */
204 instr = nine_queue_alloc(ctx->pool, sizeof(struct csmt_instruction));
205 assert(instr);
206 instr->func = nop_func;
207
208 p_atomic_set(&ctx->processed, FALSE);
209 nine_queue_flush(ctx->pool);
210
211 nine_csmt_wait_processed(ctx);
212 }
213
214 /* Destroys a CSMT context.
215 * Waits for the worker thread to terminate.
216 */
217 void
218 nine_csmt_destroy( struct NineDevice9 *device, struct csmt_context *ctx )
219 {
220 struct csmt_instruction* instr;
221 thrd_t render_thread = ctx->worker;
222
223 DBG("device=%p ctx=%p\n", device, ctx);
224
225 /* Push nop and flush the queue. */
226 instr = nine_queue_alloc(ctx->pool, sizeof(struct csmt_instruction));
227 assert(instr);
228 instr->func = nop_func;
229
230 p_atomic_set(&ctx->processed, FALSE);
231 /* Signal worker to terminate. */
232 p_atomic_set(&ctx->terminate, TRUE);
233 nine_queue_flush(ctx->pool);
234
235 nine_csmt_wait_processed(ctx);
236 nine_queue_delete(ctx->pool);
237 mtx_destroy(&ctx->mutex_processed);
238
239 FREE(ctx);
240
241 thrd_join(render_thread, NULL);
242 }
243
244 static void
245 nine_csmt_pause( struct NineDevice9 *device )
246 {
247 struct csmt_context *ctx = device->csmt_ctx;
248
249 if (!device->csmt_active)
250 return;
251
252 /* No need to pause the thread */
253 if (nine_queue_no_flushed_work(ctx->pool))
254 return;
255
256 mtx_lock(&ctx->thread_resume);
257 p_atomic_set(&ctx->toPause, TRUE);
258
259 /* Wait the thread is paused */
260 mtx_lock(&ctx->thread_running);
261 ctx->hasPaused = TRUE;
262 p_atomic_set(&ctx->toPause, FALSE);
263 }
264
265 static void
266 nine_csmt_resume( struct NineDevice9 *device )
267 {
268 struct csmt_context *ctx = device->csmt_ctx;
269
270 if (!device->csmt_active)
271 return;
272
273 if (!ctx->hasPaused)
274 return;
275
276 ctx->hasPaused = FALSE;
277 mtx_unlock(&ctx->thread_running);
278 mtx_unlock(&ctx->thread_resume);
279 }
280
281 struct pipe_context *
282 nine_context_get_pipe( struct NineDevice9 *device )
283 {
284 nine_csmt_process(device);
285 return device->context.pipe;
286 }
287
288 struct pipe_context *
289 nine_context_get_pipe_multithread( struct NineDevice9 *device )
290 {
291 struct csmt_context *ctx = device->csmt_ctx;
292
293 if (!device->csmt_active)
294 return device->context.pipe;
295
296 if (!u_thread_is_self(ctx->worker))
297 nine_csmt_process(device);
298
299 return device->context.pipe;
300 }
301
302 struct pipe_context *
303 nine_context_get_pipe_acquire( struct NineDevice9 *device )
304 {
305 nine_csmt_pause(device);
306 return device->context.pipe;
307 }
308
309 void
310 nine_context_get_pipe_release( struct NineDevice9 *device )
311 {
312 nine_csmt_resume(device);
313 }
314
315 /* Nine state functions */
316
317 /* Check if some states need to be set dirty */
318
319 static inline DWORD
320 check_multisample(struct NineDevice9 *device)
321 {
322 DWORD *rs = device->context.rs;
323 DWORD new_value = (rs[D3DRS_ZENABLE] || rs[D3DRS_STENCILENABLE]) &&
324 device->context.rt[0]->desc.MultiSampleType >= 1 &&
325 rs[D3DRS_MULTISAMPLEANTIALIAS];
326 if (rs[NINED3DRS_MULTISAMPLE] != new_value) {
327 rs[NINED3DRS_MULTISAMPLE] = new_value;
328 return NINE_STATE_RASTERIZER;
329 }
330 return 0;
331 }
332
333 /* State preparation only */
334
335 static inline void
336 prepare_blend(struct NineDevice9 *device)
337 {
338 nine_convert_blend_state(&device->context.pipe_data.blend, device->context.rs);
339 device->context.commit |= NINE_STATE_COMMIT_BLEND;
340 }
341
342 static inline void
343 prepare_dsa(struct NineDevice9 *device)
344 {
345 nine_convert_dsa_state(&device->context.pipe_data.dsa, device->context.rs);
346 device->context.commit |= NINE_STATE_COMMIT_DSA;
347 }
348
349 static inline void
350 prepare_rasterizer(struct NineDevice9 *device)
351 {
352 nine_convert_rasterizer_state(device, &device->context.pipe_data.rast, device->context.rs);
353 device->context.commit |= NINE_STATE_COMMIT_RASTERIZER;
354 }
355
356 static void
357 prepare_vs_constants_userbuf_swvp(struct NineDevice9 *device)
358 {
359 struct nine_context *context = &device->context;
360
361 if (context->changed.vs_const_f || context->changed.group & NINE_STATE_SWVP) {
362 struct pipe_constant_buffer cb;
363
364 cb.buffer_offset = 0;
365 cb.buffer_size = 4096 * sizeof(float[4]);
366 cb.user_buffer = context->vs_const_f_swvp;
367
368 if (context->vs->lconstf.ranges) {
369 const struct nine_lconstf *lconstf = &(context->vs->lconstf);
370 const struct nine_range *r = lconstf->ranges;
371 unsigned n = 0;
372 float *dst = context->vs_lconstf_temp;
373 float *src = (float *)cb.user_buffer;
374 memcpy(dst, src, cb.buffer_size);
375 while (r) {
376 unsigned p = r->bgn;
377 unsigned c = r->end - r->bgn;
378 memcpy(&dst[p * 4], &lconstf->data[n * 4], c * 4 * sizeof(float));
379 n += c;
380 r = r->next;
381 }
382 cb.user_buffer = dst;
383 }
384
385 context->pipe_data.cb0_swvp.buffer_offset = cb.buffer_offset;
386 context->pipe_data.cb0_swvp.buffer_size = cb.buffer_size;
387 context->pipe_data.cb0_swvp.user_buffer = cb.user_buffer;
388
389 cb.user_buffer = (char *)cb.user_buffer + 4096 * sizeof(float[4]);
390 context->pipe_data.cb1_swvp.buffer_offset = cb.buffer_offset;
391 context->pipe_data.cb1_swvp.buffer_size = cb.buffer_size;
392 context->pipe_data.cb1_swvp.user_buffer = cb.user_buffer;
393
394 context->changed.vs_const_f = 0;
395 }
396
397 if (context->changed.vs_const_i || context->changed.group & NINE_STATE_SWVP) {
398 struct pipe_constant_buffer cb;
399
400 cb.buffer_offset = 0;
401 cb.buffer_size = 2048 * sizeof(float[4]);
402 cb.user_buffer = context->vs_const_i;
403
404 context->pipe_data.cb2_swvp.buffer_offset = cb.buffer_offset;
405 context->pipe_data.cb2_swvp.buffer_size = cb.buffer_size;
406 context->pipe_data.cb2_swvp.user_buffer = cb.user_buffer;
407 context->changed.vs_const_i = 0;
408 }
409
410 if (context->changed.vs_const_b || context->changed.group & NINE_STATE_SWVP) {
411 struct pipe_constant_buffer cb;
412
413 cb.buffer_offset = 0;
414 cb.buffer_size = 512 * sizeof(float[4]);
415 cb.user_buffer = context->vs_const_b;
416
417 context->pipe_data.cb3_swvp.buffer_offset = cb.buffer_offset;
418 context->pipe_data.cb3_swvp.buffer_size = cb.buffer_size;
419 context->pipe_data.cb3_swvp.user_buffer = cb.user_buffer;
420 context->changed.vs_const_b = 0;
421 }
422
423 context->changed.group &= ~NINE_STATE_VS_CONST;
424 context->commit |= NINE_STATE_COMMIT_CONST_VS;
425 }
426
427 static void
428 prepare_vs_constants_userbuf(struct NineDevice9 *device)
429 {
430 struct nine_context *context = &device->context;
431 struct pipe_constant_buffer cb;
432 cb.buffer = NULL;
433 cb.buffer_offset = 0;
434 cb.buffer_size = context->vs->const_used_size;
435 cb.user_buffer = context->vs_const_f;
436
437 if (context->swvp) {
438 prepare_vs_constants_userbuf_swvp(device);
439 return;
440 }
441
442 if (context->changed.vs_const_i || context->changed.group & NINE_STATE_SWVP) {
443 int *idst = (int *)&context->vs_const_f[4 * device->max_vs_const_f];
444 memcpy(idst, context->vs_const_i, NINE_MAX_CONST_I * sizeof(int[4]));
445 context->changed.vs_const_i = 0;
446 }
447
448 if (context->changed.vs_const_b || context->changed.group & NINE_STATE_SWVP) {
449 int *idst = (int *)&context->vs_const_f[4 * device->max_vs_const_f];
450 uint32_t *bdst = (uint32_t *)&idst[4 * NINE_MAX_CONST_I];
451 memcpy(bdst, context->vs_const_b, NINE_MAX_CONST_B * sizeof(BOOL));
452 context->changed.vs_const_b = 0;
453 }
454
455 if (!cb.buffer_size)
456 return;
457
458 if (context->vs->lconstf.ranges) {
459 /* TODO: Can we make it so that we don't have to copy everything ? */
460 const struct nine_lconstf *lconstf = &(context->vs->lconstf);
461 const struct nine_range *r = lconstf->ranges;
462 unsigned n = 0;
463 float *dst = context->vs_lconstf_temp;
464 float *src = (float *)cb.user_buffer;
465 memcpy(dst, src, cb.buffer_size);
466 while (r) {
467 unsigned p = r->bgn;
468 unsigned c = r->end - r->bgn;
469 memcpy(&dst[p * 4], &lconstf->data[n * 4], c * 4 * sizeof(float));
470 n += c;
471 r = r->next;
472 }
473 cb.user_buffer = dst;
474 }
475
476 context->pipe_data.cb_vs = cb;
477 context->changed.vs_const_f = 0;
478
479 context->changed.group &= ~NINE_STATE_VS_CONST;
480 context->commit |= NINE_STATE_COMMIT_CONST_VS;
481 }
482
483 static void
484 prepare_ps_constants_userbuf(struct NineDevice9 *device)
485 {
486 struct nine_context *context = &device->context;
487 struct pipe_constant_buffer cb;
488 cb.buffer = NULL;
489 cb.buffer_offset = 0;
490 cb.buffer_size = context->ps->const_used_size;
491 cb.user_buffer = context->ps_const_f;
492
493 if (context->changed.ps_const_i) {
494 int *idst = (int *)&context->ps_const_f[4 * device->max_ps_const_f];
495 memcpy(idst, context->ps_const_i, sizeof(context->ps_const_i));
496 context->changed.ps_const_i = 0;
497 }
498 if (context->changed.ps_const_b) {
499 int *idst = (int *)&context->ps_const_f[4 * device->max_ps_const_f];
500 uint32_t *bdst = (uint32_t *)&idst[4 * NINE_MAX_CONST_I];
501 memcpy(bdst, context->ps_const_b, sizeof(context->ps_const_b));
502 context->changed.ps_const_b = 0;
503 }
504
505 /* Upload special constants needed to implement PS1.x instructions like TEXBEM,TEXBEML and BEM */
506 if (context->ps->bumpenvmat_needed) {
507 memcpy(context->ps_lconstf_temp, cb.user_buffer, cb.buffer_size);
508 memcpy(&context->ps_lconstf_temp[4 * 8], &device->context.bumpmap_vars, sizeof(device->context.bumpmap_vars));
509
510 cb.user_buffer = context->ps_lconstf_temp;
511 }
512
513 if (context->ps->byte_code.version < 0x30 &&
514 context->rs[D3DRS_FOGENABLE]) {
515 float *dst = &context->ps_lconstf_temp[4 * 32];
516 if (cb.user_buffer != context->ps_lconstf_temp) {
517 memcpy(context->ps_lconstf_temp, cb.user_buffer, cb.buffer_size);
518 cb.user_buffer = context->ps_lconstf_temp;
519 }
520
521 d3dcolor_to_rgba(dst, context->rs[D3DRS_FOGCOLOR]);
522 if (context->rs[D3DRS_FOGTABLEMODE] == D3DFOG_LINEAR) {
523 dst[4] = asfloat(context->rs[D3DRS_FOGEND]);
524 dst[5] = 1.0f / (asfloat(context->rs[D3DRS_FOGEND]) - asfloat(context->rs[D3DRS_FOGSTART]));
525 } else if (context->rs[D3DRS_FOGTABLEMODE] != D3DFOG_NONE) {
526 dst[4] = asfloat(context->rs[D3DRS_FOGDENSITY]);
527 }
528 cb.buffer_size = 4 * 4 * 34;
529 }
530
531 if (!cb.buffer_size)
532 return;
533
534 context->pipe_data.cb_ps = cb;
535 context->changed.ps_const_f = 0;
536
537 context->changed.group &= ~NINE_STATE_PS_CONST;
538 context->commit |= NINE_STATE_COMMIT_CONST_PS;
539 }
540
541 static inline uint32_t
542 prepare_vs(struct NineDevice9 *device, uint8_t shader_changed)
543 {
544 struct nine_context *context = &device->context;
545 struct NineVertexShader9 *vs = context->vs;
546 uint32_t changed_group = 0;
547 int has_key_changed = 0;
548
549 if (likely(context->programmable_vs))
550 has_key_changed = NineVertexShader9_UpdateKey(vs, device);
551
552 if (!shader_changed && !has_key_changed)
553 return 0;
554
555 /* likely because we dislike FF */
556 if (likely(context->programmable_vs)) {
557 context->cso_shader.vs = NineVertexShader9_GetVariant(vs);
558 } else {
559 vs = device->ff.vs;
560 context->cso_shader.vs = vs->ff_cso;
561 }
562
563 if (context->rs[NINED3DRS_VSPOINTSIZE] != vs->point_size) {
564 context->rs[NINED3DRS_VSPOINTSIZE] = vs->point_size;
565 changed_group |= NINE_STATE_RASTERIZER;
566 }
567
568 if ((context->bound_samplers_mask_vs & vs->sampler_mask) != vs->sampler_mask)
569 /* Bound dummy sampler. */
570 changed_group |= NINE_STATE_SAMPLER;
571
572 context->commit |= NINE_STATE_COMMIT_VS;
573 return changed_group;
574 }
575
576 static inline uint32_t
577 prepare_ps(struct NineDevice9 *device, uint8_t shader_changed)
578 {
579 struct nine_context *context = &device->context;
580 struct NinePixelShader9 *ps = context->ps;
581 uint32_t changed_group = 0;
582 int has_key_changed = 0;
583
584 if (likely(ps))
585 has_key_changed = NinePixelShader9_UpdateKey(ps, context);
586
587 if (!shader_changed && !has_key_changed)
588 return 0;
589
590 if (likely(ps)) {
591 context->cso_shader.ps = NinePixelShader9_GetVariant(ps);
592 } else {
593 ps = device->ff.ps;
594 context->cso_shader.ps = ps->ff_cso;
595 }
596
597 if ((context->bound_samplers_mask_ps & ps->sampler_mask) != ps->sampler_mask)
598 /* Bound dummy sampler. */
599 changed_group |= NINE_STATE_SAMPLER;
600
601 context->commit |= NINE_STATE_COMMIT_PS;
602 return changed_group;
603 }
604
605 /* State preparation incremental */
606
607 /* State preparation + State commit */
608
609 static void
610 update_framebuffer(struct NineDevice9 *device, bool is_clear)
611 {
612 struct nine_context *context = &device->context;
613 struct pipe_context *pipe = context->pipe;
614 struct pipe_framebuffer_state *fb = &context->pipe_data.fb;
615 unsigned i;
616 struct NineSurface9 *rt0 = context->rt[0];
617 unsigned w = rt0->desc.Width;
618 unsigned h = rt0->desc.Height;
619 unsigned nr_samples = rt0->base.info.nr_samples;
620 unsigned ps_mask = context->ps ? context->ps->rt_mask : 1;
621 unsigned mask = is_clear ? 0xf : ps_mask;
622 const int sRGB = context->rs[D3DRS_SRGBWRITEENABLE] ? 1 : 0;
623
624 DBG("\n");
625
626 context->rt_mask = 0x0;
627 fb->nr_cbufs = 0;
628
629 /* all render targets must have the same size and the depth buffer must be
630 * bigger. Multisample has to match, according to spec. But some apps do
631 * things wrong there, and no error is returned. The behaviour they get
632 * apparently is that depth buffer is disabled if it doesn't match.
633 * Surely the same for render targets. */
634
635 /* Special case: D3DFMT_NULL is used to bound no real render target,
636 * but render to depth buffer. We have to not take into account the render
637 * target info. TODO: know what should happen when there are several render targers
638 * and the first one is D3DFMT_NULL */
639 if (rt0->desc.Format == D3DFMT_NULL && context->ds) {
640 w = context->ds->desc.Width;
641 h = context->ds->desc.Height;
642 nr_samples = context->ds->base.info.nr_samples;
643 }
644
645 for (i = 0; i < device->caps.NumSimultaneousRTs; ++i) {
646 struct NineSurface9 *rt = context->rt[i];
647
648 if (rt && rt->desc.Format != D3DFMT_NULL && (mask & (1 << i)) &&
649 rt->desc.Width == w && rt->desc.Height == h &&
650 rt->base.info.nr_samples == nr_samples) {
651 fb->cbufs[i] = NineSurface9_GetSurface(rt, sRGB);
652 context->rt_mask |= 1 << i;
653 fb->nr_cbufs = i + 1;
654 } else {
655 /* Color outputs must match RT slot,
656 * drivers will have to handle NULL entries for GL, too.
657 */
658 fb->cbufs[i] = NULL;
659 }
660 }
661
662 if (context->ds && context->ds->desc.Width >= w &&
663 context->ds->desc.Height >= h &&
664 context->ds->base.info.nr_samples == nr_samples) {
665 fb->zsbuf = NineSurface9_GetSurface(context->ds, 0);
666 } else {
667 fb->zsbuf = NULL;
668 }
669
670 fb->width = w;
671 fb->height = h;
672
673 pipe->set_framebuffer_state(pipe, fb); /* XXX: cso ? */
674
675 if (is_clear && context->rt_mask == ps_mask)
676 context->changed.group &= ~NINE_STATE_FB;
677 }
678
679 static void
680 update_viewport(struct NineDevice9 *device)
681 {
682 struct nine_context *context = &device->context;
683 const D3DVIEWPORT9 *vport = &context->viewport;
684 struct pipe_viewport_state pvport;
685
686 /* D3D coordinates are:
687 * -1 .. +1 for X,Y and
688 * 0 .. +1 for Z (we use pipe_rasterizer_state.clip_halfz)
689 */
690 pvport.scale[0] = (float)vport->Width * 0.5f;
691 pvport.scale[1] = (float)vport->Height * -0.5f;
692 pvport.scale[2] = vport->MaxZ - vport->MinZ;
693 pvport.translate[0] = (float)vport->Width * 0.5f + (float)vport->X;
694 pvport.translate[1] = (float)vport->Height * 0.5f + (float)vport->Y;
695 pvport.translate[2] = vport->MinZ;
696
697 /* We found R600 and SI cards have some imprecision
698 * on the barycentric coordinates used for interpolation.
699 * Some shaders rely on having something precise.
700 * We found that the proprietary driver has the imprecision issue,
701 * except when the render target width and height are powers of two.
702 * It is using some sort of workaround for these cases
703 * which covers likely all the cases the applications rely
704 * on something precise.
705 * We haven't found the workaround, but it seems like it's better
706 * for applications if the imprecision is biased towards infinity
707 * instead of -infinity (which is what measured). So shift slightly
708 * the viewport: not enough to change rasterization result (in particular
709 * for multisampling), but enough to make the imprecision biased
710 * towards infinity. We do this shift only if render target width and
711 * height are powers of two.
712 * Solves 'red shadows' bug on UE3 games.
713 */
714 if (device->driver_bugs.buggy_barycentrics &&
715 ((vport->Width & (vport->Width-1)) == 0) &&
716 ((vport->Height & (vport->Height-1)) == 0)) {
717 pvport.translate[0] -= 1.0f / 128.0f;
718 pvport.translate[1] -= 1.0f / 128.0f;
719 }
720
721 cso_set_viewport(context->cso, &pvport);
722 }
723
724 /* Loop through VS inputs and pick the vertex elements with the declared
725 * usage from the vertex declaration, then insert the instance divisor from
726 * the stream source frequency setting.
727 */
728 static void
729 update_vertex_elements(struct NineDevice9 *device)
730 {
731 struct nine_context *context = &device->context;
732 const struct NineVertexDeclaration9 *vdecl = device->context.vdecl;
733 const struct NineVertexShader9 *vs;
734 unsigned n, b, i;
735 int index;
736 char vdecl_index_map[16]; /* vs->num_inputs <= 16 */
737 char used_streams[device->caps.MaxStreams];
738 int dummy_vbo_stream = -1;
739 BOOL need_dummy_vbo = FALSE;
740 struct pipe_vertex_element ve[PIPE_MAX_ATTRIBS];
741
742 context->stream_usage_mask = 0;
743 memset(vdecl_index_map, -1, 16);
744 memset(used_streams, 0, device->caps.MaxStreams);
745 vs = context->programmable_vs ? context->vs : device->ff.vs;
746
747 if (vdecl) {
748 for (n = 0; n < vs->num_inputs; ++n) {
749 DBG("looking up input %u (usage %u) from vdecl(%p)\n",
750 n, vs->input_map[n].ndecl, vdecl);
751
752 for (i = 0; i < vdecl->nelems; i++) {
753 if (vdecl->usage_map[i] == vs->input_map[n].ndecl) {
754 vdecl_index_map[n] = i;
755 used_streams[vdecl->elems[i].vertex_buffer_index] = 1;
756 break;
757 }
758 }
759 if (vdecl_index_map[n] < 0)
760 need_dummy_vbo = TRUE;
761 }
762 } else {
763 /* No vertex declaration. Likely will never happen in practice,
764 * but we need not crash on this */
765 need_dummy_vbo = TRUE;
766 }
767
768 if (need_dummy_vbo) {
769 for (i = 0; i < device->caps.MaxStreams; i++ ) {
770 if (!used_streams[i]) {
771 dummy_vbo_stream = i;
772 break;
773 }
774 }
775 }
776 /* there are less vertex shader inputs than stream slots,
777 * so if we need a slot for the dummy vbo, we should have found one */
778 assert (!need_dummy_vbo || dummy_vbo_stream != -1);
779
780 for (n = 0; n < vs->num_inputs; ++n) {
781 index = vdecl_index_map[n];
782 if (index >= 0) {
783 ve[n] = vdecl->elems[index];
784 b = ve[n].vertex_buffer_index;
785 context->stream_usage_mask |= 1 << b;
786 /* XXX wine just uses 1 here: */
787 if (context->stream_freq[b] & D3DSTREAMSOURCE_INSTANCEDATA)
788 ve[n].instance_divisor = context->stream_freq[b] & 0x7FFFFF;
789 } else {
790 /* if the vertex declaration is incomplete compared to what the
791 * vertex shader needs, we bind a dummy vbo with 0 0 0 0.
792 * This is not precised by the spec, but is the behaviour
793 * tested on win */
794 ve[n].vertex_buffer_index = dummy_vbo_stream;
795 ve[n].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
796 ve[n].src_offset = 0;
797 ve[n].instance_divisor = 0;
798 }
799 }
800
801 if (context->dummy_vbo_bound_at != dummy_vbo_stream) {
802 if (context->dummy_vbo_bound_at >= 0)
803 context->changed.vtxbuf |= 1 << context->dummy_vbo_bound_at;
804 if (dummy_vbo_stream >= 0) {
805 context->changed.vtxbuf |= 1 << dummy_vbo_stream;
806 context->vbo_bound_done = FALSE;
807 }
808 context->dummy_vbo_bound_at = dummy_vbo_stream;
809 }
810
811 cso_set_vertex_elements(context->cso, vs->num_inputs, ve);
812 }
813
814 static void
815 update_vertex_buffers(struct NineDevice9 *device)
816 {
817 struct nine_context *context = &device->context;
818 struct pipe_context *pipe = context->pipe;
819 struct pipe_vertex_buffer dummy_vtxbuf;
820 uint32_t mask = context->changed.vtxbuf;
821 unsigned i;
822
823 DBG("mask=%x\n", mask);
824
825 if (context->dummy_vbo_bound_at >= 0) {
826 if (!context->vbo_bound_done) {
827 dummy_vtxbuf.buffer.resource = device->dummy_vbo;
828 dummy_vtxbuf.stride = 0;
829 dummy_vtxbuf.is_user_buffer = false;
830 dummy_vtxbuf.buffer_offset = 0;
831 pipe->set_vertex_buffers(pipe, context->dummy_vbo_bound_at,
832 1, &dummy_vtxbuf);
833 context->vbo_bound_done = TRUE;
834 }
835 mask &= ~(1 << context->dummy_vbo_bound_at);
836 }
837
838 for (i = 0; mask; mask >>= 1, ++i) {
839 if (mask & 1) {
840 if (context->vtxbuf[i].buffer.resource)
841 pipe->set_vertex_buffers(pipe, i, 1, &context->vtxbuf[i]);
842 else
843 pipe->set_vertex_buffers(pipe, i, 1, NULL);
844 }
845 }
846
847 context->changed.vtxbuf = 0;
848 }
849
850 static inline boolean
851 update_sampler_derived(struct nine_context *context, unsigned s)
852 {
853 boolean changed = FALSE;
854
855 if (context->samp[s][NINED3DSAMP_SHADOW] != context->texture[s].shadow) {
856 changed = TRUE;
857 context->samp[s][NINED3DSAMP_SHADOW] = context->texture[s].shadow;
858 }
859
860 if (context->samp[s][NINED3DSAMP_CUBETEX] !=
861 (context->texture[s].type == D3DRTYPE_CUBETEXTURE)) {
862 changed = TRUE;
863 context->samp[s][NINED3DSAMP_CUBETEX] =
864 context->texture[s].type == D3DRTYPE_CUBETEXTURE;
865 }
866
867 if (context->samp[s][D3DSAMP_MIPFILTER] != D3DTEXF_NONE) {
868 int lod = context->samp[s][D3DSAMP_MAXMIPLEVEL] - context->texture[s].lod;
869 if (lod < 0)
870 lod = 0;
871 if (context->samp[s][NINED3DSAMP_MINLOD] != lod) {
872 changed = TRUE;
873 context->samp[s][NINED3DSAMP_MINLOD] = lod;
874 }
875 } else {
876 context->changed.sampler[s] &= ~0x300; /* lod changes irrelevant */
877 }
878
879 return changed;
880 }
881
882 /* TODO: add sRGB override to pipe_sampler_state ? */
883 static void
884 update_textures_and_samplers(struct NineDevice9 *device)
885 {
886 struct nine_context *context = &device->context;
887 struct pipe_sampler_view *view[NINE_MAX_SAMPLERS];
888 unsigned num_textures;
889 unsigned i;
890 boolean commit_samplers;
891 uint16_t sampler_mask = context->ps ? context->ps->sampler_mask :
892 device->ff.ps->sampler_mask;
893
894 /* TODO: Can we reduce iterations here ? */
895
896 commit_samplers = FALSE;
897 context->bound_samplers_mask_ps = 0;
898 for (num_textures = 0, i = 0; i < NINE_MAX_SAMPLERS_PS; ++i) {
899 const unsigned s = NINE_SAMPLER_PS(i);
900 int sRGB;
901
902 if (!context->texture[s].enabled && !(sampler_mask & (1 << i))) {
903 view[i] = NULL;
904 continue;
905 }
906
907 if (context->texture[s].enabled) {
908 sRGB = context->samp[s][D3DSAMP_SRGBTEXTURE] ? 1 : 0;
909
910 view[i] = context->texture[s].view[sRGB];
911 num_textures = i + 1;
912
913 if (update_sampler_derived(context, s) || (context->changed.sampler[s] & 0x05fe)) {
914 context->changed.sampler[s] = 0;
915 commit_samplers = TRUE;
916 nine_convert_sampler_state(context->cso, s, context->samp[s]);
917 }
918 } else {
919 /* Bind dummy sampler. We do not bind dummy sampler when
920 * it is not needed because it could add overhead. The
921 * dummy sampler should have r=g=b=0 and a=1. We do not
922 * unbind dummy sampler directly when they are not needed
923 * anymore, but they're going to be removed as long as texture
924 * or sampler states are changed. */
925 view[i] = device->dummy_sampler_view;
926 num_textures = i + 1;
927
928 cso_single_sampler(context->cso, PIPE_SHADER_FRAGMENT,
929 s - NINE_SAMPLER_PS(0), &device->dummy_sampler_state);
930
931 commit_samplers = TRUE;
932 context->changed.sampler[s] = ~0;
933 }
934
935 context->bound_samplers_mask_ps |= (1 << s);
936 }
937
938 cso_set_sampler_views(context->cso, PIPE_SHADER_FRAGMENT, num_textures, view);
939
940 if (commit_samplers)
941 cso_single_sampler_done(context->cso, PIPE_SHADER_FRAGMENT);
942
943 commit_samplers = FALSE;
944 sampler_mask = context->programmable_vs ? context->vs->sampler_mask : 0;
945 context->bound_samplers_mask_vs = 0;
946 for (num_textures = 0, i = 0; i < NINE_MAX_SAMPLERS_VS; ++i) {
947 const unsigned s = NINE_SAMPLER_VS(i);
948 int sRGB;
949
950 if (!context->texture[s].enabled && !(sampler_mask & (1 << i))) {
951 view[i] = NULL;
952 continue;
953 }
954
955 if (context->texture[s].enabled) {
956 sRGB = context->samp[s][D3DSAMP_SRGBTEXTURE] ? 1 : 0;
957
958 view[i] = context->texture[s].view[sRGB];
959 num_textures = i + 1;
960
961 if (update_sampler_derived(context, s) || (context->changed.sampler[s] & 0x05fe)) {
962 context->changed.sampler[s] = 0;
963 commit_samplers = TRUE;
964 nine_convert_sampler_state(context->cso, s, context->samp[s]);
965 }
966 } else {
967 /* Bind dummy sampler. We do not bind dummy sampler when
968 * it is not needed because it could add overhead. The
969 * dummy sampler should have r=g=b=0 and a=1. We do not
970 * unbind dummy sampler directly when they are not needed
971 * anymore, but they're going to be removed as long as texture
972 * or sampler states are changed. */
973 view[i] = device->dummy_sampler_view;
974 num_textures = i + 1;
975
976 cso_single_sampler(context->cso, PIPE_SHADER_VERTEX,
977 s - NINE_SAMPLER_VS(0), &device->dummy_sampler_state);
978
979 commit_samplers = TRUE;
980 context->changed.sampler[s] = ~0;
981 }
982
983 context->bound_samplers_mask_vs |= (1 << i);
984 }
985
986 cso_set_sampler_views(context->cso, PIPE_SHADER_VERTEX, num_textures, view);
987
988 if (commit_samplers)
989 cso_single_sampler_done(context->cso, PIPE_SHADER_VERTEX);
990 }
991
992 /* State commit only */
993
994 static inline void
995 commit_blend(struct NineDevice9 *device)
996 {
997 struct nine_context *context = &device->context;
998
999 cso_set_blend(context->cso, &context->pipe_data.blend);
1000 }
1001
1002 static inline void
1003 commit_dsa(struct NineDevice9 *device)
1004 {
1005 struct nine_context *context = &device->context;
1006
1007 cso_set_depth_stencil_alpha(context->cso, &context->pipe_data.dsa);
1008 }
1009
1010 static inline void
1011 commit_scissor(struct NineDevice9 *device)
1012 {
1013 struct nine_context *context = &device->context;
1014 struct pipe_context *pipe = context->pipe;
1015
1016 pipe->set_scissor_states(pipe, 0, 1, &context->scissor);
1017 }
1018
1019 static inline void
1020 commit_rasterizer(struct NineDevice9 *device)
1021 {
1022 struct nine_context *context = &device->context;
1023
1024 cso_set_rasterizer(context->cso, &context->pipe_data.rast);
1025 }
1026
1027 static inline void
1028 commit_vs_constants(struct NineDevice9 *device)
1029 {
1030 struct nine_context *context = &device->context;
1031 struct pipe_context *pipe = context->pipe;
1032
1033 if (unlikely(!context->programmable_vs))
1034 pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, &context->pipe_data.cb_vs_ff);
1035 else {
1036 if (context->swvp) {
1037 pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, &context->pipe_data.cb0_swvp);
1038 pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 1, &context->pipe_data.cb1_swvp);
1039 pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 2, &context->pipe_data.cb2_swvp);
1040 pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 3, &context->pipe_data.cb3_swvp);
1041 } else {
1042 pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, &context->pipe_data.cb_vs);
1043 }
1044 }
1045 }
1046
1047 static inline void
1048 commit_ps_constants(struct NineDevice9 *device)
1049 {
1050 struct nine_context *context = &device->context;
1051 struct pipe_context *pipe = context->pipe;
1052
1053 if (unlikely(!context->ps))
1054 pipe->set_constant_buffer(pipe, PIPE_SHADER_FRAGMENT, 0, &context->pipe_data.cb_ps_ff);
1055 else
1056 pipe->set_constant_buffer(pipe, PIPE_SHADER_FRAGMENT, 0, &context->pipe_data.cb_ps);
1057 }
1058
1059 static inline void
1060 commit_vs(struct NineDevice9 *device)
1061 {
1062 struct nine_context *context = &device->context;
1063
1064 context->pipe->bind_vs_state(context->pipe, context->cso_shader.vs);
1065 }
1066
1067
1068 static inline void
1069 commit_ps(struct NineDevice9 *device)
1070 {
1071 struct nine_context *context = &device->context;
1072
1073 context->pipe->bind_fs_state(context->pipe, context->cso_shader.ps);
1074 }
1075 /* State Update */
1076
1077 #define NINE_STATE_SHADER_CHANGE_VS \
1078 (NINE_STATE_VS | \
1079 NINE_STATE_TEXTURE | \
1080 NINE_STATE_VS_PARAMS_MISC | \
1081 NINE_STATE_SWVP)
1082
1083 #define NINE_STATE_SHADER_CHANGE_PS \
1084 (NINE_STATE_PS | \
1085 NINE_STATE_TEXTURE | \
1086 NINE_STATE_PS_PARAMS_MISC)
1087
1088 #define NINE_STATE_FREQUENT \
1089 (NINE_STATE_RASTERIZER | \
1090 NINE_STATE_TEXTURE | \
1091 NINE_STATE_SAMPLER | \
1092 NINE_STATE_VS_CONST | \
1093 NINE_STATE_PS_CONST | \
1094 NINE_STATE_MULTISAMPLE)
1095
1096 #define NINE_STATE_COMMON \
1097 (NINE_STATE_FB | \
1098 NINE_STATE_BLEND | \
1099 NINE_STATE_DSA | \
1100 NINE_STATE_VIEWPORT | \
1101 NINE_STATE_VDECL | \
1102 NINE_STATE_IDXBUF | \
1103 NINE_STATE_STREAMFREQ)
1104
1105 #define NINE_STATE_RARE \
1106 (NINE_STATE_SCISSOR | \
1107 NINE_STATE_BLEND_COLOR | \
1108 NINE_STATE_STENCIL_REF | \
1109 NINE_STATE_SAMPLE_MASK)
1110
1111 static void
1112 nine_update_state(struct NineDevice9 *device)
1113 {
1114 struct nine_context *context = &device->context;
1115 struct pipe_context *pipe = context->pipe;
1116 uint32_t group;
1117
1118 DBG("changed state groups: %x\n", context->changed.group);
1119
1120 /* NOTE: We may want to use the cso cache for everything, or let
1121 * NineDevice9.RestoreNonCSOState actually set the states, then we wouldn't
1122 * have to care about state being clobbered here and could merge this back
1123 * into update_textures. Except, we also need to re-validate textures that
1124 * may be dirty anyway, even if no texture bindings changed.
1125 */
1126
1127 /* ff_update may change VS/PS dirty bits */
1128 if (unlikely(!context->programmable_vs || !context->ps))
1129 nine_ff_update(device);
1130 group = context->changed.group;
1131
1132 if (group & (NINE_STATE_SHADER_CHANGE_VS | NINE_STATE_SHADER_CHANGE_PS)) {
1133 if (group & NINE_STATE_SHADER_CHANGE_VS)
1134 group |= prepare_vs(device, (group & NINE_STATE_VS) != 0); /* may set NINE_STATE_RASTERIZER and NINE_STATE_SAMPLER*/
1135 if (group & NINE_STATE_SHADER_CHANGE_PS)
1136 group |= prepare_ps(device, (group & NINE_STATE_PS) != 0);
1137 }
1138
1139 if (group & (NINE_STATE_COMMON | NINE_STATE_VS)) {
1140 if (group & NINE_STATE_FB)
1141 update_framebuffer(device, FALSE);
1142 if (group & NINE_STATE_BLEND)
1143 prepare_blend(device);
1144 if (group & NINE_STATE_DSA)
1145 prepare_dsa(device);
1146 if (group & NINE_STATE_VIEWPORT)
1147 update_viewport(device);
1148 if (group & (NINE_STATE_VDECL | NINE_STATE_VS | NINE_STATE_STREAMFREQ))
1149 update_vertex_elements(device);
1150 }
1151
1152 if (likely(group & (NINE_STATE_FREQUENT | NINE_STATE_VS | NINE_STATE_PS | NINE_STATE_SWVP))) {
1153 if (group & NINE_STATE_MULTISAMPLE)
1154 group |= check_multisample(device);
1155 if (group & NINE_STATE_RASTERIZER)
1156 prepare_rasterizer(device);
1157 if (group & (NINE_STATE_TEXTURE | NINE_STATE_SAMPLER))
1158 update_textures_and_samplers(device);
1159 if ((group & (NINE_STATE_VS_CONST | NINE_STATE_VS | NINE_STATE_SWVP)) && context->programmable_vs)
1160 prepare_vs_constants_userbuf(device);
1161 if ((group & (NINE_STATE_PS_CONST | NINE_STATE_PS)) && context->ps)
1162 prepare_ps_constants_userbuf(device);
1163 }
1164
1165 if (context->changed.vtxbuf)
1166 update_vertex_buffers(device);
1167
1168 if (context->commit & NINE_STATE_COMMIT_BLEND)
1169 commit_blend(device);
1170 if (context->commit & NINE_STATE_COMMIT_DSA)
1171 commit_dsa(device);
1172 if (context->commit & NINE_STATE_COMMIT_RASTERIZER)
1173 commit_rasterizer(device);
1174 if (context->commit & NINE_STATE_COMMIT_CONST_VS)
1175 commit_vs_constants(device);
1176 if (context->commit & NINE_STATE_COMMIT_CONST_PS)
1177 commit_ps_constants(device);
1178 if (context->commit & NINE_STATE_COMMIT_VS)
1179 commit_vs(device);
1180 if (context->commit & NINE_STATE_COMMIT_PS)
1181 commit_ps(device);
1182
1183 context->commit = 0;
1184
1185 if (unlikely(context->changed.ucp)) {
1186 pipe->set_clip_state(pipe, &context->clip);
1187 context->changed.ucp = FALSE;
1188 }
1189
1190 if (unlikely(group & NINE_STATE_RARE)) {
1191 if (group & NINE_STATE_SCISSOR)
1192 commit_scissor(device);
1193 if (group & NINE_STATE_BLEND_COLOR) {
1194 struct pipe_blend_color color;
1195 d3dcolor_to_rgba(&color.color[0], context->rs[D3DRS_BLENDFACTOR]);
1196 pipe->set_blend_color(pipe, &color);
1197 }
1198 if (group & NINE_STATE_SAMPLE_MASK) {
1199 if (context->rt[0]->desc.MultiSampleType <= D3DMULTISAMPLE_NONMASKABLE) {
1200 pipe->set_sample_mask(pipe, ~0);
1201 } else {
1202 pipe->set_sample_mask(pipe, context->rs[D3DRS_MULTISAMPLEMASK]);
1203 }
1204 }
1205 if (group & NINE_STATE_STENCIL_REF) {
1206 struct pipe_stencil_ref ref;
1207 ref.ref_value[0] = context->rs[D3DRS_STENCILREF];
1208 ref.ref_value[1] = ref.ref_value[0];
1209 pipe->set_stencil_ref(pipe, &ref);
1210 }
1211 }
1212
1213 context->changed.group &=
1214 (NINE_STATE_FF | NINE_STATE_VS_CONST | NINE_STATE_PS_CONST);
1215
1216 DBG("finished\n");
1217 }
1218
1219 #define RESZ_CODE 0x7fa05000
1220
1221 static void
1222 NineDevice9_ResolveZ( struct NineDevice9 *device )
1223 {
1224 struct nine_context *context = &device->context;
1225 const struct util_format_description *desc;
1226 struct NineSurface9 *source = context->ds;
1227 struct pipe_resource *src, *dst;
1228 struct pipe_blit_info blit;
1229
1230 DBG("RESZ resolve\n");
1231
1232 if (!source || !context->texture[0].enabled ||
1233 context->texture[0].type != D3DRTYPE_TEXTURE)
1234 return;
1235
1236 src = source->base.resource;
1237 dst = context->texture[0].resource;
1238
1239 if (!src || !dst)
1240 return;
1241
1242 /* check dst is depth format. we know already for src */
1243 desc = util_format_description(dst->format);
1244 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
1245 return;
1246
1247 memset(&blit, 0, sizeof(blit));
1248 blit.src.resource = src;
1249 blit.src.level = 0;
1250 blit.src.format = src->format;
1251 blit.src.box.z = 0;
1252 blit.src.box.depth = 1;
1253 blit.src.box.x = 0;
1254 blit.src.box.y = 0;
1255 blit.src.box.width = src->width0;
1256 blit.src.box.height = src->height0;
1257
1258 blit.dst.resource = dst;
1259 blit.dst.level = 0;
1260 blit.dst.format = dst->format;
1261 blit.dst.box.z = 0;
1262 blit.dst.box.depth = 1;
1263 blit.dst.box.x = 0;
1264 blit.dst.box.y = 0;
1265 blit.dst.box.width = dst->width0;
1266 blit.dst.box.height = dst->height0;
1267
1268 blit.mask = PIPE_MASK_ZS;
1269 blit.filter = PIPE_TEX_FILTER_NEAREST;
1270 blit.scissor_enable = FALSE;
1271
1272 context->pipe->blit(context->pipe, &blit);
1273 }
1274
1275 #define ALPHA_TO_COVERAGE_ENABLE MAKEFOURCC('A', '2', 'M', '1')
1276 #define ALPHA_TO_COVERAGE_DISABLE MAKEFOURCC('A', '2', 'M', '0')
1277
1278 /* Nine_context functions.
1279 * Serialized through CSMT macros.
1280 */
1281
1282 static void
1283 nine_context_set_texture_apply(struct NineDevice9 *device,
1284 DWORD stage,
1285 BOOL enabled,
1286 BOOL shadow,
1287 DWORD lod,
1288 D3DRESOURCETYPE type,
1289 uint8_t pstype,
1290 struct pipe_resource *res,
1291 struct pipe_sampler_view *view0,
1292 struct pipe_sampler_view *view1);
1293 static void
1294 nine_context_set_stream_source_apply(struct NineDevice9 *device,
1295 UINT StreamNumber,
1296 struct pipe_resource *res,
1297 UINT OffsetInBytes,
1298 UINT Stride);
1299
1300 static void
1301 nine_context_set_indices_apply(struct NineDevice9 *device,
1302 struct pipe_resource *res,
1303 UINT IndexSize,
1304 UINT OffsetInBytes);
1305
1306 static void
1307 nine_context_set_pixel_shader_constant_i_transformed(struct NineDevice9 *device,
1308 UINT StartRegister,
1309 const int *pConstantData,
1310 unsigned pConstantData_size,
1311 UINT Vector4iCount);
1312
1313 CSMT_ITEM_NO_WAIT(nine_context_set_render_state,
1314 ARG_VAL(D3DRENDERSTATETYPE, State),
1315 ARG_VAL(DWORD, Value))
1316 {
1317 struct nine_context *context = &device->context;
1318
1319 /* Amd hacks (equivalent to GL extensions) */
1320 if (unlikely(State == D3DRS_POINTSIZE)) {
1321 if (Value == RESZ_CODE) {
1322 NineDevice9_ResolveZ(device);
1323 return;
1324 }
1325
1326 if (Value == ALPHA_TO_COVERAGE_ENABLE ||
1327 Value == ALPHA_TO_COVERAGE_DISABLE) {
1328 context->rs[NINED3DRS_ALPHACOVERAGE] = (Value == ALPHA_TO_COVERAGE_ENABLE);
1329 context->changed.group |= NINE_STATE_BLEND;
1330 return;
1331 }
1332 }
1333
1334 /* NV hack */
1335 if (unlikely(State == D3DRS_ADAPTIVETESS_Y)) {
1336 if (Value == D3DFMT_ATOC || (Value == D3DFMT_UNKNOWN && context->rs[NINED3DRS_ALPHACOVERAGE])) {
1337 context->rs[NINED3DRS_ALPHACOVERAGE] = (Value == D3DFMT_ATOC) ? 3 : 0;
1338 context->rs[NINED3DRS_ALPHACOVERAGE] &= context->rs[D3DRS_ALPHATESTENABLE] ? 3 : 2;
1339 context->changed.group |= NINE_STATE_BLEND;
1340 return;
1341 }
1342 }
1343 if (unlikely(State == D3DRS_ALPHATESTENABLE && (context->rs[NINED3DRS_ALPHACOVERAGE] & 2))) {
1344 DWORD alphacoverage_prev = context->rs[NINED3DRS_ALPHACOVERAGE];
1345 context->rs[NINED3DRS_ALPHACOVERAGE] = (Value ? 3 : 2);
1346 if (context->rs[NINED3DRS_ALPHACOVERAGE] != alphacoverage_prev)
1347 context->changed.group |= NINE_STATE_BLEND;
1348 }
1349
1350 context->rs[State] = nine_fix_render_state_value(State, Value);
1351 context->changed.group |= nine_render_state_group[State];
1352 }
1353
1354 CSMT_ITEM_NO_WAIT(nine_context_set_texture_apply,
1355 ARG_VAL(DWORD, stage),
1356 ARG_VAL(BOOL, enabled),
1357 ARG_VAL(BOOL, shadow),
1358 ARG_VAL(DWORD, lod),
1359 ARG_VAL(D3DRESOURCETYPE, type),
1360 ARG_VAL(uint8_t, pstype),
1361 ARG_BIND_RES(struct pipe_resource, res),
1362 ARG_BIND_VIEW(struct pipe_sampler_view, view0),
1363 ARG_BIND_VIEW(struct pipe_sampler_view, view1))
1364 {
1365 struct nine_context *context = &device->context;
1366
1367 context->texture[stage].enabled = enabled;
1368 context->samplers_shadow &= ~(1 << stage);
1369 context->samplers_shadow |= shadow << stage;
1370 context->texture[stage].shadow = shadow;
1371 context->texture[stage].lod = lod;
1372 context->texture[stage].type = type;
1373 context->texture[stage].pstype = pstype;
1374 pipe_resource_reference(&context->texture[stage].resource, res);
1375 pipe_sampler_view_reference(&context->texture[stage].view[0], view0);
1376 pipe_sampler_view_reference(&context->texture[stage].view[1], view1);
1377
1378 context->changed.group |= NINE_STATE_TEXTURE;
1379 }
1380
1381 void
1382 nine_context_set_texture(struct NineDevice9 *device,
1383 DWORD Stage,
1384 struct NineBaseTexture9 *tex)
1385 {
1386 BOOL enabled = FALSE;
1387 BOOL shadow = FALSE;
1388 DWORD lod = 0;
1389 D3DRESOURCETYPE type = D3DRTYPE_TEXTURE;
1390 uint8_t pstype = 0;
1391 struct pipe_resource *res = NULL;
1392 struct pipe_sampler_view *view0 = NULL, *view1 = NULL;
1393
1394 /* For managed pool, the data can be initially incomplete.
1395 * In that case, the texture is rebound later
1396 * (in NineBaseTexture9_Validate/NineBaseTexture9_UploadSelf). */
1397 if (tex && tex->base.resource) {
1398 enabled = TRUE;
1399 shadow = tex->shadow;
1400 lod = tex->managed.lod;
1401 type = tex->base.type;
1402 pstype = tex->pstype;
1403 res = tex->base.resource;
1404 view0 = NineBaseTexture9_GetSamplerView(tex, 0);
1405 view1 = NineBaseTexture9_GetSamplerView(tex, 1);
1406 }
1407
1408 nine_context_set_texture_apply(device, Stage, enabled,
1409 shadow, lod, type, pstype,
1410 res, view0, view1);
1411 }
1412
1413 CSMT_ITEM_NO_WAIT(nine_context_set_sampler_state,
1414 ARG_VAL(DWORD, Sampler),
1415 ARG_VAL(D3DSAMPLERSTATETYPE, Type),
1416 ARG_VAL(DWORD, Value))
1417 {
1418 struct nine_context *context = &device->context;
1419
1420 if (unlikely(!nine_check_sampler_state_value(Type, Value)))
1421 return;
1422
1423 context->samp[Sampler][Type] = Value;
1424 context->changed.group |= NINE_STATE_SAMPLER;
1425 context->changed.sampler[Sampler] |= 1 << Type;
1426 }
1427
1428 CSMT_ITEM_NO_WAIT(nine_context_set_stream_source_apply,
1429 ARG_VAL(UINT, StreamNumber),
1430 ARG_BIND_RES(struct pipe_resource, res),
1431 ARG_VAL(UINT, OffsetInBytes),
1432 ARG_VAL(UINT, Stride))
1433 {
1434 struct nine_context *context = &device->context;
1435 const unsigned i = StreamNumber;
1436
1437 context->vtxbuf[i].stride = Stride;
1438 context->vtxbuf[i].buffer_offset = OffsetInBytes;
1439 pipe_resource_reference(&context->vtxbuf[i].buffer.resource, res);
1440
1441 context->changed.vtxbuf |= 1 << StreamNumber;
1442 }
1443
1444 void
1445 nine_context_set_stream_source(struct NineDevice9 *device,
1446 UINT StreamNumber,
1447 struct NineVertexBuffer9 *pVBuf9,
1448 UINT OffsetInBytes,
1449 UINT Stride)
1450 {
1451 struct pipe_resource *res = NULL;
1452 unsigned offset = 0;
1453
1454 if (pVBuf9)
1455 res = NineVertexBuffer9_GetResource(pVBuf9, &offset);
1456 /* in the future when there is internal offset, add it
1457 * to OffsetInBytes */
1458
1459 nine_context_set_stream_source_apply(device, StreamNumber,
1460 res, offset + OffsetInBytes,
1461 Stride);
1462 }
1463
1464 CSMT_ITEM_NO_WAIT(nine_context_set_stream_source_freq,
1465 ARG_VAL(UINT, StreamNumber),
1466 ARG_VAL(UINT, Setting))
1467 {
1468 struct nine_context *context = &device->context;
1469
1470 context->stream_freq[StreamNumber] = Setting;
1471
1472 if (Setting & D3DSTREAMSOURCE_INSTANCEDATA)
1473 context->stream_instancedata_mask |= 1 << StreamNumber;
1474 else
1475 context->stream_instancedata_mask &= ~(1 << StreamNumber);
1476
1477 if (StreamNumber != 0)
1478 context->changed.group |= NINE_STATE_STREAMFREQ;
1479 }
1480
1481 CSMT_ITEM_NO_WAIT(nine_context_set_indices_apply,
1482 ARG_BIND_RES(struct pipe_resource, res),
1483 ARG_VAL(UINT, IndexSize),
1484 ARG_VAL(UINT, OffsetInBytes))
1485 {
1486 struct nine_context *context = &device->context;
1487
1488 context->index_size = IndexSize;
1489 context->index_offset = OffsetInBytes;
1490 pipe_resource_reference(&context->idxbuf, res);
1491
1492 context->changed.group |= NINE_STATE_IDXBUF;
1493 }
1494
1495 void
1496 nine_context_set_indices(struct NineDevice9 *device,
1497 struct NineIndexBuffer9 *idxbuf)
1498 {
1499 struct pipe_resource *res = NULL;
1500 UINT IndexSize = 0;
1501 unsigned OffsetInBytes = 0;
1502
1503 if (idxbuf) {
1504 res = NineIndexBuffer9_GetBuffer(idxbuf, &OffsetInBytes);
1505 IndexSize = idxbuf->index_size;
1506 }
1507
1508 nine_context_set_indices_apply(device, res, IndexSize, OffsetInBytes);
1509 }
1510
1511 CSMT_ITEM_NO_WAIT(nine_context_set_vertex_declaration,
1512 ARG_BIND_REF(struct NineVertexDeclaration9, vdecl))
1513 {
1514 struct nine_context *context = &device->context;
1515 BOOL was_programmable_vs = context->programmable_vs;
1516
1517 nine_bind(&context->vdecl, vdecl);
1518
1519 context->programmable_vs = context->vs && !(context->vdecl && context->vdecl->position_t);
1520 if (was_programmable_vs != context->programmable_vs) {
1521 context->commit |= NINE_STATE_COMMIT_CONST_VS;
1522 context->changed.group |= NINE_STATE_VS;
1523 }
1524
1525 context->changed.group |= NINE_STATE_VDECL;
1526 }
1527
1528 CSMT_ITEM_NO_WAIT(nine_context_set_vertex_shader,
1529 ARG_BIND_REF(struct NineVertexShader9, pShader))
1530 {
1531 struct nine_context *context = &device->context;
1532 BOOL was_programmable_vs = context->programmable_vs;
1533
1534 nine_bind(&context->vs, pShader);
1535
1536 context->programmable_vs = context->vs && !(context->vdecl && context->vdecl->position_t);
1537
1538 /* ff -> non-ff: commit back non-ff constants */
1539 if (!was_programmable_vs && context->programmable_vs)
1540 context->commit |= NINE_STATE_COMMIT_CONST_VS;
1541
1542 context->changed.group |= NINE_STATE_VS;
1543 }
1544
1545 CSMT_ITEM_NO_WAIT(nine_context_set_vertex_shader_constant_f,
1546 ARG_VAL(UINT, StartRegister),
1547 ARG_MEM(float, pConstantData),
1548 ARG_MEM_SIZE(unsigned, pConstantData_size),
1549 ARG_VAL(UINT, Vector4fCount))
1550 {
1551 struct nine_context *context = &device->context;
1552 float *vs_const_f = device->may_swvp ? context->vs_const_f_swvp : context->vs_const_f;
1553
1554 memcpy(&vs_const_f[StartRegister * 4],
1555 pConstantData,
1556 pConstantData_size);
1557
1558 if (device->may_swvp) {
1559 Vector4fCount = MIN2(StartRegister + Vector4fCount, NINE_MAX_CONST_F) - StartRegister;
1560 if (StartRegister < NINE_MAX_CONST_F)
1561 memcpy(&context->vs_const_f[StartRegister * 4],
1562 pConstantData,
1563 Vector4fCount * 4 * sizeof(context->vs_const_f[0]));
1564 }
1565
1566 context->changed.vs_const_f = TRUE;
1567 context->changed.group |= NINE_STATE_VS_CONST;
1568 }
1569
1570 CSMT_ITEM_NO_WAIT(nine_context_set_vertex_shader_constant_i,
1571 ARG_VAL(UINT, StartRegister),
1572 ARG_MEM(int, pConstantData),
1573 ARG_MEM_SIZE(unsigned, pConstantData_size),
1574 ARG_VAL(UINT, Vector4iCount))
1575 {
1576 struct nine_context *context = &device->context;
1577 int i;
1578
1579 if (device->driver_caps.vs_integer) {
1580 memcpy(&context->vs_const_i[4 * StartRegister],
1581 pConstantData,
1582 pConstantData_size);
1583 } else {
1584 for (i = 0; i < Vector4iCount; i++) {
1585 context->vs_const_i[4 * (StartRegister + i)] = fui((float)(pConstantData[4 * i]));
1586 context->vs_const_i[4 * (StartRegister + i) + 1] = fui((float)(pConstantData[4 * i + 1]));
1587 context->vs_const_i[4 * (StartRegister + i) + 2] = fui((float)(pConstantData[4 * i + 2]));
1588 context->vs_const_i[4 * (StartRegister + i) + 3] = fui((float)(pConstantData[4 * i + 3]));
1589 }
1590 }
1591
1592 context->changed.vs_const_i = TRUE;
1593 context->changed.group |= NINE_STATE_VS_CONST;
1594 }
1595
1596 CSMT_ITEM_NO_WAIT(nine_context_set_vertex_shader_constant_b,
1597 ARG_VAL(UINT, StartRegister),
1598 ARG_MEM(BOOL, pConstantData),
1599 ARG_MEM_SIZE(unsigned, pConstantData_size),
1600 ARG_VAL(UINT, BoolCount))
1601 {
1602 struct nine_context *context = &device->context;
1603 int i;
1604 uint32_t bool_true = device->driver_caps.vs_integer ? 0xFFFFFFFF : fui(1.0f);
1605
1606 (void) pConstantData_size;
1607
1608 for (i = 0; i < BoolCount; i++)
1609 context->vs_const_b[StartRegister + i] = pConstantData[i] ? bool_true : 0;
1610
1611 context->changed.vs_const_b = TRUE;
1612 context->changed.group |= NINE_STATE_VS_CONST;
1613 }
1614
1615 CSMT_ITEM_NO_WAIT(nine_context_set_pixel_shader,
1616 ARG_BIND_REF(struct NinePixelShader9, ps))
1617 {
1618 struct nine_context *context = &device->context;
1619 unsigned old_mask = context->ps ? context->ps->rt_mask : 1;
1620 unsigned mask;
1621
1622 /* ff -> non-ff: commit back non-ff constants */
1623 if (!context->ps && ps)
1624 context->commit |= NINE_STATE_COMMIT_CONST_PS;
1625
1626 nine_bind(&context->ps, ps);
1627
1628 context->changed.group |= NINE_STATE_PS;
1629
1630 mask = context->ps ? context->ps->rt_mask : 1;
1631 /* We need to update cbufs if the pixel shader would
1632 * write to different render targets */
1633 if (mask != old_mask)
1634 context->changed.group |= NINE_STATE_FB;
1635 }
1636
1637 CSMT_ITEM_NO_WAIT(nine_context_set_pixel_shader_constant_f,
1638 ARG_VAL(UINT, StartRegister),
1639 ARG_MEM(float, pConstantData),
1640 ARG_MEM_SIZE(unsigned, pConstantData_size),
1641 ARG_VAL(UINT, Vector4fCount))
1642 {
1643 struct nine_context *context = &device->context;
1644
1645 memcpy(&context->ps_const_f[StartRegister * 4],
1646 pConstantData,
1647 pConstantData_size);
1648
1649 context->changed.ps_const_f = TRUE;
1650 context->changed.group |= NINE_STATE_PS_CONST;
1651 }
1652
1653 /* For stateblocks */
1654 CSMT_ITEM_NO_WAIT(nine_context_set_pixel_shader_constant_i_transformed,
1655 ARG_VAL(UINT, StartRegister),
1656 ARG_MEM(int, pConstantData),
1657 ARG_MEM_SIZE(unsigned, pConstantData_size),
1658 ARG_VAL(UINT, Vector4iCount))
1659 {
1660 struct nine_context *context = &device->context;
1661
1662 memcpy(&context->ps_const_i[StartRegister][0],
1663 pConstantData,
1664 Vector4iCount * sizeof(context->ps_const_i[0]));
1665
1666 context->changed.ps_const_i = TRUE;
1667 context->changed.group |= NINE_STATE_PS_CONST;
1668 }
1669
1670 CSMT_ITEM_NO_WAIT(nine_context_set_pixel_shader_constant_i,
1671 ARG_VAL(UINT, StartRegister),
1672 ARG_MEM(int, pConstantData),
1673 ARG_MEM_SIZE(unsigned, pConstantData_size),
1674 ARG_VAL(UINT, Vector4iCount))
1675 {
1676 struct nine_context *context = &device->context;
1677 int i;
1678
1679 if (device->driver_caps.ps_integer) {
1680 memcpy(&context->ps_const_i[StartRegister][0],
1681 pConstantData,
1682 pConstantData_size);
1683 } else {
1684 for (i = 0; i < Vector4iCount; i++) {
1685 context->ps_const_i[StartRegister+i][0] = fui((float)(pConstantData[4*i]));
1686 context->ps_const_i[StartRegister+i][1] = fui((float)(pConstantData[4*i+1]));
1687 context->ps_const_i[StartRegister+i][2] = fui((float)(pConstantData[4*i+2]));
1688 context->ps_const_i[StartRegister+i][3] = fui((float)(pConstantData[4*i+3]));
1689 }
1690 }
1691 context->changed.ps_const_i = TRUE;
1692 context->changed.group |= NINE_STATE_PS_CONST;
1693 }
1694
1695 CSMT_ITEM_NO_WAIT(nine_context_set_pixel_shader_constant_b,
1696 ARG_VAL(UINT, StartRegister),
1697 ARG_MEM(BOOL, pConstantData),
1698 ARG_MEM_SIZE(unsigned, pConstantData_size),
1699 ARG_VAL(UINT, BoolCount))
1700 {
1701 struct nine_context *context = &device->context;
1702 int i;
1703 uint32_t bool_true = device->driver_caps.ps_integer ? 0xFFFFFFFF : fui(1.0f);
1704
1705 (void) pConstantData_size;
1706
1707 for (i = 0; i < BoolCount; i++)
1708 context->ps_const_b[StartRegister + i] = pConstantData[i] ? bool_true : 0;
1709
1710 context->changed.ps_const_b = TRUE;
1711 context->changed.group |= NINE_STATE_PS_CONST;
1712 }
1713
1714 /* XXX: use resource, as resource might change */
1715 CSMT_ITEM_NO_WAIT(nine_context_set_render_target,
1716 ARG_VAL(DWORD, RenderTargetIndex),
1717 ARG_BIND_REF(struct NineSurface9, rt))
1718 {
1719 struct nine_context *context = &device->context;
1720 const unsigned i = RenderTargetIndex;
1721
1722 if (i == 0) {
1723 context->viewport.X = 0;
1724 context->viewport.Y = 0;
1725 context->viewport.Width = rt->desc.Width;
1726 context->viewport.Height = rt->desc.Height;
1727 context->viewport.MinZ = 0.0f;
1728 context->viewport.MaxZ = 1.0f;
1729
1730 context->scissor.minx = 0;
1731 context->scissor.miny = 0;
1732 context->scissor.maxx = rt->desc.Width;
1733 context->scissor.maxy = rt->desc.Height;
1734
1735 context->changed.group |= NINE_STATE_VIEWPORT | NINE_STATE_SCISSOR | NINE_STATE_MULTISAMPLE;
1736
1737 if (context->rt[0] &&
1738 (context->rt[0]->desc.MultiSampleType <= D3DMULTISAMPLE_NONMASKABLE) !=
1739 (rt->desc.MultiSampleType <= D3DMULTISAMPLE_NONMASKABLE))
1740 context->changed.group |= NINE_STATE_SAMPLE_MASK;
1741 }
1742
1743 if (context->rt[i] != rt) {
1744 nine_bind(&context->rt[i], rt);
1745 context->changed.group |= NINE_STATE_FB;
1746 }
1747 }
1748
1749 /* XXX: use resource instead of ds, as resource might change */
1750 CSMT_ITEM_NO_WAIT(nine_context_set_depth_stencil,
1751 ARG_BIND_REF(struct NineSurface9, ds))
1752 {
1753 struct nine_context *context = &device->context;
1754
1755 nine_bind(&context->ds, ds);
1756 context->changed.group |= NINE_STATE_FB;
1757 }
1758
1759 CSMT_ITEM_NO_WAIT(nine_context_set_viewport,
1760 ARG_COPY_REF(D3DVIEWPORT9, viewport))
1761 {
1762 struct nine_context *context = &device->context;
1763
1764 context->viewport = *viewport;
1765 context->changed.group |= NINE_STATE_VIEWPORT;
1766 }
1767
1768 CSMT_ITEM_NO_WAIT(nine_context_set_scissor,
1769 ARG_COPY_REF(struct pipe_scissor_state, scissor))
1770 {
1771 struct nine_context *context = &device->context;
1772
1773 context->scissor = *scissor;
1774 context->changed.group |= NINE_STATE_SCISSOR;
1775 }
1776
1777 CSMT_ITEM_NO_WAIT(nine_context_set_transform,
1778 ARG_VAL(D3DTRANSFORMSTATETYPE, State),
1779 ARG_COPY_REF(D3DMATRIX, pMatrix))
1780 {
1781 struct nine_context *context = &device->context;
1782 D3DMATRIX *M = nine_state_access_transform(&context->ff, State, TRUE);
1783
1784 *M = *pMatrix;
1785 context->ff.changed.transform[State / 32] |= 1 << (State % 32);
1786 context->changed.group |= NINE_STATE_FF;
1787 }
1788
1789 CSMT_ITEM_NO_WAIT(nine_context_set_material,
1790 ARG_COPY_REF(D3DMATERIAL9, pMaterial))
1791 {
1792 struct nine_context *context = &device->context;
1793
1794 context->ff.material = *pMaterial;
1795 context->changed.group |= NINE_STATE_FF_MATERIAL;
1796 }
1797
1798 CSMT_ITEM_NO_WAIT(nine_context_set_light,
1799 ARG_VAL(DWORD, Index),
1800 ARG_COPY_REF(D3DLIGHT9, pLight))
1801 {
1802 struct nine_context *context = &device->context;
1803
1804 (void)nine_state_set_light(&context->ff, Index, pLight);
1805 context->changed.group |= NINE_STATE_FF_LIGHTING;
1806 }
1807
1808
1809 /* For stateblocks */
1810 static void
1811 nine_context_light_enable_stateblock(struct NineDevice9 *device,
1812 const uint16_t active_light[NINE_MAX_LIGHTS_ACTIVE], /* TODO: use pointer that convey size for csmt */
1813 unsigned int num_lights_active)
1814 {
1815 struct nine_context *context = &device->context;
1816
1817 /* TODO: Use CSMT_* to avoid calling nine_csmt_process */
1818 nine_csmt_process(device);
1819 memcpy(context->ff.active_light, active_light, NINE_MAX_LIGHTS_ACTIVE * sizeof(context->ff.active_light[0]));
1820 context->ff.num_lights_active = num_lights_active;
1821 context->changed.group |= NINE_STATE_FF_LIGHTING;
1822 }
1823
1824 CSMT_ITEM_NO_WAIT(nine_context_light_enable,
1825 ARG_VAL(DWORD, Index),
1826 ARG_VAL(BOOL, Enable))
1827 {
1828 struct nine_context *context = &device->context;
1829
1830 nine_state_light_enable(&context->ff, &context->changed.group, Index, Enable);
1831 }
1832
1833 CSMT_ITEM_NO_WAIT(nine_context_set_texture_stage_state,
1834 ARG_VAL(DWORD, Stage),
1835 ARG_VAL(D3DTEXTURESTAGESTATETYPE, Type),
1836 ARG_VAL(DWORD, Value))
1837 {
1838 struct nine_context *context = &device->context;
1839 int bumpmap_index = -1;
1840
1841 context->ff.tex_stage[Stage][Type] = Value;
1842 switch (Type) {
1843 case D3DTSS_BUMPENVMAT00:
1844 bumpmap_index = 4 * Stage;
1845 break;
1846 case D3DTSS_BUMPENVMAT01:
1847 bumpmap_index = 4 * Stage + 1;
1848 break;
1849 case D3DTSS_BUMPENVMAT10:
1850 bumpmap_index = 4 * Stage + 2;
1851 break;
1852 case D3DTSS_BUMPENVMAT11:
1853 bumpmap_index = 4 * Stage + 3;
1854 break;
1855 case D3DTSS_BUMPENVLSCALE:
1856 bumpmap_index = 4 * 8 + 2 * Stage;
1857 break;
1858 case D3DTSS_BUMPENVLOFFSET:
1859 bumpmap_index = 4 * 8 + 2 * Stage + 1;
1860 break;
1861 case D3DTSS_TEXTURETRANSFORMFLAGS:
1862 context->changed.group |= NINE_STATE_PS_PARAMS_MISC;
1863 break;
1864 default:
1865 break;
1866 }
1867
1868 if (bumpmap_index >= 0) {
1869 context->bumpmap_vars[bumpmap_index] = Value;
1870 context->changed.group |= NINE_STATE_PS_CONST;
1871 }
1872
1873 context->changed.group |= NINE_STATE_FF_PS_CONSTS;
1874 context->ff.changed.tex_stage[Stage][Type / 32] |= 1 << (Type % 32);
1875 }
1876
1877 CSMT_ITEM_NO_WAIT(nine_context_set_clip_plane,
1878 ARG_VAL(DWORD, Index),
1879 ARG_COPY_REF(struct nine_clipplane, pPlane))
1880 {
1881 struct nine_context *context = &device->context;
1882
1883 memcpy(&context->clip.ucp[Index][0], pPlane, sizeof(context->clip.ucp[0]));
1884 context->changed.ucp = TRUE;
1885 }
1886
1887 CSMT_ITEM_NO_WAIT(nine_context_set_swvp,
1888 ARG_VAL(boolean, swvp))
1889 {
1890 struct nine_context *context = &device->context;
1891
1892 context->swvp = swvp;
1893 context->changed.group |= NINE_STATE_SWVP;
1894 }
1895
1896 /* Do not write to nine_context directly. Slower,
1897 * but works with csmt. TODO: write a special csmt version that
1898 * would record the list of commands as much as possible,
1899 * and use the version above else.
1900 */
1901 void
1902 nine_context_apply_stateblock(struct NineDevice9 *device,
1903 const struct nine_state *src)
1904 {
1905 int i;
1906
1907 /* No need to apply src->changed.group, since all calls do
1908 * set context->changed.group */
1909
1910 for (i = 0; i < ARRAY_SIZE(src->changed.rs); ++i) {
1911 uint32_t m = src->changed.rs[i];
1912 while (m) {
1913 const int r = ffs(m) - 1;
1914 m &= ~(1 << r);
1915 nine_context_set_render_state(device, i * 32 + r, src->rs_advertised[i * 32 + r]);
1916 }
1917 }
1918
1919 /* Textures */
1920 if (src->changed.texture) {
1921 uint32_t m = src->changed.texture;
1922 unsigned s;
1923
1924 for (s = 0; m; ++s, m >>= 1) {
1925 struct NineBaseTexture9 *tex = src->texture[s];
1926 if (!(m & 1))
1927 continue;
1928 nine_context_set_texture(device, s, tex);
1929 }
1930 }
1931
1932 /* Sampler state */
1933 if (src->changed.group & NINE_STATE_SAMPLER) {
1934 unsigned s;
1935
1936 for (s = 0; s < NINE_MAX_SAMPLERS; ++s) {
1937 uint32_t m = src->changed.sampler[s];
1938 while (m) {
1939 const int i = ffs(m) - 1;
1940 m &= ~(1 << i);
1941 nine_context_set_sampler_state(device, s, i, src->samp_advertised[s][i]);
1942 }
1943 }
1944 }
1945
1946 /* Vertex buffers */
1947 if (src->changed.vtxbuf | src->changed.stream_freq) {
1948 uint32_t m = src->changed.vtxbuf | src->changed.stream_freq;
1949 for (i = 0; m; ++i, m >>= 1) {
1950 if (src->changed.vtxbuf & (1 << i))
1951 nine_context_set_stream_source(device, i, src->stream[i], src->vtxbuf[i].buffer_offset, src->vtxbuf[i].stride);
1952 if (src->changed.stream_freq & (1 << i))
1953 nine_context_set_stream_source_freq(device, i, src->stream_freq[i]);
1954 }
1955 }
1956
1957 /* Index buffer */
1958 if (src->changed.group & NINE_STATE_IDXBUF)
1959 nine_context_set_indices(device, src->idxbuf);
1960
1961 /* Vertex declaration */
1962 if ((src->changed.group & NINE_STATE_VDECL) && src->vdecl)
1963 nine_context_set_vertex_declaration(device, src->vdecl);
1964
1965 /* Vertex shader */
1966 if (src->changed.group & NINE_STATE_VS)
1967 nine_context_set_vertex_shader(device, src->vs);
1968
1969 /* Pixel shader */
1970 if (src->changed.group & NINE_STATE_PS)
1971 nine_context_set_pixel_shader(device, src->ps);
1972
1973 /* Vertex constants */
1974 if (src->changed.group & NINE_STATE_VS_CONST) {
1975 struct nine_range *r;
1976 for (r = src->changed.vs_const_f; r; r = r->next)
1977 nine_context_set_vertex_shader_constant_f(device, r->bgn,
1978 &src->vs_const_f[r->bgn * 4],
1979 sizeof(float[4]) * (r->end - r->bgn),
1980 r->end - r->bgn);
1981 for (r = src->changed.vs_const_i; r; r = r->next)
1982 nine_context_set_vertex_shader_constant_i(device, r->bgn,
1983 &src->vs_const_i[r->bgn * 4],
1984 sizeof(int[4]) * (r->end - r->bgn),
1985 r->end - r->bgn);
1986 for (r = src->changed.vs_const_b; r; r = r->next)
1987 nine_context_set_vertex_shader_constant_b(device, r->bgn,
1988 &src->vs_const_b[r->bgn * 4],
1989 sizeof(BOOL) * (r->end - r->bgn),
1990 r->end - r->bgn);
1991 }
1992
1993 /* Pixel constants */
1994 if (src->changed.group & NINE_STATE_PS_CONST) {
1995 struct nine_range *r;
1996 for (r = src->changed.ps_const_f; r; r = r->next)
1997 nine_context_set_pixel_shader_constant_f(device, r->bgn,
1998 &src->ps_const_f[r->bgn * 4],
1999 sizeof(float[4]) * (r->end - r->bgn),
2000 r->end - r->bgn);
2001 if (src->changed.ps_const_i) {
2002 uint16_t m = src->changed.ps_const_i;
2003 for (i = ffs(m) - 1, m >>= i; m; ++i, m >>= 1)
2004 if (m & 1)
2005 nine_context_set_pixel_shader_constant_i_transformed(device, i,
2006 src->ps_const_i[i], sizeof(int[4]), 1);
2007 }
2008 if (src->changed.ps_const_b) {
2009 uint16_t m = src->changed.ps_const_b;
2010 for (i = ffs(m) - 1, m >>= i; m; ++i, m >>= 1)
2011 if (m & 1)
2012 nine_context_set_pixel_shader_constant_b(device, i,
2013 &src->ps_const_b[i], sizeof(BOOL), 1);
2014 }
2015 }
2016
2017 /* Viewport */
2018 if (src->changed.group & NINE_STATE_VIEWPORT)
2019 nine_context_set_viewport(device, &src->viewport);
2020
2021 /* Scissor */
2022 if (src->changed.group & NINE_STATE_SCISSOR)
2023 nine_context_set_scissor(device, &src->scissor);
2024
2025 /* User Clip Planes */
2026 if (src->changed.ucp)
2027 for (i = 0; i < PIPE_MAX_CLIP_PLANES; ++i)
2028 if (src->changed.ucp & (1 << i))
2029 nine_context_set_clip_plane(device, i, (struct nine_clipplane*)&src->clip.ucp[i][0]);
2030
2031 if (!(src->changed.group & NINE_STATE_FF))
2032 return;
2033
2034 /* Fixed function state. */
2035
2036 if (src->changed.group & NINE_STATE_FF_MATERIAL)
2037 nine_context_set_material(device, &src->ff.material);
2038
2039 if (src->changed.group & NINE_STATE_FF_PS_CONSTS) {
2040 unsigned s;
2041 for (s = 0; s < NINE_MAX_TEXTURE_STAGES; ++s) {
2042 for (i = 0; i < NINED3DTSS_COUNT; ++i)
2043 if (src->ff.changed.tex_stage[s][i / 32] & (1 << (i % 32)))
2044 nine_context_set_texture_stage_state(device, s, i, src->ff.tex_stage[s][i]);
2045 }
2046 }
2047 if (src->changed.group & NINE_STATE_FF_LIGHTING) {
2048 for (i = 0; i < src->ff.num_lights; ++i)
2049 if (src->ff.light[i].Type != NINED3DLIGHT_INVALID)
2050 nine_context_set_light(device, i, &src->ff.light[i]);
2051
2052 nine_context_light_enable_stateblock(device, src->ff.active_light, src->ff.num_lights_active);
2053 }
2054 if (src->changed.group & NINE_STATE_FF_VSTRANSF) {
2055 for (i = 0; i < ARRAY_SIZE(src->ff.changed.transform); ++i) {
2056 unsigned s;
2057 if (!src->ff.changed.transform[i])
2058 continue;
2059 for (s = i * 32; s < (i * 32 + 32); ++s) {
2060 if (!(src->ff.changed.transform[i] & (1 << (s % 32))))
2061 continue;
2062 nine_context_set_transform(device, s,
2063 nine_state_access_transform(
2064 (struct nine_ff_state *)&src->ff,
2065 s, FALSE));
2066 }
2067 }
2068 }
2069 }
2070
2071 static void
2072 nine_update_state_framebuffer_clear(struct NineDevice9 *device)
2073 {
2074 struct nine_context *context = &device->context;
2075
2076 if (context->changed.group & NINE_STATE_FB)
2077 update_framebuffer(device, TRUE);
2078 }
2079
2080 CSMT_ITEM_NO_WAIT(nine_context_clear_fb,
2081 ARG_VAL(DWORD, Count),
2082 ARG_COPY_REF(D3DRECT, pRects),
2083 ARG_VAL(DWORD, Flags),
2084 ARG_VAL(D3DCOLOR, Color),
2085 ARG_VAL(float, Z),
2086 ARG_VAL(DWORD, Stencil))
2087 {
2088 struct nine_context *context = &device->context;
2089 const int sRGB = context->rs[D3DRS_SRGBWRITEENABLE] ? 1 : 0;
2090 struct pipe_surface *cbuf, *zsbuf;
2091 struct pipe_context *pipe = context->pipe;
2092 struct NineSurface9 *zsbuf_surf = context->ds;
2093 struct NineSurface9 *rt;
2094 unsigned bufs = 0;
2095 unsigned r, i;
2096 union pipe_color_union rgba;
2097 unsigned rt_mask = 0;
2098 D3DRECT rect;
2099
2100 nine_update_state_framebuffer_clear(device);
2101
2102 if (Flags & D3DCLEAR_TARGET) bufs |= PIPE_CLEAR_COLOR;
2103 /* Ignore Z buffer if not bound */
2104 if (context->pipe_data.fb.zsbuf != NULL) {
2105 if (Flags & D3DCLEAR_ZBUFFER) bufs |= PIPE_CLEAR_DEPTH;
2106 if (Flags & D3DCLEAR_STENCIL) bufs |= PIPE_CLEAR_STENCIL;
2107 }
2108 if (!bufs)
2109 return;
2110 d3dcolor_to_pipe_color_union(&rgba, Color);
2111
2112 rect.x1 = context->viewport.X;
2113 rect.y1 = context->viewport.Y;
2114 rect.x2 = context->viewport.Width + rect.x1;
2115 rect.y2 = context->viewport.Height + rect.y1;
2116
2117 /* Both rectangles apply, which is weird, but that's D3D9. */
2118 if (context->rs[D3DRS_SCISSORTESTENABLE]) {
2119 rect.x1 = MAX2(rect.x1, context->scissor.minx);
2120 rect.y1 = MAX2(rect.y1, context->scissor.miny);
2121 rect.x2 = MIN2(rect.x2, context->scissor.maxx);
2122 rect.y2 = MIN2(rect.y2, context->scissor.maxy);
2123 }
2124
2125 if (Count) {
2126 /* Maybe apps like to specify a large rect ? */
2127 if (pRects[0].x1 <= rect.x1 && pRects[0].x2 >= rect.x2 &&
2128 pRects[0].y1 <= rect.y1 && pRects[0].y2 >= rect.y2) {
2129 DBG("First rect covers viewport.\n");
2130 Count = 0;
2131 pRects = NULL;
2132 }
2133 }
2134
2135 if (rect.x1 >= context->pipe_data.fb.width || rect.y1 >= context->pipe_data.fb.height)
2136 return;
2137
2138 for (i = 0; i < device->caps.NumSimultaneousRTs; ++i) {
2139 if (context->rt[i] && context->rt[i]->desc.Format != D3DFMT_NULL)
2140 rt_mask |= 1 << i;
2141 }
2142
2143 /* fast path, clears everything at once */
2144 if (!Count &&
2145 (!(bufs & PIPE_CLEAR_COLOR) || (rt_mask == context->rt_mask)) &&
2146 rect.x1 == 0 && rect.y1 == 0 &&
2147 /* Case we clear only render target. Check clear region vs rt. */
2148 ((!(bufs & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)) &&
2149 rect.x2 >= context->pipe_data.fb.width &&
2150 rect.y2 >= context->pipe_data.fb.height) ||
2151 /* Case we clear depth buffer (and eventually rt too).
2152 * depth buffer size is always >= rt size. Compare to clear region */
2153 ((bufs & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)) &&
2154 rect.x2 >= zsbuf_surf->desc.Width &&
2155 rect.y2 >= zsbuf_surf->desc.Height))) {
2156 DBG("Clear fast path\n");
2157 pipe->clear(pipe, bufs, &rgba, Z, Stencil);
2158 return;
2159 }
2160
2161 if (!Count) {
2162 Count = 1;
2163 pRects = &rect;
2164 }
2165
2166 for (i = 0; i < device->caps.NumSimultaneousRTs; ++i) {
2167 rt = context->rt[i];
2168 if (!rt || rt->desc.Format == D3DFMT_NULL ||
2169 !(bufs & PIPE_CLEAR_COLOR))
2170 continue; /* save space, compiler should hoist this */
2171 cbuf = NineSurface9_GetSurface(rt, sRGB);
2172 for (r = 0; r < Count; ++r) {
2173 /* Don't trust users to pass these in the right order. */
2174 unsigned x1 = MIN2(pRects[r].x1, pRects[r].x2);
2175 unsigned y1 = MIN2(pRects[r].y1, pRects[r].y2);
2176 unsigned x2 = MAX2(pRects[r].x1, pRects[r].x2);
2177 unsigned y2 = MAX2(pRects[r].y1, pRects[r].y2);
2178 #ifndef NINE_LAX
2179 /* Drop negative rectangles (like wine expects). */
2180 if (pRects[r].x1 > pRects[r].x2) continue;
2181 if (pRects[r].y1 > pRects[r].y2) continue;
2182 #endif
2183
2184 x1 = MAX2(x1, rect.x1);
2185 y1 = MAX2(y1, rect.y1);
2186 x2 = MIN3(x2, rect.x2, rt->desc.Width);
2187 y2 = MIN3(y2, rect.y2, rt->desc.Height);
2188
2189 DBG("Clearing (%u..%u)x(%u..%u)\n", x1, x2, y1, y2);
2190 pipe->clear_render_target(pipe, cbuf, &rgba,
2191 x1, y1, x2 - x1, y2 - y1, false);
2192 }
2193 }
2194 if (!(bufs & PIPE_CLEAR_DEPTHSTENCIL))
2195 return;
2196
2197 bufs &= PIPE_CLEAR_DEPTHSTENCIL;
2198
2199 for (r = 0; r < Count; ++r) {
2200 unsigned x1 = MIN2(pRects[r].x1, pRects[r].x2);
2201 unsigned y1 = MIN2(pRects[r].y1, pRects[r].y2);
2202 unsigned x2 = MAX2(pRects[r].x1, pRects[r].x2);
2203 unsigned y2 = MAX2(pRects[r].y1, pRects[r].y2);
2204 #ifndef NINE_LAX
2205 /* Drop negative rectangles. */
2206 if (pRects[r].x1 > pRects[r].x2) continue;
2207 if (pRects[r].y1 > pRects[r].y2) continue;
2208 #endif
2209
2210 x1 = MIN2(x1, rect.x1);
2211 y1 = MIN2(y1, rect.y1);
2212 x2 = MIN3(x2, rect.x2, zsbuf_surf->desc.Width);
2213 y2 = MIN3(y2, rect.y2, zsbuf_surf->desc.Height);
2214
2215 zsbuf = NineSurface9_GetSurface(zsbuf_surf, 0);
2216 assert(zsbuf);
2217 pipe->clear_depth_stencil(pipe, zsbuf, bufs, Z, Stencil,
2218 x1, y1, x2 - x1, y2 - y1, false);
2219 }
2220 return;
2221 }
2222
2223
2224 static inline void
2225 init_draw_info(struct pipe_draw_info *info,
2226 struct NineDevice9 *dev, D3DPRIMITIVETYPE type, UINT count)
2227 {
2228 info->mode = d3dprimitivetype_to_pipe_prim(type);
2229 info->count = prim_count_to_vertex_count(type, count);
2230 info->start_instance = 0;
2231 info->instance_count = 1;
2232 if (dev->context.stream_instancedata_mask & dev->context.stream_usage_mask)
2233 info->instance_count = MAX2(dev->context.stream_freq[0] & 0x7FFFFF, 1);
2234 info->primitive_restart = FALSE;
2235 info->has_user_indices = FALSE;
2236 info->restart_index = 0;
2237 info->count_from_stream_output = NULL;
2238 info->indirect = NULL;
2239 }
2240
2241 CSMT_ITEM_NO_WAIT(nine_context_draw_primitive,
2242 ARG_VAL(D3DPRIMITIVETYPE, PrimitiveType),
2243 ARG_VAL(UINT, StartVertex),
2244 ARG_VAL(UINT, PrimitiveCount))
2245 {
2246 struct nine_context *context = &device->context;
2247 struct pipe_draw_info info;
2248
2249 nine_update_state(device);
2250
2251 init_draw_info(&info, device, PrimitiveType, PrimitiveCount);
2252 info.index_size = 0;
2253 info.start = StartVertex;
2254 info.index_bias = 0;
2255 info.min_index = info.start;
2256 info.max_index = info.count - 1;
2257 info.index.resource = NULL;
2258
2259 context->pipe->draw_vbo(context->pipe, &info);
2260 }
2261
2262 CSMT_ITEM_NO_WAIT(nine_context_draw_indexed_primitive,
2263 ARG_VAL(D3DPRIMITIVETYPE, PrimitiveType),
2264 ARG_VAL(INT, BaseVertexIndex),
2265 ARG_VAL(UINT, MinVertexIndex),
2266 ARG_VAL(UINT, NumVertices),
2267 ARG_VAL(UINT, StartIndex),
2268 ARG_VAL(UINT, PrimitiveCount))
2269 {
2270 struct nine_context *context = &device->context;
2271 struct pipe_draw_info info;
2272
2273 nine_update_state(device);
2274
2275 init_draw_info(&info, device, PrimitiveType, PrimitiveCount);
2276 info.index_size = context->index_size;
2277 info.start = context->index_offset / context->index_size + StartIndex;
2278 info.index_bias = BaseVertexIndex;
2279 /* These don't include index bias: */
2280 info.min_index = MinVertexIndex;
2281 info.max_index = MinVertexIndex + NumVertices - 1;
2282 info.index.resource = context->idxbuf;
2283
2284 context->pipe->draw_vbo(context->pipe, &info);
2285 }
2286
2287 CSMT_ITEM_NO_WAIT(nine_context_draw_primitive_from_vtxbuf,
2288 ARG_VAL(D3DPRIMITIVETYPE, PrimitiveType),
2289 ARG_VAL(UINT, PrimitiveCount),
2290 ARG_BIND_VBUF(struct pipe_vertex_buffer, vtxbuf))
2291 {
2292 struct nine_context *context = &device->context;
2293 struct pipe_draw_info info;
2294
2295 nine_update_state(device);
2296
2297 init_draw_info(&info, device, PrimitiveType, PrimitiveCount);
2298 info.index_size = 0;
2299 info.start = 0;
2300 info.index_bias = 0;
2301 info.min_index = 0;
2302 info.max_index = info.count - 1;
2303 info.index.resource = NULL;
2304
2305 context->pipe->set_vertex_buffers(context->pipe, 0, 1, vtxbuf);
2306
2307 context->pipe->draw_vbo(context->pipe, &info);
2308 }
2309
2310 CSMT_ITEM_NO_WAIT(nine_context_draw_indexed_primitive_from_vtxbuf_idxbuf,
2311 ARG_VAL(D3DPRIMITIVETYPE, PrimitiveType),
2312 ARG_VAL(UINT, MinVertexIndex),
2313 ARG_VAL(UINT, NumVertices),
2314 ARG_VAL(UINT, PrimitiveCount),
2315 ARG_BIND_VBUF(struct pipe_vertex_buffer, vbuf),
2316 ARG_BIND_RES(struct pipe_resource, ibuf),
2317 ARG_VAL(void *, user_ibuf),
2318 ARG_VAL(UINT, index_offset),
2319 ARG_VAL(UINT, index_size))
2320 {
2321 struct nine_context *context = &device->context;
2322 struct pipe_draw_info info;
2323
2324 nine_update_state(device);
2325
2326 init_draw_info(&info, device, PrimitiveType, PrimitiveCount);
2327 info.index_size = index_size;
2328 info.start = index_offset / info.index_size;
2329 info.index_bias = 0;
2330 info.min_index = MinVertexIndex;
2331 info.max_index = MinVertexIndex + NumVertices - 1;
2332 info.has_user_indices = ibuf == NULL;
2333 if (ibuf)
2334 info.index.resource = ibuf;
2335 else
2336 info.index.user = user_ibuf;
2337
2338 context->pipe->set_vertex_buffers(context->pipe, 0, 1, vbuf);
2339
2340 context->pipe->draw_vbo(context->pipe, &info);
2341 }
2342
2343 CSMT_ITEM_NO_WAIT(nine_context_resource_copy_region,
2344 ARG_BIND_REF(struct NineUnknown, dst),
2345 ARG_BIND_REF(struct NineUnknown, src),
2346 ARG_BIND_RES(struct pipe_resource, dst_res),
2347 ARG_VAL(unsigned, dst_level),
2348 ARG_COPY_REF(struct pipe_box, dst_box),
2349 ARG_BIND_RES(struct pipe_resource, src_res),
2350 ARG_VAL(unsigned, src_level),
2351 ARG_COPY_REF(struct pipe_box, src_box))
2352 {
2353 struct nine_context *context = &device->context;
2354
2355 (void) dst;
2356 (void) src;
2357
2358 context->pipe->resource_copy_region(context->pipe,
2359 dst_res, dst_level,
2360 dst_box->x, dst_box->y, dst_box->z,
2361 src_res, src_level,
2362 src_box);
2363 }
2364
2365 CSMT_ITEM_NO_WAIT(nine_context_blit,
2366 ARG_BIND_REF(struct NineUnknown, dst),
2367 ARG_BIND_REF(struct NineUnknown, src),
2368 ARG_BIND_BLIT(struct pipe_blit_info, blit))
2369 {
2370 struct nine_context *context = &device->context;
2371
2372 (void) dst;
2373 (void) src;
2374
2375 context->pipe->blit(context->pipe, blit);
2376 }
2377
2378 CSMT_ITEM_NO_WAIT(nine_context_clear_render_target,
2379 ARG_BIND_REF(struct NineSurface9, surface),
2380 ARG_VAL(D3DCOLOR, color),
2381 ARG_VAL(UINT, x),
2382 ARG_VAL(UINT, y),
2383 ARG_VAL(UINT, width),
2384 ARG_VAL(UINT, height))
2385 {
2386 struct nine_context *context = &device->context;
2387 struct pipe_surface *surf;
2388 union pipe_color_union rgba;
2389
2390 d3dcolor_to_pipe_color_union(&rgba, color);
2391 surf = NineSurface9_GetSurface(surface, 0);
2392 context->pipe->clear_render_target(context->pipe, surf, &rgba, x, y, width, height, false);
2393 }
2394
2395 CSMT_ITEM_NO_WAIT(nine_context_gen_mipmap,
2396 ARG_BIND_REF(struct NineUnknown, dst),
2397 ARG_BIND_RES(struct pipe_resource, res),
2398 ARG_VAL(UINT, base_level),
2399 ARG_VAL(UINT, last_level),
2400 ARG_VAL(UINT, first_layer),
2401 ARG_VAL(UINT, last_layer),
2402 ARG_VAL(UINT, filter))
2403 {
2404 struct nine_context *context = &device->context;
2405
2406 /* We just bind dst for the bind count */
2407 (void)dst;
2408
2409 util_gen_mipmap(context->pipe, res, res->format, base_level,
2410 last_level, first_layer, last_layer, filter);
2411 }
2412
2413 CSMT_ITEM_NO_WAIT_WITH_COUNTER(nine_context_range_upload,
2414 ARG_BIND_RES(struct pipe_resource, res),
2415 ARG_VAL(unsigned, offset),
2416 ARG_VAL(unsigned, size),
2417 ARG_VAL(const void *, data))
2418 {
2419 struct nine_context *context = &device->context;
2420
2421 context->pipe->buffer_subdata(context->pipe, res, 0, offset, size, data);
2422 }
2423
2424 CSMT_ITEM_NO_WAIT_WITH_COUNTER(nine_context_box_upload,
2425 ARG_BIND_REF(struct NineUnknown, dst),
2426 ARG_BIND_RES(struct pipe_resource, res),
2427 ARG_VAL(unsigned, level),
2428 ARG_COPY_REF(struct pipe_box, dst_box),
2429 ARG_VAL(enum pipe_format, src_format),
2430 ARG_VAL(const void *, src),
2431 ARG_VAL(unsigned, src_stride),
2432 ARG_VAL(unsigned, src_layer_stride),
2433 ARG_COPY_REF(struct pipe_box, src_box))
2434 {
2435 struct nine_context *context = &device->context;
2436 struct pipe_context *pipe = context->pipe;
2437 struct pipe_transfer *transfer = NULL;
2438 uint8_t *map;
2439
2440 /* We just bind dst for the bind count */
2441 (void)dst;
2442
2443 map = pipe->transfer_map(pipe,
2444 res,
2445 level,
2446 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_RANGE,
2447 dst_box, &transfer);
2448 if (!map)
2449 return;
2450
2451 /* Note: if formats are the sames, it will revert
2452 * to normal memcpy */
2453 (void) util_format_translate_3d(res->format,
2454 map, transfer->stride,
2455 transfer->layer_stride,
2456 0, 0, 0,
2457 src_format,
2458 src, src_stride,
2459 src_layer_stride,
2460 src_box->x, src_box->y, src_box->z,
2461 dst_box->width, dst_box->height,
2462 dst_box->depth);
2463
2464 pipe_transfer_unmap(pipe, transfer);
2465 }
2466
2467 struct pipe_query *
2468 nine_context_create_query(struct NineDevice9 *device, unsigned query_type)
2469 {
2470 struct pipe_context *pipe;
2471 struct pipe_query *res;
2472
2473 pipe = nine_context_get_pipe_acquire(device);
2474 res = pipe->create_query(pipe, query_type, 0);
2475 nine_context_get_pipe_release(device);
2476 return res;
2477 }
2478
2479 CSMT_ITEM_DO_WAIT(nine_context_destroy_query,
2480 ARG_REF(struct pipe_query, query))
2481 {
2482 struct nine_context *context = &device->context;
2483
2484 context->pipe->destroy_query(context->pipe, query);
2485 }
2486
2487 CSMT_ITEM_NO_WAIT_WITH_COUNTER(nine_context_begin_query,
2488 ARG_REF(struct pipe_query, query))
2489 {
2490 struct nine_context *context = &device->context;
2491
2492 (void) context->pipe->begin_query(context->pipe, query);
2493 }
2494
2495 CSMT_ITEM_NO_WAIT_WITH_COUNTER(nine_context_end_query,
2496 ARG_REF(struct pipe_query, query))
2497 {
2498 struct nine_context *context = &device->context;
2499
2500 (void) context->pipe->end_query(context->pipe, query);
2501 }
2502
2503 boolean
2504 nine_context_get_query_result(struct NineDevice9 *device, struct pipe_query *query,
2505 unsigned *counter, boolean flush, boolean wait,
2506 union pipe_query_result *result)
2507 {
2508 struct pipe_context *pipe;
2509 boolean ret;
2510
2511 if (wait)
2512 nine_csmt_process(device);
2513 else if (p_atomic_read(counter) > 0) {
2514 if (flush && device->csmt_active)
2515 nine_queue_flush(device->csmt_ctx->pool);
2516 DBG("Pending begin/end. Returning\n");
2517 return false;
2518 }
2519
2520 pipe = nine_context_get_pipe_acquire(device);
2521 ret = pipe->get_query_result(pipe, query, wait, result);
2522 nine_context_get_pipe_release(device);
2523
2524 DBG("Query result %s\n", ret ? "found" : "not yet available");
2525 return ret;
2526 }
2527
2528 /* State defaults */
2529
2530 static const DWORD nine_render_state_defaults[NINED3DRS_LAST + 1] =
2531 {
2532 /* [D3DRS_ZENABLE] = D3DZB_TRUE; wine: auto_depth_stencil */
2533 [D3DRS_ZENABLE] = D3DZB_FALSE,
2534 [D3DRS_FILLMODE] = D3DFILL_SOLID,
2535 [D3DRS_SHADEMODE] = D3DSHADE_GOURAUD,
2536 /* [D3DRS_LINEPATTERN] = 0x00000000, */
2537 [D3DRS_ZWRITEENABLE] = TRUE,
2538 [D3DRS_ALPHATESTENABLE] = FALSE,
2539 [D3DRS_LASTPIXEL] = TRUE,
2540 [D3DRS_SRCBLEND] = D3DBLEND_ONE,
2541 [D3DRS_DESTBLEND] = D3DBLEND_ZERO,
2542 [D3DRS_CULLMODE] = D3DCULL_CCW,
2543 [D3DRS_ZFUNC] = D3DCMP_LESSEQUAL,
2544 [D3DRS_ALPHAFUNC] = D3DCMP_ALWAYS,
2545 [D3DRS_ALPHAREF] = 0,
2546 [D3DRS_DITHERENABLE] = FALSE,
2547 [D3DRS_ALPHABLENDENABLE] = FALSE,
2548 [D3DRS_FOGENABLE] = FALSE,
2549 [D3DRS_SPECULARENABLE] = FALSE,
2550 /* [D3DRS_ZVISIBLE] = 0, */
2551 [D3DRS_FOGCOLOR] = 0,
2552 [D3DRS_FOGTABLEMODE] = D3DFOG_NONE,
2553 [D3DRS_FOGSTART] = 0x00000000,
2554 [D3DRS_FOGEND] = 0x3F800000,
2555 [D3DRS_FOGDENSITY] = 0x3F800000,
2556 /* [D3DRS_EDGEANTIALIAS] = FALSE, */
2557 [D3DRS_RANGEFOGENABLE] = FALSE,
2558 [D3DRS_STENCILENABLE] = FALSE,
2559 [D3DRS_STENCILFAIL] = D3DSTENCILOP_KEEP,
2560 [D3DRS_STENCILZFAIL] = D3DSTENCILOP_KEEP,
2561 [D3DRS_STENCILPASS] = D3DSTENCILOP_KEEP,
2562 [D3DRS_STENCILREF] = 0,
2563 [D3DRS_STENCILMASK] = 0xFFFFFFFF,
2564 [D3DRS_STENCILFUNC] = D3DCMP_ALWAYS,
2565 [D3DRS_STENCILWRITEMASK] = 0xFFFFFFFF,
2566 [D3DRS_TEXTUREFACTOR] = 0xFFFFFFFF,
2567 [D3DRS_WRAP0] = 0,
2568 [D3DRS_WRAP1] = 0,
2569 [D3DRS_WRAP2] = 0,
2570 [D3DRS_WRAP3] = 0,
2571 [D3DRS_WRAP4] = 0,
2572 [D3DRS_WRAP5] = 0,
2573 [D3DRS_WRAP6] = 0,
2574 [D3DRS_WRAP7] = 0,
2575 [D3DRS_CLIPPING] = TRUE,
2576 [D3DRS_LIGHTING] = TRUE,
2577 [D3DRS_AMBIENT] = 0,
2578 [D3DRS_FOGVERTEXMODE] = D3DFOG_NONE,
2579 [D3DRS_COLORVERTEX] = TRUE,
2580 [D3DRS_LOCALVIEWER] = TRUE,
2581 [D3DRS_NORMALIZENORMALS] = FALSE,
2582 [D3DRS_DIFFUSEMATERIALSOURCE] = D3DMCS_COLOR1,
2583 [D3DRS_SPECULARMATERIALSOURCE] = D3DMCS_COLOR2,
2584 [D3DRS_AMBIENTMATERIALSOURCE] = D3DMCS_MATERIAL,
2585 [D3DRS_EMISSIVEMATERIALSOURCE] = D3DMCS_MATERIAL,
2586 [D3DRS_VERTEXBLEND] = D3DVBF_DISABLE,
2587 [D3DRS_CLIPPLANEENABLE] = 0,
2588 /* [D3DRS_SOFTWAREVERTEXPROCESSING] = FALSE, */
2589 [D3DRS_POINTSIZE] = 0x3F800000,
2590 [D3DRS_POINTSIZE_MIN] = 0x3F800000,
2591 [D3DRS_POINTSPRITEENABLE] = FALSE,
2592 [D3DRS_POINTSCALEENABLE] = FALSE,
2593 [D3DRS_POINTSCALE_A] = 0x3F800000,
2594 [D3DRS_POINTSCALE_B] = 0x00000000,
2595 [D3DRS_POINTSCALE_C] = 0x00000000,
2596 [D3DRS_MULTISAMPLEANTIALIAS] = TRUE,
2597 [D3DRS_MULTISAMPLEMASK] = 0xFFFFFFFF,
2598 [D3DRS_PATCHEDGESTYLE] = D3DPATCHEDGE_DISCRETE,
2599 /* [D3DRS_PATCHSEGMENTS] = 0x3F800000, */
2600 [D3DRS_DEBUGMONITORTOKEN] = 0xDEADCAFE,
2601 [D3DRS_POINTSIZE_MAX] = 0x3F800000, /* depends on cap */
2602 [D3DRS_INDEXEDVERTEXBLENDENABLE] = FALSE,
2603 [D3DRS_COLORWRITEENABLE] = 0x0000000f,
2604 [D3DRS_TWEENFACTOR] = 0x00000000,
2605 [D3DRS_BLENDOP] = D3DBLENDOP_ADD,
2606 [D3DRS_POSITIONDEGREE] = D3DDEGREE_CUBIC,
2607 [D3DRS_NORMALDEGREE] = D3DDEGREE_LINEAR,
2608 [D3DRS_SCISSORTESTENABLE] = FALSE,
2609 [D3DRS_SLOPESCALEDEPTHBIAS] = 0,
2610 [D3DRS_MINTESSELLATIONLEVEL] = 0x3F800000,
2611 [D3DRS_MAXTESSELLATIONLEVEL] = 0x3F800000,
2612 [D3DRS_ANTIALIASEDLINEENABLE] = FALSE,
2613 [D3DRS_ADAPTIVETESS_X] = 0x00000000,
2614 [D3DRS_ADAPTIVETESS_Y] = 0x00000000,
2615 [D3DRS_ADAPTIVETESS_Z] = 0x3F800000,
2616 [D3DRS_ADAPTIVETESS_W] = 0x00000000,
2617 [D3DRS_ENABLEADAPTIVETESSELLATION] = FALSE,
2618 [D3DRS_TWOSIDEDSTENCILMODE] = FALSE,
2619 [D3DRS_CCW_STENCILFAIL] = D3DSTENCILOP_KEEP,
2620 [D3DRS_CCW_STENCILZFAIL] = D3DSTENCILOP_KEEP,
2621 [D3DRS_CCW_STENCILPASS] = D3DSTENCILOP_KEEP,
2622 [D3DRS_CCW_STENCILFUNC] = D3DCMP_ALWAYS,
2623 [D3DRS_COLORWRITEENABLE1] = 0x0000000F,
2624 [D3DRS_COLORWRITEENABLE2] = 0x0000000F,
2625 [D3DRS_COLORWRITEENABLE3] = 0x0000000F,
2626 [D3DRS_BLENDFACTOR] = 0xFFFFFFFF,
2627 [D3DRS_SRGBWRITEENABLE] = 0,
2628 [D3DRS_DEPTHBIAS] = 0,
2629 [D3DRS_WRAP8] = 0,
2630 [D3DRS_WRAP9] = 0,
2631 [D3DRS_WRAP10] = 0,
2632 [D3DRS_WRAP11] = 0,
2633 [D3DRS_WRAP12] = 0,
2634 [D3DRS_WRAP13] = 0,
2635 [D3DRS_WRAP14] = 0,
2636 [D3DRS_WRAP15] = 0,
2637 [D3DRS_SEPARATEALPHABLENDENABLE] = FALSE,
2638 [D3DRS_SRCBLENDALPHA] = D3DBLEND_ONE,
2639 [D3DRS_DESTBLENDALPHA] = D3DBLEND_ZERO,
2640 [D3DRS_BLENDOPALPHA] = D3DBLENDOP_ADD,
2641 [NINED3DRS_VSPOINTSIZE] = FALSE,
2642 [NINED3DRS_RTMASK] = 0xf,
2643 [NINED3DRS_ALPHACOVERAGE] = FALSE,
2644 [NINED3DRS_MULTISAMPLE] = FALSE
2645 };
2646 static const DWORD nine_tex_stage_state_defaults[NINED3DTSS_LAST + 1] =
2647 {
2648 [D3DTSS_COLOROP] = D3DTOP_DISABLE,
2649 [D3DTSS_ALPHAOP] = D3DTOP_DISABLE,
2650 [D3DTSS_COLORARG1] = D3DTA_TEXTURE,
2651 [D3DTSS_COLORARG2] = D3DTA_CURRENT,
2652 [D3DTSS_COLORARG0] = D3DTA_CURRENT,
2653 [D3DTSS_ALPHAARG1] = D3DTA_TEXTURE,
2654 [D3DTSS_ALPHAARG2] = D3DTA_CURRENT,
2655 [D3DTSS_ALPHAARG0] = D3DTA_CURRENT,
2656 [D3DTSS_RESULTARG] = D3DTA_CURRENT,
2657 [D3DTSS_BUMPENVMAT00] = 0,
2658 [D3DTSS_BUMPENVMAT01] = 0,
2659 [D3DTSS_BUMPENVMAT10] = 0,
2660 [D3DTSS_BUMPENVMAT11] = 0,
2661 [D3DTSS_BUMPENVLSCALE] = 0,
2662 [D3DTSS_BUMPENVLOFFSET] = 0,
2663 [D3DTSS_TEXCOORDINDEX] = 0,
2664 [D3DTSS_TEXTURETRANSFORMFLAGS] = D3DTTFF_DISABLE,
2665 };
2666 static const DWORD nine_samp_state_defaults[NINED3DSAMP_LAST + 1] =
2667 {
2668 [D3DSAMP_ADDRESSU] = D3DTADDRESS_WRAP,
2669 [D3DSAMP_ADDRESSV] = D3DTADDRESS_WRAP,
2670 [D3DSAMP_ADDRESSW] = D3DTADDRESS_WRAP,
2671 [D3DSAMP_BORDERCOLOR] = 0,
2672 [D3DSAMP_MAGFILTER] = D3DTEXF_POINT,
2673 [D3DSAMP_MINFILTER] = D3DTEXF_POINT,
2674 [D3DSAMP_MIPFILTER] = D3DTEXF_NONE,
2675 [D3DSAMP_MIPMAPLODBIAS] = 0,
2676 [D3DSAMP_MAXMIPLEVEL] = 0,
2677 [D3DSAMP_MAXANISOTROPY] = 1,
2678 [D3DSAMP_SRGBTEXTURE] = 0,
2679 [D3DSAMP_ELEMENTINDEX] = 0,
2680 [D3DSAMP_DMAPOFFSET] = 0,
2681 [NINED3DSAMP_MINLOD] = 0,
2682 [NINED3DSAMP_SHADOW] = 0,
2683 [NINED3DSAMP_CUBETEX] = 0
2684 };
2685
2686 /* Note: The following 4 functions assume there is no
2687 * pending commands */
2688
2689 void nine_state_restore_non_cso(struct NineDevice9 *device)
2690 {
2691 struct nine_context *context = &device->context;
2692
2693 context->changed.group = NINE_STATE_ALL;
2694 context->changed.vtxbuf = (1ULL << device->caps.MaxStreams) - 1;
2695 context->changed.ucp = TRUE;
2696 context->commit |= NINE_STATE_COMMIT_CONST_VS | NINE_STATE_COMMIT_CONST_PS;
2697 }
2698
2699 void
2700 nine_state_set_defaults(struct NineDevice9 *device, const D3DCAPS9 *caps,
2701 boolean is_reset)
2702 {
2703 struct nine_state *state = &device->state;
2704 struct nine_context *context = &device->context;
2705 unsigned s;
2706
2707 /* Initialize defaults.
2708 */
2709 memcpy(context->rs, nine_render_state_defaults, sizeof(context->rs));
2710
2711 for (s = 0; s < ARRAY_SIZE(state->ff.tex_stage); ++s) {
2712 memcpy(&state->ff.tex_stage[s], nine_tex_stage_state_defaults,
2713 sizeof(state->ff.tex_stage[s]));
2714 state->ff.tex_stage[s][D3DTSS_TEXCOORDINDEX] = s;
2715 }
2716 state->ff.tex_stage[0][D3DTSS_COLOROP] = D3DTOP_MODULATE;
2717 state->ff.tex_stage[0][D3DTSS_ALPHAOP] = D3DTOP_SELECTARG1;
2718
2719 for (s = 0; s < ARRAY_SIZE(state->ff.tex_stage); ++s)
2720 memcpy(&context->ff.tex_stage[s], state->ff.tex_stage[s],
2721 sizeof(state->ff.tex_stage[s]));
2722
2723 memset(&context->bumpmap_vars, 0, sizeof(context->bumpmap_vars));
2724
2725 for (s = 0; s < NINE_MAX_SAMPLERS; ++s) {
2726 memcpy(&context->samp[s], nine_samp_state_defaults,
2727 sizeof(context->samp[s]));
2728 memcpy(&state->samp_advertised[s], nine_samp_state_defaults,
2729 sizeof(state->samp_advertised[s]));
2730 }
2731
2732 memset(state->vs_const_f, 0, VS_CONST_F_SIZE(device));
2733 memset(context->vs_const_f, 0, device->vs_const_size);
2734 if (context->vs_const_f_swvp)
2735 memset(context->vs_const_f_swvp, 0, NINE_MAX_CONST_F_SWVP * sizeof(float[4]));
2736 memset(state->vs_const_i, 0, VS_CONST_I_SIZE(device));
2737 memset(context->vs_const_i, 0, VS_CONST_I_SIZE(device));
2738 memset(state->vs_const_b, 0, VS_CONST_B_SIZE(device));
2739 memset(context->vs_const_b, 0, VS_CONST_B_SIZE(device));
2740 memset(state->ps_const_f, 0, device->ps_const_size);
2741 memset(context->ps_const_f, 0, device->ps_const_size);
2742 memset(state->ps_const_i, 0, sizeof(state->ps_const_i));
2743 memset(context->ps_const_i, 0, sizeof(context->ps_const_i));
2744 memset(state->ps_const_b, 0, sizeof(state->ps_const_b));
2745 memset(context->ps_const_b, 0, sizeof(context->ps_const_b));
2746
2747 /* Cap dependent initial state:
2748 */
2749 context->rs[D3DRS_POINTSIZE_MAX] = fui(caps->MaxPointSize);
2750
2751 memcpy(state->rs_advertised, context->rs, sizeof(context->rs));
2752
2753 /* Set changed flags to initialize driver.
2754 */
2755 context->changed.group = NINE_STATE_ALL;
2756 context->changed.vtxbuf = (1ULL << device->caps.MaxStreams) - 1;
2757 context->changed.ucp = TRUE;
2758
2759 context->ff.changed.transform[0] = ~0;
2760 context->ff.changed.transform[D3DTS_WORLD / 32] |= 1 << (D3DTS_WORLD % 32);
2761
2762 if (!is_reset) {
2763 state->viewport.MinZ = context->viewport.MinZ = 0.0f;
2764 state->viewport.MaxZ = context->viewport.MaxZ = 1.0f;
2765 }
2766
2767 for (s = 0; s < NINE_MAX_SAMPLERS; ++s)
2768 context->changed.sampler[s] = ~0;
2769
2770 if (!is_reset) {
2771 context->dummy_vbo_bound_at = -1;
2772 context->vbo_bound_done = FALSE;
2773 }
2774 }
2775
2776 void
2777 nine_state_clear(struct nine_state *state, const boolean device)
2778 {
2779 unsigned i;
2780
2781 for (i = 0; i < ARRAY_SIZE(state->rt); ++i)
2782 nine_bind(&state->rt[i], NULL);
2783 nine_bind(&state->ds, NULL);
2784 nine_bind(&state->vs, NULL);
2785 nine_bind(&state->ps, NULL);
2786 nine_bind(&state->vdecl, NULL);
2787 for (i = 0; i < PIPE_MAX_ATTRIBS; ++i)
2788 nine_bind(&state->stream[i], NULL);
2789
2790 nine_bind(&state->idxbuf, NULL);
2791 for (i = 0; i < NINE_MAX_SAMPLERS; ++i) {
2792 if (device &&
2793 state->texture[i] &&
2794 --state->texture[i]->bind_count == 0)
2795 list_delinit(&state->texture[i]->list);
2796 nine_bind(&state->texture[i], NULL);
2797 }
2798 }
2799
2800 void
2801 nine_context_clear(struct NineDevice9 *device)
2802 {
2803 struct nine_context *context = &device->context;
2804 struct pipe_context *pipe = context->pipe;
2805 struct cso_context *cso = context->cso;
2806 unsigned i;
2807
2808 /* Early device ctor failure. Nothing to do */
2809 if (!pipe || !cso)
2810 return;
2811
2812 pipe->bind_vs_state(pipe, NULL);
2813 pipe->bind_fs_state(pipe, NULL);
2814
2815 /* Don't unbind constant buffers, they're device-private and
2816 * do not change on Reset.
2817 */
2818
2819 cso_set_samplers(cso, PIPE_SHADER_VERTEX, 0, NULL);
2820 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, 0, NULL);
2821
2822 cso_set_sampler_views(cso, PIPE_SHADER_VERTEX, 0, NULL);
2823 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, 0, NULL);
2824
2825 pipe->set_vertex_buffers(pipe, 0, device->caps.MaxStreams, NULL);
2826
2827 for (i = 0; i < ARRAY_SIZE(context->rt); ++i)
2828 nine_bind(&context->rt[i], NULL);
2829 nine_bind(&context->ds, NULL);
2830 nine_bind(&context->vs, NULL);
2831 nine_bind(&context->ps, NULL);
2832 nine_bind(&context->vdecl, NULL);
2833 for (i = 0; i < PIPE_MAX_ATTRIBS; ++i)
2834 pipe_vertex_buffer_unreference(&context->vtxbuf[i]);
2835 pipe_resource_reference(&context->idxbuf, NULL);
2836
2837 for (i = 0; i < NINE_MAX_SAMPLERS; ++i) {
2838 context->texture[i].enabled = FALSE;
2839 pipe_resource_reference(&context->texture[i].resource,
2840 NULL);
2841 pipe_sampler_view_reference(&context->texture[i].view[0],
2842 NULL);
2843 pipe_sampler_view_reference(&context->texture[i].view[1],
2844 NULL);
2845 }
2846 }
2847
2848 void
2849 nine_state_init_sw(struct NineDevice9 *device)
2850 {
2851 struct pipe_context *pipe_sw = device->pipe_sw;
2852 struct pipe_rasterizer_state rast;
2853 struct pipe_blend_state blend;
2854 struct pipe_depth_stencil_alpha_state dsa;
2855 struct pipe_framebuffer_state fb;
2856
2857 /* Only used with Streamout */
2858 memset(&rast, 0, sizeof(rast));
2859 rast.rasterizer_discard = true;
2860 rast.point_quad_rasterization = 1; /* to make llvmpipe happy */
2861 cso_set_rasterizer(device->cso_sw, &rast);
2862
2863 /* dummy settings */
2864 memset(&blend, 0, sizeof(blend));
2865 memset(&dsa, 0, sizeof(dsa));
2866 memset(&fb, 0, sizeof(fb));
2867 cso_set_blend(device->cso_sw, &blend);
2868 cso_set_depth_stencil_alpha(device->cso_sw, &dsa);
2869 cso_set_framebuffer(device->cso_sw, &fb);
2870 cso_set_viewport_dims(device->cso_sw, 1.0, 1.0, false);
2871 cso_set_fragment_shader_handle(device->cso_sw, util_make_empty_fragment_shader(pipe_sw));
2872 }
2873
2874 /* There is duplication with update_vertex_elements.
2875 * TODO: Share the code */
2876
2877 static void
2878 update_vertex_elements_sw(struct NineDevice9 *device)
2879 {
2880 struct nine_state *state = &device->state;
2881 const struct NineVertexDeclaration9 *vdecl = device->state.vdecl;
2882 const struct NineVertexShader9 *vs;
2883 unsigned n, b, i;
2884 int index;
2885 char vdecl_index_map[16]; /* vs->num_inputs <= 16 */
2886 char used_streams[device->caps.MaxStreams];
2887 int dummy_vbo_stream = -1;
2888 BOOL need_dummy_vbo = FALSE;
2889 struct pipe_vertex_element ve[PIPE_MAX_ATTRIBS];
2890 bool programmable_vs = state->vs && !(state->vdecl && state->vdecl->position_t);
2891
2892 memset(vdecl_index_map, -1, 16);
2893 memset(used_streams, 0, device->caps.MaxStreams);
2894 vs = programmable_vs ? device->state.vs : device->ff.vs;
2895
2896 if (vdecl) {
2897 for (n = 0; n < vs->num_inputs; ++n) {
2898 DBG("looking up input %u (usage %u) from vdecl(%p)\n",
2899 n, vs->input_map[n].ndecl, vdecl);
2900
2901 for (i = 0; i < vdecl->nelems; i++) {
2902 if (vdecl->usage_map[i] == vs->input_map[n].ndecl) {
2903 vdecl_index_map[n] = i;
2904 used_streams[vdecl->elems[i].vertex_buffer_index] = 1;
2905 break;
2906 }
2907 }
2908 if (vdecl_index_map[n] < 0)
2909 need_dummy_vbo = TRUE;
2910 }
2911 } else {
2912 /* No vertex declaration. Likely will never happen in practice,
2913 * but we need not crash on this */
2914 need_dummy_vbo = TRUE;
2915 }
2916
2917 if (need_dummy_vbo) {
2918 for (i = 0; i < device->caps.MaxStreams; i++ ) {
2919 if (!used_streams[i]) {
2920 dummy_vbo_stream = i;
2921 break;
2922 }
2923 }
2924 }
2925 /* TODO handle dummy_vbo */
2926 assert (!need_dummy_vbo);
2927
2928 for (n = 0; n < vs->num_inputs; ++n) {
2929 index = vdecl_index_map[n];
2930 if (index >= 0) {
2931 ve[n] = vdecl->elems[index];
2932 b = ve[n].vertex_buffer_index;
2933 /* XXX wine just uses 1 here: */
2934 if (state->stream_freq[b] & D3DSTREAMSOURCE_INSTANCEDATA)
2935 ve[n].instance_divisor = state->stream_freq[b] & 0x7FFFFF;
2936 } else {
2937 /* if the vertex declaration is incomplete compared to what the
2938 * vertex shader needs, we bind a dummy vbo with 0 0 0 0.
2939 * This is not precised by the spec, but is the behaviour
2940 * tested on win */
2941 ve[n].vertex_buffer_index = dummy_vbo_stream;
2942 ve[n].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
2943 ve[n].src_offset = 0;
2944 ve[n].instance_divisor = 0;
2945 }
2946 }
2947
2948 cso_set_vertex_elements(device->cso_sw, vs->num_inputs, ve);
2949 }
2950
2951 static void
2952 update_vertex_buffers_sw(struct NineDevice9 *device, int start_vertice, int num_vertices)
2953 {
2954 struct pipe_context *pipe = nine_context_get_pipe_acquire(device);
2955 struct pipe_context *pipe_sw = device->pipe_sw;
2956 struct nine_state *state = &device->state;
2957 struct nine_state_sw_internal *sw_internal = &device->state_sw_internal;
2958 struct pipe_vertex_buffer vtxbuf;
2959 uint32_t mask = 0xf;
2960 unsigned i;
2961
2962 DBG("mask=%x\n", mask);
2963
2964 /* TODO: handle dummy_vbo_bound_at */
2965
2966 for (i = 0; mask; mask >>= 1, ++i) {
2967 if (mask & 1) {
2968 if (state->stream[i]) {
2969 unsigned offset;
2970 struct pipe_resource *buf;
2971 struct pipe_box box;
2972 void *userbuf;
2973
2974 vtxbuf = state->vtxbuf[i];
2975 buf = NineVertexBuffer9_GetResource(state->stream[i], &offset);
2976
2977 DBG("Locking %p (offset %d, length %d)\n", buf,
2978 vtxbuf.buffer_offset, num_vertices * vtxbuf.stride);
2979
2980 u_box_1d(vtxbuf.buffer_offset + offset + start_vertice * vtxbuf.stride,
2981 num_vertices * vtxbuf.stride, &box);
2982
2983 userbuf = pipe->transfer_map(pipe, buf, 0, PIPE_TRANSFER_READ, &box,
2984 &(sw_internal->transfers_so[i]));
2985 vtxbuf.is_user_buffer = true;
2986 vtxbuf.buffer.user = userbuf;
2987
2988 if (!device->driver_caps.user_sw_vbufs) {
2989 vtxbuf.buffer.resource = NULL;
2990 vtxbuf.is_user_buffer = false;
2991 u_upload_data(device->pipe_sw->stream_uploader,
2992 0,
2993 box.width,
2994 16,
2995 userbuf,
2996 &(vtxbuf.buffer_offset),
2997 &(vtxbuf.buffer.resource));
2998 u_upload_unmap(device->pipe_sw->stream_uploader);
2999 }
3000 pipe_sw->set_vertex_buffers(pipe_sw, i, 1, &vtxbuf);
3001 pipe_vertex_buffer_unreference(&vtxbuf);
3002 } else
3003 pipe_sw->set_vertex_buffers(pipe_sw, i, 1, NULL);
3004 }
3005 }
3006 nine_context_get_pipe_release(device);
3007 }
3008
3009 static void
3010 update_vs_constants_sw(struct NineDevice9 *device)
3011 {
3012 struct nine_state *state = &device->state;
3013 struct pipe_context *pipe_sw = device->pipe_sw;
3014
3015 DBG("updating\n");
3016
3017 {
3018 struct pipe_constant_buffer cb;
3019 const void *buf;
3020
3021 cb.buffer = NULL;
3022 cb.buffer_offset = 0;
3023 cb.buffer_size = 4096 * sizeof(float[4]);
3024 cb.user_buffer = state->vs_const_f;
3025
3026 if (state->vs->lconstf.ranges) {
3027 const struct nine_lconstf *lconstf = &device->state.vs->lconstf;
3028 const struct nine_range *r = lconstf->ranges;
3029 unsigned n = 0;
3030 float *dst = device->state.vs_lconstf_temp;
3031 float *src = (float *)cb.user_buffer;
3032 memcpy(dst, src, 8192 * sizeof(float[4]));
3033 while (r) {
3034 unsigned p = r->bgn;
3035 unsigned c = r->end - r->bgn;
3036 memcpy(&dst[p * 4], &lconstf->data[n * 4], c * 4 * sizeof(float));
3037 n += c;
3038 r = r->next;
3039 }
3040 cb.user_buffer = dst;
3041 }
3042
3043 buf = cb.user_buffer;
3044
3045 pipe_sw->set_constant_buffer(pipe_sw, PIPE_SHADER_VERTEX, 0, &cb);
3046 if (cb.buffer)
3047 pipe_resource_reference(&cb.buffer, NULL);
3048
3049 cb.user_buffer = (char *)buf + 4096 * sizeof(float[4]);
3050
3051 pipe_sw->set_constant_buffer(pipe_sw, PIPE_SHADER_VERTEX, 1, &cb);
3052 if (cb.buffer)
3053 pipe_resource_reference(&cb.buffer, NULL);
3054 }
3055
3056 {
3057 struct pipe_constant_buffer cb;
3058
3059 cb.buffer = NULL;
3060 cb.buffer_offset = 0;
3061 cb.buffer_size = 2048 * sizeof(float[4]);
3062 cb.user_buffer = state->vs_const_i;
3063
3064 pipe_sw->set_constant_buffer(pipe_sw, PIPE_SHADER_VERTEX, 2, &cb);
3065 if (cb.buffer)
3066 pipe_resource_reference(&cb.buffer, NULL);
3067 }
3068
3069 {
3070 struct pipe_constant_buffer cb;
3071
3072 cb.buffer = NULL;
3073 cb.buffer_offset = 0;
3074 cb.buffer_size = 512 * sizeof(float[4]);
3075 cb.user_buffer = state->vs_const_b;
3076
3077 pipe_sw->set_constant_buffer(pipe_sw, PIPE_SHADER_VERTEX, 3, &cb);
3078 if (cb.buffer)
3079 pipe_resource_reference(&cb.buffer, NULL);
3080 }
3081
3082 {
3083 struct pipe_constant_buffer cb;
3084 const D3DVIEWPORT9 *vport = &device->state.viewport;
3085 float viewport_data[8] = {(float)vport->Width * 0.5f,
3086 (float)vport->Height * -0.5f, vport->MaxZ - vport->MinZ, 0.f,
3087 (float)vport->Width * 0.5f + (float)vport->X,
3088 (float)vport->Height * 0.5f + (float)vport->Y,
3089 vport->MinZ, 0.f};
3090
3091 cb.buffer = NULL;
3092 cb.buffer_offset = 0;
3093 cb.buffer_size = 2 * sizeof(float[4]);
3094 cb.user_buffer = viewport_data;
3095
3096 {
3097 u_upload_data(device->pipe_sw->const_uploader,
3098 0,
3099 cb.buffer_size,
3100 16,
3101 cb.user_buffer,
3102 &(cb.buffer_offset),
3103 &(cb.buffer));
3104 u_upload_unmap(device->pipe_sw->const_uploader);
3105 cb.user_buffer = NULL;
3106 }
3107
3108 pipe_sw->set_constant_buffer(pipe_sw, PIPE_SHADER_VERTEX, 4, &cb);
3109 if (cb.buffer)
3110 pipe_resource_reference(&cb.buffer, NULL);
3111 }
3112
3113 }
3114
3115 void
3116 nine_state_prepare_draw_sw(struct NineDevice9 *device, struct NineVertexDeclaration9 *vdecl_out,
3117 int start_vertice, int num_vertices, struct pipe_stream_output_info *so)
3118 {
3119 struct nine_state *state = &device->state;
3120 bool programmable_vs = state->vs && !(state->vdecl && state->vdecl->position_t);
3121 struct NineVertexShader9 *vs = programmable_vs ? device->state.vs : device->ff.vs;
3122
3123 assert(programmable_vs);
3124
3125 DBG("Preparing draw\n");
3126 cso_set_vertex_shader_handle(device->cso_sw,
3127 NineVertexShader9_GetVariantProcessVertices(vs, vdecl_out, so));
3128 update_vertex_elements_sw(device);
3129 update_vertex_buffers_sw(device, start_vertice, num_vertices);
3130 update_vs_constants_sw(device);
3131 DBG("Preparation succeeded\n");
3132 }
3133
3134 void
3135 nine_state_after_draw_sw(struct NineDevice9 *device)
3136 {
3137 struct nine_state_sw_internal *sw_internal = &device->state_sw_internal;
3138 struct pipe_context *pipe = nine_context_get_pipe_acquire(device);
3139 struct pipe_context *pipe_sw = device->pipe_sw;
3140 int i;
3141
3142 for (i = 0; i < 4; i++) {
3143 pipe_sw->set_vertex_buffers(pipe_sw, i, 1, NULL);
3144 if (sw_internal->transfers_so[i])
3145 pipe->transfer_unmap(pipe, sw_internal->transfers_so[i]);
3146 sw_internal->transfers_so[i] = NULL;
3147 }
3148 nine_context_get_pipe_release(device);
3149 }
3150
3151 void
3152 nine_state_destroy_sw(struct NineDevice9 *device)
3153 {
3154 (void) device;
3155 /* Everything destroyed with cso */
3156 }
3157
3158 /*
3159 static const DWORD nine_render_states_pixel[] =
3160 {
3161 D3DRS_ALPHABLENDENABLE,
3162 D3DRS_ALPHAFUNC,
3163 D3DRS_ALPHAREF,
3164 D3DRS_ALPHATESTENABLE,
3165 D3DRS_ANTIALIASEDLINEENABLE,
3166 D3DRS_BLENDFACTOR,
3167 D3DRS_BLENDOP,
3168 D3DRS_BLENDOPALPHA,
3169 D3DRS_CCW_STENCILFAIL,
3170 D3DRS_CCW_STENCILPASS,
3171 D3DRS_CCW_STENCILZFAIL,
3172 D3DRS_COLORWRITEENABLE,
3173 D3DRS_COLORWRITEENABLE1,
3174 D3DRS_COLORWRITEENABLE2,
3175 D3DRS_COLORWRITEENABLE3,
3176 D3DRS_DEPTHBIAS,
3177 D3DRS_DESTBLEND,
3178 D3DRS_DESTBLENDALPHA,
3179 D3DRS_DITHERENABLE,
3180 D3DRS_FILLMODE,
3181 D3DRS_FOGDENSITY,
3182 D3DRS_FOGEND,
3183 D3DRS_FOGSTART,
3184 D3DRS_LASTPIXEL,
3185 D3DRS_SCISSORTESTENABLE,
3186 D3DRS_SEPARATEALPHABLENDENABLE,
3187 D3DRS_SHADEMODE,
3188 D3DRS_SLOPESCALEDEPTHBIAS,
3189 D3DRS_SRCBLEND,
3190 D3DRS_SRCBLENDALPHA,
3191 D3DRS_SRGBWRITEENABLE,
3192 D3DRS_STENCILENABLE,
3193 D3DRS_STENCILFAIL,
3194 D3DRS_STENCILFUNC,
3195 D3DRS_STENCILMASK,
3196 D3DRS_STENCILPASS,
3197 D3DRS_STENCILREF,
3198 D3DRS_STENCILWRITEMASK,
3199 D3DRS_STENCILZFAIL,
3200 D3DRS_TEXTUREFACTOR,
3201 D3DRS_TWOSIDEDSTENCILMODE,
3202 D3DRS_WRAP0,
3203 D3DRS_WRAP1,
3204 D3DRS_WRAP10,
3205 D3DRS_WRAP11,
3206 D3DRS_WRAP12,
3207 D3DRS_WRAP13,
3208 D3DRS_WRAP14,
3209 D3DRS_WRAP15,
3210 D3DRS_WRAP2,
3211 D3DRS_WRAP3,
3212 D3DRS_WRAP4,
3213 D3DRS_WRAP5,
3214 D3DRS_WRAP6,
3215 D3DRS_WRAP7,
3216 D3DRS_WRAP8,
3217 D3DRS_WRAP9,
3218 D3DRS_ZENABLE,
3219 D3DRS_ZFUNC,
3220 D3DRS_ZWRITEENABLE
3221 };
3222 */
3223 const uint32_t nine_render_states_pixel[(NINED3DRS_LAST + 31) / 32] =
3224 {
3225 0x0f99c380, 0x1ff00070, 0x00000000, 0x00000000,
3226 0x000000ff, 0xde01c900, 0x0003ffcf
3227 };
3228
3229 /*
3230 static const DWORD nine_render_states_vertex[] =
3231 {
3232 D3DRS_ADAPTIVETESS_W,
3233 D3DRS_ADAPTIVETESS_X,
3234 D3DRS_ADAPTIVETESS_Y,
3235 D3DRS_ADAPTIVETESS_Z,
3236 D3DRS_AMBIENT,
3237 D3DRS_AMBIENTMATERIALSOURCE,
3238 D3DRS_CLIPPING,
3239 D3DRS_CLIPPLANEENABLE,
3240 D3DRS_COLORVERTEX,
3241 D3DRS_CULLMODE,
3242 D3DRS_DIFFUSEMATERIALSOURCE,
3243 D3DRS_EMISSIVEMATERIALSOURCE,
3244 D3DRS_ENABLEADAPTIVETESSELLATION,
3245 D3DRS_FOGCOLOR,
3246 D3DRS_FOGDENSITY,
3247 D3DRS_FOGENABLE,
3248 D3DRS_FOGEND,
3249 D3DRS_FOGSTART,
3250 D3DRS_FOGTABLEMODE,
3251 D3DRS_FOGVERTEXMODE,
3252 D3DRS_INDEXEDVERTEXBLENDENABLE,
3253 D3DRS_LIGHTING,
3254 D3DRS_LOCALVIEWER,
3255 D3DRS_MAXTESSELLATIONLEVEL,
3256 D3DRS_MINTESSELLATIONLEVEL,
3257 D3DRS_MULTISAMPLEANTIALIAS,
3258 D3DRS_MULTISAMPLEMASK,
3259 D3DRS_NORMALDEGREE,
3260 D3DRS_NORMALIZENORMALS,
3261 D3DRS_PATCHEDGESTYLE,
3262 D3DRS_POINTSCALE_A,
3263 D3DRS_POINTSCALE_B,
3264 D3DRS_POINTSCALE_C,
3265 D3DRS_POINTSCALEENABLE,
3266 D3DRS_POINTSIZE,
3267 D3DRS_POINTSIZE_MAX,
3268 D3DRS_POINTSIZE_MIN,
3269 D3DRS_POINTSPRITEENABLE,
3270 D3DRS_POSITIONDEGREE,
3271 D3DRS_RANGEFOGENABLE,
3272 D3DRS_SHADEMODE,
3273 D3DRS_SPECULARENABLE,
3274 D3DRS_SPECULARMATERIALSOURCE,
3275 D3DRS_TWEENFACTOR,
3276 D3DRS_VERTEXBLEND
3277 };
3278 */
3279 const uint32_t nine_render_states_vertex[(NINED3DRS_LAST + 31) / 32] =
3280 {
3281 0x30400200, 0x0001007c, 0x00000000, 0x00000000,
3282 0xfd9efb00, 0x01fc34cf, 0x00000000
3283 };
3284
3285 /* TODO: put in the right values */
3286 const uint32_t nine_render_state_group[NINED3DRS_LAST + 1] =
3287 {
3288 [D3DRS_ZENABLE] = NINE_STATE_DSA | NINE_STATE_MULTISAMPLE,
3289 [D3DRS_FILLMODE] = NINE_STATE_RASTERIZER,
3290 [D3DRS_SHADEMODE] = NINE_STATE_RASTERIZER,
3291 [D3DRS_ZWRITEENABLE] = NINE_STATE_DSA,
3292 [D3DRS_ALPHATESTENABLE] = NINE_STATE_DSA,
3293 [D3DRS_LASTPIXEL] = NINE_STATE_RASTERIZER,
3294 [D3DRS_SRCBLEND] = NINE_STATE_BLEND,
3295 [D3DRS_DESTBLEND] = NINE_STATE_BLEND,
3296 [D3DRS_CULLMODE] = NINE_STATE_RASTERIZER,
3297 [D3DRS_ZFUNC] = NINE_STATE_DSA,
3298 [D3DRS_ALPHAREF] = NINE_STATE_DSA,
3299 [D3DRS_ALPHAFUNC] = NINE_STATE_DSA,
3300 [D3DRS_DITHERENABLE] = NINE_STATE_BLEND,
3301 [D3DRS_ALPHABLENDENABLE] = NINE_STATE_BLEND,
3302 [D3DRS_FOGENABLE] = NINE_STATE_FF_SHADER | NINE_STATE_VS_PARAMS_MISC | NINE_STATE_PS_PARAMS_MISC | NINE_STATE_PS_CONST,
3303 [D3DRS_SPECULARENABLE] = NINE_STATE_FF_LIGHTING,
3304 [D3DRS_FOGCOLOR] = NINE_STATE_FF_PS_CONSTS | NINE_STATE_PS_CONST,
3305 [D3DRS_FOGTABLEMODE] = NINE_STATE_FF_SHADER | NINE_STATE_PS_PARAMS_MISC | NINE_STATE_PS_CONST,
3306 [D3DRS_FOGSTART] = NINE_STATE_FF_VS_OTHER | NINE_STATE_FF_PS_CONSTS | NINE_STATE_PS_CONST,
3307 [D3DRS_FOGEND] = NINE_STATE_FF_VS_OTHER | NINE_STATE_FF_PS_CONSTS | NINE_STATE_PS_CONST,
3308 [D3DRS_FOGDENSITY] = NINE_STATE_FF_VS_OTHER | NINE_STATE_FF_PS_CONSTS | NINE_STATE_PS_CONST,
3309 [D3DRS_RANGEFOGENABLE] = NINE_STATE_FF_SHADER,
3310 [D3DRS_STENCILENABLE] = NINE_STATE_DSA | NINE_STATE_MULTISAMPLE,
3311 [D3DRS_STENCILFAIL] = NINE_STATE_DSA,
3312 [D3DRS_STENCILZFAIL] = NINE_STATE_DSA,
3313 [D3DRS_STENCILPASS] = NINE_STATE_DSA,
3314 [D3DRS_STENCILFUNC] = NINE_STATE_DSA,
3315 [D3DRS_STENCILREF] = NINE_STATE_STENCIL_REF,
3316 [D3DRS_STENCILMASK] = NINE_STATE_DSA,
3317 [D3DRS_STENCILWRITEMASK] = NINE_STATE_DSA,
3318 [D3DRS_TEXTUREFACTOR] = NINE_STATE_FF_PS_CONSTS,
3319 [D3DRS_WRAP0] = NINE_STATE_UNHANDLED, /* cylindrical wrap is crazy */
3320 [D3DRS_WRAP1] = NINE_STATE_UNHANDLED,
3321 [D3DRS_WRAP2] = NINE_STATE_UNHANDLED,
3322 [D3DRS_WRAP3] = NINE_STATE_UNHANDLED,
3323 [D3DRS_WRAP4] = NINE_STATE_UNHANDLED,
3324 [D3DRS_WRAP5] = NINE_STATE_UNHANDLED,
3325 [D3DRS_WRAP6] = NINE_STATE_UNHANDLED,
3326 [D3DRS_WRAP7] = NINE_STATE_UNHANDLED,
3327 [D3DRS_CLIPPING] = 0, /* software vertex processing only */
3328 [D3DRS_LIGHTING] = NINE_STATE_FF_LIGHTING,
3329 [D3DRS_AMBIENT] = NINE_STATE_FF_LIGHTING | NINE_STATE_FF_MATERIAL,
3330 [D3DRS_FOGVERTEXMODE] = NINE_STATE_FF_SHADER,
3331 [D3DRS_COLORVERTEX] = NINE_STATE_FF_LIGHTING,
3332 [D3DRS_LOCALVIEWER] = NINE_STATE_FF_LIGHTING,
3333 [D3DRS_NORMALIZENORMALS] = NINE_STATE_FF_SHADER,
3334 [D3DRS_DIFFUSEMATERIALSOURCE] = NINE_STATE_FF_LIGHTING,
3335 [D3DRS_SPECULARMATERIALSOURCE] = NINE_STATE_FF_LIGHTING,
3336 [D3DRS_AMBIENTMATERIALSOURCE] = NINE_STATE_FF_LIGHTING,
3337 [D3DRS_EMISSIVEMATERIALSOURCE] = NINE_STATE_FF_LIGHTING,
3338 [D3DRS_VERTEXBLEND] = NINE_STATE_FF_SHADER,
3339 [D3DRS_CLIPPLANEENABLE] = NINE_STATE_RASTERIZER,
3340 [D3DRS_POINTSIZE] = NINE_STATE_RASTERIZER | NINE_STATE_FF_VS_OTHER,
3341 [D3DRS_POINTSIZE_MIN] = NINE_STATE_RASTERIZER | NINE_STATE_FF_VS_OTHER | NINE_STATE_VS_PARAMS_MISC,
3342 [D3DRS_POINTSPRITEENABLE] = NINE_STATE_RASTERIZER,
3343 [D3DRS_POINTSCALEENABLE] = NINE_STATE_FF_SHADER,
3344 [D3DRS_POINTSCALE_A] = NINE_STATE_FF_VS_OTHER,
3345 [D3DRS_POINTSCALE_B] = NINE_STATE_FF_VS_OTHER,
3346 [D3DRS_POINTSCALE_C] = NINE_STATE_FF_VS_OTHER,
3347 [D3DRS_MULTISAMPLEANTIALIAS] = NINE_STATE_MULTISAMPLE,
3348 [D3DRS_MULTISAMPLEMASK] = NINE_STATE_SAMPLE_MASK,
3349 [D3DRS_PATCHEDGESTYLE] = NINE_STATE_UNHANDLED,
3350 [D3DRS_DEBUGMONITORTOKEN] = NINE_STATE_UNHANDLED,
3351 [D3DRS_POINTSIZE_MAX] = NINE_STATE_RASTERIZER | NINE_STATE_FF_VS_OTHER | NINE_STATE_VS_PARAMS_MISC,
3352 [D3DRS_INDEXEDVERTEXBLENDENABLE] = NINE_STATE_FF_SHADER,
3353 [D3DRS_COLORWRITEENABLE] = NINE_STATE_BLEND,
3354 [D3DRS_TWEENFACTOR] = NINE_STATE_FF_VS_OTHER,
3355 [D3DRS_BLENDOP] = NINE_STATE_BLEND,
3356 [D3DRS_POSITIONDEGREE] = NINE_STATE_UNHANDLED,
3357 [D3DRS_NORMALDEGREE] = NINE_STATE_UNHANDLED,
3358 [D3DRS_SCISSORTESTENABLE] = NINE_STATE_RASTERIZER,
3359 [D3DRS_SLOPESCALEDEPTHBIAS] = NINE_STATE_RASTERIZER,
3360 [D3DRS_ANTIALIASEDLINEENABLE] = NINE_STATE_RASTERIZER,
3361 [D3DRS_MINTESSELLATIONLEVEL] = NINE_STATE_UNHANDLED,
3362 [D3DRS_MAXTESSELLATIONLEVEL] = NINE_STATE_UNHANDLED,
3363 [D3DRS_ADAPTIVETESS_X] = NINE_STATE_UNHANDLED,
3364 [D3DRS_ADAPTIVETESS_Y] = NINE_STATE_UNHANDLED,
3365 [D3DRS_ADAPTIVETESS_Z] = NINE_STATE_UNHANDLED,
3366 [D3DRS_ADAPTIVETESS_W] = NINE_STATE_UNHANDLED,
3367 [D3DRS_ENABLEADAPTIVETESSELLATION] = NINE_STATE_UNHANDLED,
3368 [D3DRS_TWOSIDEDSTENCILMODE] = NINE_STATE_DSA,
3369 [D3DRS_CCW_STENCILFAIL] = NINE_STATE_DSA,
3370 [D3DRS_CCW_STENCILZFAIL] = NINE_STATE_DSA,
3371 [D3DRS_CCW_STENCILPASS] = NINE_STATE_DSA,
3372 [D3DRS_CCW_STENCILFUNC] = NINE_STATE_DSA,
3373 [D3DRS_COLORWRITEENABLE1] = NINE_STATE_BLEND,
3374 [D3DRS_COLORWRITEENABLE2] = NINE_STATE_BLEND,
3375 [D3DRS_COLORWRITEENABLE3] = NINE_STATE_BLEND,
3376 [D3DRS_BLENDFACTOR] = NINE_STATE_BLEND_COLOR,
3377 [D3DRS_SRGBWRITEENABLE] = NINE_STATE_FB,
3378 [D3DRS_DEPTHBIAS] = NINE_STATE_RASTERIZER,
3379 [D3DRS_WRAP8] = NINE_STATE_UNHANDLED, /* cylwrap has to be done via GP */
3380 [D3DRS_WRAP9] = NINE_STATE_UNHANDLED,
3381 [D3DRS_WRAP10] = NINE_STATE_UNHANDLED,
3382 [D3DRS_WRAP11] = NINE_STATE_UNHANDLED,
3383 [D3DRS_WRAP12] = NINE_STATE_UNHANDLED,
3384 [D3DRS_WRAP13] = NINE_STATE_UNHANDLED,
3385 [D3DRS_WRAP14] = NINE_STATE_UNHANDLED,
3386 [D3DRS_WRAP15] = NINE_STATE_UNHANDLED,
3387 [D3DRS_SEPARATEALPHABLENDENABLE] = NINE_STATE_BLEND,
3388 [D3DRS_SRCBLENDALPHA] = NINE_STATE_BLEND,
3389 [D3DRS_DESTBLENDALPHA] = NINE_STATE_BLEND,
3390 [D3DRS_BLENDOPALPHA] = NINE_STATE_BLEND
3391 };
3392
3393 /* Misc */
3394
3395 D3DMATRIX *
3396 nine_state_access_transform(struct nine_ff_state *ff_state, D3DTRANSFORMSTATETYPE t,
3397 boolean alloc)
3398 {
3399 static D3DMATRIX Identity = { .m[0] = { 1, 0, 0, 0 },
3400 .m[1] = { 0, 1, 0, 0 },
3401 .m[2] = { 0, 0, 1, 0 },
3402 .m[3] = { 0, 0, 0, 1 } };
3403 unsigned index;
3404
3405 switch (t) {
3406 case D3DTS_VIEW: index = 0; break;
3407 case D3DTS_PROJECTION: index = 1; break;
3408 case D3DTS_TEXTURE0: index = 2; break;
3409 case D3DTS_TEXTURE1: index = 3; break;
3410 case D3DTS_TEXTURE2: index = 4; break;
3411 case D3DTS_TEXTURE3: index = 5; break;
3412 case D3DTS_TEXTURE4: index = 6; break;
3413 case D3DTS_TEXTURE5: index = 7; break;
3414 case D3DTS_TEXTURE6: index = 8; break;
3415 case D3DTS_TEXTURE7: index = 9; break;
3416 default:
3417 if (!(t >= D3DTS_WORLDMATRIX(0) && t <= D3DTS_WORLDMATRIX(255)))
3418 return NULL;
3419 index = 10 + (t - D3DTS_WORLDMATRIX(0));
3420 break;
3421 }
3422
3423 if (index >= ff_state->num_transforms) {
3424 unsigned N = index + 1;
3425 unsigned n = ff_state->num_transforms;
3426
3427 if (!alloc)
3428 return &Identity;
3429 ff_state->transform = REALLOC(ff_state->transform,
3430 n * sizeof(D3DMATRIX),
3431 N * sizeof(D3DMATRIX));
3432 for (; n < N; ++n)
3433 ff_state->transform[n] = Identity;
3434 ff_state->num_transforms = N;
3435 }
3436 return &ff_state->transform[index];
3437 }
3438
3439 HRESULT
3440 nine_state_set_light(struct nine_ff_state *ff_state, DWORD Index,
3441 const D3DLIGHT9 *pLight)
3442 {
3443 if (Index >= ff_state->num_lights) {
3444 unsigned n = ff_state->num_lights;
3445 unsigned N = Index + 1;
3446
3447 ff_state->light = REALLOC(ff_state->light, n * sizeof(D3DLIGHT9),
3448 N * sizeof(D3DLIGHT9));
3449 if (!ff_state->light)
3450 return E_OUTOFMEMORY;
3451 ff_state->num_lights = N;
3452
3453 for (; n < Index; ++n) {
3454 memset(&ff_state->light[n], 0, sizeof(D3DLIGHT9));
3455 ff_state->light[n].Type = (D3DLIGHTTYPE)NINED3DLIGHT_INVALID;
3456 }
3457 }
3458 ff_state->light[Index] = *pLight;
3459
3460 if (pLight->Type == D3DLIGHT_SPOT && pLight->Theta >= pLight->Phi) {
3461 DBG("Warning: clamping D3DLIGHT9.Theta\n");
3462 ff_state->light[Index].Theta = ff_state->light[Index].Phi;
3463 }
3464 return D3D_OK;
3465 }
3466
3467 HRESULT
3468 nine_state_light_enable(struct nine_ff_state *ff_state, uint32_t *change_group,
3469 DWORD Index, BOOL Enable)
3470 {
3471 unsigned i;
3472
3473 user_assert(Index < ff_state->num_lights, D3DERR_INVALIDCALL);
3474
3475 for (i = 0; i < ff_state->num_lights_active; ++i) {
3476 if (ff_state->active_light[i] == Index)
3477 break;
3478 }
3479
3480 if (Enable) {
3481 if (i < ff_state->num_lights_active)
3482 return D3D_OK;
3483 /* XXX wine thinks this should still succeed:
3484 */
3485 user_assert(i < NINE_MAX_LIGHTS_ACTIVE, D3DERR_INVALIDCALL);
3486
3487 ff_state->active_light[i] = Index;
3488 ff_state->num_lights_active++;
3489 } else {
3490 if (i == ff_state->num_lights_active)
3491 return D3D_OK;
3492 --ff_state->num_lights_active;
3493 for (; i < ff_state->num_lights_active; ++i)
3494 ff_state->active_light[i] = ff_state->active_light[i + 1];
3495 }
3496
3497 *change_group |= NINE_STATE_FF_LIGHTING;
3498
3499 return D3D_OK;
3500 }
3501
3502 #define D3DRS_TO_STRING_CASE(n) case D3DRS_##n: return "D3DRS_"#n
3503 const char *nine_d3drs_to_string(DWORD State)
3504 {
3505 switch (State) {
3506 D3DRS_TO_STRING_CASE(ZENABLE);
3507 D3DRS_TO_STRING_CASE(FILLMODE);
3508 D3DRS_TO_STRING_CASE(SHADEMODE);
3509 D3DRS_TO_STRING_CASE(ZWRITEENABLE);
3510 D3DRS_TO_STRING_CASE(ALPHATESTENABLE);
3511 D3DRS_TO_STRING_CASE(LASTPIXEL);
3512 D3DRS_TO_STRING_CASE(SRCBLEND);
3513 D3DRS_TO_STRING_CASE(DESTBLEND);
3514 D3DRS_TO_STRING_CASE(CULLMODE);
3515 D3DRS_TO_STRING_CASE(ZFUNC);
3516 D3DRS_TO_STRING_CASE(ALPHAREF);
3517 D3DRS_TO_STRING_CASE(ALPHAFUNC);
3518 D3DRS_TO_STRING_CASE(DITHERENABLE);
3519 D3DRS_TO_STRING_CASE(ALPHABLENDENABLE);
3520 D3DRS_TO_STRING_CASE(FOGENABLE);
3521 D3DRS_TO_STRING_CASE(SPECULARENABLE);
3522 D3DRS_TO_STRING_CASE(FOGCOLOR);
3523 D3DRS_TO_STRING_CASE(FOGTABLEMODE);
3524 D3DRS_TO_STRING_CASE(FOGSTART);
3525 D3DRS_TO_STRING_CASE(FOGEND);
3526 D3DRS_TO_STRING_CASE(FOGDENSITY);
3527 D3DRS_TO_STRING_CASE(RANGEFOGENABLE);
3528 D3DRS_TO_STRING_CASE(STENCILENABLE);
3529 D3DRS_TO_STRING_CASE(STENCILFAIL);
3530 D3DRS_TO_STRING_CASE(STENCILZFAIL);
3531 D3DRS_TO_STRING_CASE(STENCILPASS);
3532 D3DRS_TO_STRING_CASE(STENCILFUNC);
3533 D3DRS_TO_STRING_CASE(STENCILREF);
3534 D3DRS_TO_STRING_CASE(STENCILMASK);
3535 D3DRS_TO_STRING_CASE(STENCILWRITEMASK);
3536 D3DRS_TO_STRING_CASE(TEXTUREFACTOR);
3537 D3DRS_TO_STRING_CASE(WRAP0);
3538 D3DRS_TO_STRING_CASE(WRAP1);
3539 D3DRS_TO_STRING_CASE(WRAP2);
3540 D3DRS_TO_STRING_CASE(WRAP3);
3541 D3DRS_TO_STRING_CASE(WRAP4);
3542 D3DRS_TO_STRING_CASE(WRAP5);
3543 D3DRS_TO_STRING_CASE(WRAP6);
3544 D3DRS_TO_STRING_CASE(WRAP7);
3545 D3DRS_TO_STRING_CASE(CLIPPING);
3546 D3DRS_TO_STRING_CASE(LIGHTING);
3547 D3DRS_TO_STRING_CASE(AMBIENT);
3548 D3DRS_TO_STRING_CASE(FOGVERTEXMODE);
3549 D3DRS_TO_STRING_CASE(COLORVERTEX);
3550 D3DRS_TO_STRING_CASE(LOCALVIEWER);
3551 D3DRS_TO_STRING_CASE(NORMALIZENORMALS);
3552 D3DRS_TO_STRING_CASE(DIFFUSEMATERIALSOURCE);
3553 D3DRS_TO_STRING_CASE(SPECULARMATERIALSOURCE);
3554 D3DRS_TO_STRING_CASE(AMBIENTMATERIALSOURCE);
3555 D3DRS_TO_STRING_CASE(EMISSIVEMATERIALSOURCE);
3556 D3DRS_TO_STRING_CASE(VERTEXBLEND);
3557 D3DRS_TO_STRING_CASE(CLIPPLANEENABLE);
3558 D3DRS_TO_STRING_CASE(POINTSIZE);
3559 D3DRS_TO_STRING_CASE(POINTSIZE_MIN);
3560 D3DRS_TO_STRING_CASE(POINTSPRITEENABLE);
3561 D3DRS_TO_STRING_CASE(POINTSCALEENABLE);
3562 D3DRS_TO_STRING_CASE(POINTSCALE_A);
3563 D3DRS_TO_STRING_CASE(POINTSCALE_B);
3564 D3DRS_TO_STRING_CASE(POINTSCALE_C);
3565 D3DRS_TO_STRING_CASE(MULTISAMPLEANTIALIAS);
3566 D3DRS_TO_STRING_CASE(MULTISAMPLEMASK);
3567 D3DRS_TO_STRING_CASE(PATCHEDGESTYLE);
3568 D3DRS_TO_STRING_CASE(DEBUGMONITORTOKEN);
3569 D3DRS_TO_STRING_CASE(POINTSIZE_MAX);
3570 D3DRS_TO_STRING_CASE(INDEXEDVERTEXBLENDENABLE);
3571 D3DRS_TO_STRING_CASE(COLORWRITEENABLE);
3572 D3DRS_TO_STRING_CASE(TWEENFACTOR);
3573 D3DRS_TO_STRING_CASE(BLENDOP);
3574 D3DRS_TO_STRING_CASE(POSITIONDEGREE);
3575 D3DRS_TO_STRING_CASE(NORMALDEGREE);
3576 D3DRS_TO_STRING_CASE(SCISSORTESTENABLE);
3577 D3DRS_TO_STRING_CASE(SLOPESCALEDEPTHBIAS);
3578 D3DRS_TO_STRING_CASE(ANTIALIASEDLINEENABLE);
3579 D3DRS_TO_STRING_CASE(MINTESSELLATIONLEVEL);
3580 D3DRS_TO_STRING_CASE(MAXTESSELLATIONLEVEL);
3581 D3DRS_TO_STRING_CASE(ADAPTIVETESS_X);
3582 D3DRS_TO_STRING_CASE(ADAPTIVETESS_Y);
3583 D3DRS_TO_STRING_CASE(ADAPTIVETESS_Z);
3584 D3DRS_TO_STRING_CASE(ADAPTIVETESS_W);
3585 D3DRS_TO_STRING_CASE(ENABLEADAPTIVETESSELLATION);
3586 D3DRS_TO_STRING_CASE(TWOSIDEDSTENCILMODE);
3587 D3DRS_TO_STRING_CASE(CCW_STENCILFAIL);
3588 D3DRS_TO_STRING_CASE(CCW_STENCILZFAIL);
3589 D3DRS_TO_STRING_CASE(CCW_STENCILPASS);
3590 D3DRS_TO_STRING_CASE(CCW_STENCILFUNC);
3591 D3DRS_TO_STRING_CASE(COLORWRITEENABLE1);
3592 D3DRS_TO_STRING_CASE(COLORWRITEENABLE2);
3593 D3DRS_TO_STRING_CASE(COLORWRITEENABLE3);
3594 D3DRS_TO_STRING_CASE(BLENDFACTOR);
3595 D3DRS_TO_STRING_CASE(SRGBWRITEENABLE);
3596 D3DRS_TO_STRING_CASE(DEPTHBIAS);
3597 D3DRS_TO_STRING_CASE(WRAP8);
3598 D3DRS_TO_STRING_CASE(WRAP9);
3599 D3DRS_TO_STRING_CASE(WRAP10);
3600 D3DRS_TO_STRING_CASE(WRAP11);
3601 D3DRS_TO_STRING_CASE(WRAP12);
3602 D3DRS_TO_STRING_CASE(WRAP13);
3603 D3DRS_TO_STRING_CASE(WRAP14);
3604 D3DRS_TO_STRING_CASE(WRAP15);
3605 D3DRS_TO_STRING_CASE(SEPARATEALPHABLENDENABLE);
3606 D3DRS_TO_STRING_CASE(SRCBLENDALPHA);
3607 D3DRS_TO_STRING_CASE(DESTBLENDALPHA);
3608 D3DRS_TO_STRING_CASE(BLENDOPALPHA);
3609 default:
3610 return "(invalid)";
3611 }
3612 }