[g3dvl] cleanup and documentation
[mesa.git] / src / gallium / auxiliary / vl / vl_idct.c
index b84b447ce6b386a923bb77fd89761192b41ad1c5..6b0010a04bbf44d03b2035cd0b4270c60b783762 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "vl_idct.h"
 #include "vl_vertex_buffers.h"
+#include "vl_defines.h"
 #include "util/u_draw.h"
 #include <assert.h>
 #include <pipe/p_context.h>
 #include <tgsi/tgsi_ureg.h>
 #include "vl_types.h"
 
-#define BLOCK_WIDTH 8
-#define BLOCK_HEIGHT 8
-
 #define SCALE_FACTOR_16_TO_9 (32768.0f / 256.0f)
 
 #define NR_RENDER_TARGETS 4
 
-enum VS_INPUT
-{
-   VS_I_RECT,
-   VS_I_VPOS,
-
-   NUM_VS_INPUTS
-};
-
 enum VS_OUTPUT
 {
    VS_O_VPOS,
@@ -99,13 +89,14 @@ calc_addr(struct ureg_program *shader, struct ureg_dst addr[2],
 }
 
 static void *
-create_vert_shader(struct vl_idct *idct, bool matrix_stage)
+create_vert_shader(struct vl_idct *idct, bool matrix_stage, int color_swizzle)
 {
    struct ureg_program *shader;
-   struct ureg_src scale;
-   struct ureg_src vrect, vpos;
+   struct ureg_src vrect, vpos, vblock, eb[4];
+   struct ureg_src scale, blocks_xy, t_eb;
    struct ureg_dst t_tex, t_start;
    struct ureg_dst o_vpos, o_l_addr[2], o_r_addr[2];
+   unsigned label;
 
    shader = ureg_create(TGSI_PROCESSOR_VERTEX);
    if (!shader)
@@ -116,9 +107,15 @@ create_vert_shader(struct vl_idct *idct, bool matrix_stage)
 
    vrect = ureg_DECL_vs_input(shader, VS_I_RECT);
    vpos = ureg_DECL_vs_input(shader, VS_I_VPOS);
+   vblock = ureg_swizzle(vrect, TGSI_SWIZZLE_Z, TGSI_SWIZZLE_W, TGSI_SWIZZLE_X, TGSI_SWIZZLE_X);
 
    o_vpos = ureg_DECL_output(shader, TGSI_SEMANTIC_POSITION, VS_O_VPOS);
 
+   eb[0] = ureg_DECL_vs_input(shader, VS_I_EB_0_0);
+   eb[1] = ureg_DECL_vs_input(shader, VS_I_EB_1_0);
+   eb[2] = ureg_DECL_vs_input(shader, VS_I_EB_0_1);
+   eb[3] = ureg_DECL_vs_input(shader, VS_I_EB_1_1);
+
    o_l_addr[0] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_L_ADDR0);
    o_l_addr[1] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_L_ADDR1);
 
@@ -127,38 +124,74 @@ create_vert_shader(struct vl_idct *idct, bool matrix_stage)
 
    /*
     * scale = (BLOCK_WIDTH, BLOCK_HEIGHT) / (dst.width, dst.height)
+    * blocks_xy = (blocks_x, blocks_y)
     *
-    * t_vpos = vpos + vrect
-    * o_vpos.xy = t_vpos * scale
-    * o_vpos.zw = vpos
+    * ar = vblock.y * blocks.x + vblock.x
+    * if eb[ar].(color_swizzle)
+    *    o_vpos.xy = -1
+    * else
+    *    t_tex = vpos * blocks_xy + vblock
+    *    t_start = t_tex * scale
+    *    t_tex = t_tex + vrect
+    *    o_vpos.xy = t_tex * scale
     *
-    * o_l_addr = calc_addr(...)
-    * o_r_addr = calc_addr(...)
+    *    o_l_addr = calc_addr(...)
+    *    o_r_addr = calc_addr(...)
+    * endif
+    * o_vpos.zw = vpos
     *
     */
+
    scale = ureg_imm2f(shader,
       (float)BLOCK_WIDTH / idct->buffer_width,
       (float)BLOCK_HEIGHT / idct->buffer_height);
 
-   ureg_ADD(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_XY), vpos, vrect);
-   ureg_MUL(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_XY), ureg_src(t_tex), scale);
-   ureg_MUL(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_Z),
-      ureg_scalar(vrect, TGSI_SWIZZLE_X),
-      ureg_imm1f(shader, BLOCK_WIDTH / NR_RENDER_TARGETS));
+   blocks_xy = ureg_imm2f(shader, idct->blocks_x, idct->blocks_y);
 
