brw_state.c

Go to the documentation of this file.
00001 /**************************************************************************
00002  *
00003  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
00004  * All Rights Reserved.
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a
00007  * copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sub license, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  *
00014  * The above copyright notice and this permission notice (including the
00015  * next paragraph) shall be included in all copies or substantial portions
00016  * of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00020  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
00021  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
00022  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00023  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00024  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00025  *
00026  **************************************************************************/
00027 
00028 /* Authors:  Zack Rusin <zack@tungstengraphics.com>
00029  *           Keith Whitwell <keith@tungstengraphics.com>
00030  */
00031 
00032 
00033 #include "pipe/p_winsys.h"
00034 #include "util/u_memory.h"
00035 #include "pipe/p_inlines.h"
00036 #include "pipe/p_shader_tokens.h"
00037 #include "tgsi/tgsi_dump.h"
00038 #include "tgsi/tgsi_parse.h"
00039 
00040 #include "brw_context.h"
00041 #include "brw_defines.h"
00042 #include "brw_state.h"
00043 #include "brw_draw.h"
00044 
00045 
00046 #define DUP( TYPE, VAL )                        \
00047 do {                                            \
00048    struct TYPE *x = malloc(sizeof(*x));         \
00049    memcpy(x, VAL, sizeof(*x) );                 \
00050    return x;                                    \
00051 } while (0)
00052 
00053 /************************************************************************
00054  * Blend 
00055  */
00056 static void *
00057 brw_create_blend_state(struct pipe_context *pipe,
00058                         const struct pipe_blend_state *blend)
00059 {   
00060    DUP( pipe_blend_state, blend );
00061 }
00062 
00063 static void brw_bind_blend_state(struct pipe_context *pipe,
00064                                  void *blend)
00065 {
00066    struct brw_context *brw = brw_context(pipe);
00067 
00068    brw->attribs.Blend = (struct pipe_blend_state*)blend;
00069    brw->state.dirty.brw |= BRW_NEW_BLEND;
00070 }
00071 
00072 
00073 static void brw_delete_blend_state(struct pipe_context *pipe, void *blend)
00074 {
00075    free(blend);
00076 }
00077 
00078 static void brw_set_blend_color( struct pipe_context *pipe,
00079                              const struct pipe_blend_color *blend_color )
00080 {
00081    struct brw_context *brw = brw_context(pipe);
00082 
00083    brw->attribs.BlendColor = *blend_color;
00084 
00085    brw->state.dirty.brw |= BRW_NEW_BLEND;
00086 }
00087 
00088 /************************************************************************
00089  * Sampler 
00090  */
00091 
00092 static void *
00093 brw_create_sampler_state(struct pipe_context *pipe,
00094                           const struct pipe_sampler_state *sampler)
00095 {
00096    DUP( pipe_sampler_state, sampler );
00097 }
00098 
00099 static void brw_bind_sampler_states(struct pipe_context *pipe,
00100                                     unsigned num, void **sampler)
00101 {
00102    struct brw_context *brw = brw_context(pipe);
00103 
00104    assert(num <= PIPE_MAX_SAMPLERS);
00105 
00106    /* Check for no-op */
00107    if (num == brw->num_samplers &&
00108        !memcmp(brw->attribs.Samplers, sampler, num * sizeof(void *)))
00109       return;
00110 
00111    memcpy(brw->attribs.Samplers, sampler, num * sizeof(void *));
00112    memset(&brw->attribs.Samplers[num], 0, (PIPE_MAX_SAMPLERS - num) *
00113           sizeof(void *));
00114 
00115    brw->num_samplers = num;
00116 
00117    brw->state.dirty.brw |= BRW_NEW_SAMPLER;
00118 }
00119 
00120 static void brw_delete_sampler_state(struct pipe_context *pipe,
00121                                       void *sampler)
00122 {
00123    free(sampler);
00124 }
00125 
00126 
00127 /************************************************************************
00128  * Depth stencil 
00129  */
00130 
00131 static void *
00132 brw_create_depth_stencil_state(struct pipe_context *pipe,
00133                            const struct pipe_depth_stencil_alpha_state *depth_stencil)
00134 {
00135    DUP( pipe_depth_stencil_alpha_state, depth_stencil );
00136 }
00137 
00138 static void brw_bind_depth_stencil_state(struct pipe_context *pipe,
00139                                          void *depth_stencil)
00140 {
00141    struct brw_context *brw = brw_context(pipe);
00142 
00143    brw->attribs.DepthStencil = (const struct pipe_depth_stencil_alpha_state *)depth_stencil;
00144 
00145    brw->state.dirty.brw |= BRW_NEW_DEPTH_STENCIL;
00146 }
00147 
00148 static void brw_delete_depth_stencil_state(struct pipe_context *pipe,
00149                                            void *depth_stencil)
00150 {
00151    free(depth_stencil);
00152 }
00153 
00154 /************************************************************************
00155  * Scissor
00156  */
00157 static void brw_set_scissor_state( struct pipe_context *pipe,
00158                                  const struct pipe_scissor_state *scissor )
00159 {
00160    struct brw_context *brw = brw_context(pipe);
00161 
00162    memcpy( &brw->attribs.Scissor, scissor, sizeof(*scissor) );
00163    brw->state.dirty.brw |= BRW_NEW_SCISSOR;
00164 }
00165 
00166 
00167 /************************************************************************
00168  * Stipple
00169  */
00170 
00171 static void brw_set_polygon_stipple( struct pipe_context *pipe,
00172                                    const struct pipe_poly_stipple *stipple )
00173 {
00174 }
00175 
00176 
00177 /************************************************************************
00178  * Fragment shader
00179  */
00180 
00181 static void * brw_create_fs_state(struct pipe_context *pipe,
00182                                    const struct pipe_shader_state *shader)
00183 {
00184    struct brw_fragment_program *brw_fp = CALLOC_STRUCT(brw_fragment_program);
00185 
00186    brw_fp->program.tokens = tgsi_dup_tokens(shader->tokens);
00187    brw_fp->id = brw_context(pipe)->program_id++;
00188 
00189    tgsi_scan_shader(shader->tokens, &brw_fp->info);
00190 
00191 #if 0
00192    brw_shader_info(shader->tokens,
00193                    &brw_fp->info2);
00194 #endif
00195 
00196    tgsi_dump(shader->tokens, 0);
00197 
00198 
00199    return (void *)brw_fp;
00200 }
00201 
00202 static void brw_bind_fs_state(struct pipe_context *pipe, void *shader)
00203 {
00204    struct brw_context *brw = brw_context(pipe);
00205 
00206    brw->attribs.FragmentProgram = (struct brw_fragment_program *)shader;
00207    brw->state.dirty.brw |= BRW_NEW_FS;
00208 }
00209 
00210 static void brw_delete_fs_state(struct pipe_context *pipe, void *shader)
00211 {
00212    struct brw_fragment_program *brw_fp = (struct brw_fragment_program *) shader;
00213 
00214    FREE((void *) brw_fp->program.tokens);
00215    FREE(brw_fp);
00216 }
00217 
00218 
00219 /************************************************************************
00220  * Vertex shader and other TNL state 
00221  */
00222 
00223 static void *brw_create_vs_state(struct pipe_context *pipe,
00224                                  const struct pipe_shader_state *shader)
00225 {
00226    struct brw_vertex_program *brw_vp = CALLOC_STRUCT(brw_vertex_program);
00227 
00228    brw_vp->program.tokens = tgsi_dup_tokens(shader->tokens);
00229    brw_vp->id = brw_context(pipe)->program_id++;
00230 
00231    tgsi_scan_shader(shader->tokens, &brw_vp->info);
00232 
00233 #if 0
00234    brw_shader_info(shader->tokens,
00235                    &brw_vp->info2);
00236 #endif
00237    tgsi_dump(shader->tokens, 0);
00238 
00239    return (void *)brw_vp;
00240 }
00241 
00242 static void brw_bind_vs_state(struct pipe_context *pipe, void *vs)
00243 {
00244    struct brw_context *brw = brw_context(pipe);
00245 
00246    brw->attribs.VertexProgram = (struct brw_vertex_program *)vs;
00247    brw->state.dirty.brw |= BRW_NEW_VS;
00248 
00249    debug_printf("YYYYYYYYYYYYY BINDING VERTEX SHADER\n");
00250 }
00251 
00252 static void brw_delete_vs_state(struct pipe_context *pipe, void *shader)
00253 {
00254    struct brw_vertex_program *brw_vp = (struct brw_vertex_program *) shader;
00255 
00256    FREE((void *) brw_vp->program.tokens);
00257    FREE(brw_vp);
00258 }
00259 
00260 
00261 static void brw_set_clip_state( struct pipe_context *pipe,
00262                                 const struct pipe_clip_state *clip )
00263 {
00264    struct brw_context *brw = brw_context(pipe);
00265 
00266    brw->attribs.Clip = *clip;
00267 }
00268 
00269 
00270 static void brw_set_viewport_state( struct pipe_context *pipe,
00271                                      const struct pipe_viewport_state *viewport )
00272 {
00273    struct brw_context *brw = brw_context(pipe);
00274 
00275    brw->attribs.Viewport = *viewport; /* struct copy */
00276    brw->state.dirty.brw |= BRW_NEW_VIEWPORT;
00277 
00278    /* pass the viewport info to the draw module */
00279    //draw_set_viewport_state(brw->draw, viewport);
00280 }
00281 
00282 
00283 static void brw_set_vertex_buffers(struct pipe_context *pipe,
00284                                    unsigned count,
00285                                    const struct pipe_vertex_buffer *buffers)
00286 {
00287    struct brw_context *brw = brw_context(pipe);
00288    memcpy(brw->vb.vbo_array, buffers, count * sizeof(buffers[0]));
00289 }
00290 
00291 static void brw_set_vertex_elements(struct pipe_context *pipe,
00292                                     unsigned count,
00293                                     const struct pipe_vertex_element *elements)
00294 {
00295    /* flush ? */
00296    struct brw_context *brw = brw_context(pipe);
00297    uint i;
00298 
00299    assert(count <= PIPE_MAX_ATTRIBS);
00300 
00301    for (i = 0; i < count; i++) {
00302       struct brw_vertex_element_state el;
00303       memset(&el, 0, sizeof(el));
00304 
00305       el.ve0.src_offset = elements[i].src_offset;
00306       el.ve0.src_format = brw_translate_surface_format(elements[i].src_format);
00307       el.ve0.valid = 1;
00308       el.ve0.vertex_buffer_index = elements[i].vertex_buffer_index;
00309 
00310       el.ve1.dst_offset   = i * 4;
00311 
00312       el.ve1.vfcomponent3 = BRW_VFCOMPONENT_STORE_SRC;
00313       el.ve1.vfcomponent2 = BRW_VFCOMPONENT_STORE_SRC;
00314       el.ve1.vfcomponent1 = BRW_VFCOMPONENT_STORE_SRC;
00315       el.ve1.vfcomponent0 = BRW_VFCOMPONENT_STORE_SRC;
00316 
00317       switch (elements[i].nr_components) {
00318       case 1: el.ve1.vfcomponent1 = BRW_VFCOMPONENT_STORE_0;
00319       case 2: el.ve1.vfcomponent2 = BRW_VFCOMPONENT_STORE_0;
00320       case 3: el.ve1.vfcomponent3 = BRW_VFCOMPONENT_STORE_1_FLT;
00321          break;
00322       }
00323 
00324       brw->vb.inputs[i] = el;
00325    }
00326 }
00327 
00328 
00329 
00330 /************************************************************************
00331  * Constant buffers
00332  */
00333 
00334 static void brw_set_constant_buffer(struct pipe_context *pipe,
00335                                      uint shader, uint index,
00336                                      const struct pipe_constant_buffer *buf)
00337 {
00338    struct brw_context *brw = brw_context(pipe);
00339 
00340    assert(buf == 0 || index == 0);
00341 
00342    brw->attribs.Constants[shader] = buf;
00343    brw->state.dirty.brw |= BRW_NEW_CONSTANTS;
00344 }
00345 
00346 
00347 /************************************************************************
00348  * Texture surfaces
00349  */
00350 
00351 
00352 static void brw_set_sampler_textures(struct pipe_context *pipe,
00353                                      unsigned num,
00354                                      struct pipe_texture **texture)
00355 {
00356    struct brw_context *brw = brw_context(pipe);
00357    uint i;
00358 
00359    assert(num <= PIPE_MAX_SAMPLERS);
00360 
00361    /* Check for no-op */
00362    if (num == brw->num_textures &&
00363        !memcmp(brw->attribs.Texture, texture, num *
00364                sizeof(struct pipe_texture *)))
00365       return;
00366 
00367    for (i = 0; i < num; i++)
00368       pipe_texture_reference((struct pipe_texture **) &brw->attribs.Texture[i],
00369                              texture[i]);
00370 
00371    for (i = num; i < brw->num_textures; i++)
00372       pipe_texture_reference((struct pipe_texture **) &brw->attribs.Texture[i],
00373                              NULL);
00374 
00375    brw->num_textures = num;
00376 
00377    brw->state.dirty.brw |= BRW_NEW_TEXTURE;
00378 }
00379 
00380 
00381 /************************************************************************
00382  * Render targets, etc
00383  */
00384 
00385 static void brw_set_framebuffer_state(struct pipe_context *pipe,
00386                                        const struct pipe_framebuffer_state *fb)
00387 {
00388    struct brw_context *brw = brw_context(pipe);
00389 
00390    brw->attribs.FrameBuffer = *fb; /* struct copy */
00391 
00392    brw->state.dirty.brw |= BRW_NEW_FRAMEBUFFER;
00393 }
00394 
00395 
00396 
00397 /************************************************************************
00398  * Rasterizer state
00399  */
00400 
00401 static void *
00402 brw_create_rasterizer_state(struct pipe_context *pipe,
00403                              const struct pipe_rasterizer_state *rasterizer)
00404 {
00405    DUP(pipe_rasterizer_state, rasterizer);
00406 }
00407 
00408 static void brw_bind_rasterizer_state( struct pipe_context *pipe,
00409                                         void *setup )
00410 {
00411    struct brw_context *brw = brw_context(pipe);
00412 
00413    brw->attribs.Raster = (struct pipe_rasterizer_state *)setup;
00414 
00415    /* Also pass-through to draw module:
00416     */
00417    //draw_set_rasterizer_state(brw->draw, setup);
00418 
00419    brw->state.dirty.brw |= BRW_NEW_RASTERIZER;
00420 }
00421 
00422 static void brw_delete_rasterizer_state(struct pipe_context *pipe,
00423                                          void *setup)
00424 {
00425    free(setup);
00426 }
00427 
00428 
00429 
00430 void
00431 brw_init_state_functions( struct brw_context *brw )
00432 {
00433    brw->pipe.create_blend_state = brw_create_blend_state;
00434    brw->pipe.bind_blend_state = brw_bind_blend_state;
00435    brw->pipe.delete_blend_state = brw_delete_blend_state;
00436 
00437    brw->pipe.create_sampler_state = brw_create_sampler_state;
00438    brw->pipe.bind_sampler_states = brw_bind_sampler_states;
00439    brw->pipe.delete_sampler_state = brw_delete_sampler_state;
00440 
00441    brw->pipe.create_depth_stencil_alpha_state = brw_create_depth_stencil_state;
00442    brw->pipe.bind_depth_stencil_alpha_state = brw_bind_depth_stencil_state;
00443    brw->pipe.delete_depth_stencil_alpha_state = brw_delete_depth_stencil_state;
00444 
00445    brw->pipe.create_rasterizer_state = brw_create_rasterizer_state;
00446    brw->pipe.bind_rasterizer_state = brw_bind_rasterizer_state;
00447    brw->pipe.delete_rasterizer_state = brw_delete_rasterizer_state;
00448    brw->pipe.create_fs_state = brw_create_fs_state;
00449    brw->pipe.bind_fs_state = brw_bind_fs_state;
00450    brw->pipe.delete_fs_state = brw_delete_fs_state;
00451    brw->pipe.create_vs_state = brw_create_vs_state;
00452    brw->pipe.bind_vs_state = brw_bind_vs_state;
00453    brw->pipe.delete_vs_state = brw_delete_vs_state;
00454 
00455    brw->pipe.set_blend_color = brw_set_blend_color;
00456    brw->pipe.set_clip_state = brw_set_clip_state;
00457    brw->pipe.set_constant_buffer = brw_set_constant_buffer;
00458    brw->pipe.set_framebuffer_state = brw_set_framebuffer_state;
00459 
00460 //   brw->pipe.set_feedback_state = brw_set_feedback_state;
00461 //   brw->pipe.set_feedback_buffer = brw_set_feedback_buffer;
00462 
00463    brw->pipe.set_polygon_stipple = brw_set_polygon_stipple;
00464    brw->pipe.set_scissor_state = brw_set_scissor_state;
00465    brw->pipe.set_sampler_textures = brw_set_sampler_textures;
00466    brw->pipe.set_viewport_state = brw_set_viewport_state;
00467    brw->pipe.set_vertex_buffers = brw_set_vertex_buffers;
00468    brw->pipe.set_vertex_elements = brw_set_vertex_elements;
00469 }

Generated on Tue Sep 29 06:25:17 2009 for Gallium3D by  doxygen 1.5.4