Merge branch '7.8'
[mesa.git] / src / gallium / state_trackers / python / st_device.c
index fbd56712e01bc25423118809a5f1018629f248cc..aac28cacfdea1425f78b49821cd41182f9a2a321 100644 (file)
  **************************************************************************/
 
 
-#include "pipe/p_util.h"
-#include "pipe/p_winsys.h"
+#include "pipe/p_screen.h"
 #include "pipe/p_context.h"
 #include "pipe/p_shader_tokens.h"
-#include "pipe/p_inlines.h"
+#include "util/u_inlines.h"
 #include "cso_cache/cso_context.h"
+#include "util/u_inlines.h"
+#include "util/u_math.h"
+#include "util/u_memory.h"
+#include "util/u_sampler.h"
 #include "util/u_simple_shaders.h"
+#include "trace/tr_public.h"
+
 #include "st_device.h"
 #include "st_winsys.h"
 
 static void
 st_device_really_destroy(struct st_device *st_dev) 
 {
-   if(st_dev->screen)
-      st_dev->st_ws->screen_destroy(st_dev->screen);
+   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
+      st_dev->screen->destroy(st_dev->screen);
+#endif
+   }
    
    FREE(st_dev);
 }
 
 
+static void
+st_device_reference(struct st_device **ptr, struct st_device *st_dev)
+{
+   struct st_device *old_dev = *ptr;
+
+   if (pipe_reference(&(*ptr)->reference, &st_dev->reference))
+      st_device_really_destroy(old_dev);
+   *ptr = st_dev;
+}
+
+
 void
 st_device_destroy(struct st_device *st_dev) 
 {
-   if(!--st_dev->refcount)
-      st_device_really_destroy(st_dev);
+   st_device_reference(&st_dev, NULL);
 }
 
 
-static struct st_device *
-st_device_create_from_st_winsys(const struct st_winsys *st_ws) 
+struct st_device *
+st_device_create(boolean hardware)
 {
+   struct pipe_screen *screen;
    struct st_device *st_dev;
-   
-   if(!st_ws->screen_create ||
-      !st_ws->screen_destroy ||
-      !st_ws->context_create ||
-      !st_ws->context_destroy)
-      return NULL;
-   
+
+   if (hardware)
+      screen = st_hardware_screen_create();
+   else
+      screen = st_software_screen_create("softpipe");
+
+   screen = trace_screen_create(screen);
+   if (!screen)
+      goto no_screen;
+
    st_dev = CALLOC_STRUCT(st_device);
-   if(!st_dev)
-      return NULL;
-   
-   st_dev->refcount = 1;
-   st_dev->st_ws = st_ws;
+   if (!st_dev)
+      goto no_device;
    
-   st_dev->screen = st_ws->screen_create();
-   if(!st_dev->screen)
-      st_device_destroy(st_dev);
+   pipe_reference_init(&st_dev->reference, 1);
+   st_dev->screen = screen;
    
    return st_dev;
-}
-
 
-struct st_device *
-st_device_create(boolean hardware) {
-   if(hardware)
-      return st_device_create_from_st_winsys(&st_hardpipe_winsys);
-   else
-      return st_device_create_from_st_winsys(&st_softpipe_winsys);
+no_device:
+   screen->destroy(screen);
+no_screen:
+   return NULL;
 }
 
 
@@ -106,16 +123,17 @@ st_context_destroy(struct st_context *st_ctx)
       }
       
       if(st_ctx->pipe)
-         st_ctx->st_dev->st_ws->context_destroy(st_ctx->pipe);
+         st_ctx->pipe->destroy(st_ctx->pipe);
       
       for(i = 0; i < PIPE_MAX_SAMPLERS; ++i)
-         pipe_texture_reference(&st_ctx->sampler_textures[i], NULL);
-      pipe_texture_reference(&st_ctx->default_texture, NULL);
+         pipe_sampler_view_reference(&st_ctx->fragment_sampler_views[i], NULL);
+      for(i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; ++i)
+         pipe_sampler_view_reference(&st_ctx->vertex_sampler_views[i], NULL);
+      pipe_resource_reference(&st_ctx->default_texture, NULL);
 
       FREE(st_ctx);
       
-      if(!--st_dev->refcount)
-         st_device_really_destroy(st_dev);
+      st_device_reference(&st_dev, NULL);
    }
 }
 
@@ -129,26 +147,29 @@ st_context_create(struct st_device *st_dev)
    if(!st_ctx)
       return NULL;
    
-   st_ctx->st_dev = st_dev;
-   ++st_dev->refcount;
+   st_device_reference(&st_ctx->st_dev, st_dev);
    
-   st_ctx->pipe = st_dev->st_ws->context_create(st_dev->screen);
-   if(!st_ctx->pipe)
+   st_ctx->pipe = st_dev->screen->context_create(st_dev->screen, NULL);
+   if(!st_ctx->pipe) {
       st_context_destroy(st_ctx);
-   
+      return NULL;
+   }
+
    st_ctx->cso = cso_create_context(st_ctx->pipe);
-   if(!st_ctx->cso)
+   if(!st_ctx->cso) {
       st_context_destroy(st_ctx);
+      return NULL;
+   }
    
    /* disabled blending/masking */
    {
       struct pipe_blend_state blend;
       memset(&blend, 0, sizeof(blend));
-      blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
-      blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
-      blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
-      blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
-      blend.colormask = PIPE_MASK_RGBA;
+      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;
       cso_set_blend(st_ctx->cso, &blend);
    }
 
