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/internal/p_winsys_screen.h"
34 #include "util/u_memory.h"
35 #include "pipe/p_inlines.h"
36 #include "pipe/p_shader_tokens.h"
37 #include "tgsi/tgsi_dump.h"
38 #include "tgsi/tgsi_parse.h"
40 #include "brw_context.h"
41 #include "brw_defines.h"
42 #include "brw_state.h"
46 #define DUP( TYPE, VAL ) \
48 struct TYPE *x = malloc(sizeof(*x)); \
49 memcpy(x, VAL, sizeof(*x) ); \
53 /************************************************************************
57 brw_create_blend_state(struct pipe_context
*pipe
,
58 const struct pipe_blend_state
*blend
)
60 DUP( pipe_blend_state
, blend
);
63 static void brw_bind_blend_state(struct pipe_context
*pipe
,
66 struct brw_context
*brw
= brw_context(pipe
);
68 brw
->attribs
.Blend
= (struct pipe_blend_state
*)blend
;
69 brw
->state
.dirty
.brw
|= BRW_NEW_BLEND
;
73 static void brw_delete_blend_state(struct pipe_context
*pipe
, void *blend
)
78 static void brw_set_blend_color( struct pipe_context
*pipe
,
79 const struct pipe_blend_color
*blend_color
)
81 struct brw_context
*brw
= brw_context(pipe
);
83 brw
->attribs
.BlendColor
= *blend_color
;
85 brw
->state
.dirty
.brw
|= BRW_NEW_BLEND
;
88 /************************************************************************
93 brw_create_sampler_state(struct pipe_context
*pipe
,
94 const struct pipe_sampler_state
*sampler
)
96 DUP( pipe_sampler_state
, sampler
);
99 static void brw_bind_sampler_states(struct pipe_context
*pipe
,
100 unsigned num
, void **sampler
)
102 struct brw_context
*brw
= brw_context(pipe
);
104 assert(num
<= PIPE_MAX_SAMPLERS
);
106 /* Check for no-op */
107 if (num
== brw
->num_samplers
&&
108 !memcmp(brw
->attribs
.Samplers
, sampler
, num
* sizeof(void *)))
111 memcpy(brw
->attribs
.Samplers
, sampler
, num
* sizeof(void *));
112 memset(&brw
->attribs
.Samplers
[num
], 0, (PIPE_MAX_SAMPLERS
- num
) *
115 brw
->num_samplers
= num
;
117 brw
->state
.dirty
.brw
|= BRW_NEW_SAMPLER
;
120 static void brw_delete_sampler_state(struct pipe_context
*pipe
,
127 /************************************************************************
132 brw_create_depth_stencil_state(struct pipe_context
*pipe
,
133 const struct pipe_depth_stencil_alpha_state
*depth_stencil
)
135 DUP( pipe_depth_stencil_alpha_state
, depth_stencil
);
138 static void brw_bind_depth_stencil_state(struct pipe_context
*pipe
,
141 struct brw_context
*brw
= brw_context(pipe
);
143 brw
->attribs
.DepthStencil
= (const struct pipe_depth_stencil_alpha_state
*)depth_stencil
;
145 brw
->state
.dirty
.brw
|= BRW_NEW_DEPTH_STENCIL
;
148 static void brw_delete_depth_stencil_state(struct pipe_context
*pipe
,
154 /************************************************************************
157 static void brw_set_scissor_state( struct pipe_context
*pipe
,
158 const struct pipe_scissor_state
*scissor
)
160 struct brw_context
*brw
= brw_context(pipe
);
162 memcpy( &brw
->attribs
.Scissor
, scissor
, sizeof(*scissor
) );
163 brw
->state
.dirty
.brw
|= BRW_NEW_SCISSOR
;
167 /************************************************************************
171 static void brw_set_polygon_stipple( struct pipe_context
*pipe
,
172 const struct pipe_poly_stipple
*stipple
)
177 /************************************************************************
181 static void * brw_create_fs_state(struct pipe_context
*pipe
,
182 const struct pipe_shader_state
*shader
)
184 struct brw_fragment_program
*brw_fp
= CALLOC_STRUCT(brw_fragment_program
);
186 brw_fp
->program
.tokens
= tgsi_dup_tokens(shader
->tokens
);
187 brw_fp
->id
= brw_context(pipe
)->program_id
++;
189 tgsi_scan_shader(shader
->tokens
, &brw_fp
->info
);
192 brw_shader_info(shader
->tokens
,
196 tgsi_dump(shader
->tokens
, 0);
199 return (void *)brw_fp
;
202 static void brw_bind_fs_state(struct pipe_context
*pipe
, void *shader
)
204 struct brw_context
*brw
= brw_context(pipe
);
206 brw
->attribs
.FragmentProgram
= (struct brw_fragment_program
*)shader
;
207 brw
->state
.dirty
.brw
|= BRW_NEW_FS
;
210 static void brw_delete_fs_state(struct pipe_context
*pipe
, void *shader
)
212 struct brw_fragment_program
*brw_fp
= (struct brw_fragment_program
*) shader
;
214 FREE((void *) brw_fp
->program
.tokens
);
219 /************************************************************************
220 * Vertex shader and other TNL state
223 static void *brw_create_vs_state(struct pipe_context
*pipe
,
224 const struct pipe_shader_state
*shader
)
226 struct brw_vertex_program
*brw_vp
= CALLOC_STRUCT(brw_vertex_program
);
228 brw_vp
->program
.tokens
= tgsi_dup_tokens(shader
->tokens
);
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
)
254 struct brw_vertex_program
*brw_vp
= (struct brw_vertex_program
*) shader
;
256 FREE((void *) brw_vp
->program
.tokens
);
261 static void brw_set_clip_state( struct pipe_context
*pipe
,
262 const struct pipe_clip_state
*clip
)
264 struct brw_context
*brw
= brw_context(pipe
);
266 brw
->attribs
.Clip
= *clip
;
270 static void brw_set_viewport_state( struct pipe_context
*pipe
,
271 const struct pipe_viewport_state
*viewport
)
273 struct brw_context
*brw
= brw_context(pipe
);
275 brw
->attribs
.Viewport
= *viewport
; /* struct copy */
276 brw
->state
.dirty
.brw
|= BRW_NEW_VIEWPORT
;
278 /* pass the viewport info to the draw module */
279 //draw_set_viewport_state(brw->draw, viewport);
283 static void brw_set_vertex_buffers(struct pipe_context
*pipe
,
285 const struct pipe_vertex_buffer
*buffers
)
287 struct brw_context
*brw
= brw_context(pipe
);
288 memcpy(brw
->vb
.vbo_array
, buffers
, count
* sizeof(buffers
[0]));
291 static void brw_set_vertex_elements(struct pipe_context
*pipe
,
293 const struct pipe_vertex_element
*elements
)
296 struct brw_context
*brw
= brw_context(pipe
);
299 assert(count
<= PIPE_MAX_ATTRIBS
);
301 for (i
= 0; i
< count
; i
++) {
302 struct brw_vertex_element_state el
;
303 memset(&el
, 0, sizeof(el
));
305 el
.ve0
.src_offset
= elements
[i
].src_offset
;
306 el
.ve0
.src_format
= brw_translate_surface_format(elements
[i
].src_format
);
308 el
.ve0
.vertex_buffer_index
= elements
[i
].vertex_buffer_index
;
310 el
.ve1
.dst_offset
= i
* 4;
312 el
.ve1
.vfcomponent3
= BRW_VFCOMPONENT_STORE_SRC
;
313 el
.ve1
.vfcomponent2
= BRW_VFCOMPONENT_STORE_SRC
;
314 el
.ve1
.vfcomponent1
= BRW_VFCOMPONENT_STORE_SRC
;
315 el
.ve1
.vfcomponent0
= BRW_VFCOMPONENT_STORE_SRC
;
317 switch (elements
[i
].nr_components
) {
318 case 1: el
.ve1
.vfcomponent1
= BRW_VFCOMPONENT_STORE_0
;
319 case 2: el
.ve1
.vfcomponent2
= BRW_VFCOMPONENT_STORE_0
;
320 case 3: el
.ve1
.vfcomponent3
= BRW_VFCOMPONENT_STORE_1_FLT
;
324 brw
->vb
.inputs
[i
] = el
;
330 /************************************************************************
334 static void brw_set_constant_buffer(struct pipe_context
*pipe
,
335 uint shader
, uint index
,
336 const struct pipe_constant_buffer
*buf
)
338 struct brw_context
*brw
= brw_context(pipe
);
340 assert(buf
== 0 || index
== 0);
342 brw
->attribs
.Constants
[shader
] = buf
;
343 brw
->state
.dirty
.brw
|= BRW_NEW_CONSTANTS
;
347 /************************************************************************
352 static void brw_set_sampler_textures(struct pipe_context
*pipe
,
354 struct pipe_texture
**texture
)
356 struct brw_context
*brw
= brw_context(pipe
);
359 assert(num
<= PIPE_MAX_SAMPLERS
);
361 /* Check for no-op */
362 if (num
== brw
->num_textures
&&
363 !memcmp(brw
->attribs
.Texture
, texture
, num
*
364 sizeof(struct pipe_texture
*)))
367 for (i
= 0; i
< num
; i
++)
368 pipe_texture_reference((struct pipe_texture
**) &brw
->attribs
.Texture
[i
],
371 for (i
= num
; i
< brw
->num_textures
; i
++)
372 pipe_texture_reference((struct pipe_texture
**) &brw
->attribs
.Texture
[i
],
375 brw
->num_textures
= num
;
377 brw
->state
.dirty
.brw
|= BRW_NEW_TEXTURE
;
381 /************************************************************************
382 * Render targets, etc
385 static void brw_set_framebuffer_state(struct pipe_context
*pipe
,
386 const struct pipe_framebuffer_state
*fb
)
388 struct brw_context
*brw
= brw_context(pipe
);
390 brw
->attribs
.FrameBuffer
= *fb
; /* struct copy */
392 brw
->state
.dirty
.brw
|= BRW_NEW_FRAMEBUFFER
;
397 /************************************************************************
402 brw_create_rasterizer_state(struct pipe_context
*pipe
,
403 const struct pipe_rasterizer_state
*rasterizer
)
405 DUP(pipe_rasterizer_state
, rasterizer
);
408 static void brw_bind_rasterizer_state( struct pipe_context
*pipe
,
411 struct brw_context
*brw
= brw_context(pipe
);
413 brw
->attribs
.Raster
= (struct pipe_rasterizer_state
*)setup
;
415 /* Also pass-through to draw module:
417 //draw_set_rasterizer_state(brw->draw, setup);
419 brw
->state
.dirty
.brw
|= BRW_NEW_RASTERIZER
;
422 static void brw_delete_rasterizer_state(struct pipe_context
*pipe
,
431 brw_init_state_functions( struct brw_context
*brw
)
433 brw
->pipe
.create_blend_state
= brw_create_blend_state
;
434 brw
->pipe
.bind_blend_state
= brw_bind_blend_state
;
435 brw
->pipe
.delete_blend_state
= brw_delete_blend_state
;
437 brw
->pipe
.create_sampler_state
= brw_create_sampler_state
;
438 brw
->pipe
.bind_sampler_states
= brw_bind_sampler_states
;
439 brw
->pipe
.delete_sampler_state
= brw_delete_sampler_state
;
441 brw
->pipe
.create_depth_stencil_alpha_state
= brw_create_depth_stencil_state
;
442 brw
->pipe
.bind_depth_stencil_alpha_state
= brw_bind_depth_stencil_state
;
443 brw
->pipe
.delete_depth_stencil_alpha_state
= brw_delete_depth_stencil_state
;
445 brw
->pipe
.create_rasterizer_state
= brw_create_rasterizer_state
;
446 brw
->pipe
.bind_rasterizer_state
= brw_bind_rasterizer_state
;
447 brw
->pipe
.delete_rasterizer_state
= brw_delete_rasterizer_state
;
448 brw
->pipe
.create_fs_state
= brw_create_fs_state
;
449 brw
->pipe
.bind_fs_state
= brw_bind_fs_state
;
450 brw
->pipe
.delete_fs_state
= brw_delete_fs_state
;
451 brw
->pipe
.create_vs_state
= brw_create_vs_state
;
452 brw
->pipe
.bind_vs_state
= brw_bind_vs_state
;
453 brw
->pipe
.delete_vs_state
= brw_delete_vs_state
;
455 brw
->pipe
.set_blend_color
= brw_set_blend_color
;
456 brw
->pipe
.set_clip_state
= brw_set_clip_state
;
457 brw
->pipe
.set_constant_buffer
= brw_set_constant_buffer
;
458 brw
->pipe
.set_framebuffer_state
= brw_set_framebuffer_state
;
460 // brw->pipe.set_feedback_state = brw_set_feedback_state;
461 // brw->pipe.set_feedback_buffer = brw_set_feedback_buffer;
463 brw
->pipe
.set_polygon_stipple
= brw_set_polygon_stipple
;
464 brw
->pipe
.set_scissor_state
= brw_set_scissor_state
;
465 brw
->pipe
.set_sampler_textures
= brw_set_sampler_textures
;
466 brw
->pipe
.set_viewport_state
= brw_set_viewport_state
;
467 brw
->pipe
.set_vertex_buffers
= brw_set_vertex_buffers
;
468 brw
->pipe
.set_vertex_elements
= brw_set_vertex_elements
;