2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 * Copyright 2013 Christoph Bumiller
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:
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
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. */
25 #include "basetexture9.h"
26 #include "indexbuffer9.h"
28 #include "vertexdeclaration9.h"
29 #include "vertexshader9.h"
30 #include "pixelshader9.h"
31 #include "nine_pipe.h"
33 #include "pipe/p_context.h"
34 #include "pipe/p_state.h"
35 #include "cso_cache/cso_context.h"
36 #include "util/u_math.h"
38 #define DBG_CHANNEL DBG_DEVICE
41 update_framebuffer(struct NineDevice9
*device
)
43 struct pipe_context
*pipe
= device
->pipe
;
44 struct nine_state
*state
= &device
->state
;
45 struct pipe_framebuffer_state
*fb
= &device
->state
.fb
;
47 struct NineSurface9
*rt0
= state
->rt
[0];
48 unsigned w
= rt0
->desc
.Width
;
49 unsigned h
= rt0
->desc
.Height
;
50 D3DMULTISAMPLE_TYPE nr_samples
= rt0
->desc
.MultiSampleType
;
51 unsigned mask
= state
->ps
? state
->ps
->rt_mask
: 1;
52 const int sRGB
= state
->rs
[D3DRS_SRGBWRITEENABLE
] ? 1 : 0;
59 /* all render targets must have the same size and the depth buffer must be
60 * bigger. Multisample has to match, according to spec. But some apps do
61 * things wrong there, and no error is returned. The behaviour they get
62 * apparently is that depth buffer is disabled if it doesn't match.
63 * Surely the same for render targets. */
65 /* Special case: D3DFMT_NULL is used to bound no real render target,
66 * but render to depth buffer. We have to not take into account the render
67 * target info. TODO: know what should happen when there are several render targers
68 * and the first one is D3DFMT_NULL */
69 if (rt0
->desc
.Format
== D3DFMT_NULL
&& state
->ds
) {
70 w
= state
->ds
->desc
.Width
;
71 h
= state
->ds
->desc
.Height
;
72 nr_samples
= state
->ds
->desc
.MultiSampleType
;
75 for (i
= 0; i
< device
->caps
.NumSimultaneousRTs
; ++i
) {
76 struct NineSurface9
*rt
= state
->rt
[i
];
78 if (rt
&& rt
->desc
.Format
!= D3DFMT_NULL
&& (mask
& (1 << i
)) &&
79 rt
->desc
.Width
== w
&& rt
->desc
.Height
== h
&&
80 rt
->desc
.MultiSampleType
== nr_samples
) {
81 fb
->cbufs
[i
] = NineSurface9_GetSurface(rt
, sRGB
);
82 state
->rt_mask
|= 1 << i
;
85 if (unlikely(rt
->desc
.Usage
& D3DUSAGE_AUTOGENMIPMAP
)) {
86 assert(rt
->texture
== D3DRTYPE_TEXTURE
||
87 rt
->texture
== D3DRTYPE_CUBETEXTURE
);
88 NineBaseTexture9(rt
->base
.base
.container
)->dirty_mip
= TRUE
;
91 /* Color outputs must match RT slot,
92 * drivers will have to handle NULL entries for GL, too.
98 if (state
->ds
&& state
->ds
->desc
.Width
>= w
&&
99 state
->ds
->desc
.Height
>= h
&&
100 state
->ds
->desc
.MultiSampleType
== nr_samples
) {
101 fb
->zsbuf
= NineSurface9_GetSurface(state
->ds
, 0);
109 pipe
->set_framebuffer_state(pipe
, fb
); /* XXX: cso ? */
113 switch (fb
->zsbuf
->format
) {
114 case PIPE_FORMAT_Z32_FLOAT
:
115 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
118 case PIPE_FORMAT_Z16_UNORM
:
119 scale
= fui((float)(1 << 16));
122 scale
= fui((float)(1 << 24));
125 if (state
->rs
[NINED3DRS_ZBIASSCALE
] != scale
) {
126 state
->rs
[NINED3DRS_ZBIASSCALE
] = scale
;
127 state
->changed
.group
|= NINE_STATE_RASTERIZER
;
131 return state
->changed
.group
;
135 update_viewport(struct NineDevice9
*device
)
137 struct pipe_context
*pipe
= device
->pipe
;
138 const D3DVIEWPORT9
*vport
= &device
->state
.viewport
;
139 struct pipe_viewport_state pvport
;
142 * I hope D3D clip coordinates are still
143 * -1 .. +1 for X,Y and
144 * 0 .. +1 for Z (use pipe_rasterizer_state.clip_halfz)
146 pvport
.scale
[0] = (float)vport
->Width
* 0.5f
;
147 pvport
.scale
[1] = (float)vport
->Height
* -0.5f
;
148 pvport
.scale
[2] = vport
->MaxZ
- vport
->MinZ
;
149 pvport
.translate
[0] = (float)vport
->Width
* 0.5f
+ (float)vport
->X
;
150 pvport
.translate
[1] = (float)vport
->Height
* 0.5f
+ (float)vport
->Y
;
151 pvport
.translate
[2] = vport
->MinZ
;
153 pipe
->set_viewport_states(pipe
, 0, 1, &pvport
);
157 update_scissor(struct NineDevice9
*device
)
159 struct pipe_context
*pipe
= device
->pipe
;
161 pipe
->set_scissor_states(pipe
, 0, 1, &device
->state
.scissor
);
165 update_blend(struct NineDevice9
*device
)
167 nine_convert_blend_state(device
->cso
, device
->state
.rs
);
171 update_dsa(struct NineDevice9
*device
)
173 nine_convert_dsa_state(device
->cso
, device
->state
.rs
);
177 update_rasterizer(struct NineDevice9
*device
)
179 nine_convert_rasterizer_state(device
->cso
, device
->state
.rs
);
182 /* Loop through VS inputs and pick the vertex elements with the declared
183 * usage from the vertex declaration, then insert the instance divisor from
184 * the stream source frequency setting.
187 update_vertex_elements(struct NineDevice9
*device
)
189 struct nine_state
*state
= &device
->state
;
190 const struct NineVertexDeclaration9
*vdecl
= device
->state
.vdecl
;
191 const struct NineVertexShader9
*vs
;
194 struct pipe_vertex_element ve
[PIPE_MAX_ATTRIBS
];
196 state
->stream_usage_mask
= 0;
198 vs
= device
->state
.vs
? device
->state
.vs
: device
->ff
.vs
;
200 if (!vdecl
) /* no inputs */
202 for (n
= 0; n
< vs
->num_inputs
; ++n
) {
203 DBG("looking up input %u (usage %u) from vdecl(%p)\n",
204 n
, vs
->input_map
[n
].ndecl
, vdecl
);
207 for (i
= 0; i
< vdecl
->nelems
; i
++) {
208 if (vdecl
->usage_map
[i
] == vs
->input_map
[n
].ndecl
) {
215 ve
[n
] = vdecl
->elems
[index
];
216 b
= ve
[n
].vertex_buffer_index
;
217 state
->stream_usage_mask
|= 1 << b
;
218 /* XXX wine just uses 1 here: */
219 if (state
->stream_freq
[b
] & D3DSTREAMSOURCE_INSTANCEDATA
)
220 ve
[n
].instance_divisor
= state
->stream_freq
[b
] & 0x7FFFFF;
222 /* TODO: msdn doesn't specify what should happen when the vertex
223 * declaration doesn't match the vertex shader inputs.
224 * Some websites say the code will pass but nothing will get rendered.
225 * We should check and implement the correct behaviour. */
226 /* Put PIPE_FORMAT_NONE.
227 * Some drivers (r300) are very unhappy with that */
228 ve
[n
].src_format
= PIPE_FORMAT_NONE
;
229 ve
[n
].src_offset
= 0;
230 ve
[n
].instance_divisor
= 0;
231 ve
[n
].vertex_buffer_index
= 0;
234 cso_set_vertex_elements(device
->cso
, vs
->num_inputs
, ve
);
236 state
->changed
.stream_freq
= 0;
239 static INLINE
uint32_t
240 update_shader_variant_keys(struct NineDevice9
*device
)
242 struct nine_state
*state
= &device
->state
;
244 uint32_t vs_key
= state
->samplers_shadow
;
245 uint32_t ps_key
= state
->samplers_shadow
;
247 vs_key
= (vs_key
& NINE_VS_SAMPLERS_MASK
) >> NINE_SAMPLER_VS(0);
248 ps_key
= (ps_key
& NINE_PS_SAMPLERS_MASK
) >> NINE_SAMPLER_PS(0);
250 if (state
->vs
) vs_key
&= state
->vs
->sampler_mask
;
252 if (unlikely(state
->ps
->byte_code
.version
< 0x20)) {
253 /* no depth textures, but variable targets */
254 uint32_t m
= state
->ps
->sampler_mask
;
259 ps_key
|= (state
->texture
[s
] ? state
->texture
[s
]->pstype
: 1) << (s
* 2);
262 ps_key
&= state
->ps
->sampler_mask
;
266 if (state
->vs
&& state
->vs_key
!= vs_key
) {
267 state
->vs_key
= vs_key
;
268 mask
|= NINE_STATE_VS
;
270 if (state
->ps
&& state
->ps_key
!= ps_key
) {
271 state
->ps_key
= ps_key
;
272 mask
|= NINE_STATE_PS
;
277 static INLINE
uint32_t
278 update_vs(struct NineDevice9
*device
)
280 struct nine_state
*state
= &device
->state
;
281 struct NineVertexShader9
*vs
= state
->vs
;
282 uint32_t changed_group
= 0;
284 /* likely because we dislike FF */
286 state
->cso
.vs
= NineVertexShader9_GetVariant(vs
, state
->vs_key
);
289 state
->cso
.vs
= vs
->variant
.cso
;
291 device
->pipe
->bind_vs_state(device
->pipe
, state
->cso
.vs
);
293 if (state
->rs
[NINED3DRS_VSPOINTSIZE
] != vs
->point_size
) {
294 state
->rs
[NINED3DRS_VSPOINTSIZE
] = vs
->point_size
;
295 changed_group
|= NINE_STATE_RASTERIZER
;
298 if ((state
->bound_samplers_mask_vs
& vs
->sampler_mask
) != vs
->sampler_mask
)
299 /* Bound dummy sampler. */
300 changed_group
|= NINE_STATE_SAMPLER
;
301 return changed_group
;
304 static INLINE
uint32_t
305 update_ps(struct NineDevice9
*device
)
307 struct nine_state
*state
= &device
->state
;
308 struct NinePixelShader9
*ps
= state
->ps
;
309 uint32_t changed_group
= 0;
312 state
->cso
.ps
= NinePixelShader9_GetVariant(ps
, state
->ps_key
);
315 state
->cso
.ps
= ps
->variant
.cso
;
317 device
->pipe
->bind_fs_state(device
->pipe
, state
->cso
.ps
);
319 if ((state
->bound_samplers_mask_ps
& ps
->sampler_mask
) != ps
->sampler_mask
)
320 /* Bound dummy sampler. */
321 changed_group
|= NINE_STATE_SAMPLER
;
322 return changed_group
;
325 #define DO_UPLOAD_CONST_F(buf,p,c,d) \
327 DBG("upload ConstantF [%u .. %u]\n", x, (x) + (c) - 1); \
328 box.x = (p) * 4 * sizeof(float); \
329 box.width = (c) * 4 * sizeof(float); \
330 pipe->transfer_inline_write(pipe, buf, 0, usage, &box, &((d)[p * 4]), \
334 /* OK, this is a bit ugly ... */
336 update_constants(struct NineDevice9
*device
, unsigned shader_type
)
338 struct pipe_context
*pipe
= device
->pipe
;
339 struct pipe_resource
*buf
;
342 const float *const_f
;
345 uint32_t data_b
[NINE_MAX_CONST_B
];
348 const unsigned usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD_RANGE
;
349 unsigned x
= 0; /* silence warning */
351 struct nine_range
*r
, *p
, *lconstf_ranges
;
359 if (shader_type
== PIPE_SHADER_VERTEX
) {
361 buf
= device
->constbuf_vs
;
363 const_f
= device
->state
.vs_const_f
;
364 for (p
= r
= device
->state
.changed
.vs_const_f
; r
; p
= r
, r
= r
->next
)
365 DO_UPLOAD_CONST_F(buf
, r
->bgn
, r
->end
- r
->bgn
, const_f
);
367 nine_range_pool_put_chain(&device
->range_pool
,
368 device
->state
.changed
.vs_const_f
, p
);
369 device
->state
.changed
.vs_const_f
= NULL
;
372 dirty_i
= device
->state
.changed
.vs_const_i
;
373 device
->state
.changed
.vs_const_i
= 0;
374 const_i
= &device
->state
.vs_const_i
[0][0];
376 dirty_b
= device
->state
.changed
.vs_const_b
;
377 device
->state
.changed
.vs_const_b
= 0;
378 const_b
= device
->state
.vs_const_b
;
380 lconstf_ranges
= device
->state
.vs
->lconstf
.ranges
;
381 lconstf_data
= device
->state
.vs
->lconstf
.data
;
383 device
->state
.ff
.clobber
.vs_const
= TRUE
;
384 device
->state
.changed
.group
&= ~NINE_STATE_VS_CONST
;
387 buf
= device
->constbuf_ps
;
389 const_f
= device
->state
.ps_const_f
;
390 for (p
= r
= device
->state
.changed
.ps_const_f
; r
; p
= r
, r
= r
->next
)
391 DO_UPLOAD_CONST_F(buf
, r
->bgn
, r
->end
- r
->bgn
, const_f
);
393 nine_range_pool_put_chain(&device
->range_pool
,
394 device
->state
.changed
.ps_const_f
, p
);
395 device
->state
.changed
.ps_const_f
= NULL
;
398 dirty_i
= device
->state
.changed
.ps_const_i
;
399 device
->state
.changed
.ps_const_i
= 0;
400 const_i
= &device
->state
.ps_const_i
[0][0];
402 dirty_b
= device
->state
.changed
.ps_const_b
;
403 device
->state
.changed
.ps_const_b
= 0;
404 const_b
= device
->state
.ps_const_b
;
406 lconstf_ranges
= NULL
;
409 device
->state
.ff
.clobber
.ps_const
= TRUE
;
410 device
->state
.changed
.group
&= ~NINE_STATE_PS_CONST
;
413 /* write range from min to max changed, it's not much data */
416 c
= util_last_bit(dirty_b
);
417 i
= ffs(dirty_b
) - 1;
418 x
= buf
->width0
- (NINE_MAX_CONST_B
- i
) * 4;
420 memcpy(data_b
, &(const_b
[i
]), c
* sizeof(uint32_t));
423 DBG("upload ConstantB [%u .. %u]\n", x
, x
+ c
- 1);
424 pipe
->transfer_inline_write(pipe
, buf
, 0, usage
, &box
, data_b
, 0, 0);
428 for (c
= 0, i
= 0; dirty_i
; i
++, dirty_i
>>= 1) {
435 DBG("upload ConstantI [%u .. %u]\n", x
, x
+ c
- 1);
436 data
= &const_i
[x
* 4];
437 box
.x
= buf
->width0
- (NINE_MAX_CONST_I
* 4 + NINE_MAX_CONST_B
) * 4;
438 box
.x
+= x
* 4 * sizeof(int);
439 box
.width
= c
* 4 * sizeof(int);
441 pipe
->transfer_inline_write(pipe
, buf
, 0, usage
, &box
, data
, 0, 0);
445 DBG("upload ConstantI [%u .. %u]\n", x
, x
+ c
- 1);
446 data
= &const_i
[x
* 4];
447 box
.x
= buf
->width0
- (NINE_MAX_CONST_I
* 4 + NINE_MAX_CONST_B
) * 4;
448 box
.x
+= x
* 4 * sizeof(int);
449 box
.width
= c
* 4 * sizeof(int);
450 pipe
->transfer_inline_write(pipe
, buf
, 0, usage
, &box
, data
, 0, 0);
453 /* TODO: only upload these when shader itself changes */
454 if (lconstf_ranges
) {
456 struct nine_range
*r
= lconstf_ranges
;
458 box
.x
= r
->bgn
* 4 * sizeof(float);
459 n
+= r
->end
- r
->bgn
;
460 box
.width
= (r
->end
- r
->bgn
) * 4 * sizeof(float);
461 data
= &lconstf_data
[4 * n
];
462 pipe
->transfer_inline_write(pipe
, buf
, 0, usage
, &box
, data
, 0, 0);
469 update_vs_constants_userbuf(struct NineDevice9
*device
)
471 struct nine_state
*state
= &device
->state
;
472 struct pipe_context
*pipe
= device
->pipe
;
473 struct pipe_constant_buffer cb
;
475 cb
.buffer_offset
= 0;
476 cb
.buffer_size
= device
->state
.vs
->const_used_size
;
477 cb
.user_buffer
= device
->state
.vs_const_f
;
482 if (state
->changed
.vs_const_i
) {
483 int *idst
= (int *)&state
->vs_const_f
[4 * device
->max_vs_const_f
];
484 memcpy(idst
, state
->vs_const_i
, sizeof(state
->vs_const_i
));
485 state
->changed
.vs_const_i
= 0;
487 if (state
->changed
.vs_const_b
) {
488 int *idst
= (int *)&state
->vs_const_f
[4 * device
->max_vs_const_f
];
489 uint32_t *bdst
= (uint32_t *)&idst
[4 * NINE_MAX_CONST_I
];
490 memcpy(bdst
, state
->vs_const_b
, sizeof(state
->vs_const_b
));
491 state
->changed
.vs_const_b
= 0;
494 if (device
->state
.vs
->lconstf
.ranges
) {
495 /* TODO: Can we make it so that we don't have to copy everything ? */
496 const struct nine_lconstf
*lconstf
= &device
->state
.vs
->lconstf
;
497 const struct nine_range
*r
= lconstf
->ranges
;
499 float *dst
= device
->state
.vs_lconstf_temp
;
500 float *src
= (float *)cb
.user_buffer
;
501 memcpy(dst
, src
, cb
.buffer_size
);
504 unsigned c
= r
->end
- r
->bgn
;
505 memcpy(&dst
[p
* 4], &lconstf
->data
[n
* 4], c
* 4 * sizeof(float));
509 cb
.user_buffer
= dst
;
512 pipe
->set_constant_buffer(pipe
, PIPE_SHADER_VERTEX
, 0, &cb
);
514 if (device
->state
.changed
.vs_const_f
) {
515 struct nine_range
*r
= device
->state
.changed
.vs_const_f
;
516 struct nine_range
*p
= r
;
519 nine_range_pool_put_chain(&device
->range_pool
, r
, p
);
520 device
->state
.changed
.vs_const_f
= NULL
;
522 state
->changed
.group
&= ~NINE_STATE_VS_CONST
;
526 update_ps_constants_userbuf(struct NineDevice9
*device
)
528 struct nine_state
*state
= &device
->state
;
529 struct pipe_context
*pipe
= device
->pipe
;
530 struct pipe_constant_buffer cb
;
532 cb
.buffer_offset
= 0;
533 cb
.buffer_size
= device
->state
.ps
->const_used_size
;
534 cb
.user_buffer
= device
->state
.ps_const_f
;
539 if (state
->changed
.ps_const_i
) {
540 int *idst
= (int *)&state
->ps_const_f
[4 * device
->max_ps_const_f
];
541 memcpy(idst
, state
->ps_const_i
, sizeof(state
->ps_const_i
));
542 state
->changed
.ps_const_i
= 0;
544 if (state
->changed
.ps_const_b
) {
545 int *idst
= (int *)&state
->ps_const_f
[4 * device
->max_ps_const_f
];
546 uint32_t *bdst
= (uint32_t *)&idst
[4 * NINE_MAX_CONST_I
];
547 memcpy(bdst
, state
->ps_const_b
, sizeof(state
->ps_const_b
));
548 state
->changed
.ps_const_b
= 0;
551 pipe
->set_constant_buffer(pipe
, PIPE_SHADER_FRAGMENT
, 0, &cb
);
553 if (device
->state
.changed
.ps_const_f
) {
554 struct nine_range
*r
= device
->state
.changed
.ps_const_f
;
555 struct nine_range
*p
= r
;
558 nine_range_pool_put_chain(&device
->range_pool
, r
, p
);
559 device
->state
.changed
.ps_const_f
= NULL
;
561 state
->changed
.group
&= ~NINE_STATE_PS_CONST
;
565 update_vertex_buffers(struct NineDevice9
*device
)
567 struct pipe_context
*pipe
= device
->pipe
;
568 struct nine_state
*state
= &device
->state
;
569 uint32_t mask
= state
->changed
.vtxbuf
;
574 DBG("mask=%x\n", mask
);
576 for (i
= 0; mask
; mask
>>= 1, ++i
) {
583 pipe
->set_vertex_buffers(pipe
,
584 start
, count
, &state
->vtxbuf
[start
]);
589 pipe
->set_vertex_buffers(pipe
, start
, count
, &state
->vtxbuf
[start
]);
591 state
->changed
.vtxbuf
= 0;
595 update_index_buffer(struct NineDevice9
*device
)
597 struct pipe_context
*pipe
= device
->pipe
;
598 if (device
->state
.idxbuf
)
599 pipe
->set_index_buffer(pipe
, &device
->state
.idxbuf
->buffer
);
601 pipe
->set_index_buffer(pipe
, NULL
);
604 /* TODO: only go through dirty textures */
606 validate_textures(struct NineDevice9
*device
)
608 struct NineBaseTexture9
*tex
, *ptr
;
609 LIST_FOR_EACH_ENTRY_SAFE(tex
, ptr
, &device
->update_textures
, list
) {
610 list_delinit(&tex
->list
);
611 NineBaseTexture9_Validate(tex
);
615 static INLINE boolean
616 update_sampler_derived(struct nine_state
*state
, unsigned s
)
618 boolean changed
= FALSE
;
620 if (state
->samp
[s
][NINED3DSAMP_SHADOW
] != state
->texture
[s
]->shadow
) {
622 state
->samp
[s
][NINED3DSAMP_SHADOW
] = state
->texture
[s
]->shadow
;
625 if (state
->samp
[s
][D3DSAMP_MIPFILTER
] != D3DTEXF_NONE
) {
626 int lod
= state
->samp
[s
][D3DSAMP_MAXMIPLEVEL
] - state
->texture
[s
]->lod
;
629 if (state
->samp
[s
][NINED3DSAMP_MINLOD
] != lod
) {
631 state
->samp
[s
][NINED3DSAMP_MINLOD
] = lod
;
634 state
->changed
.sampler
[s
] &= ~0x300; /* lod changes irrelevant */
640 /* TODO: add sRGB override to pipe_sampler_state ? */
642 update_textures_and_samplers(struct NineDevice9
*device
)
644 struct pipe_context
*pipe
= device
->pipe
;
645 struct nine_state
*state
= &device
->state
;
646 struct pipe_sampler_view
*view
[NINE_MAX_SAMPLERS
];
647 struct pipe_sampler_state samp
;
648 unsigned num_textures
;
650 boolean commit_views
;
651 boolean commit_samplers
;
652 uint16_t sampler_mask
= state
->ps
? state
->ps
->sampler_mask
:
653 device
->ff
.ps
->sampler_mask
;
655 /* TODO: Can we reduce iterations here ? */
657 commit_views
= FALSE
;
658 commit_samplers
= FALSE
;
659 state
->bound_samplers_mask_ps
= 0;
660 for (num_textures
= 0, i
= 0; i
< NINE_MAX_SAMPLERS_PS
; ++i
) {
661 const unsigned s
= NINE_SAMPLER_PS(i
);
664 if (!state
->texture
[s
] && !(sampler_mask
& (1 << i
))) {
669 if (state
->texture
[s
]) {
670 sRGB
= state
->samp
[s
][D3DSAMP_SRGBTEXTURE
] ? 1 : 0;
672 view
[i
] = NineBaseTexture9_GetSamplerView(state
->texture
[s
], sRGB
);
673 num_textures
= i
+ 1;
675 if (update_sampler_derived(state
, s
) || (state
->changed
.sampler
[s
] & 0x05fe)) {
676 state
->changed
.sampler
[s
] = 0;
677 commit_samplers
= TRUE
;
678 nine_convert_sampler_state(device
->cso
, s
, state
->samp
[s
]);
681 /* Bind dummy sampler. We do not bind dummy sampler when
682 * it is not needed because it could add overhead. The
683 * dummy sampler should have r=g=b=0 and a=1. We do not
684 * unbind dummy sampler directly when they are not needed
685 * anymore, but they're going to be removed as long as texture
686 * or sampler states are changed. */
687 view
[i
] = device
->dummy_sampler
;
688 num_textures
= i
+ 1;
690 memset(&samp
, 0, sizeof(samp
));
691 samp
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
692 samp
.max_lod
= 15.0f
;
693 samp
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
694 samp
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
695 samp
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
696 samp
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
697 samp
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
698 samp
.compare_mode
= PIPE_TEX_COMPARE_NONE
;
699 samp
.compare_func
= PIPE_FUNC_LEQUAL
;
700 samp
.normalized_coords
= 1;
701 samp
.seamless_cube_map
= 1;
703 cso_single_sampler(device
->cso
, PIPE_SHADER_FRAGMENT
,
704 s
- NINE_SAMPLER_PS(0), &samp
);
707 commit_samplers
= TRUE
;
708 state
->changed
.sampler
[s
] = ~0;
711 state
->bound_samplers_mask_ps
|= (1 << s
);
714 commit_views
|= (state
->changed
.texture
& NINE_PS_SAMPLERS_MASK
) != 0;
715 commit_views
|= state
->changed
.srgb
;
717 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
721 cso_single_sampler_done(device
->cso
, PIPE_SHADER_FRAGMENT
);
723 commit_views
= FALSE
;
724 commit_samplers
= FALSE
;
725 sampler_mask
= state
->vs
? state
->vs
->sampler_mask
: 0;
726 state
->bound_samplers_mask_vs
= 0;
727 for (num_textures
= 0, i
= 0; i
< NINE_MAX_SAMPLERS_VS
; ++i
) {
728 const unsigned s
= NINE_SAMPLER_VS(i
);
731 if (!state
->texture
[s
] && !(sampler_mask
& (1 << i
))) {
736 if (state
->texture
[s
]) {
737 sRGB
= state
->samp
[s
][D3DSAMP_SRGBTEXTURE
] ? 1 : 0;
739 view
[i
] = NineBaseTexture9_GetSamplerView(state
->texture
[s
], sRGB
);
740 num_textures
= i
+ 1;
742 if (update_sampler_derived(state
, s
) || (state
->changed
.sampler
[s
] & 0x05fe)) {
743 state
->changed
.sampler
[s
] = 0;
744 commit_samplers
= TRUE
;
745 nine_convert_sampler_state(device
->cso
, s
, state
->samp
[s
]);
748 /* Bind dummy sampler. We do not bind dummy sampler when
749 * it is not needed because it could add overhead. The
750 * dummy sampler should have r=g=b=0 and a=1. We do not
751 * unbind dummy sampler directly when they are not needed
752 * anymore, but they're going to be removed as long as texture
753 * or sampler states are changed. */
754 view
[i
] = device
->dummy_sampler
;
755 num_textures
= i
+ 1;
757 memset(&samp
, 0, sizeof(samp
));
758 samp
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
759 samp
.max_lod
= 15.0f
;
760 samp
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
761 samp
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
762 samp
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
763 samp
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
764 samp
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
765 samp
.compare_mode
= PIPE_TEX_COMPARE_NONE
;
766 samp
.compare_func
= PIPE_FUNC_LEQUAL
;
767 samp
.normalized_coords
= 1;
768 samp
.seamless_cube_map
= 1;
770 cso_single_sampler(device
->cso
, PIPE_SHADER_VERTEX
,
771 s
- NINE_SAMPLER_VS(0), &samp
);
774 commit_samplers
= TRUE
;
775 state
->changed
.sampler
[s
] = ~0;
778 state
->bound_samplers_mask_vs
|= (1 << s
);
780 commit_views
|= (state
->changed
.texture
& NINE_VS_SAMPLERS_MASK
) != 0;
781 commit_views
|= state
->changed
.srgb
;
783 pipe
->set_sampler_views(pipe
, PIPE_SHADER_VERTEX
, 0,
787 cso_single_sampler_done(device
->cso
, PIPE_SHADER_VERTEX
);
789 state
->changed
.srgb
= FALSE
;
790 state
->changed
.texture
= 0;
794 #define NINE_STATE_FREQ_GROUP_0 \
796 NINE_STATE_VIEWPORT | \
797 NINE_STATE_SCISSOR | \
800 NINE_STATE_RASTERIZER | \
803 NINE_STATE_BLEND_COLOR | \
804 NINE_STATE_STENCIL_REF | \
805 NINE_STATE_SAMPLE_MASK)
807 #define NINE_STATE_FREQ_GROUP_1 ~NINE_STATE_FREQ_GROUP_0
809 #define NINE_STATE_SHADER_VARIANT_GROUP \
810 (NINE_STATE_TEXTURE | \
815 nine_update_state(struct NineDevice9
*device
, uint32_t mask
)
817 struct pipe_context
*pipe
= device
->pipe
;
818 struct nine_state
*state
= &device
->state
;
821 DBG("changed state groups: %x | %x\n",
822 state
->changed
.group
& NINE_STATE_FREQ_GROUP_0
,
823 state
->changed
.group
& NINE_STATE_FREQ_GROUP_1
);
825 /* NOTE: We may want to use the cso cache for everything, or let
826 * NineDevice9.RestoreNonCSOState actually set the states, then we wouldn't
827 * have to care about state being clobbered here and could merge this back
828 * into update_textures. Except, we also need to re-validate textures that
829 * may be dirty anyway, even if no texture bindings changed.
831 validate_textures(device
); /* may clobber state */
833 /* ff_update may change VS/PS dirty bits */
834 if ((mask
& NINE_STATE_FF
) && unlikely(!state
->vs
|| !state
->ps
))
835 nine_ff_update(device
);
836 group
= state
->changed
.group
& mask
;
838 if (group
& NINE_STATE_SHADER_VARIANT_GROUP
)
839 group
|= update_shader_variant_keys(device
);
841 if (group
& NINE_STATE_FREQ_GROUP_0
) {
842 if (group
& NINE_STATE_FB
)
843 group
= update_framebuffer(device
) & mask
;
844 if (group
& NINE_STATE_VIEWPORT
)
845 update_viewport(device
);
846 if (group
& NINE_STATE_SCISSOR
)
847 update_scissor(device
);
849 if (group
& NINE_STATE_DSA
)
851 if (group
& NINE_STATE_BLEND
)
852 update_blend(device
);
854 if (group
& NINE_STATE_VS
)
855 group
|= update_vs(device
);
857 if (group
& NINE_STATE_RASTERIZER
)
858 update_rasterizer(device
);
860 if (group
& NINE_STATE_PS
)
861 group
|= update_ps(device
);
863 if (group
& NINE_STATE_BLEND_COLOR
) {
864 struct pipe_blend_color color
;
865 d3dcolor_to_rgba(&color
.color
[0], state
->rs
[D3DRS_BLENDFACTOR
]);
866 pipe
->set_blend_color(pipe
, &color
);
868 if (group
& NINE_STATE_SAMPLE_MASK
) {
869 pipe
->set_sample_mask(pipe
, state
->rs
[D3DRS_MULTISAMPLEMASK
]);
871 if (group
& NINE_STATE_STENCIL_REF
) {
872 struct pipe_stencil_ref ref
;
873 ref
.ref_value
[0] = state
->rs
[D3DRS_STENCILREF
];
874 ref
.ref_value
[1] = ref
.ref_value
[0];
875 pipe
->set_stencil_ref(pipe
, &ref
);
879 if (state
->changed
.ucp
) {
880 pipe
->set_clip_state(pipe
, &state
->clip
);
881 state
->changed
.ucp
= 0;
884 if (group
& (NINE_STATE_FREQ_GROUP_1
| NINE_STATE_VS
)) {
885 if (group
& (NINE_STATE_TEXTURE
| NINE_STATE_SAMPLER
))
886 update_textures_and_samplers(device
);
888 if (group
& NINE_STATE_IDXBUF
)
889 update_index_buffer(device
);
891 if ((group
& (NINE_STATE_VDECL
| NINE_STATE_VS
)) ||
892 state
->changed
.stream_freq
& ~1)
893 update_vertex_elements(device
);
895 if (device
->prefer_user_constbuf
) {
896 if ((group
& (NINE_STATE_VS_CONST
| NINE_STATE_VS
)) && state
->vs
)
897 update_vs_constants_userbuf(device
);
898 if ((group
& (NINE_STATE_PS_CONST
| NINE_STATE_PS
)) && state
->ps
)
899 update_ps_constants_userbuf(device
);
901 if ((group
& NINE_STATE_VS_CONST
) && state
->vs
)
902 update_constants(device
, PIPE_SHADER_VERTEX
);
903 if ((group
& NINE_STATE_PS_CONST
) && state
->ps
)
904 update_constants(device
, PIPE_SHADER_FRAGMENT
);
907 if (state
->changed
.vtxbuf
)
908 update_vertex_buffers(device
);
910 device
->state
.changed
.group
&= ~mask
|
911 (NINE_STATE_FF
| NINE_STATE_VS_CONST
| NINE_STATE_PS_CONST
);
919 static const DWORD nine_render_state_defaults
[NINED3DRS_LAST
+ 1] =
921 /* [D3DRS_ZENABLE] = D3DZB_TRUE; wine: auto_depth_stencil */
922 [D3DRS_ZENABLE
] = D3DZB_FALSE
,
923 [D3DRS_FILLMODE
] = D3DFILL_SOLID
,
924 [D3DRS_SHADEMODE
] = D3DSHADE_GOURAUD
,
925 /* [D3DRS_LINEPATTERN] = 0x00000000, */
926 [D3DRS_ZWRITEENABLE
] = TRUE
,
927 [D3DRS_ALPHATESTENABLE
] = FALSE
,
928 [D3DRS_LASTPIXEL
] = TRUE
,
929 [D3DRS_SRCBLEND
] = D3DBLEND_ONE
,
930 [D3DRS_DESTBLEND
] = D3DBLEND_ZERO
,
931 [D3DRS_CULLMODE
] = D3DCULL_CCW
,
932 [D3DRS_ZFUNC
] = D3DCMP_LESSEQUAL
,
933 [D3DRS_ALPHAFUNC
] = D3DCMP_ALWAYS
,
934 [D3DRS_ALPHAREF
] = 0,
935 [D3DRS_DITHERENABLE
] = FALSE
,
936 [D3DRS_ALPHABLENDENABLE
] = FALSE
,
937 [D3DRS_FOGENABLE
] = FALSE
,
938 [D3DRS_SPECULARENABLE
] = FALSE
,
939 /* [D3DRS_ZVISIBLE] = 0, */
940 [D3DRS_FOGCOLOR
] = 0,
941 [D3DRS_FOGTABLEMODE
] = D3DFOG_NONE
,
942 [D3DRS_FOGSTART
] = 0x00000000,
943 [D3DRS_FOGEND
] = 0x3F800000,
944 [D3DRS_FOGDENSITY
] = 0x3F800000,
945 /* [D3DRS_EDGEANTIALIAS] = FALSE, */
946 [D3DRS_RANGEFOGENABLE
] = FALSE
,
947 [D3DRS_STENCILENABLE
] = FALSE
,
948 [D3DRS_STENCILFAIL
] = D3DSTENCILOP_KEEP
,
949 [D3DRS_STENCILZFAIL
] = D3DSTENCILOP_KEEP
,
950 [D3DRS_STENCILPASS
] = D3DSTENCILOP_KEEP
,
951 [D3DRS_STENCILREF
] = 0,
952 [D3DRS_STENCILMASK
] = 0xFFFFFFFF,
953 [D3DRS_STENCILFUNC
] = D3DCMP_ALWAYS
,
954 [D3DRS_STENCILWRITEMASK
] = 0xFFFFFFFF,
955 [D3DRS_TEXTUREFACTOR
] = 0xFFFFFFFF,
964 [D3DRS_CLIPPING
] = TRUE
,
965 [D3DRS_LIGHTING
] = TRUE
,
967 [D3DRS_FOGVERTEXMODE
] = D3DFOG_NONE
,
968 [D3DRS_COLORVERTEX
] = TRUE
,
969 [D3DRS_LOCALVIEWER
] = TRUE
,
970 [D3DRS_NORMALIZENORMALS
] = FALSE
,
971 [D3DRS_DIFFUSEMATERIALSOURCE
] = D3DMCS_COLOR1
,
972 [D3DRS_SPECULARMATERIALSOURCE
] = D3DMCS_COLOR2
,
973 [D3DRS_AMBIENTMATERIALSOURCE
] = D3DMCS_MATERIAL
,
974 [D3DRS_EMISSIVEMATERIALSOURCE
] = D3DMCS_MATERIAL
,
975 [D3DRS_VERTEXBLEND
] = D3DVBF_DISABLE
,
976 [D3DRS_CLIPPLANEENABLE
] = 0,
977 /* [D3DRS_SOFTWAREVERTEXPROCESSING] = FALSE, */
978 [D3DRS_POINTSIZE
] = 0x3F800000,
979 [D3DRS_POINTSIZE_MIN
] = 0x3F800000,
980 [D3DRS_POINTSPRITEENABLE
] = FALSE
,
981 [D3DRS_POINTSCALEENABLE
] = FALSE
,
982 [D3DRS_POINTSCALE_A
] = 0x3F800000,
983 [D3DRS_POINTSCALE_B
] = 0x00000000,
984 [D3DRS_POINTSCALE_C
] = 0x00000000,
985 [D3DRS_MULTISAMPLEANTIALIAS
] = TRUE
,
986 [D3DRS_MULTISAMPLEMASK
] = 0xFFFFFFFF,
987 [D3DRS_PATCHEDGESTYLE
] = D3DPATCHEDGE_DISCRETE
,
988 /* [D3DRS_PATCHSEGMENTS] = 0x3F800000, */
989 [D3DRS_DEBUGMONITORTOKEN
] = 0xDEADCAFE,
990 [D3DRS_POINTSIZE_MAX
] = 0x3F800000, /* depends on cap */
991 [D3DRS_INDEXEDVERTEXBLENDENABLE
] = FALSE
,
992 [D3DRS_COLORWRITEENABLE
] = 0x0000000f,
993 [D3DRS_TWEENFACTOR
] = 0x00000000,
994 [D3DRS_BLENDOP
] = D3DBLENDOP_ADD
,
995 [D3DRS_POSITIONDEGREE
] = D3DDEGREE_CUBIC
,
996 [D3DRS_NORMALDEGREE
] = D3DDEGREE_LINEAR
,
997 [D3DRS_SCISSORTESTENABLE
] = FALSE
,
998 [D3DRS_SLOPESCALEDEPTHBIAS
] = 0,
999 [D3DRS_MINTESSELLATIONLEVEL
] = 0x3F800000,
1000 [D3DRS_MAXTESSELLATIONLEVEL
] = 0x3F800000,
1001 [D3DRS_ANTIALIASEDLINEENABLE
] = FALSE
,
1002 [D3DRS_ADAPTIVETESS_X
] = 0x00000000,
1003 [D3DRS_ADAPTIVETESS_Y
] = 0x00000000,
1004 [D3DRS_ADAPTIVETESS_Z
] = 0x3F800000,
1005 [D3DRS_ADAPTIVETESS_W
] = 0x00000000,
1006 [D3DRS_ENABLEADAPTIVETESSELLATION
] = FALSE
,
1007 [D3DRS_TWOSIDEDSTENCILMODE
] = FALSE
,
1008 [D3DRS_CCW_STENCILFAIL
] = D3DSTENCILOP_KEEP
,
1009 [D3DRS_CCW_STENCILZFAIL
] = D3DSTENCILOP_KEEP
,
1010 [D3DRS_CCW_STENCILPASS
] = D3DSTENCILOP_KEEP
,
1011 [D3DRS_CCW_STENCILFUNC
] = D3DCMP_ALWAYS
,
1012 [D3DRS_COLORWRITEENABLE1
] = 0x0000000F,
1013 [D3DRS_COLORWRITEENABLE2
] = 0x0000000F,
1014 [D3DRS_COLORWRITEENABLE3
] = 0x0000000F,
1015 [D3DRS_BLENDFACTOR
] = 0xFFFFFFFF,
1016 [D3DRS_SRGBWRITEENABLE
] = 0,
1017 [D3DRS_DEPTHBIAS
] = 0,
1026 [D3DRS_SEPARATEALPHABLENDENABLE
] = FALSE
,
1027 [D3DRS_SRCBLENDALPHA
] = D3DBLEND_ONE
,
1028 [D3DRS_DESTBLENDALPHA
] = D3DBLEND_ZERO
,
1029 [D3DRS_BLENDOPALPHA
] = D3DBLENDOP_ADD
,
1030 [NINED3DRS_VSPOINTSIZE
] = FALSE
,
1031 [NINED3DRS_RTMASK
] = 0xf,
1032 [NINED3DRS_ALPHACOVERAGE
] = FALSE
1034 static const DWORD nine_tex_stage_state_defaults
[NINED3DTSS_LAST
+ 1] =
1036 [D3DTSS_COLOROP
] = D3DTOP_DISABLE
,
1037 [D3DTSS_ALPHAOP
] = D3DTOP_DISABLE
,
1038 [D3DTSS_COLORARG1
] = D3DTA_TEXTURE
,
1039 [D3DTSS_COLORARG2
] = D3DTA_CURRENT
,
1040 [D3DTSS_COLORARG0
] = D3DTA_CURRENT
,
1041 [D3DTSS_ALPHAARG1
] = D3DTA_TEXTURE
,
1042 [D3DTSS_ALPHAARG2
] = D3DTA_CURRENT
,
1043 [D3DTSS_ALPHAARG0
] = D3DTA_CURRENT
,
1044 [D3DTSS_RESULTARG
] = D3DTA_CURRENT
,
1045 [D3DTSS_BUMPENVMAT00
] = 0,
1046 [D3DTSS_BUMPENVMAT01
] = 0,
1047 [D3DTSS_BUMPENVMAT10
] = 0,
1048 [D3DTSS_BUMPENVMAT11
] = 0,
1049 [D3DTSS_BUMPENVLSCALE
] = 0,
1050 [D3DTSS_BUMPENVLOFFSET
] = 0,
1051 [D3DTSS_TEXCOORDINDEX
] = 0,
1052 [D3DTSS_TEXTURETRANSFORMFLAGS
] = D3DTTFF_DISABLE
,
1054 static const DWORD nine_samp_state_defaults
[NINED3DSAMP_LAST
+ 1] =
1056 [D3DSAMP_ADDRESSU
] = D3DTADDRESS_WRAP
,
1057 [D3DSAMP_ADDRESSV
] = D3DTADDRESS_WRAP
,
1058 [D3DSAMP_ADDRESSW
] = D3DTADDRESS_WRAP
,
1059 [D3DSAMP_BORDERCOLOR
] = 0,
1060 [D3DSAMP_MAGFILTER
] = D3DTEXF_POINT
,
1061 [D3DSAMP_MINFILTER
] = D3DTEXF_POINT
,
1062 [D3DSAMP_MIPFILTER
] = D3DTEXF_NONE
,
1063 [D3DSAMP_MIPMAPLODBIAS
] = 0,
1064 [D3DSAMP_MAXMIPLEVEL
] = 0,
1065 [D3DSAMP_MAXANISOTROPY
] = 1,
1066 [D3DSAMP_SRGBTEXTURE
] = 0,
1067 [D3DSAMP_ELEMENTINDEX
] = 0,
1068 [D3DSAMP_DMAPOFFSET
] = 0,
1069 [NINED3DSAMP_MINLOD
] = 0,
1070 [NINED3DSAMP_SHADOW
] = 0
1073 nine_state_set_defaults(struct NineDevice9
*device
, const D3DCAPS9
*caps
,
1076 struct nine_state
*state
= &device
->state
;
1079 /* Initialize defaults.
1081 memcpy(state
->rs
, nine_render_state_defaults
, sizeof(state
->rs
));
1083 for (s
= 0; s
< Elements(state
->ff
.tex_stage
); ++s
) {
1084 memcpy(&state
->ff
.tex_stage
[s
], nine_tex_stage_state_defaults
,
1085 sizeof(state
->ff
.tex_stage
[s
]));
1086 state
->ff
.tex_stage
[s
][D3DTSS_TEXCOORDINDEX
] = s
;
1088 state
->ff
.tex_stage
[0][D3DTSS_COLOROP
] = D3DTOP_MODULATE
;
1089 state
->ff
.tex_stage
[0][D3DTSS_ALPHAOP
] = D3DTOP_SELECTARG1
;
1091 for (s
= 0; s
< Elements(state
->samp
); ++s
) {
1092 memcpy(&state
->samp
[s
], nine_samp_state_defaults
,
1093 sizeof(state
->samp
[s
]));
1096 if (state
->vs_const_f
)
1097 memset(state
->vs_const_f
, 0, device
->vs_const_size
);
1098 if (state
->ps_const_f
)
1099 memset(state
->ps_const_f
, 0, device
->ps_const_size
);
1101 /* Cap dependent initial state:
1103 state
->rs
[D3DRS_POINTSIZE_MAX
] = fui(caps
->MaxPointSize
);
1105 /* Set changed flags to initialize driver.
1107 state
->changed
.group
= NINE_STATE_ALL
;
1109 state
->ff
.changed
.transform
[0] = ~0;
1110 state
->ff
.changed
.transform
[D3DTS_WORLD
/ 32] |= 1 << (D3DTS_WORLD
% 32);
1113 state
->viewport
.MinZ
= 0.0f
;
1114 state
->viewport
.MaxZ
= 1.0f
;
1117 for (s
= 0; s
< Elements(state
->changed
.sampler
); ++s
)
1118 state
->changed
.sampler
[s
] = ~0;
1122 nine_state_clear(struct nine_state
*state
, const boolean device
)
1126 for (i
= 0; i
< Elements(state
->rt
); ++i
)
1127 nine_bind(&state
->rt
[i
], NULL
);
1128 nine_bind(&state
->ds
, NULL
);
1129 nine_bind(&state
->vs
, NULL
);
1130 nine_bind(&state
->ps
, NULL
);
1131 nine_bind(&state
->vdecl
, NULL
);
1132 for (i
= 0; i
< PIPE_MAX_ATTRIBS
; ++i
)
1133 nine_bind(&state
->stream
[i
], NULL
);
1134 nine_bind(&state
->idxbuf
, NULL
);
1135 for (i
= 0; i
< NINE_MAX_SAMPLERS
; ++i
) {
1137 state
->texture
[i
] &&
1138 --state
->texture
[i
]->bind_count
== 0)
1139 list_delinit(&state
->texture
[i
]->list
);
1140 nine_bind(&state
->texture
[i
], NULL
);
1145 static const DWORD nine_render_states_pixel[] =
1147 D3DRS_ALPHABLENDENABLE,
1150 D3DRS_ALPHATESTENABLE,
1151 D3DRS_ANTIALIASEDLINEENABLE,
1155 D3DRS_CCW_STENCILFAIL,
1156 D3DRS_CCW_STENCILPASS,
1157 D3DRS_CCW_STENCILZFAIL,
1158 D3DRS_COLORWRITEENABLE,
1159 D3DRS_COLORWRITEENABLE1,
1160 D3DRS_COLORWRITEENABLE2,
1161 D3DRS_COLORWRITEENABLE3,
1164 D3DRS_DESTBLENDALPHA,
1171 D3DRS_SCISSORTESTENABLE,
1172 D3DRS_SEPARATEALPHABLENDENABLE,
1174 D3DRS_SLOPESCALEDEPTHBIAS,
1176 D3DRS_SRCBLENDALPHA,
1177 D3DRS_SRGBWRITEENABLE,
1178 D3DRS_STENCILENABLE,
1184 D3DRS_STENCILWRITEMASK,
1186 D3DRS_TEXTUREFACTOR,
1187 D3DRS_TWOSIDEDSTENCILMODE,
1209 const uint32_t nine_render_states_pixel
[(NINED3DRS_LAST
+ 31) / 32] =
1211 0x0f99c380, 0x1ff00070, 0x00000000, 0x00000000,
1212 0x000000ff, 0xde01c900, 0x0003ffcf
1216 static const DWORD nine_render_states_vertex[] =
1218 D3DRS_ADAPTIVETESS_W,
1219 D3DRS_ADAPTIVETESS_X,
1220 D3DRS_ADAPTIVETESS_Y,
1221 D3DRS_ADAPTIVETESS_Z,
1223 D3DRS_AMBIENTMATERIALSOURCE,
1225 D3DRS_CLIPPLANEENABLE,
1228 D3DRS_DIFFUSEMATERIALSOURCE,
1229 D3DRS_EMISSIVEMATERIALSOURCE,
1230 D3DRS_ENABLEADAPTIVETESSELLATION,
1237 D3DRS_FOGVERTEXMODE,
1238 D3DRS_INDEXEDVERTEXBLENDENABLE,
1241 D3DRS_MAXTESSELLATIONLEVEL,
1242 D3DRS_MINTESSELLATIONLEVEL,
1243 D3DRS_MULTISAMPLEANTIALIAS,
1244 D3DRS_MULTISAMPLEMASK,
1246 D3DRS_NORMALIZENORMALS,
1247 D3DRS_PATCHEDGESTYLE,
1251 D3DRS_POINTSCALEENABLE,
1253 D3DRS_POINTSIZE_MAX,
1254 D3DRS_POINTSIZE_MIN,
1255 D3DRS_POINTSPRITEENABLE,
1256 D3DRS_POSITIONDEGREE,
1257 D3DRS_RANGEFOGENABLE,
1259 D3DRS_SPECULARENABLE,
1260 D3DRS_SPECULARMATERIALSOURCE,
1265 const uint32_t nine_render_states_vertex
[(NINED3DRS_LAST
+ 31) / 32] =
1267 0x30400200, 0x0001007c, 0x00000000, 0x00000000,
1268 0xfd9efb00, 0x01fc34cf, 0x00000000
1271 /* TODO: put in the right values */
1272 const uint32_t nine_render_state_group
[NINED3DRS_LAST
+ 1] =
1274 [D3DRS_ZENABLE
] = NINE_STATE_DSA
,
1275 [D3DRS_FILLMODE
] = NINE_STATE_RASTERIZER
,
1276 [D3DRS_SHADEMODE
] = NINE_STATE_RASTERIZER
,
1277 [D3DRS_ZWRITEENABLE
] = NINE_STATE_DSA
,
1278 [D3DRS_ALPHATESTENABLE
] = NINE_STATE_DSA
,
1279 [D3DRS_LASTPIXEL
] = NINE_STATE_RASTERIZER
,
1280 [D3DRS_SRCBLEND
] = NINE_STATE_BLEND
,
1281 [D3DRS_DESTBLEND
] = NINE_STATE_BLEND
,
1282 [D3DRS_CULLMODE
] = NINE_STATE_RASTERIZER
,
1283 [D3DRS_ZFUNC
] = NINE_STATE_DSA
,
1284 [D3DRS_ALPHAREF
] = NINE_STATE_DSA
,
1285 [D3DRS_ALPHAFUNC
] = NINE_STATE_DSA
,
1286 [D3DRS_DITHERENABLE
] = NINE_STATE_RASTERIZER
,
1287 [D3DRS_ALPHABLENDENABLE
] = NINE_STATE_BLEND
,
1288 [D3DRS_FOGENABLE
] = NINE_STATE_FF_OTHER
,
1289 [D3DRS_SPECULARENABLE
] = NINE_STATE_FF_LIGHTING
,
1290 [D3DRS_FOGCOLOR
] = NINE_STATE_FF_OTHER
,
1291 [D3DRS_FOGTABLEMODE
] = NINE_STATE_FF_OTHER
,
1292 [D3DRS_FOGSTART
] = NINE_STATE_FF_OTHER
,
1293 [D3DRS_FOGEND
] = NINE_STATE_FF_OTHER
,
1294 [D3DRS_FOGDENSITY
] = NINE_STATE_FF_OTHER
,
1295 [D3DRS_RANGEFOGENABLE
] = NINE_STATE_FF_OTHER
,
1296 [D3DRS_STENCILENABLE
] = NINE_STATE_DSA
,
1297 [D3DRS_STENCILFAIL
] = NINE_STATE_DSA
,
1298 [D3DRS_STENCILZFAIL
] = NINE_STATE_DSA
,
1299 [D3DRS_STENCILPASS
] = NINE_STATE_DSA
,
1300 [D3DRS_STENCILFUNC
] = NINE_STATE_DSA
,
1301 [D3DRS_STENCILREF
] = NINE_STATE_STENCIL_REF
,
1302 [D3DRS_STENCILMASK
] = NINE_STATE_DSA
,
1303 [D3DRS_STENCILWRITEMASK
] = NINE_STATE_DSA
,
1304 [D3DRS_TEXTUREFACTOR
] = NINE_STATE_FF_PSSTAGES
,
1305 [D3DRS_WRAP0
] = NINE_STATE_UNHANDLED
, /* cylindrical wrap is crazy */
1306 [D3DRS_WRAP1
] = NINE_STATE_UNHANDLED
,
1307 [D3DRS_WRAP2
] = NINE_STATE_UNHANDLED
,
1308 [D3DRS_WRAP3
] = NINE_STATE_UNHANDLED
,
1309 [D3DRS_WRAP4
] = NINE_STATE_UNHANDLED
,
1310 [D3DRS_WRAP5
] = NINE_STATE_UNHANDLED
,
1311 [D3DRS_WRAP6
] = NINE_STATE_UNHANDLED
,
1312 [D3DRS_WRAP7
] = NINE_STATE_UNHANDLED
,
1313 [D3DRS_CLIPPING
] = 0, /* software vertex processing only */
1314 [D3DRS_LIGHTING
] = NINE_STATE_FF_LIGHTING
,
1315 [D3DRS_AMBIENT
] = NINE_STATE_FF_LIGHTING
| NINE_STATE_FF_MATERIAL
,
1316 [D3DRS_FOGVERTEXMODE
] = NINE_STATE_FF_OTHER
,
1317 [D3DRS_COLORVERTEX
] = NINE_STATE_FF_LIGHTING
,
1318 [D3DRS_LOCALVIEWER
] = NINE_STATE_FF_LIGHTING
,
1319 [D3DRS_NORMALIZENORMALS
] = NINE_STATE_FF_OTHER
,
1320 [D3DRS_DIFFUSEMATERIALSOURCE
] = NINE_STATE_FF_LIGHTING
,
1321 [D3DRS_SPECULARMATERIALSOURCE
] = NINE_STATE_FF_LIGHTING
,
1322 [D3DRS_AMBIENTMATERIALSOURCE
] = NINE_STATE_FF_LIGHTING
,
1323 [D3DRS_EMISSIVEMATERIALSOURCE
] = NINE_STATE_FF_LIGHTING
,
1324 [D3DRS_VERTEXBLEND
] = NINE_STATE_FF_OTHER
,
1325 [D3DRS_CLIPPLANEENABLE
] = NINE_STATE_RASTERIZER
,
1326 [D3DRS_POINTSIZE
] = NINE_STATE_RASTERIZER
,
1327 [D3DRS_POINTSIZE_MIN
] = NINE_STATE_MISC_CONST
,
1328 [D3DRS_POINTSPRITEENABLE
] = NINE_STATE_RASTERIZER
,
1329 [D3DRS_POINTSCALEENABLE
] = NINE_STATE_FF_OTHER
,
1330 [D3DRS_POINTSCALE_A
] = NINE_STATE_FF_OTHER
,
1331 [D3DRS_POINTSCALE_B
] = NINE_STATE_FF_OTHER
,
1332 [D3DRS_POINTSCALE_C
] = NINE_STATE_FF_OTHER
,
1333 [D3DRS_MULTISAMPLEANTIALIAS
] = NINE_STATE_RASTERIZER
,
1334 [D3DRS_MULTISAMPLEMASK
] = NINE_STATE_SAMPLE_MASK
,
1335 [D3DRS_PATCHEDGESTYLE
] = NINE_STATE_UNHANDLED
,
1336 [D3DRS_DEBUGMONITORTOKEN
] = NINE_STATE_UNHANDLED
,
1337 [D3DRS_POINTSIZE_MAX
] = NINE_STATE_MISC_CONST
,
1338 [D3DRS_INDEXEDVERTEXBLENDENABLE
] = NINE_STATE_FF_OTHER
,
1339 [D3DRS_COLORWRITEENABLE
] = NINE_STATE_BLEND
,
1340 [D3DRS_TWEENFACTOR
] = NINE_STATE_FF_OTHER
,
1341 [D3DRS_BLENDOP
] = NINE_STATE_BLEND
,
1342 [D3DRS_POSITIONDEGREE
] = NINE_STATE_UNHANDLED
,
1343 [D3DRS_NORMALDEGREE
] = NINE_STATE_UNHANDLED
,
1344 [D3DRS_SCISSORTESTENABLE
] = NINE_STATE_RASTERIZER
,
1345 [D3DRS_SLOPESCALEDEPTHBIAS
] = NINE_STATE_RASTERIZER
,
1346 [D3DRS_ANTIALIASEDLINEENABLE
] = NINE_STATE_RASTERIZER
,
1347 [D3DRS_MINTESSELLATIONLEVEL
] = NINE_STATE_UNHANDLED
,
1348 [D3DRS_MAXTESSELLATIONLEVEL
] = NINE_STATE_UNHANDLED
,
1349 [D3DRS_ADAPTIVETESS_X
] = NINE_STATE_UNHANDLED
,
1350 [D3DRS_ADAPTIVETESS_Y
] = NINE_STATE_UNHANDLED
,
1351 [D3DRS_ADAPTIVETESS_Z
] = NINE_STATE_UNHANDLED
,
1352 [D3DRS_ADAPTIVETESS_W
] = NINE_STATE_UNHANDLED
,
1353 [D3DRS_ENABLEADAPTIVETESSELLATION
] = NINE_STATE_UNHANDLED
,
1354 [D3DRS_TWOSIDEDSTENCILMODE
] = NINE_STATE_DSA
,
1355 [D3DRS_CCW_STENCILFAIL
] = NINE_STATE_DSA
,
1356 [D3DRS_CCW_STENCILZFAIL
] = NINE_STATE_DSA
,
1357 [D3DRS_CCW_STENCILPASS
] = NINE_STATE_DSA
,
1358 [D3DRS_CCW_STENCILFUNC
] = NINE_STATE_DSA
,
1359 [D3DRS_COLORWRITEENABLE1
] = NINE_STATE_BLEND
,
1360 [D3DRS_COLORWRITEENABLE2
] = NINE_STATE_BLEND
,
1361 [D3DRS_COLORWRITEENABLE3
] = NINE_STATE_BLEND
,
1362 [D3DRS_BLENDFACTOR
] = NINE_STATE_BLEND_COLOR
,
1363 [D3DRS_SRGBWRITEENABLE
] = NINE_STATE_FB
,
1364 [D3DRS_DEPTHBIAS
] = NINE_STATE_RASTERIZER
,
1365 [D3DRS_WRAP8
] = NINE_STATE_UNHANDLED
, /* cylwrap has to be done via GP */
1366 [D3DRS_WRAP9
] = NINE_STATE_UNHANDLED
,
1367 [D3DRS_WRAP10
] = NINE_STATE_UNHANDLED
,
1368 [D3DRS_WRAP11
] = NINE_STATE_UNHANDLED
,
1369 [D3DRS_WRAP12
] = NINE_STATE_UNHANDLED
,
1370 [D3DRS_WRAP13
] = NINE_STATE_UNHANDLED
,
1371 [D3DRS_WRAP14
] = NINE_STATE_UNHANDLED
,
1372 [D3DRS_WRAP15
] = NINE_STATE_UNHANDLED
,
1373 [D3DRS_SEPARATEALPHABLENDENABLE
] = NINE_STATE_BLEND
,
1374 [D3DRS_SRCBLENDALPHA
] = NINE_STATE_BLEND
,
1375 [D3DRS_DESTBLENDALPHA
] = NINE_STATE_BLEND
,
1376 [D3DRS_BLENDOPALPHA
] = NINE_STATE_BLEND
1380 nine_state_access_transform(struct nine_state
*state
, D3DTRANSFORMSTATETYPE t
,
1383 static D3DMATRIX Identity
= { .m
[0] = { 1, 0, 0, 0 },
1384 .m
[1] = { 0, 1, 0, 0 },
1385 .m
[2] = { 0, 0, 1, 0 },
1386 .m
[3] = { 0, 0, 0, 1 } };
1390 case D3DTS_VIEW
: index
= 0; break;
1391 case D3DTS_PROJECTION
: index
= 1; break;
1392 case D3DTS_TEXTURE0
: index
= 2; break;
1393 case D3DTS_TEXTURE1
: index
= 3; break;
1394 case D3DTS_TEXTURE2
: index
= 4; break;
1395 case D3DTS_TEXTURE3
: index
= 5; break;
1396 case D3DTS_TEXTURE4
: index
= 6; break;
1397 case D3DTS_TEXTURE5
: index
= 7; break;
1398 case D3DTS_TEXTURE6
: index
= 8; break;
1399 case D3DTS_TEXTURE7
: index
= 9; break;
1401 if (!(t
>= D3DTS_WORLDMATRIX(0) && t
<= D3DTS_WORLDMATRIX(255)))
1403 index
= 10 + (t
- D3DTS_WORLDMATRIX(0));
1407 if (index
>= state
->ff
.num_transforms
) {
1408 unsigned N
= index
+ 1;
1409 unsigned n
= state
->ff
.num_transforms
;
1413 state
->ff
.transform
= REALLOC(state
->ff
.transform
,
1414 n
* sizeof(D3DMATRIX
),
1415 N
* sizeof(D3DMATRIX
));
1417 state
->ff
.transform
[n
] = Identity
;
1418 state
->ff
.num_transforms
= N
;
1420 return &state
->ff
.transform
[index
];
1423 #define D3DRS_TO_STRING_CASE(n) case D3DRS_##n: return "D3DRS_"#n
1424 const char *nine_d3drs_to_string(DWORD State
)
1427 D3DRS_TO_STRING_CASE(ZENABLE
);
1428 D3DRS_TO_STRING_CASE(FILLMODE
);
1429 D3DRS_TO_STRING_CASE(SHADEMODE
);
1430 D3DRS_TO_STRING_CASE(ZWRITEENABLE
);
1431 D3DRS_TO_STRING_CASE(ALPHATESTENABLE
);
1432 D3DRS_TO_STRING_CASE(LASTPIXEL
);
1433 D3DRS_TO_STRING_CASE(SRCBLEND
);
1434 D3DRS_TO_STRING_CASE(DESTBLEND
);
1435 D3DRS_TO_STRING_CASE(CULLMODE
);
1436 D3DRS_TO_STRING_CASE(ZFUNC
);
1437 D3DRS_TO_STRING_CASE(ALPHAREF
);
1438 D3DRS_TO_STRING_CASE(ALPHAFUNC
);
1439 D3DRS_TO_STRING_CASE(DITHERENABLE
);
1440 D3DRS_TO_STRING_CASE(ALPHABLENDENABLE
);
1441 D3DRS_TO_STRING_CASE(FOGENABLE
);
1442 D3DRS_TO_STRING_CASE(SPECULARENABLE
);
1443 D3DRS_TO_STRING_CASE(FOGCOLOR
);
1444 D3DRS_TO_STRING_CASE(FOGTABLEMODE
);
1445 D3DRS_TO_STRING_CASE(FOGSTART
);
1446 D3DRS_TO_STRING_CASE(FOGEND
);
1447 D3DRS_TO_STRING_CASE(FOGDENSITY
);
1448 D3DRS_TO_STRING_CASE(RANGEFOGENABLE
);
1449 D3DRS_TO_STRING_CASE(STENCILENABLE
);
1450 D3DRS_TO_STRING_CASE(STENCILFAIL
);
1451 D3DRS_TO_STRING_CASE(STENCILZFAIL
);
1452 D3DRS_TO_STRING_CASE(STENCILPASS
);
1453 D3DRS_TO_STRING_CASE(STENCILFUNC
);
1454 D3DRS_TO_STRING_CASE(STENCILREF
);
1455 D3DRS_TO_STRING_CASE(STENCILMASK
);
1456 D3DRS_TO_STRING_CASE(STENCILWRITEMASK
);
1457 D3DRS_TO_STRING_CASE(TEXTUREFACTOR
);
1458 D3DRS_TO_STRING_CASE(WRAP0
);
1459 D3DRS_TO_STRING_CASE(WRAP1
);
1460 D3DRS_TO_STRING_CASE(WRAP2
);
1461 D3DRS_TO_STRING_CASE(WRAP3
);
1462 D3DRS_TO_STRING_CASE(WRAP4
);
1463 D3DRS_TO_STRING_CASE(WRAP5
);
1464 D3DRS_TO_STRING_CASE(WRAP6
);
1465 D3DRS_TO_STRING_CASE(WRAP7
);
1466 D3DRS_TO_STRING_CASE(CLIPPING
);
1467 D3DRS_TO_STRING_CASE(LIGHTING
);
1468 D3DRS_TO_STRING_CASE(AMBIENT
);
1469 D3DRS_TO_STRING_CASE(FOGVERTEXMODE
);
1470 D3DRS_TO_STRING_CASE(COLORVERTEX
);
1471 D3DRS_TO_STRING_CASE(LOCALVIEWER
);
1472 D3DRS_TO_STRING_CASE(NORMALIZENORMALS
);
1473 D3DRS_TO_STRING_CASE(DIFFUSEMATERIALSOURCE
);
1474 D3DRS_TO_STRING_CASE(SPECULARMATERIALSOURCE
);
1475 D3DRS_TO_STRING_CASE(AMBIENTMATERIALSOURCE
);
1476 D3DRS_TO_STRING_CASE(EMISSIVEMATERIALSOURCE
);
1477 D3DRS_TO_STRING_CASE(VERTEXBLEND
);
1478 D3DRS_TO_STRING_CASE(CLIPPLANEENABLE
);
1479 D3DRS_TO_STRING_CASE(POINTSIZE
);
1480 D3DRS_TO_STRING_CASE(POINTSIZE_MIN
);
1481 D3DRS_TO_STRING_CASE(POINTSPRITEENABLE
);
1482 D3DRS_TO_STRING_CASE(POINTSCALEENABLE
);
1483 D3DRS_TO_STRING_CASE(POINTSCALE_A
);
1484 D3DRS_TO_STRING_CASE(POINTSCALE_B
);
1485 D3DRS_TO_STRING_CASE(POINTSCALE_C
);
1486 D3DRS_TO_STRING_CASE(MULTISAMPLEANTIALIAS
);
1487 D3DRS_TO_STRING_CASE(MULTISAMPLEMASK
);
1488 D3DRS_TO_STRING_CASE(PATCHEDGESTYLE
);
1489 D3DRS_TO_STRING_CASE(DEBUGMONITORTOKEN
);
1490 D3DRS_TO_STRING_CASE(POINTSIZE_MAX
);
1491 D3DRS_TO_STRING_CASE(INDEXEDVERTEXBLENDENABLE
);
1492 D3DRS_TO_STRING_CASE(COLORWRITEENABLE
);
1493 D3DRS_TO_STRING_CASE(TWEENFACTOR
);
1494 D3DRS_TO_STRING_CASE(BLENDOP
);
1495 D3DRS_TO_STRING_CASE(POSITIONDEGREE
);
1496 D3DRS_TO_STRING_CASE(NORMALDEGREE
);
1497 D3DRS_TO_STRING_CASE(SCISSORTESTENABLE
);
1498 D3DRS_TO_STRING_CASE(SLOPESCALEDEPTHBIAS
);
1499 D3DRS_TO_STRING_CASE(ANTIALIASEDLINEENABLE
);
1500 D3DRS_TO_STRING_CASE(MINTESSELLATIONLEVEL
);
1501 D3DRS_TO_STRING_CASE(MAXTESSELLATIONLEVEL
);
1502 D3DRS_TO_STRING_CASE(ADAPTIVETESS_X
);
1503 D3DRS_TO_STRING_CASE(ADAPTIVETESS_Y
);
1504 D3DRS_TO_STRING_CASE(ADAPTIVETESS_Z
);
1505 D3DRS_TO_STRING_CASE(ADAPTIVETESS_W
);
1506 D3DRS_TO_STRING_CASE(ENABLEADAPTIVETESSELLATION
);
1507 D3DRS_TO_STRING_CASE(TWOSIDEDSTENCILMODE
);
1508 D3DRS_TO_STRING_CASE(CCW_STENCILFAIL
);
1509 D3DRS_TO_STRING_CASE(CCW_STENCILZFAIL
);
1510 D3DRS_TO_STRING_CASE(CCW_STENCILPASS
);
1511 D3DRS_TO_STRING_CASE(CCW_STENCILFUNC
);
1512 D3DRS_TO_STRING_CASE(COLORWRITEENABLE1
);
1513 D3DRS_TO_STRING_CASE(COLORWRITEENABLE2
);
1514 D3DRS_TO_STRING_CASE(COLORWRITEENABLE3
);
1515 D3DRS_TO_STRING_CASE(BLENDFACTOR
);
1516 D3DRS_TO_STRING_CASE(SRGBWRITEENABLE
);
1517 D3DRS_TO_STRING_CASE(DEPTHBIAS
);
1518 D3DRS_TO_STRING_CASE(WRAP8
);
1519 D3DRS_TO_STRING_CASE(WRAP9
);
1520 D3DRS_TO_STRING_CASE(WRAP10
);
1521 D3DRS_TO_STRING_CASE(WRAP11
);
1522 D3DRS_TO_STRING_CASE(WRAP12
);
1523 D3DRS_TO_STRING_CASE(WRAP13
);
1524 D3DRS_TO_STRING_CASE(WRAP14
);
1525 D3DRS_TO_STRING_CASE(WRAP15
);
1526 D3DRS_TO_STRING_CASE(SEPARATEALPHABLENDENABLE
);
1527 D3DRS_TO_STRING_CASE(SRCBLENDALPHA
);
1528 D3DRS_TO_STRING_CASE(DESTBLENDALPHA
);
1529 D3DRS_TO_STRING_CASE(BLENDOPALPHA
);