gallium: split depth_clip into depth_clip_near & depth_clip_far
[mesa.git] / src / gallium / state_trackers / xa / xa_renderer.c
index 559b2699da6647a1d7edea7969feb3b4aed574fb..d87a14e80880cc154a6175d296042f97018ce0a2 100644 (file)
@@ -45,14 +45,14 @@ void
 renderer_set_constants(struct xa_context *r,
                       int shader_type, const float *params, int param_bytes);
 
-static INLINE boolean
+static inline boolean
 is_affine(float *matrix)
 {
     return floatIsZero(matrix[2]) && floatIsZero(matrix[5])
        && floatsEqual(matrix[8], 1);
 }
 
-static INLINE void
+static inline void
 map_point(float *mat, float x, float y, float *out_x, float *out_y)
 {
     if (!mat) {
@@ -71,43 +71,33 @@ map_point(float *mat, float x, float y, float *out_x, float *out_y)
     }
 }
 
-static INLINE struct pipe_resource *
-renderer_buffer_create(struct xa_context *r)
-{
-    struct pipe_resource *buf = pipe_user_buffer_create(r->pipe->screen,
-                                                       r->buffer,
-                                                       sizeof(float) *
-                                                       r->buffer_size,
-                                                       PIPE_BIND_VERTEX_BUFFER);
-
-    r->buffer_size = 0;
-
-    return buf;
-}
-
-static INLINE void
+static inline void
 renderer_draw(struct xa_context *r)
 {
-    struct pipe_context *pipe = r->pipe;
-    struct pipe_resource *buf = 0;
     int num_verts = r->buffer_size / (r->attrs_per_vertex * NUM_COMPONENTS);
 
     if (!r->buffer_size)
        return;
 
-    buf = renderer_buffer_create(r);
+    if (!r->scissor_valid) {
+       r->scissor.minx = 0;
+       r->scissor.miny = 0;
+       r->scissor.maxx = r->dst->tex->width0;
+       r->scissor.maxy = r->dst->tex->height0;
+    }
 
-    if (buf) {
-       cso_set_vertex_elements(r->cso, r->attrs_per_vertex, r->velems);
+    r->pipe->set_scissor_states(r->pipe, 0, 1, &r->scissor);
 
-       util_draw_vertex_buffer(pipe, r->cso, buf, 0, PIPE_PRIM_QUADS, num_verts,       /* verts */
-                               r->attrs_per_vertex);   /* attribs/vert */
+    cso_set_vertex_elements(r->cso, r->attrs_per_vertex, r->velems);
+    util_draw_user_vertex_buffer(r->cso, r->buffer, PIPE_PRIM_QUADS,
+                                 num_verts,    /* verts */
+                                 r->attrs_per_vertex); /* attribs/vert */
+    r->buffer_size = 0;
 
-       pipe_resource_reference(&buf, NULL);
-    }
+    xa_scissor_reset(r);
 }
 
-static INLINE void
+static inline void
 renderer_draw_conditional(struct xa_context *r, int next_batch)
 {
     if (r->buffer_size + next_batch >= XA_VB_SIZE ||
@@ -129,7 +119,11 @@ renderer_init_state(struct xa_context *r)
 
     /* XXX: move to renderer_init_state? */
     memset(&raster, 0, sizeof(struct pipe_rasterizer_state));
-    raster.gl_rasterization_rules = 1;
+    raster.half_pixel_center = 1;
+    raster.bottom_edge_rule = 1;
+    raster.depth_clip_near = 1;
+    raster.depth_clip_far = 1;
+    raster.scissor = 1;
     cso_set_rasterizer(r->cso, &raster);
 
     /* vertex elements state */
@@ -142,7 +136,7 @@ renderer_init_state(struct xa_context *r)
     }
 }
 
-static INLINE void
+static inline void
 add_vertex_color(struct xa_context *r, float x, float y, float color[4])
 {
     float *vertex = r->buffer + r->buffer_size;
@@ -160,7 +154,7 @@ add_vertex_color(struct xa_context *r, float x, float y, float color[4])
     r->buffer_size += 8;
 }
 
