[Mesa-dev] [PATCH 1/2] vl: add a luma key filter
Nayan Deshmukh
nayan26deshmukh at gmail.com
Fri May 27 10:50:49 UTC 2016
Signed-off-by: Nayan Deshmukh <nayan26deshmukh at gmail.com>
---
src/gallium/auxiliary/Makefile.sources | 2 +
src/gallium/auxiliary/vl/vl_lumakey_filter.c | 254 +++++++++++++++++++++++++++
src/gallium/auxiliary/vl/vl_lumakey_filter.h | 60 +++++++
3 files changed, 316 insertions(+)
create mode 100644 src/gallium/auxiliary/vl/vl_lumakey_filter.c
create mode 100644 src/gallium/auxiliary/vl/vl_lumakey_filter.h
diff --git a/src/gallium/auxiliary/Makefile.sources b/src/gallium/auxiliary/Makefile.sources
index 9b0c9a3..a6de370 100644
--- a/src/gallium/auxiliary/Makefile.sources
+++ b/src/gallium/auxiliary/Makefile.sources
@@ -328,6 +328,8 @@ VL_SOURCES := \
vl/vl_deint_filter.h \
vl/vl_idct.c \
vl/vl_idct.h \
+ vl/vl_lumakey_filter.c \
+ vl/vl_lumakey_filter.h \
vl/vl_matrix_filter.c \
vl/vl_matrix_filter.h \
vl/vl_mc.c \
diff --git a/src/gallium/auxiliary/vl/vl_lumakey_filter.c b/src/gallium/auxiliary/vl/vl_lumakey_filter.c
new file mode 100644
index 0000000..7767f16
--- /dev/null
+++ b/src/gallium/auxiliary/vl/vl_lumakey_filter.c
@@ -0,0 +1,254 @@
+/**************************************************************************
+ *
+ * Copyright 2016 Nayan Deshmukh.
+ * 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 VMWARE 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.
+ *
+ **************************************************************************/
+
+#include <stdio.h>
+
+#include "pipe/p_context.h"
+
+#include "tgsi/tgsi_ureg.h"
+
+#include "util/u_draw.h"
+#include "util/u_memory.h"
+#include "util/u_math.h"
+
+#include "vl_types.h"
+#include "vl_vertex_buffers.h"
+#include "vl_lumakey_filter.h"
+
+enum VS_OUTPUT
+{
+ VS_O_VPOS = 0,
+ VS_O_VTEX = 0
+};
+
+static void *
+create_vert_shader(struct vl_lumakey_filter *filter)
+{
+ struct ureg_program *shader;
+ struct ureg_src i_vpos;
+ struct ureg_dst o_vpos, o_vtex;
+
+ shader = ureg_create(PIPE_SHADER_VERTEX);
+ if (!shader)
+ return NULL;
+
+ i_vpos = ureg_DECL_vs_input(shader, 0);
+ o_vpos = ureg_DECL_output(shader, TGSI_SEMANTIC_POSITION, VS_O_VPOS);
+ o_vtex = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_VTEX);
+
+ ureg_MOV(shader, o_vpos, i_vpos);
+ ureg_MOV(shader, o_vtex, i_vpos);
+
+ ureg_END(shader);
+
+ return ureg_create_shader_and_destroy(shader, filter->pipe);
+}
+
+static void *
+create_frag_shader(struct vl_lumakey_filter *filter, float min_luma,
+ float max_luma)
+{
+
+ struct ureg_program *shader;
+ struct ureg_src i_vtex;
+ struct ureg_src sampler;
+ struct ureg_dst t_tex[3];
+ struct ureg_dst o_fragment;
+ unsigned i;
+
+ shader = ureg_create(PIPE_SHADER_FRAGMENT);
+
+ i_vtex = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_VTEX, TGSI_INTERPOLATE_LINEAR);
+ sampler = ureg_DECL_sampler(shader, 0);
+
+ for (i = 0; i < 3; ++i) {
+ t_tex[i] = ureg_DECL_temporary(shader);
+ }
+
+ o_fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);
+
+ ureg_MOV(shader, t_tex[0], i_vtex);
+
+ struct ureg_src src = ureg_src(t_tex[0]);
+ ureg_TEX(shader, t_tex[0], TGSI_TEXTURE_2D, src, sampler);
+
+ ureg_MOV(shader, o_fragment, ureg_src(t_tex[0]));
+ ureg_DP3(shader, t_tex[0], ureg_src(t_tex[0]), ureg_imm3f(shader, 0.299, 0.587, 0.144));
+ ureg_SLE(shader, t_tex[1], ureg_src(t_tex[0]), ureg_imm1f(shader, min_luma));
+ ureg_SGT(shader, t_tex[2], ureg_src(t_tex[0]), ureg_imm1f(shader, max_luma));
+ ureg_MAX(shader, ureg_writemask(o_fragment, TGSI_WRITEMASK_W), ureg_src(t_tex[1]), ureg_src(t_tex[2]));
+ ureg_END(shader);
+
+ return ureg_create_shader_and_destroy(shader, filter->pipe);
+}
+
+bool
+vl_lumakey_filter_init(struct vl_lumakey_filter *filter, struct pipe_context *pipe,
+ unsigned video_width, unsigned video_height, float min_luma,
+ float max_luma)
+{
+ struct pipe_rasterizer_state rs_state;
+ struct pipe_blend_state blend;
+ struct pipe_sampler_state sampler;
+ struct pipe_vertex_element ve;
+
+ assert(filter && pipe);
+ assert(video_width && video_height);
+
+ memset(filter, 0, sizeof(*filter));
+ filter->pipe = pipe;
+
+ memset(&rs_state, 0, sizeof(rs_state));
+ rs_state.half_pixel_center = true;
+ rs_state.bottom_edge_rule = true;
+ rs_state.depth_clip = 1;
+ filter->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
+ if (!filter->rs_state)
+ goto error_rs_state;
+
+ memset(&blend, 0, sizeof blend);
+ blend.rt[0].rgb_func = PIPE_BLEND_ADD;
+ blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
+ blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ONE;
+ blend.rt[0].alpha_func = PIPE_BLEND_ADD;
+ blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
+ blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
+ blend.logicop_func = PIPE_LOGICOP_CLEAR;
+ blend.rt[0].colormask = PIPE_MASK_RGBA;
+ filter->blend = pipe->create_blend_state(pipe, &blend);
+ if (!filter->blend)
+ goto error_blend;
+
+ 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;
+ sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
+ sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
+ sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
+ sampler.compare_mode = PIPE_TEX_COMPARE_NONE;
+ sampler.compare_func = PIPE_FUNC_ALWAYS;
+ sampler.normalized_coords = 1;
+ filter->sampler = pipe->create_sampler_state(pipe, &sampler);
+ if (!filter->sampler)
+ goto error_sampler;
+
+ filter->quad = vl_vb_upload_quads(pipe);
+ if(!filter->quad.buffer)
+ goto error_quad;
+
+ memset(&ve, 0, sizeof(ve));
+ ve.src_offset = 0;
+ ve.instance_divisor = 0;
+ ve.vertex_buffer_index = 0;
+ ve.src_format = PIPE_FORMAT_R32G32_FLOAT;
+ filter->ves = pipe->create_vertex_elements_state(pipe, 1, &ve);
+ if (!filter->ves)
+ goto error_ves;
+
+ filter->vs = create_vert_shader(filter);
+ if (!filter->vs)
+ goto error_vs;
+
+ filter->fs = create_frag_shader(filter, min_luma, max_luma);
+ if (!filter->fs)
+ goto error_fs;
+
+ return true;
+
+error_fs:
+ pipe->delete_vs_state(pipe, filter->vs);
+
+error_ves:
+ pipe_resource_reference(&filter->quad.buffer, NULL);
+
+error_quad:
+ pipe->delete_sampler_state(pipe, filter->sampler);
+
+error_sampler:
+ pipe->delete_blend_state(pipe, filter->blend);
+
+error_blend:
+ pipe->delete_rasterizer_state(pipe, filter->rs_state);
+
+error_vs:
+
+error_rs_state:
+ return false;
+}
+
+void
+vl_lumakey_filter_cleanup(struct vl_lumakey_filter *filter)
+{
+ assert(filter);
+
+ filter->pipe->delete_sampler_state(filter->pipe, filter->sampler);
+ filter->pipe->delete_blend_state(filter->pipe, filter->blend);
+ filter->pipe->delete_rasterizer_state(filter->pipe, filter->rs_state);
+ filter->pipe->delete_vertex_elements_state(filter->pipe, filter->ves);
+ pipe_resource_reference(&filter->quad.buffer, NULL);
+
+ filter->pipe->delete_vs_state(filter->pipe, filter->vs);
+ filter->pipe->delete_fs_state(filter->pipe, filter->fs);
+}
+
+void
+vl_lumakey_filter_render(struct vl_lumakey_filter *filter,
+ struct pipe_sampler_view *src,
+ struct pipe_surface *dst)
+{
+ struct pipe_viewport_state viewport;
+ struct pipe_framebuffer_state fb_state;
+
+ assert(filter && src && dst);
+
+ memset(&viewport, 0, sizeof(viewport));
+ viewport.scale[0] = dst->width;
+ viewport.scale[1] = dst->height;
+ viewport.scale[2] = 1;
+
+ memset(&fb_state, 0, sizeof(fb_state));
+ fb_state.width = dst->width;
+ fb_state.height = dst->height;
+ fb_state.nr_cbufs = 1;
+ fb_state.cbufs[0] = dst;
+
+ filter->pipe->bind_rasterizer_state(filter->pipe, filter->rs_state);
+ filter->pipe->bind_blend_state(filter->pipe, filter->blend);
+ filter->pipe->bind_sampler_states(filter->pipe, PIPE_SHADER_FRAGMENT,
+ 0, 1, &filter->sampler);
+ filter->pipe->set_sampler_views(filter->pipe, PIPE_SHADER_FRAGMENT,
+ 0, 1, &src);
+ filter->pipe->bind_vs_state(filter->pipe, filter->vs);
+ filter->pipe->bind_fs_state(filter->pipe, filter->fs);
+ filter->pipe->set_framebuffer_state(filter->pipe, &fb_state);
+ filter->pipe->set_viewport_states(filter->pipe, 0, 1, &viewport);
+ filter->pipe->set_vertex_buffers(filter->pipe, 0, 1, &filter->quad);
+ filter->pipe->bind_vertex_elements_state(filter->pipe, filter->ves);
+
+ util_draw_arrays(filter->pipe, PIPE_PRIM_QUADS, 0, 4);
+}
diff --git a/src/gallium/auxiliary/vl/vl_lumakey_filter.h b/src/gallium/auxiliary/vl/vl_lumakey_filter.h
new file mode 100644
index 0000000..9c8125d
--- /dev/null
+++ b/src/gallium/auxiliary/vl/vl_lumakey_filter.h
@@ -0,0 +1,60 @@
+/**************************************************************************
+ *
+ * Copyright 2016 Nayan Deshmukh.
+ * 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 VMWARE 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.
+ *
+ **************************************************************************/
+
+#ifndef vl_lumakey_filter_h
+#define vl_lumakey_filter_h
+
+#include "pipe/p_state.h"
+
+struct vl_lumakey_filter
+{
+ struct pipe_context *pipe;
+ struct pipe_vertex_buffer quad;
+
+ void *rs_state;
+ void *blend;
+ void *sampler;
+ void *ves;
+ void *vs, *fs;
+};
+
+bool
+vl_lumakey_filter_init(struct vl_lumakey_filter *filter, struct pipe_context *pipe,
+ unsigned video_width, unsigned video_height,
+ float min_luma, float max_luma);
+
+void
+vl_lumakey_filter_cleanup(struct vl_lumakey_filter *filter);
+
+
+void
+vl_lumakey_filter_render(struct vl_lumakey_filter *filter,
+ struct pipe_sampler_view *src,
+ struct pipe_surface *dst);
+
+
+#endif /* vl_lumakey_filter_h */
--
2.5.5
More information about the mesa-dev
mailing list