st_device.c

Go to the documentation of this file.
00001 /**************************************************************************
00002  * 
00003  * Copyright 2008 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 
00029 #include "pipe/p_winsys.h"
00030 #include "pipe/p_context.h"
00031 #include "pipe/p_shader_tokens.h"
00032 #include "pipe/p_inlines.h"
00033 #include "cso_cache/cso_context.h"
00034 #include "util/u_math.h"
00035 #include "util/u_memory.h"
00036 #include "util/u_simple_shaders.h"
00037 #include "trace/tr_screen.h"
00038 #include "trace/tr_context.h"
00039 
00040 #include "st_device.h"
00041 #include "st_winsys.h"
00042 
00043 
00044 static void
00045 st_device_really_destroy(struct st_device *st_dev) 
00046 {
00047    if(st_dev->screen)
00048       st_dev->screen->destroy(st_dev->screen);
00049    
00050    FREE(st_dev);
00051 }
00052 
00053 
00054 void
00055 st_device_destroy(struct st_device *st_dev) 
00056 {
00057    if(!--st_dev->refcount)
00058       st_device_really_destroy(st_dev);
00059 }
00060 
00061 
00062 static struct st_device *
00063 st_device_create_from_st_winsys(const struct st_winsys *st_ws) 
00064 {
00065    struct st_device *st_dev;
00066    
00067    if(!st_ws->screen_create ||
00068       !st_ws->context_create)
00069       return NULL;
00070    
00071    st_dev = CALLOC_STRUCT(st_device);
00072    if(!st_dev)
00073       return NULL;
00074    
00075    st_dev->refcount = 1;
00076    st_dev->st_ws = st_ws;
00077    
00078    st_dev->real_screen = st_ws->screen_create();
00079    if(!st_dev->real_screen) {
00080       st_device_destroy(st_dev);
00081       return NULL;
00082    }
00083 
00084    st_dev->screen = trace_screen_create(st_dev->real_screen);
00085    if(!st_dev->screen) {
00086       st_device_destroy(st_dev);
00087       return NULL;
00088    }
00089    
00090    return st_dev;
00091 }
00092 
00093 
00094 struct st_device *
00095 st_device_create(boolean hardware) {
00096    if(hardware)
00097       return st_device_create_from_st_winsys(&st_hardpipe_winsys);
00098    else
00099       return st_device_create_from_st_winsys(&st_softpipe_winsys);
00100 }
00101 
00102 
00103 void
00104 st_context_destroy(struct st_context *st_ctx) 
00105 {
00106    unsigned i;
00107    
00108    if(st_ctx) {
00109       struct st_device *st_dev = st_ctx->st_dev;
00110       
00111       if(st_ctx->cso) {
00112          cso_delete_vertex_shader(st_ctx->cso, st_ctx->vs);
00113          cso_delete_fragment_shader(st_ctx->cso, st_ctx->fs);
00114          
00115          cso_destroy_context(st_ctx->cso);
00116       }
00117       
00118       if(st_ctx->pipe)
00119          st_ctx->pipe->destroy(st_ctx->pipe);
00120       
00121       for(i = 0; i < PIPE_MAX_SAMPLERS; ++i)
00122          pipe_texture_reference(&st_ctx->sampler_textures[i], NULL);
00123       pipe_texture_reference(&st_ctx->default_texture, NULL);
00124 
00125       FREE(st_ctx);
00126       
00127       if(!--st_dev->refcount)
00128          st_device_really_destroy(st_dev);
00129    }
00130 }
00131 
00132 
00133 struct st_context *
00134 st_context_create(struct st_device *st_dev) 
00135 {
00136    struct st_context *st_ctx;
00137    
00138    st_ctx = CALLOC_STRUCT(st_context);
00139    if(!st_ctx)
00140       return NULL;
00141    
00142    st_ctx->st_dev = st_dev;
00143    ++st_dev->refcount;
00144    
00145    st_ctx->real_pipe = st_dev->st_ws->context_create(st_dev->real_screen);
00146    if(!st_ctx->real_pipe) {
00147       st_context_destroy(st_ctx);
00148       return NULL;
00149    }
00150    
00151    st_ctx->pipe = trace_context_create(st_dev->screen, st_ctx->real_pipe);
00152    if(!st_ctx->pipe) {
00153       st_context_destroy(st_ctx);
00154       return NULL;
00155    }
00156 
00157    st_ctx->cso = cso_create_context(st_ctx->pipe);
00158    if(!st_ctx->cso) {
00159       st_context_destroy(st_ctx);
00160       return NULL;
00161    }
00162    
00163    /* disabled blending/masking */
00164    {
00165       struct pipe_blend_state blend;
00166       memset(&blend, 0, sizeof(blend));
00167       blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
00168       blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
00169       blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
00170       blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
00171       blend.colormask = PIPE_MASK_RGBA;
00172       cso_set_blend(st_ctx->cso, &blend);
00173    }
00174 
00175    /* no-op depth/stencil/alpha */
00176    {
00177       struct pipe_depth_stencil_alpha_state depthstencil;
00178       memset(&depthstencil, 0, sizeof(depthstencil));
00179       cso_set_depth_stencil_alpha(st_ctx->cso, &depthstencil);
00180    }
00181 
00182    /* rasterizer */
00183    {
00184       struct pipe_rasterizer_state rasterizer;
00185       memset(&rasterizer, 0, sizeof(rasterizer));
00186       rasterizer.front_winding = PIPE_WINDING_CW;
00187       rasterizer.cull_mode = PIPE_WINDING_NONE;
00188       rasterizer.bypass_clipping = 1;
00189       /*rasterizer.bypass_vs = 1;*/
00190       cso_set_rasterizer(st_ctx->cso, &rasterizer);
00191    }
00192 
00193    /* identity viewport */
00194    {
00195       struct pipe_viewport_state viewport;
00196       viewport.scale[0] = 1.0;
00197       viewport.scale[1] = 1.0;
00198       viewport.scale[2] = 1.0;
00199       viewport.scale[3] = 1.0;
00200       viewport.translate[0] = 0.0;
00201       viewport.translate[1] = 0.0;
00202       viewport.translate[2] = 0.0;
00203       viewport.translate[3] = 0.0;
00204       cso_set_viewport(st_ctx->cso, &viewport);
00205    }
00206 
00207    /* samplers */
00208    {
00209       struct pipe_sampler_state sampler;
00210       unsigned i;
00211       memset(&sampler, 0, sizeof(sampler));
00212       sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
00213       sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
00214       sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
00215       sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
00216       sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
00217       sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
00218       sampler.normalized_coords = 1;
00219       for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
00220          cso_single_sampler(st_ctx->cso, i, &sampler);
00221       cso_single_sampler_done(st_ctx->cso);
00222    }
00223 
00224    /* default textures */
00225    {
00226       struct pipe_screen *screen = st_dev->screen;
00227       struct pipe_texture templat;
00228       struct pipe_surface *surface;
00229       unsigned i;
00230 
00231       memset( &templat, 0, sizeof( templat ) );
00232       templat.target = PIPE_TEXTURE_2D;
00233       templat.format = PIPE_FORMAT_A8R8G8B8_UNORM;
00234       templat.block.size = 4;
00235       templat.block.width = 1;
00236       templat.block.height = 1;
00237       templat.width[0] = 1;
00238       templat.height[0] = 1;
00239       templat.depth[0] = 1;
00240       templat.last_level = 0;
00241    
00242       st_ctx->default_texture = screen->texture_create( screen, &templat );
00243       if(st_ctx->default_texture) {
00244          surface = screen->get_tex_surface( screen, 
00245                                             st_ctx->default_texture, 0, 0, 0,
00246                                             PIPE_BUFFER_USAGE_CPU_WRITE );
00247          if(surface) {
00248             uint32_t *map;
00249             map = (uint32_t *) pipe_surface_map(surface, PIPE_BUFFER_USAGE_CPU_WRITE );
00250             if(map) {
00251                *map = 0x00000000;
00252                pipe_surface_unmap( surface );
00253             }
00254             pipe_surface_reference(&surface, NULL);
00255          }
00256       }
00257    
00258       for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
00259          pipe_texture_reference(&st_ctx->sampler_textures[i], st_ctx->default_texture);
00260       
00261       cso_set_sampler_textures(st_ctx->cso, PIPE_MAX_SAMPLERS, st_ctx->sampler_textures);
00262    }
00263    
00264    /* vertex shader */
00265    {
00266       struct pipe_shader_state vert_shader;
00267 
00268       const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
00269                                       TGSI_SEMANTIC_GENERIC };
00270       const uint semantic_indexes[] = { 0, 0 };
00271       st_ctx->vs = util_make_vertex_passthrough_shader(st_ctx->pipe, 
00272                                                        2, 
00273                                                        semantic_names,
00274                                                        semantic_indexes,
00275                                                        &vert_shader);
00276       cso_set_vertex_shader_handle(st_ctx->cso, st_ctx->vs);
00277    }
00278 
00279    /* fragment shader */
00280    {
00281       struct pipe_shader_state frag_shader;
00282       st_ctx->fs = util_make_fragment_passthrough_shader(st_ctx->pipe, 
00283                                                          &frag_shader);
00284       cso_set_fragment_shader_handle(st_ctx->cso, st_ctx->fs);
00285    }
00286 
00287    return st_ctx;
00288 }
00289 
00290 
00291 void
00292 st_buffer_destroy(struct st_buffer *st_buf)
00293 {
00294    if(st_buf) {
00295       struct pipe_screen *screen = st_buf->st_dev->screen;
00296       pipe_buffer_reference(screen, &st_buf->buffer, NULL);
00297       FREE(st_buf);
00298    }
00299 }
00300 
00301 
00302 struct st_buffer *
00303 st_buffer_create(struct st_device *st_dev,
00304                  unsigned alignment, unsigned usage, unsigned size)
00305 {
00306    struct pipe_screen *screen = st_dev->screen;
00307    struct st_buffer *st_buf;
00308    
00309    st_buf = CALLOC_STRUCT(st_buffer);
00310    if(!st_buf)
00311       return NULL;
00312 
00313    st_buf->st_dev = st_dev;
00314    
00315    st_buf->buffer = pipe_buffer_create(screen, alignment, usage, size);
00316    if(!st_buf->buffer) {
00317       st_buffer_destroy(st_buf);
00318       return NULL;
00319    }
00320    
00321    return st_buf;
00322 }
00323 

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