st_device.c 9.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/**************************************************************************
 * 
 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/


Zack Rusin's avatar
Zack Rusin committed
29
#include "pipe/p_screen.h"
30 31
#include "pipe/p_context.h"
#include "pipe/p_shader_tokens.h"
32
#include "util/u_inlines.h"
33
#include "cso_cache/cso_context.h"
34
#include "util/u_inlines.h"
35 36
#include "util/u_math.h"
#include "util/u_memory.h"
37
#include "util/u_sampler.h"
38
#include "util/u_simple_shaders.h"
Jose Fonseca's avatar
Jose Fonseca committed
39
#include "trace/tr_public.h"
40

41 42 43 44 45 46 47
#include "st_device.h"
#include "st_winsys.h"


static void
st_device_really_destroy(struct st_device *st_dev) 
{
48 49 50 51 52
   if(st_dev->screen) {
      /* FIXME: Don't really destroy until we keep track of every single 
       * reference or we end up causing a segmentation fault every time 
       * python exits. */
#if 0
José Fonseca's avatar
José Fonseca committed
53
      st_dev->screen->destroy(st_dev->screen);
54 55
#endif
   }
56 57 58 59 60
   
   FREE(st_dev);
}


61 62 63 64 65
static void
st_device_reference(struct st_device **ptr, struct st_device *st_dev)
{
   struct st_device *old_dev = *ptr;

66
   if (pipe_reference(&(*ptr)->reference, &st_dev->reference))
67
      st_device_really_destroy(old_dev);
68
   *ptr = st_dev;
69 70 71
}


72 73 74
void
st_device_destroy(struct st_device *st_dev) 
{
75
   st_device_reference(&st_dev, NULL);
76 77 78
}


79 80
struct st_device *
st_device_create(boolean hardware)
81
{
82
   struct pipe_screen *screen;
83
   struct st_device *st_dev;
84 85 86 87 88 89

   if (hardware)
      screen = st_hardware_screen_create();
   else
      screen = st_software_screen_create();

Jose Fonseca's avatar
Jose Fonseca committed
90 91 92 93
   screen = trace_screen_create(screen);
   if (!screen)
      goto no_screen;

94
   st_dev = CALLOC_STRUCT(st_device);
95 96
   if (!st_dev)
      goto no_device;
97
   
98
   pipe_reference_init(&st_dev->reference, 1);
99
   st_dev->screen = screen;
100 101 102
   
   return st_dev;

103 104 105 106
no_device:
   screen->destroy(screen);
no_screen:
   return NULL;
107 108 109 110 111 112 113 114 115 116 117
}


void
st_context_destroy(struct st_context *st_ctx) 
{
   unsigned i;
   
   if(st_ctx) {
      struct st_device *st_dev = st_ctx->st_dev;
      
118 119 120 121
      if(st_ctx->cso) {
         cso_delete_vertex_shader(st_ctx->cso, st_ctx->vs);
         cso_delete_fragment_shader(st_ctx->cso, st_ctx->fs);
         
122
         cso_destroy_context(st_ctx->cso);
123
      }
124 125
      
      if(st_ctx->pipe)
José Fonseca's avatar
José Fonseca committed
126
         st_ctx->pipe->destroy(st_ctx->pipe);
127 128
      
      for(i = 0; i < PIPE_MAX_SAMPLERS; ++i)
129
         pipe_sampler_view_reference(&st_ctx->fragment_sampler_views[i], NULL);
130
      for(i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; ++i)
131
         pipe_sampler_view_reference(&st_ctx->vertex_sampler_views[i], NULL);
132
      pipe_resource_reference(&st_ctx->default_texture, NULL);
José Fonseca's avatar
José Fonseca committed
133

134 135
      FREE(st_ctx);
      
136
      st_device_reference(&st_dev, NULL);
137 138 139 140 141 142 143 144 145 146 147 148 149
   }
}