@@ -165,11 +186,16 @@ st_context_create(struct st_device *st_dev)
       memset(&rasterizer, 0, sizeof(rasterizer));
       rasterizer.front_winding = PIPE_WINDING_CW;
       rasterizer.cull_mode = PIPE_WINDING_NONE;
-      rasterizer.bypass_clipping = 1;
-      /*rasterizer.bypass_vs = 1;*/
       cso_set_rasterizer(st_ctx->cso, &rasterizer);
    }
 
+   /* clip */
+   {
+      struct pipe_clip_state clip;
+      memset(&clip, 0, sizeof(clip));
+      st_ctx->pipe->set_clip_state(st_ctx->pipe, &clip);
+   }
+
    /* identity viewport */
    {
       struct pipe_viewport_state viewport;
@@ -203,99 +229,78 @@ st_context_create(struct st_device *st_dev)
 
    /* default textures */
    {
+      struct pipe_context *pipe = st_ctx->pipe;
       struct pipe_screen *screen = st_dev->screen;
-      struct pipe_texture templat;
-      struct pipe_surface *surface;
+      struct pipe_resource templat;
+      struct pipe_sampler_view view_templ;
+      struct pipe_sampler_view *view;
       unsigned i;
 
       memset( &templat, 0, sizeof( templat ) );
       templat.target = PIPE_TEXTURE_2D;
-      templat.format = PIPE_FORMAT_A8R8G8B8_UNORM;
-      templat.block.size = 4;
-      templat.block.width = 1;
-      templat.block.height = 1;
-      templat.width[0] = 1;
-      templat.height[0] = 1;
-      templat.depth[0] = 1;
+      templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
+      templat.width0 = 1;
+      templat.height0 = 1;
+      templat.depth0 = 1;
       templat.last_level = 0;
+      templat.bind = PIPE_BIND_SAMPLER_VIEW;
    
-      st_ctx->default_texture = screen->texture_create( screen, &templat );
+      st_ctx->default_texture = screen->resource_create( screen, &templat );
       if(st_ctx->default_texture) {
-         surface = screen->get_tex_surface( screen, 
-                                            st_ctx->default_texture, 0, 0, 0,
-                                            PIPE_BUFFER_USAGE_CPU_WRITE );
-         if(surface) {
-            uint32_t *map;
-            map = (uint32_t *) pipe_surface_map(surface, PIPE_BUFFER_USAGE_CPU_WRITE );
-            if(map) {
-               *map = 0x00000000;
-               pipe_surface_unmap( surface );
-            }
-            pipe_surface_reference(&surface, NULL);
-         }
+        struct pipe_box box;
+        uint32_t zero = 0;
+        
+        u_box_origin_2d( 1, 1, &box );
+
+        pipe->transfer_inline_write(pipe,
+                                    st_ctx->default_texture,
+                                    u_subresource(0,0),
+                                    PIPE_TRANSFER_WRITE,
+                                    &box,
+                                    &zero,
+                                    sizeof zero,
+                                    0);
       }
-   
+
+      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);
+
       for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
-         pipe_texture_reference(&st_ctx->sampler_textures[i], st_ctx->default_texture);
-      
-      cso_set_sampler_textures(st_ctx->cso, PIPE_MAX_SAMPLERS, st_ctx->sampler_textures);
+         pipe_sampler_view_reference(&st_ctx->fragment_sampler_views[i], view);
+      for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++)
+         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);
    }
    
    /* vertex shader */
    {
-      struct pipe_shader_state vert_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,
-                                                       semantic_indexes,
-                                                       &vert_shader);
+                                                       semantic_indexes);
       cso_set_vertex_shader_handle(st_ctx->cso, st_ctx->vs);
    }
 
    /* fragment shader */
    {
-      struct pipe_shader_state frag_shader;
-      st_ctx->fs = util_make_fragment_passthrough_shader(st_ctx->pipe, 
-                                                         &frag_shader);
+      st_ctx->fs = util_make_fragment_passthrough_shader(st_ctx->pipe);
       cso_set_fragment_shader_handle(st_ctx->cso, st_ctx->fs);
    }
 
    return st_ctx;
 }
-
-
-void
-st_buffer_destroy(struct st_buffer *st_buf)
-{
-   if(st_buf) {
-      struct pipe_winsys *winsys = st_buf->st_dev->screen->winsys;
-      pipe_buffer_reference(winsys, &st_buf->buffer, NULL);
-      FREE(st_buf);
-   }
-}
-
-
-struct st_buffer *
-st_buffer_create(struct st_device *st_dev,
-                 unsigned alignment, unsigned usage, unsigned size)
-{
-   struct pipe_winsys *winsys = st_dev->screen->winsys;
-   struct st_buffer *st_buf;
-   
-   st_buf = CALLOC_STRUCT(st_buffer);
-   if(!st_buf)
-      return NULL;
-
-   st_buf->st_dev = st_dev;
-   
-   st_buf->buffer = winsys->buffer_create(winsys, alignment, usage, size);
-   if(!st_buf->buffer)
-      st_buffer_destroy(st_buf);
-   
-   return st_buf;
-}
-