-   ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_XY), ureg_src(t_tex));
-   ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_ZW), vpos);
+   if (idct->blocks_x > 1 || idct->blocks_y > 1) {
+      struct ureg_dst ar = ureg_DECL_address(shader);
 
-   ureg_MUL(shader, ureg_writemask(t_start, TGSI_WRITEMASK_XY), vpos, scale);
+      ureg_MAD(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_X),
+               ureg_scalar(vblock, TGSI_SWIZZLE_Y), blocks_xy, vblock);
 
-   if(matrix_stage) {
-      calc_addr(shader, o_l_addr, ureg_src(t_tex), ureg_src(t_start), false, false, idct->buffer_width / 4);
-      calc_addr(shader, o_r_addr, vrect, ureg_imm1f(shader, 0.0f), true, true, BLOCK_WIDTH / 4);
+      ureg_ARL(shader, ureg_writemask(ar, TGSI_WRITEMASK_X), ureg_src(t_tex));
+      t_eb = ureg_src_indirect(eb[0], ureg_src(ar));
    } else {
-      calc_addr(shader, o_l_addr, vrect, ureg_imm1f(shader, 0.0f), false, false, BLOCK_WIDTH / 4);
-      calc_addr(shader, o_r_addr, ureg_src(t_tex), ureg_src(t_start), true, false, idct->buffer_height / 4);
+      t_eb = eb[0];
    }
 
+   ureg_IF(shader, ureg_scalar(t_eb, color_swizzle), &label);
+
+      ureg_MOV(shader, o_vpos, ureg_imm1f(shader, -1.0f));
+
+   ureg_fixup_label(shader, label, ureg_get_instruction_number(shader));
+   ureg_ELSE(shader, &label);
+
+      ureg_MAD(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_XY), vpos, blocks_xy, vblock);
+      ureg_MUL(shader, ureg_writemask(t_start, TGSI_WRITEMASK_XY), ureg_src(t_tex), scale);
+
+      ureg_ADD(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_XY), ureg_src(t_tex), vrect);
+
+      ureg_MUL(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_XY), ureg_src(t_tex), scale);
+      ureg_MUL(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_Z),
+         ureg_scalar(vrect, TGSI_SWIZZLE_X),
+         ureg_imm1f(shader, BLOCK_WIDTH / NR_RENDER_TARGETS));
+
+      ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_XY), ureg_src(t_tex));
+
+      if(matrix_stage) {
+         calc_addr(shader, o_l_addr, ureg_src(t_tex), ureg_src(t_start), false, false, idct->buffer_width / 4);
+         calc_addr(shader, o_r_addr, vrect, ureg_imm1f(shader, 0.0f), true, true, BLOCK_WIDTH / 4);
+      } else {
+         calc_addr(shader, o_l_addr, vrect, ureg_imm1f(shader, 0.0f), false, false, BLOCK_WIDTH / 4);
+         calc_addr(shader, o_r_addr, ureg_src(t_tex), ureg_src(t_start), true, false, idct->buffer_height / 4);
+      }
+
+   ureg_fixup_label(shader, label, ureg_get_instruction_number(shader));
+   ureg_ENDIF(shader);
+
+   ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_ZW), vpos);
+
    ureg_release_temporary(shader, t_tex);
    ureg_release_temporary(shader, t_start);
 
@@ -175,11 +208,11 @@ increment_addr(struct ureg_program *shader, struct ureg_dst daddr[2],
    unsigned wm_start = (right_side == transposed) ? TGSI_WRITEMASK_X : TGSI_WRITEMASK_Y;
    unsigned wm_tc = (right_side == transposed) ? TGSI_WRITEMASK_Y : TGSI_WRITEMASK_X;
 
-   /* 
-    * daddr[0..1].(start) = saddr[0..1].(start) 
-    * daddr[0..1].(tc) = saddr[0..1].(tc) 
+   /*
+    * daddr[0..1].(start) = saddr[0..1].(start)
+    * daddr[0..1].(tc) = saddr[0..1].(tc)
     */
-   
+
    ureg_MOV(shader, ureg_writemask(daddr[0], wm_start), saddr[0]);
    ureg_ADD(shader, ureg_writemask(daddr[0], wm_tc), saddr[0], ureg_imm1f(shader, pos / size));
    ureg_MOV(shader, ureg_writemask(daddr[1], wm_start), saddr[1]);
@@ -256,7 +289,7 @@ create_matrix_frag_shader(struct vl_idct *idct)
       s_addr[1] = i == 0 ? l_addr[1] : ureg_src(l[i][1]);
       fetch_four(shader, l[i], s_addr, ureg_DECL_sampler(shader, 1));
    }