struct st_context *
st_context_create(struct st_device *st_dev) 
{
   struct st_context *st_ctx;
   
   st_ctx = CALLOC_STRUCT(st_context);
   if(!st_ctx)
      return NULL;
   
150
   st_device_reference(&st_ctx->st_dev, st_dev);
151
   
152
   st_ctx->pipe = st_dev->screen->context_create(st_dev->screen, NULL);
153 154 155 156 157
   if(!st_ctx->pipe) {
      st_context_destroy(st_ctx);
      return NULL;
   }

158
   st_ctx->cso = cso_create_context(st_ctx->pipe);
159
   if(!st_ctx->cso) {
160
      st_context_destroy(st_ctx);
161 162
      return NULL;
   }
163
   
José Fonseca's avatar
José Fonseca committed
164 165 166 167
   /* disabled blending/masking */
   {
      struct pipe_blend_state blend;
      memset(&blend, 0, sizeof(blend));
168 169 170 171 172
      blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
      blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
      blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
      blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
      blend.rt[0].colormask = PIPE_MASK_RGBA;
José Fonseca's avatar
José Fonseca committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
      cso_set_blend(st_ctx->cso, &blend);
   }

   /* no-op depth/stencil/alpha */
   {
      struct pipe_depth_stencil_alpha_state depthstencil;
      memset(&depthstencil, 0, sizeof(depthstencil));
      cso_set_depth_stencil_alpha(st_ctx->cso, &depthstencil);
   }

   /* rasterizer */
   {
      struct pipe_rasterizer_state rasterizer;
      memset(&rasterizer, 0, sizeof(rasterizer));
      rasterizer.front_winding = PIPE_WINDING_CW;
      rasterizer.cull_mode = PIPE_WINDING_NONE;
      cso_set_rasterizer(st_ctx->cso, &rasterizer);
   }

192 193 194 195 196 197 198
   /* clip */
   {
      struct pipe_clip_state clip;
      memset(&clip, 0, sizeof(clip));
      st_ctx->pipe->set_clip_state(st_ctx->pipe, &clip);
   }

José Fonseca's avatar
José Fonseca committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
   /* identity viewport */
   {
      struct pipe_viewport_state viewport;
      viewport.scale[0] = 1.0;
      viewport.scale[1] = 1.0;
      viewport.scale[2] = 1.0;
      viewport.scale[3] = 1.0;
      viewport.translate[0] = 0.0;
      viewport.translate[1] = 0.0;
      viewport.translate[2] = 0.0;
      viewport.translate[3] = 0.0;
      cso_set_viewport(st_ctx->cso, &viewport);
   }

   /* samplers */
   {
      struct pipe_sampler_state sampler;
      unsigned i;
      memset(&sampler, 0, sizeof(sampler));
      sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
      sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
      sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
221
      sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
José Fonseca's avatar
José Fonseca committed
222 223 224 225 226 227 228 229 230 231
      sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
      sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
      sampler.normalized_coords = 1;
      for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
         cso_single_sampler(st_ctx->cso, i, &sampler);
      cso_single_sampler_done(st_ctx->cso);
   }

   /* default textures */
   {
232
      struct pipe_context *pipe = st_ctx->pipe;
José Fonseca's avatar
José Fonseca committed
233
      struct pipe_screen *screen = st_dev->screen;
234
      struct pipe_resource templat;
235 236
      struct pipe_sampler_view view_templ;
      struct pipe_sampler_view *view;
José Fonseca's avatar
José Fonseca committed
237 238 239 240
      unsigned i;

      memset( &templat, 0, sizeof( templat ) );
      templat.target = PIPE_TEXTURE_2D;
Jose Fonseca's avatar
Jose Fonseca committed
241
      templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
242 243 244
      templat.width0 = 1;
      templat.height0 = 1;
      templat.depth0 = 1;
José Fonseca's avatar
José Fonseca committed
245 246
      templat.last_level = 0;
   
247
      st_ctx->default_texture = screen->resource_create( screen, &templat );
José Fonseca's avatar
José Fonseca committed
248
      if(st_ctx->default_texture) {
249 250 251
	 struct pipe_box box;
	 uint32_t zero = 0;
	 
252
	 u_box_origin_2d( 1, 1, &box );
253

254 255 256 257 258 259 260 261
	 pipe->transfer_inline_write(pipe,
				     st_ctx->default_texture,
				     u_subresource(0,0),
				     PIPE_TRANSFER_WRITE,
				     &box,
				     &zero,
				     sizeof zero,
				     0);
José Fonseca's avatar
José Fonseca committed
262
      }
263 264 265 266 267 268 269 270

      u_sampler_view_default_template(&view_templ,
                                      st_ctx->default_texture,
                                      st_ctx->default_texture->format);
      view = st_ctx->pipe->create_sampler_view(st_ctx->pipe,
                                               st_ctx->default_texture,
                                               &view_templ);

José Fonseca's avatar
José Fonseca committed
271
      for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
272
         pipe_sampler_view_reference(&st_ctx->fragment_sampler_views[i], view);
273
      for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++)
274 275 276 277 278 279 280 281 282 283
         pipe_sampler_view_reference(&st_ctx->vertex_sampler_views[i], view);

      st_ctx->pipe->set_fragment_sampler_views(st_ctx->pipe,
                                               PIPE_MAX_SAMPLERS,
                                               st_ctx->fragment_sampler_views);
      st_ctx->pipe->set_vertex_sampler_views(st_ctx->pipe,
                                             PIPE_MAX_VERTEX_SAMPLERS,
                                             st_ctx->vertex_sampler_views);

      pipe_sampler_view_reference(&view, NULL);
José Fonseca's avatar
José Fonseca committed
284 285
   }
   
286 287 288 289 290 291 292 293
   /* vertex shader */
   {
      const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
                                      TGSI_SEMANTIC_GENERIC };
      const uint semantic_indexes[] = { 0, 0 };
      st_ctx->vs = util_make_vertex_passthrough_shader(st_ctx->pipe, 
                                                       2, 
                                                       semantic_names,
294
                                                       semantic_indexes);
José Fonseca's avatar
José Fonseca committed
295
      cso_set_vertex_shader_handle(st_ctx->cso, st_ctx->vs);
296 297 298
   }

   /* fragment shader */
José Fonseca's avatar
José Fonseca committed
299
   {
300
      st_ctx->fs = util_make_fragment_passthrough_shader(st_ctx->pipe);
José Fonseca's avatar
José Fonseca committed
301 302
      cso_set_fragment_shader_handle(st_ctx->cso, st_ctx->fs);
   }
303 304 305

   return st_ctx;
}