draw: use float version of LLVM Mul/Add instructions
[mesa.git] / src / gallium / state_trackers / vega / api_filters.c
index 2f984fb7b9afa012d4c6ea47ad9851bcf68512ef..8ace9853368b15e8cbaeaa6f27c39c1d62afacc7 100644 (file)
 
 #include "vg_context.h"
 #include "image.h"
+#include "api.h"
 #include "renderer.h"
 #include "shaders_cache.h"
 #include "st_inlines.h"
 
 #include "pipe/p_context.h"
 #include "pipe/p_state.h"
-#include "pipe/p_inlines.h"
+#include "util/u_inlines.h"
 #include "pipe/p_screen.h"
 #include "pipe/p_shader_tokens.h"
 
 #include "util/u_format.h"
 #include "util/u_memory.h"
+#include "util/u_sampler.h"
+#include "util/u_string.h"
 
 
 #include "asm_filters.h"
@@ -53,52 +56,74 @@ struct filter_info {
    const void *const_buffer;
    VGint const_buffer_len;
    VGTilingMode tiling_mode;
-   struct pipe_texture *extra_texture;
+   struct pipe_sampler_view *extra_texture_view;
 };
 
-static INLINE struct pipe_texture *create_texture_1d(struct vg_context *ctx,
+static INLINE struct pipe_resource *create_texture_1d(struct vg_context *ctx,
                                                      const VGuint *color_data,
                                                      const VGint color_data_len)
 {
    struct pipe_context *pipe = ctx->pipe;
    struct pipe_screen *screen = pipe->screen;
-   struct pipe_texture *tex = 0;
-   struct pipe_texture templ;
+   struct pipe_resource *tex = 0;
+   struct pipe_resource templ;
 
    memset(&templ, 0, sizeof(templ));
    templ.target = PIPE_TEXTURE_1D;
-   templ.format = PIPE_FORMAT_A8R8G8B8_UNORM;
+   templ.format = PIPE_FORMAT_B8G8R8A8_UNORM;
    templ.last_level = 0;
    templ.width0 = color_data_len;
    templ.height0 = 1;
    templ.depth0 = 1;
-   templ.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER;
+   templ.bind = PIPE_BIND_SAMPLER_VIEW;
 
-   tex = screen->texture_create(screen, &templ);
+   tex = screen->resource_create(screen, &templ);
 
    { /* upload color_data */
       struct pipe_transfer *transfer =
-         screen->get_tex_transfer(screen, tex,
-                                  0, 0, 0,
-                                  PIPE_TRANSFER_READ_WRITE ,
-                                  0, 0, tex->width0, tex->height0);
-      void *map = screen->transfer_map(screen, transfer);
+         pipe_get_transfer(pipe, tex,
+                               0, 0, 0,
+                               PIPE_TRANSFER_READ_WRITE ,
+                               0, 0, tex->width0, tex->height0);
+      void *map = pipe->transfer_map(pipe, transfer);
       memcpy(map, color_data, sizeof(VGint)*color_data_len);
-      screen->transfer_unmap(screen, transfer);
-      screen->tex_transfer_destroy(transfer);
+      pipe->transfer_unmap(pipe, transfer);
+      pipe->transfer_destroy(pipe, transfer);
    }
 
    return tex;
 }
 
+static INLINE struct pipe_sampler_view *create_texture_1d_view(struct vg_context *ctx,
+                                                               const VGuint *color_data,
+                                                               const VGint color_data_len)
+{
+   struct pipe_context *pipe = ctx->pipe;
+   struct pipe_resource *texture;
+   struct pipe_sampler_view view_templ;
+   struct pipe_sampler_view *view;
+
+   texture = create_texture_1d(ctx, color_data, color_data_len);
+
+   if (!texture)
+      return NULL;
+
+   u_sampler_view_default_template(&view_templ, texture, texture->format);
+   view = pipe->create_sampler_view(pipe, texture, &view_templ);
+   /* want the texture to go away if the view is freed */
+   pipe_resource_reference(&texture, NULL);
+
+   return view;
+}
+
 static INLINE struct pipe_surface * setup_framebuffer(struct vg_image *dst)
 {
    struct vg_context *ctx = vg_current_context();
    struct pipe_context *pipe = ctx->pipe;
    struct pipe_framebuffer_state fb;
    struct pipe_surface *dst_surf = pipe->screen->get_tex_surface(
-      pipe->screen, dst->texture, 0, 0, 0,
-      PIPE_BUFFER_USAGE_GPU_WRITE);
+      pipe->screen, dst->sampler_view->texture, 0, 0, 0,
+      PIPE_BIND_RENDER_TARGET);
 
    /* drawing dest */
    memset(&fb, 0, sizeof(fb));
@@ -127,19 +152,19 @@ static void setup_blend()
    struct vg_context *ctx = vg_current_context();
    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.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;
    if (ctx->state.vg.filter_channel_mask & VG_RED)
-      blend.colormask |= PIPE_MASK_R;
+      blend.rt[0].colormask |= PIPE_MASK_R;
    if (ctx->state.vg.filter_channel_mask & VG_GREEN)
-      blend.colormask |= PIPE_MASK_G;
+      blend.rt[0].colormask |= PIPE_MASK_G;
    if (ctx->state.vg.filter_channel_mask & VG_BLUE)
-      blend.colormask |= PIPE_MASK_B;
+      blend.rt[0].colormask |= PIPE_MASK_B;
    if (ctx->state.vg.filter_channel_mask & VG_ALPHA)
-      blend.colormask |= PIPE_MASK_A;
-   blend.blend_enable = 1;
+      blend.rt[0].colormask |= PIPE_MASK_A;
+   blend.rt[0].blend_enable = 0;
    cso_set_blend(ctx->cso_context, &blend);
 }
 