-   
+
    for (i = 0; i < NR_RENDER_TARGETS; ++i) {
       if(i > 0)
          increment_addr(shader, r, r_addr, true, true, i, BLOCK_HEIGHT);
@@ -326,15 +359,15 @@ create_transpose_frag_shader(struct vl_idct *idct)
 }
 
 static bool
-init_shaders(struct vl_idct *idct)
+init_shaders(struct vl_idct *idct, int color_swizzle)
 {
-   idct->matrix_vs = create_vert_shader(idct, true);
+   idct->matrix_vs = create_vert_shader(idct, true, color_swizzle);
    idct->matrix_fs = create_matrix_frag_shader(idct);
 
-   idct->transpose_vs = create_vert_shader(idct, false);
+   idct->transpose_vs = create_vert_shader(idct, false, color_swizzle);
    idct->transpose_fs = create_transpose_frag_shader(idct);
 
-   return 
+   return
       idct->matrix_vs != NULL &&
       idct->matrix_fs != NULL &&
       idct->transpose_vs != NULL &&
@@ -353,18 +386,12 @@ cleanup_shaders(struct vl_idct *idct)
 static bool
 init_state(struct vl_idct *idct)
 {
-   struct pipe_vertex_element vertex_elems[NUM_VS_INPUTS];
    struct pipe_sampler_state sampler;
    struct pipe_rasterizer_state rs_state;
    unsigned i;
 
    assert(idct);
 
-   idct->quad = vl_vb_upload_quads(idct->pipe, idct->max_blocks);
-
-   if(idct->quad.buffer == NULL)
-      return false;
-
    for (i = 0; i < 4; ++i) {
       memset(&sampler, 0, sizeof(sampler));
       sampler.wrap_s = PIPE_TEX_WRAP_REPEAT;
@@ -393,14 +420,6 @@ init_state(struct vl_idct *idct)
    rs_state.gl_rasterization_rules = false;
    idct->rs_state = idct->pipe->create_rasterizer_state(idct->pipe, &rs_state);
 
-   vertex_elems[VS_I_RECT] = vl_vb_get_quad_vertex_element();
-
-   /* Pos element */
-   vertex_elems[VS_I_VPOS].src_format = PIPE_FORMAT_R32G32_FLOAT;
-
-   idct->vertex_buffer_stride = vl_vb_element_helper(&vertex_elems[VS_I_VPOS], 1, 1);
-   idct->vertex_elems_state = idct->pipe->create_vertex_elements_state(idct->pipe, 2, vertex_elems);
-
    return true;
 }
 
@@ -413,7 +432,6 @@ cleanup_state(struct vl_idct *idct)
       idct->pipe->delete_sampler_state(idct->pipe, idct->samplers.all[i]);
 
    idct->pipe->delete_rasterizer_state(idct->pipe, idct->rs_state);
-   idct->pipe->delete_vertex_elements_state(idct->pipe, idct->vertex_elems_state);
 }
 
 static bool
@@ -428,7 +446,6 @@ init_textures(struct vl_idct *idct, struct vl_idct_buffer *buffer)
    /* create textures */
    memset(&template, 0, sizeof(struct pipe_resource));
    template.last_level = 0;
-   template.depth0 = 1;
    template.bind = PIPE_BIND_SAMPLER_VIEW;
    template.flags = 0;
 
@@ -437,6 +454,7 @@ init_textures(struct vl_idct *idct, struct vl_idct_buffer *buffer)
    template.width0 = idct->buffer_width / 4;
    template.height0 = idct->buffer_height;
    template.depth0 = 1;
+   template.array_size = 1;
    template.usage = PIPE_USAGE_STREAM;
    buffer->textures.individual.source = idct->pipe->screen->resource_create(idct->pipe->screen, &template);
 
@@ -456,6 +474,15 @@ init_textures(struct vl_idct *idct, struct vl_idct_buffer *buffer)
       buffer->sampler_views.all[i] = idct->pipe->create_sampler_view(idct->pipe, buffer->textures.all[i], &sampler_view);
    }
 
+   template.target = PIPE_TEXTURE_2D;
+   /* TODO: Accomodate HW that can't do this and also for cases when this isn't precise enough */
+   template.format = PIPE_FORMAT_R16_SNORM;
+   template.width0 = idct->buffer_width;
+   template.height0 = idct->buffer_height;
+   template.depth0 = 1;
+
+   buffer->destination = idct->pipe->screen->resource_create(idct->pipe->screen, &template);
+
    return true;
 }
 