-static INLINE void
+static inline void
 add_vertex_1tex(struct xa_context *r, float x, float y, float s, float t)
 {
     float *vertex = r->buffer + r->buffer_size;
@@ -178,7 +172,7 @@ add_vertex_1tex(struct xa_context *r, float x, float y, float s, float t)
     r->buffer_size += 8;
 }
 
-static INLINE void
+static inline void
 add_vertex_2tex(struct xa_context *r,
                float x, float y, float s0, float t0, float s1, float t1)
 {
@@ -303,7 +297,7 @@ add_vertex_data2(struct xa_context *r,
                    src_s0, src_t1, mask_s0, mask_t1);
 }
 
-static struct pipe_resource *
+static void
 setup_vertex_data_yuv(struct xa_context *r,
                      float srcX,
                      float srcY,
@@ -336,8 +330,6 @@ setup_vertex_data_yuv(struct xa_context *r,
     add_vertex_1tex(r, dstX + dstW, dstY + dstH, s1, t1);
     /* 4th vertex */
     add_vertex_1tex(r, dstX, dstY + dstH, s0, t1);
-
-    return renderer_buffer_create(r);
 }
 
 /* Set up framebuffer, viewport and vertex shader constant buffer
@@ -346,12 +338,16 @@ setup_vertex_data_yuv(struct xa_context *r,
  */
 void
 renderer_bind_destination(struct xa_context *r,
-                         struct pipe_surface *surface, int width, int height)
+                         struct pipe_surface *surface)
 {
+    int width = surface->width;
+    int height = surface->height;
 
     struct pipe_framebuffer_state fb;
     struct pipe_viewport_state viewport;
 
+    xa_scissor_reset(r);
+
     /* Framebuffer uses actual surface width/height
      */
     memset(&fb, 0, sizeof fb);
@@ -366,11 +362,9 @@ renderer_bind_destination(struct xa_context *r,
     viewport.scale[0] = width / 2.f;
     viewport.scale[1] = height / 2.f;
     viewport.scale[2] = 1.0;
-    viewport.scale[3] = 1.0;
     viewport.translate[0] = width / 2.f;
     viewport.translate[1] = height / 2.f;
     viewport.translate[2] = 0.0;
-    viewport.translate[3] = 0.0;
 
     /* Constant buffer set up to match viewport dimensions:
      */
@@ -400,30 +394,36 @@ renderer_set_constants(struct xa_context *r,
        &r->fs_const_buffer;
 
     pipe_resource_reference(cbuf, NULL);
-    *cbuf = pipe_buffer_create(r->pipe->screen,
-                              PIPE_BIND_CONSTANT_BUFFER, PIPE_USAGE_STATIC,
-                              param_bytes);
+    *cbuf = pipe_buffer_create_const0(r->pipe->screen,
+                                      PIPE_BIND_CONSTANT_BUFFER,
+                                      PIPE_USAGE_DEFAULT,
+                                      param_bytes);
 
     if (*cbuf) {
        pipe_buffer_write(r->pipe, *cbuf, 0, param_bytes, params);
     }
-    r->pipe->set_constant_buffer(r->pipe, shader_type, 0, *cbuf);
+    pipe_set_constant_buffer(r->pipe, shader_type, 0, *cbuf);
 }
 
 void
 renderer_copy_prepare(struct xa_context *r,
                      struct pipe_surface *dst_surface,
-                     struct pipe_resource *src_texture)
+                     struct pipe_resource *src_texture,
+                     const enum xa_formats src_xa_format,
+                     const enum xa_formats dst_xa_format)
 {
     struct pipe_context *pipe = r->pipe;
     struct pipe_screen *screen = pipe->screen;
     struct xa_shader shader;
+    uint32_t fs_traits = FS_COMPOSITE;
 
     assert(screen->is_format_supported(screen, dst_surface->format,
-                                      PIPE_TEXTURE_2D, 0,
+                                      PIPE_TEXTURE_2D, 0, 0,
                                       PIPE_BIND_RENDER_TARGET));
     (void)screen;
 
+    renderer_bind_destination(r, dst_surface);
+
     /* set misc state we care about */
     {
        struct pipe_blend_state blend;
@@ -440,6 +440,7 @@ renderer_copy_prepare(struct xa_context *r,
     /* sampler */
     {
        struct pipe_sampler_state sampler;
+        const struct pipe_sampler_state *p_sampler = &sampler;
 
        memset(&sampler, 0, sizeof(sampler));
        sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
@@ -449,13 +450,10 @@ renderer_copy_prepare(struct xa_context *r,
        sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
        sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
        sampler.normalized_coords = 1;
-       cso_single_sampler(r->cso, 0, &sampler);
-       cso_single_sampler_done(r->cso);
+        cso_set_samplers(r->cso, PIPE_SHADER_FRAGMENT, 1, &p_sampler);
+        r->num_bound_samplers = 1;
     }
 
-    renderer_bind_destination(r, dst_surface,
-                             dst_surface->width, dst_surface->height);
-
     /* texture/sampler view */
     {
        struct pipe_sampler_view templ;
@@ -464,12 +462,22 @@ renderer_copy_prepare(struct xa_context *r,
        u_sampler_view_default_template(&templ,
                                        src_texture, src_texture->format);
        src_view = pipe->create_sampler_view(pipe, src_texture, &templ);
-       cso_set_fragment_sampler_views(r->cso, 1, &src_view);
+       cso_set_sampler_views(r->cso, PIPE_SHADER_FRAGMENT, 1, &src_view);
        pipe_sampler_view_reference(&src_view, NULL);
     }
 
     /* shaders */
-    shader = xa_shaders_get(r->shaders, VS_COMPOSITE, FS_COMPOSITE);
+    if (src_texture->format == PIPE_FORMAT_L8_UNORM ||
+        src_texture->format == PIPE_FORMAT_R8_UNORM)
+       fs_traits |= FS_SRC_LUMINANCE;
+    if (dst_surface->format == PIPE_FORMAT_L8_UNORM ||
+        dst_surface->format == PIPE_FORMAT_R8_UNORM)
+       fs_traits |= FS_DST_LUMINANCE;
+    if (xa_format_a(dst_xa_format) != 0 &&
+       xa_format_a(src_xa_format) == 0)
+       fs_traits |= FS_SRC_SET_ALPHA;
+
+    shader = xa_shaders_get(r->shaders, VS_COMPOSITE, fs_traits);
     cso_set_vertex_shader_handle(r->cso, shader.vs);
     cso_set_fragment_shader_handle(r->cso, shader.fs);
 
@@ -518,23 +526,28 @@ renderer_draw_yuv(struct xa_context *r,
                  int dst_x,
                  int dst_y, int dst_w, int dst_h, struct xa_surface *srf[])
 {
-    struct pipe_context *pipe = r->pipe;
-    struct pipe_resource *buf = 0;
+   const int num_attribs = 2;  /*pos + tex coord */
 
-    buf = setup_vertex_data_yuv(r,
-                               src_x, src_y, src_w, src_h, dst_x, dst_y, dst_w,
-                               dst_h, srf);
+   setup_vertex_data_yuv(r,
+                         src_x, src_y, src_w, src_h,
+                         dst_x, dst_y, dst_w, dst_h, srf);
 
-    if (buf) {
-       const int num_attribs = 2;      /*pos + tex coord */
+   if (!r->scissor_valid) {
+       r->scissor.minx = 0;
+       r->scissor.miny = 0;
+       r->scissor.maxx = r->dst->tex->width0;
+       r->scissor.maxy = r->dst->tex->height0;
+   }
 
-       cso_set_vertex_elements(r->cso, num_attribs, r->velems);
+   r->pipe->set_scissor_states(r->pipe, 0, 1, &r->scissor);
 
-       util_draw_vertex_buffer(pipe, r->cso, buf, 0, PIPE_PRIM_QUADS, 4,       /* verts */
-                               num_attribs);   /* attribs/vert */
+   cso_set_vertex_elements(r->cso, num_attribs, r->velems);
+   util_draw_user_vertex_buffer(r->cso, r->buffer, PIPE_PRIM_QUADS,
+                                4,     /* verts */
+                                num_attribs);  /* attribs/vert */
+   r->buffer_size = 0;
 
-       pipe_resource_reference(&buf, NULL);
-    }
+   xa_scissor_reset(r);
 }
 
 void