@@ -147,28 +172,28 @@ static void setup_constant_buffer(struct vg_context *ctx, const void *buffer,
                                   VGint param_bytes)
 {
    struct pipe_context *pipe = ctx->pipe;
-   struct pipe_constant_buffer *cbuf = &ctx->filter.buffer;
+   struct pipe_resource **cbuf = &ctx->filter.buffer;
 
    /* We always need to get a new buffer, to keep the drivers simple and
     * avoid gratuitous rendering synchronization. */
-   pipe_buffer_reference(&cbuf->buffer, NULL);
+   pipe_resource_reference(cbuf, NULL);
 
-   cbuf->buffer = pipe_buffer_create(pipe->screen, 16,
-                                     PIPE_BUFFER_USAGE_CONSTANT,
-                                     param_bytes);
+   *cbuf = pipe_buffer_create(pipe->screen, 
+                              PIPE_BIND_CONSTANT_BUFFER,
+                              param_bytes);
 
-   if (cbuf->buffer) {
-      st_no_flush_pipe_buffer_write(ctx, cbuf->buffer,
+   if (*cbuf) {
+      st_no_flush_pipe_buffer_write(ctx, *cbuf,
                                     0, param_bytes, buffer);
    }
 
-   ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_FRAGMENT, 0, cbuf);
+   ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_FRAGMENT, 0, *cbuf);
 }
 
 static void setup_samplers(struct vg_context *ctx, struct filter_info *info)
 {
    struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
-   struct pipe_texture *textures[PIPE_MAX_SAMPLERS];
+   struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
    struct pipe_sampler_state sampler[3];
    int num_samplers = 0;
    int num_textures = 0;
@@ -177,10 +202,10 @@ static void setup_samplers(struct vg_context *ctx, struct filter_info *info)
    samplers[1] = NULL;
    samplers[2] = NULL;
    samplers[3] = NULL;
-   textures[0] = NULL;
-   textures[1] = NULL;
-   textures[2] = NULL;
-   textures[3] = NULL;
+   sampler_views[0] = NULL;
+   sampler_views[1] = NULL;
+   sampler_views[2] = NULL;
+   sampler_views[3] = NULL;
 
    memset(&sampler[0], 0, sizeof(struct pipe_sampler_state));
    sampler[0].wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
@@ -215,21 +240,21 @@ static void setup_samplers(struct vg_context *ctx, struct filter_info *info)
    }
 
    samplers[0] = &sampler[0];