@@ -472,40 +499,11 @@ cleanup_textures(struct vl_idct *idct, struct vl_idct_buffer *buffer)
    }
 }
 
-static bool
-init_vertex_buffers(struct vl_idct *idct, struct vl_idct_buffer *buffer)
-{
-   assert(idct && buffer);
-
-   buffer->vertex_bufs.individual.quad.stride = idct->quad.stride;
-   buffer->vertex_bufs.individual.quad.max_index = idct->quad.max_index;
-   buffer->vertex_bufs.individual.quad.buffer_offset = idct->quad.buffer_offset;
-   pipe_resource_reference(&buffer->vertex_bufs.individual.quad.buffer, idct->quad.buffer);
-
-   buffer->vertex_bufs.individual.pos = vl_vb_init(
-      &buffer->blocks, idct->pipe, idct->max_blocks, 2,
-      idct->vertex_buffer_stride);
-
-   if(buffer->vertex_bufs.individual.pos.buffer == NULL)
-      return false;
-
-   return true;
-}
-
-static void
-cleanup_vertex_buffers(struct vl_idct *idct, struct vl_idct_buffer *buffer)
-{
-   assert(idct && buffer);
-
-   pipe_resource_reference(&buffer->vertex_bufs.individual.quad.buffer, NULL);
-   pipe_resource_reference(&buffer->vertex_bufs.individual.pos.buffer, NULL);
-
-   vl_vb_cleanup(&buffer->blocks);
-}
-
 struct pipe_resource *
 vl_idct_upload_matrix(struct pipe_context *pipe)
 {
+   const float scale = sqrtf(SCALE_FACTOR_16_TO_9);
+
    struct pipe_resource template, *matrix;
    struct pipe_transfer *buf_transfer;
    unsigned i, j, pitch;
@@ -526,6 +524,7 @@ vl_idct_upload_matrix(struct pipe_context *pipe)
    template.width0 = 2;
    template.height0 = 8;
    template.depth0 = 1;
+   template.array_size = 1;
    template.usage = PIPE_USAGE_IMMUTABLE;
    template.bind = PIPE_BIND_SAMPLER_VIEW;
    template.flags = 0;
@@ -545,7 +544,7 @@ vl_idct_upload_matrix(struct pipe_context *pipe)
    for(i = 0; i < BLOCK_HEIGHT; ++i)
       for(j = 0; j < BLOCK_WIDTH; ++j)
          // transpose and scale
-         f[i * pitch + j] = const_matrix[j][i] * sqrtf(SCALE_FACTOR_16_TO_9);
+         f[i * pitch + j] = const_matrix[j][i] * scale;
 
    pipe->transfer_unmap(pipe, buf_transfer);
    pipe->transfer_destroy(pipe, buf_transfer);
@@ -553,22 +552,21 @@ vl_idct_upload_matrix(struct pipe_context *pipe)
    return matrix;
 }
 
-bool vl_idct_init(struct vl_idct *idct, struct pipe_context *pipe, 
+bool vl_idct_init(struct vl_idct *idct, struct pipe_context *pipe,
                   unsigned buffer_width, unsigned buffer_height,
-                  struct pipe_resource *matrix)
+                  unsigned blocks_x, unsigned blocks_y,
+                  int color_swizzle, struct pipe_resource *matrix)
 {
    assert(idct && pipe && matrix);
 
    idct->pipe = pipe;
    idct->buffer_width = buffer_width;
    idct->buffer_height = buffer_height;
+   idct->blocks_x = blocks_x;
+   idct->blocks_y = blocks_y;
    pipe_resource_reference(&idct->matrix, matrix);
 
-   idct->max_blocks =
-      align(buffer_width, BLOCK_WIDTH) / BLOCK_WIDTH *
-      align(buffer_height, BLOCK_HEIGHT) / BLOCK_HEIGHT;
-
-   if(!init_shaders(idct))
+   if(!init_shaders(idct, color_swizzle))
       return false;
 
    if(!init_state(idct)) {
@@ -588,8 +586,8 @@ vl_idct_cleanup(struct vl_idct *idct)
    pipe_resource_reference(&idct->matrix, NULL);
 }
 
-bool
-vl_idct_init_buffer(struct vl_idct *idct, struct vl_idct_buffer *buffer, struct pipe_resource *dst)
+struct pipe_resource *
+vl_idct_init_buffer(struct vl_idct *idct, struct vl_idct_buffer *buffer)
 {
    struct pipe_surface template;
 
@@ -597,17 +595,12 @@ vl_idct_init_buffer(struct vl_idct *idct, struct vl_idct_buffer *buffer, struct
 
    assert(buffer);
    assert(idct);
-   assert(dst);
 
    pipe_resource_reference(&buffer->textures.individual.matrix, idct->matrix);
    pipe_resource_reference(&buffer->textures.individual.transpose, idct->matrix);
-   pipe_resource_reference(&buffer->destination, dst);
 
    if (!init_textures(idct, buffer))
-      return false;
-
-   if (!init_vertex_buffers(idct, buffer))
-      return false;
+      return NULL;
 
    /* init state */
    buffer->viewport[0].scale[0] = buffer->textures.individual.intermediate->width0;
@@ -653,7 +646,7 @@ vl_idct_init_buffer(struct vl_idct *idct, struct vl_idct_buffer *buffer, struct
       buffer->fb_state[i].zsbuf = NULL;
    }
 
-   return true;
+   return buffer->destination;
 }
 
 void
@@ -670,7 +663,6 @@ vl_idct_cleanup_buffer(struct vl_idct *idct, struct vl_idct_buffer *buffer)
    idct->pipe->surface_destroy(idct->pipe, buffer->fb_state[1].cbufs[0]);
 
    cleanup_textures(idct, buffer);
-   cleanup_vertex_buffers(idct, buffer);
 }
 
 void
@@ -694,14 +686,11 @@ vl_idct_map_buffers(struct vl_idct *idct, struct vl_idct_buffer *buffer)
    );
 
    buffer->texels = idct->pipe->transfer_map(idct->pipe, buffer->tex_transfer);
