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