-   textures[0] = info->src->texture;
+   sampler_views[0] = info->src->sampler_view;
    ++num_samplers;
    ++num_textures;
 
-   if (info->extra_texture) {
+   if (info->extra_texture_view) {
       memcpy(&sampler[1], &sampler[0], sizeof(struct pipe_sampler_state));
       samplers[1] = &sampler[1];
-      textures[1] = info->extra_texture;
+      sampler_views[1] = info->extra_texture_view;
       ++num_samplers;
       ++num_textures;
    }
 
 
    cso_set_samplers(ctx->cso_context, num_samplers, (const struct pipe_sampler_state **)samplers);
-   cso_set_sampler_textures(ctx->cso_context, num_textures, textures);
+   cso_set_fragment_sampler_views(ctx->cso_context, num_textures, sampler_views);
 }
 
 static struct vg_shader * setup_color_matrix(struct vg_context *ctx, void *user_data)
@@ -247,7 +272,7 @@ static struct vg_shader * setup_convolution(struct vg_context *ctx, void *user_d
    VGint num_consts = (VGint)(long)(user_data);
    struct vg_shader *shader;
 
-   snprintf(buffer, 1023, convolution_asm, num_consts, num_consts / 2 + 1);
+   util_snprintf(buffer, 1023, convolution_asm, num_consts, num_consts / 2 + 1);
 
    shader = shader_create_from_text(ctx->pipe, buffer, 200,
                                     PIPE_SHADER_FRAGMENT);
@@ -275,16 +300,16 @@ static struct vg_shader * setup_lookup_single(struct vg_context *ctx, void *user
 
    switch(channel) {
    case VG_RED:
-      snprintf(buffer, 1023, lookup_single_asm, "xxxx");
+      util_snprintf(buffer, 1023, lookup_single_asm, "xxxx");
       break;
    case VG_GREEN:
-      snprintf(buffer, 1023, lookup_single_asm, "yyyy");
+      util_snprintf(buffer, 1023, lookup_single_asm, "yyyy");
       break;
    case VG_BLUE:
-      snprintf(buffer, 1023, lookup_single_asm, "zzzz");
+      util_snprintf(buffer, 1023, lookup_single_asm, "zzzz");
       break;
    case VG_ALPHA:
-      snprintf(buffer, 1023, lookup_single_asm, "wwww");
+      util_snprintf(buffer, 1023, lookup_single_asm, "wwww");
       break;
    default:
       debug_assert(!"Unknown color channel");
@@ -308,7 +333,7 @@ static void execute_filter(struct vg_context *ctx,
    cso_save_viewport(ctx->cso_context);
    cso_save_blend(ctx->cso_context);
    cso_save_samplers(ctx->cso_context);
-   cso_save_sampler_textures(ctx->cso_context);
+   cso_save_fragment_sampler_views(ctx->cso_context);
 
    dst_surf = setup_framebuffer(info->dst);
    setup_viewport(info->dst);
@@ -318,7 +343,7 @@ static void execute_filter(struct vg_context *ctx,
    setup_samplers(ctx, info);
 
    renderer_draw_texture(ctx->renderer,
-                         info->src->texture,
+                         info->src->sampler_view->texture,
                          info->dst->x, info->dst->y,
                          info->dst->x + info->dst->width,
                          info->dst->y + info->dst->height,
@@ -331,15 +356,15 @@ static void execute_filter(struct vg_context *ctx,
    cso_restore_viewport(ctx->cso_context);
    cso_restore_blend(ctx->cso_context);
    cso_restore_samplers(ctx->cso_context);
-   cso_restore_sampler_textures(ctx->cso_context);
+   cso_restore_fragment_sampler_views(ctx->cso_context);
 
    vg_shader_destroy(ctx, shader);
 
    pipe_surface_reference(&dst_surf, NULL);
 }
 
-void vgColorMatrix(VGImage dst, VGImage src,
-                   const VGfloat * matrix)
+void vegaColorMatrix(VGImage dst, VGImage src,
+                     const VGfloat * matrix)
 {
    struct vg_context *ctx = vg_current_context();
    struct vg_image *d, *s;
@@ -369,7 +394,7 @@ void vgColorMatrix(VGImage dst, VGImage src,
    info.const_buffer = matrix;
    info.const_buffer_len = 20 * sizeof(VGfloat);
    info.tiling_mode = VG_TILE_PAD;
-   info.extra_texture = 0;
+   info.extra_texture_view = NULL;
    execute_filter(ctx, &info);
 }
 
@@ -380,13 +405,13 @@ static VGfloat texture_offset(VGfloat width, VGint kernelSize, VGint current, VG
    return diff / width;
 }
 
-void vgConvolve(VGImage dst, VGImage src,
-                VGint kernelWidth, VGint kernelHeight,
-                VGint shiftX, VGint shiftY,
-                const VGshort * kernel,
-                VGfloat scale,
-                VGfloat bias,
-                VGTilingMode tilingMode)
+void vegaConvolve(VGImage dst, VGImage src,
+                  VGint kernelWidth, VGint kernelHeight,
+                  VGint shiftX, VGint shiftY,
+                  const VGshort * kernel,
+                  VGfloat scale,
+                  VGfloat bias,
+                  VGTilingMode tilingMode)
 {
    struct vg_context *ctx = vg_current_context();
    VGfloat *buffer;
@@ -479,21 +504,21 @@ void vgConvolve(VGImage dst, VGImage src,
    info.const_buffer = buffer;
    info.const_buffer_len = buffer_len * sizeof(VGfloat);
    info.tiling_mode = tilingMode;
-   info.extra_texture = 0;
+   info.extra_texture_view = NULL;
    execute_filter(ctx, &info);
 
    free(buffer);
 }
 
-void vgSeparableConvolve(VGImage dst, VGImage src,
-                         VGint kernelWidth,
-                         VGint kernelHeight,
-                         VGint shiftX, VGint shiftY,
-                         const VGshort * kernelX,
-                         const VGshort * kernelY,
-                         VGfloat scale,
-                         VGfloat bias,
-                         VGTilingMode tilingMode)
+void vegaSeparableConvolve(VGImage dst, VGImage src,
+                           VGint kernelWidth,
+                           VGint kernelHeight,
+                           VGint shiftX, VGint shiftY,
+                           const VGshort * kernelX,
+                           const VGshort * kernelY,
+                           VGfloat scale,
+                           VGfloat bias,
+                           VGTilingMode tilingMode)
 {
    struct vg_context *ctx = vg_current_context();
    VGshort *kernel;
@@ -577,10 +602,10 @@ static void compute_gaussian_kernel(VGfloat *kernel,
    }
 }
 
-void vgGaussianBlur(VGImage dst, VGImage src,
-                    VGfloat stdDeviationX,
-                    VGfloat stdDeviationY,
-                    VGTilingMode tilingMode)
+void vegaGaussianBlur(VGImage dst, VGImage src,
+                      VGfloat stdDeviationX,
+                      VGfloat stdDeviationY,
+                      VGTilingMode tilingMode)
 {
    struct vg_context *ctx = vg_current_context();
    struct vg_image *d, *s;
@@ -669,26 +694,26 @@ void vgGaussianBlur(VGImage dst, VGImage src,
    info.const_buffer = buffer;
    info.const_buffer_len = buffer_len * sizeof(VGfloat);
    info.tiling_mode = tilingMode;
-   info.extra_texture = 0;
+   info.extra_texture_view = NULL;
    execute_filter(ctx, &info);
 
    free(buffer);
    free(kernel);
 }
 
-void vgLookup(VGImage dst, VGImage src,
-              const VGubyte * redLUT,
-              const VGubyte * greenLUT,
-              const VGubyte * blueLUT,
-              const VGubyte * alphaLUT,
-              VGboolean outputLinear,
-              VGboolean outputPremultiplied)
+void vegaLookup(VGImage dst, VGImage src,
+                const VGubyte * redLUT,
+                const VGubyte * greenLUT,
+                const VGubyte * blueLUT,
+                const VGubyte * alphaLUT,
+                VGboolean outputLinear,
+                VGboolean outputPremultiplied)
 {
    struct vg_context *ctx = vg_current_context();
    struct vg_image *d, *s;
    VGuint color_data[256];
    VGint i;
-   struct pipe_texture *lut_texture;
+   struct pipe_sampler_view *lut_texture_view;
    VGfloat buffer[4];
    struct filter_info info;
 
@@ -714,7 +739,7 @@ void vgLookup(VGImage dst, VGImage src,
       color_data[i] = blueLUT[i] << 24 | greenLUT[i] << 16 |
                       redLUT[i]  <<  8 | alphaLUT[i];
    }
-   lut_texture = create_texture_1d(ctx, color_data, 255);
+   lut_texture_view = create_texture_1d_view(ctx, color_data, 255);
 
    buffer[0] = 0.f;
    buffer[1] = 0.f;
@@ -728,22 +753,22 @@ void vgLookup(VGImage dst, VGImage src,
    info.const_buffer = buffer;
    info.const_buffer_len = 4 * sizeof(VGfloat);
    info.tiling_mode = VG_TILE_PAD;
-   info.extra_texture = lut_texture;
+   info.extra_texture_view = lut_texture_view;
 
    execute_filter(ctx, &info);
 
-   pipe_texture_reference(&lut_texture, NULL);
+   pipe_sampler_view_reference(&lut_texture_view, NULL);
 }
 
-void vgLookupSingle(VGImage dst, VGImage src,
-                    const VGuint * lookupTable,
-                    VGImageChannel sourceChannel,
-                    VGboolean outputLinear,
-                    VGboolean outputPremultiplied)
+void vegaLookupSingle(VGImage dst, VGImage src,
+                      const VGuint * lookupTable,
+                      VGImageChannel sourceChannel,
+                      VGboolean outputLinear,
+                      VGboolean outputPremultiplied)
 {
    struct vg_context *ctx = vg_current_context();
    struct vg_image *d, *s;
-   struct pipe_texture *lut_texture;
+   struct pipe_sampler_view *lut_texture_view;
    VGfloat buffer[4];
    struct filter_info info;
    VGuint color_data[256];
@@ -783,7 +808,7 @@ void vgLookupSingle(VGImage dst, VGImage src,
       color_data[i] = blue << 24 | green << 16 |
                       red  <<  8 | alpha;
    }
-   lut_texture = create_texture_1d(ctx, color_data, 256);
+   lut_texture_view = create_texture_1d_view(ctx, color_data, 256);
 
    buffer[0] = 0.f;
    buffer[1] = 0.f;
@@ -797,9 +822,9 @@ void vgLookupSingle(VGImage dst, VGImage src,
    info.const_buffer = buffer;
    info.const_buffer_len = 4 * sizeof(VGfloat);
    info.tiling_mode = VG_TILE_PAD;
-   info.extra_texture = lut_texture;
+   info.extra_texture_view = lut_texture_view;
 
    execute_filter(ctx, &info);
 
-   pipe_texture_reference(&lut_texture, NULL);
+   pipe_sampler_view_reference(&lut_texture_view, NULL);
 }