-
-   vl_vb_map(&buffer->blocks, idct->pipe);
 }
 
 void
 vl_idct_add_block(struct vl_idct_buffer *buffer, unsigned x, unsigned y, short *block)
 {
-   struct vertex2f v;
    unsigned tex_pitch;
    short *texels;
 
@@ -714,10 +703,6 @@ vl_idct_add_block(struct vl_idct_buffer *buffer, unsigned x, unsigned y, short *
 
    for (i = 0; i < BLOCK_HEIGHT; ++i)
       memcpy(texels + i * tex_pitch, block + i * BLOCK_WIDTH, BLOCK_WIDTH * sizeof(short));
-
-   v.x = x;
-   v.y = y;
-   vl_vb_add_block(&buffer->blocks, (float*)&v);
 }
 
 void
@@ -727,23 +712,20 @@ vl_idct_unmap_buffers(struct vl_idct *idct, struct vl_idct_buffer *buffer)
 
    idct->pipe->transfer_unmap(idct->pipe, buffer->tex_transfer);
    idct->pipe->transfer_destroy(idct->pipe, buffer->tex_transfer);
-   vl_vb_unmap(&buffer->blocks, idct->pipe);
 }
 
 void
-vl_idct_flush(struct vl_idct *idct, struct vl_idct_buffer *buffer)
+vl_idct_flush(struct vl_idct *idct, struct vl_idct_buffer *buffer, unsigned num_instances)
 {
    unsigned num_verts;
 
    assert(idct);
+   assert(buffer);
 
-   num_verts = vl_vb_restart(&buffer->blocks);
-
-   if(num_verts > 0) {
+   if(num_instances > 0) {
+      num_verts = idct->blocks_x * idct->blocks_y * 4;
 
       idct->pipe->bind_rasterizer_state(idct->pipe, idct->rs_state);
-      idct->pipe->set_vertex_buffers(idct->pipe, 2, buffer->vertex_bufs.all);
-      idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state);
 
       /* first stage */
       idct->pipe->set_framebuffer_state(idct->pipe, &buffer->fb_state[0]);
@@ -752,7 +734,7 @@ vl_idct_flush(struct vl_idct *idct, struct vl_idct_buffer *buffer)
       idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers.stage[0]);
       idct->pipe->bind_vs_state(idct->pipe, idct->matrix_vs);
       idct->pipe->bind_fs_state(idct->pipe, idct->matrix_fs);
-      util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, num_verts);
+      util_draw_arrays_instanced(idct->pipe, PIPE_PRIM_QUADS, 0, num_verts, 0, num_instances);
 
       /* second stage */
       idct->pipe->set_framebuffer_state(idct->pipe, &buffer->fb_state[1]);
@@ -761,6 +743,6 @@ vl_idct_flush(struct vl_idct *idct, struct vl_idct_buffer *buffer)
       idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers.stage[1]);
       idct->pipe->bind_vs_state(idct->pipe, idct->transpose_vs);
       idct->pipe->bind_fs_state(idct->pipe, idct->transpose_fs);
-      util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, num_verts);
+      util_draw_arrays_instanced(idct->pipe, PIPE_PRIM_QUADS, 0, num_verts, 0, num_instances);
    }
 }