Re: [Mesa-dev] [PATCH 05/16] nir: add lowering pass for glBitmap

2016-04-08 Thread Eric Anholt
Rob Clark  writes:

> From: Rob Clark 
>
> Signed-off-by: Rob Clark 
> Reviewed-by: Connor Abbott 
> ---
>  src/compiler/Makefile.sources   |   1 +
>  src/compiler/nir/nir.h  |   7 ++
>  src/compiler/nir/nir_lower_bitmap.c | 166 
> 
>  3 files changed, 174 insertions(+)
>  create mode 100644 src/compiler/nir/nir_lower_bitmap.c
>
> diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
> index c396128..cb9eee9 100644
> --- a/src/compiler/Makefile.sources
> +++ b/src/compiler/Makefile.sources
> @@ -185,6 +185,7 @@ NIR_FILES = \
>   nir/nir_liveness.c \
>   nir/nir_lower_alu_to_scalar.c \
>   nir/nir_lower_atomics.c \
> + nir/nir_lower_bitmap.c \
>   nir/nir_lower_clip.c \
>   nir/nir_lower_drawpixels.c \
>   nir/nir_lower_global_vars_to_local.c \
> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
> index 34635ed..85b28d2 100644
> --- a/src/compiler/nir/nir.h
> +++ b/src/compiler/nir/nir.h
> @@ -2269,6 +2269,13 @@ typedef struct nir_lower_drawpixels_options {
>  void nir_lower_drawpixels(nir_shader *shader,
>const nir_lower_drawpixels_options *options);
>  
> +typedef struct nir_lower_bitmap_options {
> +   unsigned sampler;
> +   bool swizzle_;
> +} nir_lower_bitmap_options;
> +
> +void nir_lower_bitmap(nir_shader *shader, const nir_lower_bitmap_options 
> *options);
> +
>  void nir_lower_atomics(nir_shader *shader,
> const struct gl_shader_program *shader_program);
>  void nir_lower_to_source_mods(nir_shader *shader);
> diff --git a/src/compiler/nir/nir_lower_bitmap.c 
> b/src/compiler/nir/nir_lower_bitmap.c
> new file mode 100644
> index 000..3cba69a
> --- /dev/null
> +++ b/src/compiler/nir/nir_lower_bitmap.c
> @@ -0,0 +1,166 @@
> +/*
> + * Copyright © 2015 Red Hat
> + *
> + * 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, sublicense,
> + * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
> + * THE AUTHORS OR COPYRIGHT HOLDERS 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 "nir.h"
> +#include "nir_builder.h"
> +
> +/* Lower glBitmap().
> + *
> + * This is based on the logic in st_get_bitmap_shader() in TGSI compiler.
> + * From st_cb_bitmap.c:
> + *
> + *glBitmaps are drawn as textured quads.  The user's bitmap pattern
> + *is stored in a texture image.  An alpha8 texture format is used.
> + *The fragment shader samples a bit (texel) from the texture, then
> + *discards the fragment if the bit is off.
> + *
> + *Note that we actually store the inverse image of the bitmap to
> + *simplify the fragment program.  An "on" bit gets stored as texel=0x0
> + *and an "off" bit is stored as texel=0xff.  Then we kill the
> + *fragment if the negated texel value is less than zero.
> + *
> + * Note that the texture format will be, according to what driver supports,
> + * in order of preference (with swizzle):
> + *
> + *I8_UNORM - .
> + *A8_UNORM - .000x
> + *L8_UNORM - .xxx1
> + *
> + * If L8_UNORM, options->swizzle_ is true.  Otherwise we can just use
> + * the .w comp.
> + *
> + * Run before nir_lower_io.
> + */
> +
> +typedef struct {
> +   const nir_lower_bitmap_options *options;
> +   nir_shader   *shader;
> +   nir_builder   b;
> +   nir_variable *texcoord;
> +} lower_bitmap_state;
> +
> +static nir_ssa_def *
> +get_texcoord(lower_bitmap_state *state)
> +{
> +   if (state->texcoord == NULL) {
> +  nir_variable *texcoord = NULL;
> +
> +  /* find gl_TexCoord, if it exists: */
> +  nir_foreach_variable(var, >shader->inputs) {
> + if (strcmp(var->name, "gl_TexCoord") == 0) {
> +texcoord = var;
> +break;
> + }
> +  }
> +
> +  /* otherwise create it: */
> +  if (texcoord == NULL) {
> + texcoord = nir_variable_create(state->shader,
> + 

[Mesa-dev] [PATCH 05/16] nir: add lowering pass for glBitmap

2016-03-26 Thread Rob Clark
From: Rob Clark 

Signed-off-by: Rob Clark 
Reviewed-by: Connor Abbott 
---
 src/compiler/Makefile.sources   |   1 +
 src/compiler/nir/nir.h  |   7 ++
 src/compiler/nir/nir_lower_bitmap.c | 166 
 3 files changed, 174 insertions(+)
 create mode 100644 src/compiler/nir/nir_lower_bitmap.c

diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
index c396128..cb9eee9 100644
--- a/src/compiler/Makefile.sources
+++ b/src/compiler/Makefile.sources
@@ -185,6 +185,7 @@ NIR_FILES = \
nir/nir_liveness.c \
nir/nir_lower_alu_to_scalar.c \
nir/nir_lower_atomics.c \
+   nir/nir_lower_bitmap.c \
nir/nir_lower_clip.c \
nir/nir_lower_drawpixels.c \
nir/nir_lower_global_vars_to_local.c \
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index 34635ed..85b28d2 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -2269,6 +2269,13 @@ typedef struct nir_lower_drawpixels_options {
 void nir_lower_drawpixels(nir_shader *shader,
   const nir_lower_drawpixels_options *options);
 
+typedef struct nir_lower_bitmap_options {
+   unsigned sampler;
+   bool swizzle_;
+} nir_lower_bitmap_options;
+
+void nir_lower_bitmap(nir_shader *shader, const nir_lower_bitmap_options 
*options);
+
 void nir_lower_atomics(nir_shader *shader,
const struct gl_shader_program *shader_program);
 void nir_lower_to_source_mods(nir_shader *shader);
diff --git a/src/compiler/nir/nir_lower_bitmap.c 
b/src/compiler/nir/nir_lower_bitmap.c
new file mode 100644
index 000..3cba69a
--- /dev/null
+++ b/src/compiler/nir/nir_lower_bitmap.c
@@ -0,0 +1,166 @@
+/*
+ * Copyright © 2015 Red Hat
+ *
+ * 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, sublicense,
+ * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS 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 "nir.h"
+#include "nir_builder.h"
+
+/* Lower glBitmap().
+ *
+ * This is based on the logic in st_get_bitmap_shader() in TGSI compiler.
+ * From st_cb_bitmap.c:
+ *
+ *glBitmaps are drawn as textured quads.  The user's bitmap pattern
+ *is stored in a texture image.  An alpha8 texture format is used.
+ *The fragment shader samples a bit (texel) from the texture, then
+ *discards the fragment if the bit is off.
+ *
+ *Note that we actually store the inverse image of the bitmap to
+ *simplify the fragment program.  An "on" bit gets stored as texel=0x0
+ *and an "off" bit is stored as texel=0xff.  Then we kill the
+ *fragment if the negated texel value is less than zero.
+ *
+ * Note that the texture format will be, according to what driver supports,
+ * in order of preference (with swizzle):
+ *
+ *I8_UNORM - .
+ *A8_UNORM - .000x
+ *L8_UNORM - .xxx1
+ *
+ * If L8_UNORM, options->swizzle_ is true.  Otherwise we can just use
+ * the .w comp.
+ *
+ * Run before nir_lower_io.
+ */
+
+typedef struct {
+   const nir_lower_bitmap_options *options;
+   nir_shader   *shader;
+   nir_builder   b;
+   nir_variable *texcoord;
+} lower_bitmap_state;
+
+static nir_ssa_def *
+get_texcoord(lower_bitmap_state *state)
+{
+   if (state->texcoord == NULL) {
+  nir_variable *texcoord = NULL;
+
+  /* find gl_TexCoord, if it exists: */
+  nir_foreach_variable(var, >shader->inputs) {
+ if (strcmp(var->name, "gl_TexCoord") == 0) {
+texcoord = var;
+break;
+ }
+  }
+
+  /* otherwise create it: */
+  if (texcoord == NULL) {
+ texcoord = nir_variable_create(state->shader,
+nir_var_shader_in,
+glsl_vec4_type(),
+"gl_TexCoord");
+ texcoord->data.location = VARYING_SLOT_TEX0;
+  }
+
+  state->texcoord = texcoord;
+   }
+   return nir_load_var(>b,