1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 /* Authors: Zack Rusin <zack@tungstengraphics.com>
29 * Keith Whitwell <keith@tungstengraphics.com>
33 #include "pipe/p_winsys.h"
34 #include "pipe/p_util.h"
35 #include "pipe/p_inlines.h"
36 #include "pipe/p_shader_tokens.h"
37 #include "tgsi/util/tgsi_dump.h"
39 #include "brw_context.h"
40 #include "brw_defines.h"
41 #include "brw_state.h"
45 #define DUP( TYPE, VAL ) \
47 struct TYPE *x = malloc(sizeof(*x)); \
48 memcpy(x, VAL, sizeof(*x) ); \
52 /************************************************************************
56 brw_create_blend_state(struct pipe_context
*pipe
,
57 const struct pipe_blend_state
*blend
)
59 DUP( pipe_blend_state
, blend
);
62 static void brw_bind_blend_state(struct pipe_context
*pipe
,
65 struct brw_context
*brw
= brw_context(pipe
);
67 brw
->attribs
.Blend
= (struct pipe_blend_state
*)blend
;
68 brw
->state
.dirty
.brw
|= BRW_NEW_BLEND
;
72 static void brw_delete_blend_state(struct pipe_context
*pipe
, void *blend
)
77 static void brw_set_blend_color( struct pipe_context
*pipe
,
78 const struct pipe_blend_color
*blend_color
)
80 struct brw_context
*brw
= brw_context(pipe
);
82 brw
->attribs
.BlendColor
= *blend_color
;
84 brw
->state
.dirty
.brw
|= BRW_NEW_BLEND
;
87 /************************************************************************
92 brw_create_sampler_state(struct pipe_context
*pipe
,
93 const struct pipe_sampler_state
*sampler
)
95 DUP( pipe_sampler_state
, sampler
);
98 static void brw_bind_sampler_states(struct pipe_context
*pipe
,
99 unsigned num
, void **sampler
)
101 struct brw_context
*brw
= brw_context(pipe
);
103 assert(num
<= PIPE_MAX_SAMPLERS
);
105 /* Check for no-op */
106 if (num
== brw
->num_samplers
&&
107 !memcmp(brw
->attribs
.Samplers
, sampler
, num
* sizeof(void *)))
110 memcpy(brw
->attribs
.Samplers
, sampler
, num
* sizeof(void *));
111 memset(&brw
->attribs
.Samplers
[num
], 0, (PIPE_MAX_SAMPLERS
- num
) *
114 brw
->num_samplers
= num
;
116 brw
->state
.dirty
.brw
|= BRW_NEW_SAMPLER
;
119 static void brw_delete_sampler_state(struct pipe_context
*pipe
,
126 /************************************************************************
131 brw_create_depth_stencil_state(struct pipe_context
*pipe
,
132 const struct pipe_depth_stencil_alpha_state
*depth_stencil
)
134 DUP( pipe_depth_stencil_alpha_state
, depth_stencil
);
137 static void brw_bind_depth_stencil_state(struct pipe_context
*pipe
,
140 struct brw_context
*brw
= brw_context(pipe
);
142 brw
->attribs
.DepthStencil
= (const struct pipe_depth_stencil_alpha_state
*)depth_stencil
;
144 brw
->state
.dirty
.brw
|= BRW_NEW_DEPTH_STENCIL
;
147 static void brw_delete_depth_stencil_state(struct pipe_context
*pipe
,
153 /************************************************************************
156 static void brw_set_scissor_state( struct pipe_context
*pipe
,
157 const struct pipe_scissor_state
*scissor
)
159 struct brw_context
*brw
= brw_context(pipe
);
161 memcpy( &brw
->attribs
.Scissor
, scissor
, sizeof(*scissor
) );
162 brw
->state
.dirty
.brw
|= BRW_NEW_SCISSOR
;
166 /************************************************************************
170 static void brw_set_polygon_stipple( struct pipe_context
*pipe
,
171 const struct pipe_poly_stipple
*stipple
)
176 /************************************************************************
180 static void * brw_create_fs_state(struct pipe_context
*pipe
,
181 const struct pipe_shader_state
*shader
)
183 struct brw_fragment_program
*brw_fp
= CALLOC_STRUCT(brw_fragment_program
);
185 /* XXX: Do I have to duplicate the tokens as well??
187 brw_fp
->program
= *shader
;
188 brw_fp
->id
= brw_context(pipe
)->program_id
++;
190 tgsi_scan_shader(shader
->tokens
, &brw_fp
->info
);
193 brw_shader_info(shader
->tokens
,
197 tgsi_dump(shader
->tokens
, 0);
200 return (void *)brw_fp
;
203 static void brw_bind_fs_state(struct pipe_context
*pipe
, void *shader
)
205 struct brw_context
*brw
= brw_context(pipe
);
207 brw
->attribs
.FragmentProgram
= (struct brw_fragment_program
*)shader
;
208 brw
->state
.dirty
.brw
|= BRW_NEW_FS
;
211 static void brw_delete_fs_state(struct pipe_context
*pipe
, void *shader
)
217 /************************************************************************
218 * Vertex shader and other TNL state
221 static void *brw_create_vs_state(struct pipe_context
*pipe
,
222 const struct pipe_shader_state
*shader
)
224 struct brw_vertex_program
*brw_vp
= CALLOC_STRUCT(brw_vertex_program
);
226 /* XXX: Do I have to duplicate the tokens as well??
228 brw_vp
->program
= *shader
;
229 brw_vp
->id
= brw_context(pipe
)->program_id
++;
231 tgsi_scan_shader(shader
->tokens
, &brw_vp
->info
);
234 brw_shader_info(shader
->tokens
,
237 tgsi_dump(shader
->tokens
, 0);
239 return (void *)brw_vp
;
242 static void brw_bind_vs_state(struct pipe_context
*pipe
, void *vs
)
244 struct brw_context
*brw
= brw_context(pipe
);
246 brw
->attribs
.VertexProgram
= (struct brw_vertex_program
*)vs
;
247 brw
->state
.dirty
.brw
|= BRW_NEW_VS
;
249 debug_printf("YYYYYYYYYYYYY BINDING VERTEX SHADER\n");
252 static void brw_delete_vs_state(struct pipe_context
*pipe
, void *shader
)
258 static void brw_set_clip_state( struct pipe_context
*pipe
,
259 const struct pipe_clip_state
*clip
)
261 struct brw_context
*brw
= brw_context(pipe
);
263 brw
->attribs
.Clip
= *clip
;
267 static void brw_set_viewport_state( struct pipe_context
*pipe
,
268 const struct pipe_viewport_state
*viewport
)
270 struct brw_context
*brw
= brw_context(pipe
);
272 brw
->attribs
.Viewport
= *viewport
; /* struct copy */
273 brw
->state
.dirty
.brw
|= BRW_NEW_VIEWPORT
;
275 /* pass the viewport info to the draw module */
276 //draw_set_viewport_state(brw->draw, viewport);
280 static void brw_set_vertex_buffers(struct pipe_context
*pipe
,
282 const struct pipe_vertex_buffer
*buffers
)
284 struct brw_context
*brw
= brw_context(pipe
);
285 memcpy(brw
->vb
.vbo_array
, buffers
, count
* sizeof(buffers
[0]));
288 static void brw_set_vertex_elements(struct pipe_context
*pipe
,
290 const struct pipe_vertex_element
*elements
)
293 struct brw_context
*brw
= brw_context(pipe
);
296 assert(count
<= PIPE_MAX_ATTRIBS
);
298 for (i
= 0; i
< count
; i
++) {
299 struct brw_vertex_element_state el
;
300 memset(&el
, 0, sizeof(el
));
302 el
.ve0
.src_offset
= elements
[i
].src_offset
;
303 el
.ve0
.src_format
= brw_translate_surface_format(elements
[i
].src_format
);
305 el
.ve0
.vertex_buffer_index
= elements
[i
].vertex_buffer_index
;
307 el
.ve1
.dst_offset
= i
* 4;
309 el
.ve1
.vfcomponent3
= BRW_VFCOMPONENT_STORE_SRC
;
310 el
.ve1
.vfcomponent2
= BRW_VFCOMPONENT_STORE_SRC
;
311 el
.ve1
.vfcomponent1
= BRW_VFCOMPONENT_STORE_SRC
;
312 el
.ve1
.vfcomponent0
= BRW_VFCOMPONENT_STORE_SRC
;
314 switch (elements
[i
].nr_components
) {
315 case 1: el
.ve1
.vfcomponent1
= BRW_VFCOMPONENT_STORE_0
;
316 case 2: el
.ve1
.vfcomponent2
= BRW_VFCOMPONENT_STORE_0
;
317 case 3: el
.ve1
.vfcomponent3
= BRW_VFCOMPONENT_STORE_1_FLT
;
321 brw
->vb
.inputs
[i
] = el
;
327 /************************************************************************
331 static void brw_set_constant_buffer(struct pipe_context
*pipe
,
332 uint shader
, uint index
,
333 const struct pipe_constant_buffer
*buf
)
335 struct brw_context
*brw
= brw_context(pipe
);
337 assert(buf
== 0 || index
== 0);
339 brw
->attribs
.Constants
[shader
] = buf
;
340 brw
->state
.dirty
.brw
|= BRW_NEW_CONSTANTS
;
344 /************************************************************************
349 static void brw_set_sampler_textures(struct pipe_context
*pipe
,
351 struct pipe_texture
**texture
)
353 struct brw_context
*brw
= brw_context(pipe
);
356 assert(num
<= PIPE_MAX_SAMPLERS
);
358 /* Check for no-op */
359 if (num
== brw
->num_textures
&&
360 !memcmp(brw
->attribs
.Texture
, texture
, num
*
361 sizeof(struct pipe_texture
*)))
364 for (i
= 0; i
< num
; i
++)
365 pipe_texture_reference((struct pipe_texture
**) &brw
->attribs
.Texture
[i
],
368 for (i
= num
; i
< brw
->num_textures
; i
++)
369 pipe_texture_reference((struct pipe_texture
**) &brw
->attribs
.Texture
[i
],
372 brw
->num_textures
= num
;
374 brw
->state
.dirty
.brw
|= BRW_NEW_TEXTURE
;
378 /************************************************************************
379 * Render targets, etc
382 static void brw_set_framebuffer_state(struct pipe_context
*pipe
,
383 const struct pipe_framebuffer_state
*fb
)
385 struct brw_context
*brw
= brw_context(pipe
);
387 brw
->attribs
.FrameBuffer
= *fb
; /* struct copy */
389 brw
->state
.dirty
.brw
|= BRW_NEW_FRAMEBUFFER
;
394 /************************************************************************
399 brw_create_rasterizer_state(struct pipe_context
*pipe
,
400 const struct pipe_rasterizer_state
*rasterizer
)
402 DUP(pipe_rasterizer_state
, rasterizer
);
405 static void brw_bind_rasterizer_state( struct pipe_context
*pipe
,
408 struct brw_context
*brw
= brw_context(pipe
);
410 brw
->attribs
.Raster
= (struct pipe_rasterizer_state
*)setup
;
412 /* Also pass-through to draw module:
414 //draw_set_rasterizer_state(brw->draw, setup);
416 brw
->state
.dirty
.brw
|= BRW_NEW_RASTERIZER
;
419 static void brw_delete_rasterizer_state(struct pipe_context
*pipe
,
428 brw_init_state_functions( struct brw_context
*brw
)
430 brw
->pipe
.create_blend_state
= brw_create_blend_state
;
431 brw
->pipe
.bind_blend_state
= brw_bind_blend_state
;
432 brw
->pipe
.delete_blend_state
= brw_delete_blend_state
;
434 brw
->pipe
.create_sampler_state
= brw_create_sampler_state
;
435 brw
->pipe
.bind_sampler_states
= brw_bind_sampler_states
;
436 brw
->pipe
.delete_sampler_state
= brw_delete_sampler_state
;
438 brw
->pipe
.create_depth_stencil_alpha_state
= brw_create_depth_stencil_state
;
439 brw
->pipe
.bind_depth_stencil_alpha_state
= brw_bind_depth_stencil_state
;
440 brw
->pipe
.delete_depth_stencil_alpha_state
= brw_delete_depth_stencil_state
;
442 brw
->pipe
.create_rasterizer_state
= brw_create_rasterizer_state
;
443 brw
->pipe
.bind_rasterizer_state
= brw_bind_rasterizer_state
;
444 brw
->pipe
.delete_rasterizer_state
= brw_delete_rasterizer_state
;
445 brw
->pipe
.create_fs_state
= brw_create_fs_state
;
446 brw
->pipe
.bind_fs_state
= brw_bind_fs_state
;
447 brw
->pipe
.delete_fs_state
= brw_delete_fs_state
;
448 brw
->pipe
.create_vs_state
= brw_create_vs_state
;
449 brw
->pipe
.bind_vs_state
= brw_bind_vs_state
;
450 brw
->pipe
.delete_vs_state
= brw_delete_vs_state
;
452 brw
->pipe
.set_blend_color
= brw_set_blend_color
;
453 brw
->pipe
.set_clip_state
= brw_set_clip_state
;
454 brw
->pipe
.set_constant_buffer
= brw_set_constant_buffer
;
455 brw
->pipe
.set_framebuffer_state
= brw_set_framebuffer_state
;
457 // brw->pipe.set_feedback_state = brw_set_feedback_state;
458 // brw->pipe.set_feedback_buffer = brw_set_feedback_buffer;
460 brw
->pipe
.set_polygon_stipple
= brw_set_polygon_stipple
;
461 brw
->pipe
.set_scissor_state
= brw_set_scissor_state
;
462 brw
->pipe
.set_sampler_textures
= brw_set_sampler_textures
;
463 brw
->pipe
.set_viewport_state
= brw_set_viewport_state
;
464 brw
->pipe
.set_vertex_buffers
= brw_set_vertex_buffers
;
465 brw
->pipe
.set_vertex_elements
= brw_set_vertex_elements
;