2
* Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* on the rights to use, copy, modify, merge, publish, distribute, sub
8
* license, and/or sell copies of the Software, and to permit persons to whom
9
* the Software is furnished to do so, subject to the following conditions:
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21
* USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
#ifndef _NINE_STATE_H_
24
#define _NINE_STATE_H_
28
#include "nine_defines.h"
29
#include "pipe/p_state.h"
30
#include "util/list.h"
32
#define NINED3DSAMP_MINLOD (D3DSAMP_DMAPOFFSET + 1)
33
#define NINED3DSAMP_SHADOW (D3DSAMP_DMAPOFFSET + 2)
34
#define NINED3DSAMP_CUBETEX (D3DSAMP_DMAPOFFSET + 3)
36
#define NINED3DRS_VSPOINTSIZE (D3DRS_BLENDOPALPHA + 1)
37
#define NINED3DRS_RTMASK (D3DRS_BLENDOPALPHA + 2)
39
* bit 0: enable alpha coverage
42
#define NINED3DRS_ALPHACOVERAGE (D3DRS_BLENDOPALPHA + 3)
43
#define NINED3DRS_MULTISAMPLE (D3DRS_BLENDOPALPHA + 4)
44
#define NINED3DRS_FETCH4 (D3DRS_BLENDOPALPHA + 5)
46
#define D3DRS_LAST D3DRS_BLENDOPALPHA
47
#define D3DSAMP_LAST D3DSAMP_DMAPOFFSET
48
#define NINED3DRS_LAST NINED3DRS_FETCH4 /* 215 */
49
#define NINED3DSAMP_LAST NINED3DSAMP_CUBETEX /* 16 */
50
#define NINED3DTSS_LAST D3DTSS_CONSTANT
51
#define NINED3DTS_LAST D3DTS_WORLDMATRIX(255)
53
#define D3DRS_COUNT (D3DRS_LAST + 1)
54
#define D3DSAMP_COUNT (D3DSAMP_LAST + 1)
55
#define NINED3DRS_COUNT (NINED3DRS_LAST + 1)
56
#define NINED3DSAMP_COUNT (NINED3DSAMP_LAST + 1)
57
#define NINED3DTSS_COUNT (NINED3DTSS_LAST + 1)
58
#define NINED3DTS_COUNT (NINED3DTS_LAST + 1)
60
#define NINE_STATE_FB (1 << 0)
61
#define NINE_STATE_VIEWPORT (1 << 1)
62
#define NINE_STATE_SCISSOR (1 << 2)
63
#define NINE_STATE_RASTERIZER (1 << 3)
64
#define NINE_STATE_BLEND (1 << 4)
65
#define NINE_STATE_DSA (1 << 5)
66
#define NINE_STATE_VS (1 << 6)
67
#define NINE_STATE_VS_CONST (1 << 7)
68
#define NINE_STATE_PS (1 << 8)
69
#define NINE_STATE_PS_CONST (1 << 9)
70
#define NINE_STATE_TEXTURE (1 << 10)
71
#define NINE_STATE_SAMPLER (1 << 11)
72
#define NINE_STATE_VDECL (1 << 12)
73
#define NINE_STATE_IDXBUF (1 << 13)
74
#define NINE_STATE_STREAMFREQ (1 << 14)
75
#define NINE_STATE_BLEND_COLOR (1 << 17)
76
#define NINE_STATE_STENCIL_REF (1 << 18)
77
#define NINE_STATE_SAMPLE_MASK (1 << 19)
78
#define NINE_STATE_FF (0x1f << 20)
79
#define NINE_STATE_FF_VS (0x17 << 20)
80
#define NINE_STATE_FF_PS (0x08 << 20)
81
#define NINE_STATE_FF_LIGHTING (1 << 20)
82
#define NINE_STATE_FF_MATERIAL (1 << 21)
83
#define NINE_STATE_FF_VSTRANSF (1 << 22)
84
#define NINE_STATE_FF_PS_CONSTS (1 << 23)
85
#define NINE_STATE_FF_VS_OTHER (1 << 24)
86
#define NINE_STATE_VS_PARAMS_MISC (1 << 25)
87
#define NINE_STATE_PS_PARAMS_MISC (1 << 26)
88
#define NINE_STATE_MULTISAMPLE (1 << 27)
89
#define NINE_STATE_SWVP (1 << 28)
90
#define NINE_STATE_ALL 0x1fffffff
91
#define NINE_STATE_UNHANDLED (1 << 29)
93
/* These states affect the ff shader key,
94
* which we recompute everytime. */
95
#define NINE_STATE_FF_SHADER 0
97
#define NINE_STATE_COMMIT_DSA (1 << 0)
98
#define NINE_STATE_COMMIT_RASTERIZER (1 << 1)
99
#define NINE_STATE_COMMIT_BLEND (1 << 2)
100
#define NINE_STATE_COMMIT_CONST_VS (1 << 3)
101
#define NINE_STATE_COMMIT_CONST_PS (1 << 4)
102
#define NINE_STATE_COMMIT_VS (1 << 5)
103
#define NINE_STATE_COMMIT_PS (1 << 6)
106
#define NINE_MAX_SIMULTANEOUS_RENDERTARGETS 4
107
#define NINE_MAX_CONST_F_PS3 224
108
#define NINE_MAX_CONST_F 256
109
#define NINE_MAX_CONST_I 16
110
#define NINE_MAX_CONST_B 16
111
#define NINE_MAX_CONST_F_SWVP 8192
112
#define NINE_MAX_CONST_I_SWVP 2048
113
#define NINE_MAX_CONST_B_SWVP 2048
114
#define NINE_MAX_CONST_ALL 276 /* B consts count only 1/4 th */
116
#define NINE_CONST_I_BASE(nconstf) \
117
((nconstf) * 4 * sizeof(float))
118
#define NINE_CONST_B_BASE(nconstf) \
119
((nconstf) * 4 * sizeof(float) + \
120
NINE_MAX_CONST_I * 4 * sizeof(int))
122
#define VS_CONST_F_SIZE(device) (device->may_swvp ? (NINE_MAX_CONST_F_SWVP * sizeof(float[4])) : (NINE_MAX_CONST_F * sizeof(float[4])))
123
#define VS_CONST_I_SIZE(device) (device->may_swvp ? (NINE_MAX_CONST_I_SWVP * sizeof(int[4])) : (NINE_MAX_CONST_I * sizeof(int[4])))
124
#define VS_CONST_B_SIZE(device) (device->may_swvp ? (NINE_MAX_CONST_B_SWVP * sizeof(BOOL)) : (NINE_MAX_CONST_B * sizeof(BOOL)))
127
#define NINE_MAX_TEXTURE_STAGES 8
129
#define NINE_MAX_LIGHTS 65536
130
#define NINE_MAX_LIGHTS_ACTIVE 8
132
#define NINED3DLIGHT_INVALID (D3DLIGHT_DIRECTIONAL + 1)
134
#define NINE_MAX_SAMPLERS_PS 16
135
#define NINE_MAX_SAMPLERS_VS 4
136
#define NINE_MAX_SAMPLERS 21 /* PS + DMAP + VS */
137
#define NINE_SAMPLER_PS(s) ( 0 + (s))
138
#define NINE_SAMPLER_DMAP 16
139
#define NINE_SAMPLER_VS(s) (17 + (s))
140
#define NINE_PS_SAMPLERS_MASK 0x00ffff
141
#define NINE_VS_SAMPLERS_MASK 0x1e0000
143
struct nine_ff_state {
145
uint32_t tex_stage[NINE_MAX_TEXTURE_STAGES][(NINED3DTSS_COUNT + 31) / 32]; /* stateblocks only */
146
uint32_t transform[(NINED3DTS_COUNT + 31) / 32];
149
D3DMATRIX *transform; /* access only via nine_state_access_transform */
150
unsigned num_transforms;
152
/* XXX: Do state blocks just change the set of active lights or do we
153
* have to store which lights have been disabled, too ?
156
uint16_t active_light[NINE_MAX_LIGHTS_ACTIVE]; /* 8 */
158
unsigned num_lights_active;
160
D3DMATERIAL9 material;
162
DWORD tex_stage[NINE_MAX_TEXTURE_STAGES][NINED3DTSS_COUNT];
169
uint32_t rs[(NINED3DRS_COUNT + 31) / 32];
171
uint32_t stream_freq;
173
uint16_t sampler[NINE_MAX_SAMPLERS];
174
struct nine_range *vs_const_f;
175
struct nine_range *ps_const_f;
176
struct nine_range *vs_const_i;
177
uint16_t ps_const_i; /* NINE_MAX_CONST_I == 16 */
178
struct nine_range *vs_const_b;
179
uint16_t ps_const_b; /* NINE_MAX_CONST_B == 16 */
181
} changed; /* stateblocks only */
183
struct NineSurface9 *rt[NINE_MAX_SIMULTANEOUS_RENDERTARGETS];
184
struct NineSurface9 *ds;
186
D3DVIEWPORT9 viewport;
188
struct pipe_scissor_state scissor;
190
/* NOTE: vs, ps will be NULL for FF and are set in device->ff.vs,ps instead
191
* (XXX: or is it better to reference FF shaders here, too ?)
192
* NOTE: const_f contains extra space for const_i,b to use as user constbuf
194
struct NineVertexShader9 *vs;
198
float *vs_lconstf_temp; /* ProcessVertices */
200
struct NinePixelShader9 *ps;
202
int ps_const_i[NINE_MAX_CONST_I][4];
203
BOOL ps_const_b[NINE_MAX_CONST_B];
205
struct NineVertexDeclaration9 *vdecl;
207
struct NineIndexBuffer9 *idxbuf;
208
struct NineVertexBuffer9 *stream[PIPE_MAX_ATTRIBS];
209
struct pipe_vertex_buffer vtxbuf[PIPE_MAX_ATTRIBS]; /* vtxbuf.buffer unused */
210
UINT stream_freq[PIPE_MAX_ATTRIBS];
212
struct pipe_clip_state clip;
214
DWORD rs_advertised[NINED3DRS_COUNT]; /* the ones apps get with GetRenderState */
216
struct NineBaseTexture9 *texture[NINE_MAX_SAMPLERS]; /* PS, DMAP, VS */
218
DWORD samp_advertised[NINE_MAX_SAMPLERS][D3DSAMP_COUNT];
220
struct nine_ff_state ff;
223
struct nine_context {
226
uint16_t sampler[NINE_MAX_SAMPLERS];
237
uint32_t bumpmap_vars[6 * NINE_MAX_TEXTURE_STAGES];
239
struct NineSurface9 *rt[NINE_MAX_SIMULTANEOUS_RENDERTARGETS];
240
struct NineSurface9 *ds;
244
unsigned *vs_const_ranges;
245
unsigned vs_const_used_size;
247
unsigned *ps_const_ranges;
248
unsigned ps_const_used_size;
251
struct pipe_context *pipe;
252
struct cso_context *cso;
256
D3DVIEWPORT9 viewport;
258
struct pipe_scissor_state scissor;
260
struct NineVertexShader9 *vs;
261
BOOL programmable_vs;
263
float *vs_const_f_swvp;
266
float *vs_lconstf_temp;
268
struct NinePixelShader9 *ps;
270
int ps_const_i[NINE_MAX_CONST_I][4];
271
BOOL ps_const_b[NINE_MAX_CONST_B];
272
float *ps_lconstf_temp;
274
struct NineVertexDeclaration9 *vdecl;
276
struct pipe_vertex_buffer vtxbuf[PIPE_MAX_ATTRIBS];
277
UINT stream_freq[PIPE_MAX_ATTRIBS];
278
uint32_t stream_instancedata_mask; /* derived from stream_freq */
279
uint32_t stream_usage_mask; /* derived from VS and vdecl */
281
struct pipe_resource *idxbuf;
282
unsigned index_offset;
285
struct pipe_clip_state clip;
287
DWORD rs[NINED3DRS_COUNT];
293
D3DRESOURCETYPE type;
294
struct pipe_resource *resource;
295
struct pipe_sampler_view *view[2];
297
} texture[NINE_MAX_SAMPLERS];
299
DWORD samp[NINE_MAX_SAMPLERS][NINED3DSAMP_COUNT];
301
uint32_t samplers_shadow;
302
uint32_t samplers_fetch4;
304
uint8_t bound_samplers_mask_vs;
305
uint8_t enabled_samplers_mask_vs;
306
uint8_t enabled_sampler_count_vs;
307
uint8_t enabled_sampler_count_ps;
308
uint16_t bound_samplers_mask_ps;
309
uint16_t enabled_samplers_mask_ps;
311
int dummy_vbo_bound_at; /* -1 = not bound , >= 0 = bound index */
312
boolean vbo_bound_done;
314
boolean inline_constants;
316
struct nine_ff_state ff;
318
/* software vertex processing */
323
struct pipe_framebuffer_state fb;
324
struct pipe_depth_stencil_alpha_state dsa;
325
struct pipe_rasterizer_state rast;
326
struct pipe_blend_state blend;
327
struct pipe_constant_buffer cb_vs;
328
struct pipe_constant_buffer cb0_swvp;
329
struct pipe_constant_buffer cb1_swvp;
330
struct pipe_constant_buffer cb2_swvp;
331
struct pipe_constant_buffer cb3_swvp;
332
struct pipe_constant_buffer cb_ps;
333
struct pipe_constant_buffer cb_vs_ff;
334
struct pipe_constant_buffer cb_ps_ff;
338
struct nine_state_sw_internal {
339
struct pipe_transfer *transfers_so[4];
342
struct nine_clipplane {
345
/* map D3DRS -> NINE_STATE_x
347
extern const uint32_t nine_render_state_group[NINED3DRS_COUNT];
349
/* for D3DSBT_PIXEL/VERTEX:
351
extern const uint32_t nine_render_states_pixel[(NINED3DRS_COUNT + 31) / 32];
352
extern const uint32_t nine_render_states_vertex[(NINED3DRS_COUNT + 31) / 32];
356
/* Internal multithreading: When enabled, the nine_context functions
357
* will append work to a worker thread when possible. Only the worker
358
* thread can access struct nine_context. */
361
nine_context_set_stream_source_apply(struct NineDevice9 *device,
363
struct pipe_resource *res,
368
nine_context_set_indices_apply(struct NineDevice9 *device,
369
struct pipe_resource *res,
374
nine_context_set_render_state(struct NineDevice9 *device,
375
D3DRENDERSTATETYPE State,
379
nine_context_set_texture(struct NineDevice9 *device,
381
struct NineBaseTexture9 *tex);
384
nine_context_set_sampler_state(struct NineDevice9 *device,
386
D3DSAMPLERSTATETYPE Type,
390
nine_context_set_stream_source(struct NineDevice9 *device,
392
struct NineVertexBuffer9 *pVBuf9,
397
nine_context_set_stream_source_freq(struct NineDevice9 *device,
402
nine_context_set_indices(struct NineDevice9 *device,
403
struct NineIndexBuffer9 *idxbuf);
406
nine_context_set_vertex_declaration(struct NineDevice9 *device,
407
struct NineVertexDeclaration9 *vdecl);
410
nine_context_set_vertex_shader(struct NineDevice9 *device,
411
struct NineVertexShader9 *pShader);
414
nine_context_set_vertex_shader_constant_f(struct NineDevice9 *device,
416
const float *pConstantData,
417
const unsigned pConstantData_size,
421
nine_context_set_vertex_shader_constant_i(struct NineDevice9 *device,
423
const int *pConstantData,
424
const unsigned pConstantData_size,
428
nine_context_set_vertex_shader_constant_b(struct NineDevice9 *device,
430
const BOOL *pConstantData,
431
const unsigned pConstantData_size,
435
nine_context_set_pixel_shader(struct NineDevice9 *device,
436
struct NinePixelShader9* ps);
439
nine_context_set_pixel_shader_constant_f(struct NineDevice9 *device,
441
const float *pConstantData,
442
const unsigned pConstantData_size,
446
nine_context_set_pixel_shader_constant_i(struct NineDevice9 *device,
448
const int *pConstantData,
449
const unsigned pConstantData_size,
453
nine_context_set_pixel_shader_constant_b(struct NineDevice9 *device,
455
const BOOL *pConstantData,
456
const unsigned pConstantData_size,
460
nine_context_set_viewport(struct NineDevice9 *device,
461
const D3DVIEWPORT9 *viewport);
464
nine_context_set_scissor(struct NineDevice9 *device,
465
const struct pipe_scissor_state *scissor);
468
nine_context_set_transform(struct NineDevice9 *device,
469
D3DTRANSFORMSTATETYPE State,
470
const D3DMATRIX *pMatrix);
473
nine_context_set_material(struct NineDevice9 *device,
474
const D3DMATERIAL9 *pMaterial);
477
nine_context_set_light(struct NineDevice9 *device,
479
const D3DLIGHT9 *pLight);
482
nine_context_light_enable(struct NineDevice9 *device,
487
nine_context_set_texture_stage_state(struct NineDevice9 *device,
489
D3DTEXTURESTAGESTATETYPE Type,
493
nine_context_set_render_target(struct NineDevice9 *device,
494
DWORD RenderTargetIndex,
495
struct NineSurface9 *rt);
498
nine_context_set_depth_stencil(struct NineDevice9 *device,
499
struct NineSurface9 *ds);
502
nine_context_set_clip_plane(struct NineDevice9 *device,
504
const struct nine_clipplane *pPlane);
507
nine_context_set_swvp(struct NineDevice9 *device,
511
nine_context_apply_stateblock(struct NineDevice9 *device,
512
const struct nine_state *src);
515
nine_context_clear_fb(struct NineDevice9 *device, DWORD Count,
516
const D3DRECT *pRects, DWORD Flags,
517
D3DCOLOR Color, float Z, DWORD Stencil);
520
nine_context_draw_primitive(struct NineDevice9 *device,
521
D3DPRIMITIVETYPE PrimitiveType,
523
UINT PrimitiveCount);
526
nine_context_draw_indexed_primitive(struct NineDevice9 *device,
527
D3DPRIMITIVETYPE PrimitiveType,
532
UINT PrimitiveCount);
535
nine_context_draw_indexed_primitive_from_vtxbuf_idxbuf(struct NineDevice9 *device,
536
D3DPRIMITIVETYPE PrimitiveType,
540
struct pipe_vertex_buffer *vbuf,
541
struct pipe_resource *ibuf,
543
unsigned index_offset,
544
unsigned index_size);
547
nine_context_resource_copy_region(struct NineDevice9 *device,
548
struct NineUnknown *dst,
549
struct NineUnknown *src,
550
struct pipe_resource* dst_res,
552
const struct pipe_box *dst_box,
553
struct pipe_resource* src_res,
555
const struct pipe_box *src_box);
558
nine_context_blit(struct NineDevice9 *device,
559
struct NineUnknown *dst,
560
struct NineUnknown *src,
561
struct pipe_blit_info *blit);
564
nine_context_clear_render_target(struct NineDevice9 *device,
565
struct NineSurface9 *surface,
573
nine_context_gen_mipmap(struct NineDevice9 *device,
574
struct NineUnknown *dst,
575
struct pipe_resource *res,
576
UINT base_level, UINT last_level,
577
UINT first_layer, UINT last_layer,
581
nine_context_range_upload(struct NineDevice9 *device,
583
struct NineUnknown *src_ref,
584
struct pipe_resource *res,
591
nine_context_box_upload(struct NineDevice9 *device,
593
struct NineUnknown *src_ref,
594
struct pipe_resource *res,
596
const struct pipe_box *dst_box,
597
enum pipe_format src_format,
598
const void *src, unsigned src_stride,
599
unsigned src_layer_stride,
600
const struct pipe_box *src_box);
603
nine_context_create_query(struct NineDevice9 *device, unsigned query_type);
606
nine_context_destroy_query(struct NineDevice9 *device, struct pipe_query *query);
609
nine_context_begin_query(struct NineDevice9 *device, unsigned *counter, struct pipe_query *query);
612
nine_context_end_query(struct NineDevice9 *device, unsigned *counter, struct pipe_query *query);
615
nine_context_get_query_result(struct NineDevice9 *device, struct pipe_query *query,
616
unsigned *counter, boolean flush, boolean wait,
617
union pipe_query_result *result);
620
nine_context_pipe_flush(struct NineDevice9 *device);
622
void nine_state_restore_non_cso(struct NineDevice9 *device);
623
void nine_state_set_defaults(struct NineDevice9 *, const D3DCAPS9 *,
625
void nine_device_state_clear(struct NineDevice9 *);
626
void nine_context_clear(struct NineDevice9 *);
627
void nine_context_update_state(struct NineDevice9 *);
629
void nine_state_init_sw(struct NineDevice9 *device);
630
void nine_state_prepare_draw_sw(struct NineDevice9 *device,
631
struct NineVertexDeclaration9 *vdecl_out,
634
struct pipe_stream_output_info *so);
635
void nine_state_after_draw_sw(struct NineDevice9 *device);
636
void nine_state_destroy_sw(struct NineDevice9 *device);
639
nine_state_resize_transform(struct nine_ff_state *ff_state, unsigned N);
641
/* If @alloc is FALSE, the return value may be a const identity matrix.
642
* Therefore, do not modify if you set alloc to FALSE !
645
nine_state_access_transform(struct nine_ff_state *, D3DTRANSFORMSTATETYPE,
649
nine_state_set_light(struct nine_ff_state *, DWORD, const D3DLIGHT9 *);
652
nine_state_light_enable(struct nine_ff_state *,
655
const char *nine_d3drs_to_string(DWORD State);
660
struct csmt_context *
661
nine_csmt_create( struct NineDevice9 *This );
664
nine_csmt_destroy( struct NineDevice9 *This, struct csmt_context *ctx );
666
/* Flushes and waits everything is executed */
668
nine_csmt_process( struct NineDevice9 *This );
670
/* Flushes and doesn't wait */
672
nine_csmt_flush( struct NineDevice9 *This );
674
/* Get the pipe_context (should not be called from the worker thread).
675
* All the work in the worker thread is finished before returning. */
676
struct pipe_context *
677
nine_context_get_pipe( struct NineDevice9 *device );
679
/* Can be called from all threads */
680
struct pipe_context *
681
nine_context_get_pipe_multithread( struct NineDevice9 *device );
684
/* Get the pipe_context (should not be called from the worker thread).
685
* All the work in the worker thread is paused before returning.
686
* It is neccessary to release in order to restart the thread.
687
* This is intended for use of the nine_context pipe_context that don't
688
* need the worker thread to finish all queued job. */
689
struct pipe_context *
690
nine_context_get_pipe_acquire( struct NineDevice9 *device );
693
nine_context_get_pipe_release( struct NineDevice9 *device );
696
nine_context_is_worker( struct NineDevice9 *device );
698
#endif /* _NINE_STATE_H_ */