Re: [Mesa-dev] [PATCH v2 12/16] glsl: Add a lowering pass to handle advanced blending modes.

2016-08-23 Thread Francisco Jerez
Kenneth Graunke  writes:

> On Monday, August 22, 2016 5:50:49 PM PDT Francisco Jerez wrote:
>> Kenneth Graunke  writes:
>> 
>> > Many GPUs cannot handle GL_KHR_blend_equation_advanced natively, and
>> > need to emulate it in the pixel shader.  This lowering pass implements
>> > all the necessary math for advanced blending.  It fetches the existing
>> > framebuffer value using the MESA_shader_framebuffer_fetch built-in
>> > variables, and the previous commit's state var uniform to select
>> > which equation to use.
>> >
>> > This is done at the GLSL IR level to make it easy for all drivers to
>> > implement the GL_KHR_blend_equation_advanced extension and share code.
>> >
>> > Drivers need to hook up MESA_shader_framebuffer_fetch functionality:
>> > 1. Hook up the fb_fetch_output variable
>> > 2. Implement BlendBarrier()
>> >
>> > Then to get KHR_blend_equation_advanced, they simply need to:
>> > 3. Disable hardware blending based on ctx->Color._AdvancedBlendEnabled
>> > 4. Call this lowering pass.
>> >
>> > Very little driver specific code should be required.
>> >
>> > v2: Handle multiple output variables per render target (which may exist
>> > due to ARB_enhanced_layouts), and array variables (even with one
>> > render target, we might have out vec4 color[1]), and non-vec4
>> > variables (it's easier than finding spec text to justify not
>> > handling it).  Thanks to Francisco Jerez for the feedback.
>> >
>> > Signed-off-by: Kenneth Graunke 
>> > ---
>> >  src/compiler/Makefile.sources  |   1 +
>> >  src/compiler/glsl/ir_optimization.h|   1 +
>> >  .../glsl/lower_blend_equation_advanced.cpp | 557 
>> > +
>> >  3 files changed, 559 insertions(+)
>> >  create mode 100644 src/compiler/glsl/lower_blend_equation_advanced.cpp
>> >
>> > diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
>> > index 0ff9b23..fe26132 100644
>> > --- a/src/compiler/Makefile.sources
>> > +++ b/src/compiler/Makefile.sources
>> > @@ -77,6 +77,7 @@ LIBGLSL_FILES = \
>> >glsl/loop_analysis.h \
>> >glsl/loop_controls.cpp \
>> >glsl/loop_unroll.cpp \
>> > +  glsl/lower_blend_equation_advanced.cpp \
>> >glsl/lower_buffer_access.cpp \
>> >glsl/lower_buffer_access.h \
>> >glsl/lower_const_arrays_to_uniforms.cpp \
>> > diff --git a/src/compiler/glsl/ir_optimization.h 
>> > b/src/compiler/glsl/ir_optimization.h
>> > index c29260a..3bd6928 100644
>> > --- a/src/compiler/glsl/ir_optimization.h
>> > +++ b/src/compiler/glsl/ir_optimization.h
>> > @@ -151,6 +151,7 @@ void optimize_dead_builtin_variables(exec_list 
>> > *instructions,
>> >  bool lower_tess_level(gl_linked_shader *shader);
>> >  
>> >  bool lower_vertex_id(gl_linked_shader *shader);
>> > +bool lower_blend_equation_advanced(gl_linked_shader *shader);
>> >  
>> >  bool lower_subroutine(exec_list *instructions, struct 
>> > _mesa_glsl_parse_state *state);
>> >  void propagate_invariance(exec_list *instructions);
>> > diff --git a/src/compiler/glsl/lower_blend_equation_advanced.cpp 
>> > b/src/compiler/glsl/lower_blend_equation_advanced.cpp
>> > new file mode 100644
>> > index 000..5632865
>> > --- /dev/null
>> > +++ b/src/compiler/glsl/lower_blend_equation_advanced.cpp
>> > @@ -0,0 +1,557 @@
>> > +/*
>> > + * Copyright © 2016 Intel Corporation
>> > + *
>> > + * 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 "ir.h"
>> > +#include "ir_builder.h"
>> > +#include "ir_optimization.h"
>> > +#include "ir_hierarchical_visitor.h"
>> > +#include "program/prog_instruction.h"
>> > +#include "program/prog_statevars.h"
>> > +#include "util/bitscan.h"
>> > +
>> > +using namespace 

Re: [Mesa-dev] [PATCH v2 12/16] glsl: Add a lowering pass to handle advanced blending modes.

2016-08-23 Thread Kenneth Graunke
On Monday, August 22, 2016 5:50:49 PM PDT Francisco Jerez wrote:
> Kenneth Graunke  writes:
> 
> > Many GPUs cannot handle GL_KHR_blend_equation_advanced natively, and
> > need to emulate it in the pixel shader.  This lowering pass implements
> > all the necessary math for advanced blending.  It fetches the existing
> > framebuffer value using the MESA_shader_framebuffer_fetch built-in
> > variables, and the previous commit's state var uniform to select
> > which equation to use.
> >
> > This is done at the GLSL IR level to make it easy for all drivers to
> > implement the GL_KHR_blend_equation_advanced extension and share code.
> >
> > Drivers need to hook up MESA_shader_framebuffer_fetch functionality:
> > 1. Hook up the fb_fetch_output variable
> > 2. Implement BlendBarrier()
> >
> > Then to get KHR_blend_equation_advanced, they simply need to:
> > 3. Disable hardware blending based on ctx->Color._AdvancedBlendEnabled
> > 4. Call this lowering pass.
> >
> > Very little driver specific code should be required.
> >
> > v2: Handle multiple output variables per render target (which may exist
> > due to ARB_enhanced_layouts), and array variables (even with one
> > render target, we might have out vec4 color[1]), and non-vec4
> > variables (it's easier than finding spec text to justify not
> > handling it).  Thanks to Francisco Jerez for the feedback.
> >
> > Signed-off-by: Kenneth Graunke 
> > ---
> >  src/compiler/Makefile.sources  |   1 +
> >  src/compiler/glsl/ir_optimization.h|   1 +
> >  .../glsl/lower_blend_equation_advanced.cpp | 557 
> > +
> >  3 files changed, 559 insertions(+)
> >  create mode 100644 src/compiler/glsl/lower_blend_equation_advanced.cpp
> >
> > diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
> > index 0ff9b23..fe26132 100644
> > --- a/src/compiler/Makefile.sources
> > +++ b/src/compiler/Makefile.sources
> > @@ -77,6 +77,7 @@ LIBGLSL_FILES = \
> > glsl/loop_analysis.h \
> > glsl/loop_controls.cpp \
> > glsl/loop_unroll.cpp \
> > +   glsl/lower_blend_equation_advanced.cpp \
> > glsl/lower_buffer_access.cpp \
> > glsl/lower_buffer_access.h \
> > glsl/lower_const_arrays_to_uniforms.cpp \
> > diff --git a/src/compiler/glsl/ir_optimization.h 
> > b/src/compiler/glsl/ir_optimization.h
> > index c29260a..3bd6928 100644
> > --- a/src/compiler/glsl/ir_optimization.h
> > +++ b/src/compiler/glsl/ir_optimization.h
> > @@ -151,6 +151,7 @@ void optimize_dead_builtin_variables(exec_list 
> > *instructions,
> >  bool lower_tess_level(gl_linked_shader *shader);
> >  
> >  bool lower_vertex_id(gl_linked_shader *shader);
> > +bool lower_blend_equation_advanced(gl_linked_shader *shader);
> >  
> >  bool lower_subroutine(exec_list *instructions, struct 
> > _mesa_glsl_parse_state *state);
> >  void propagate_invariance(exec_list *instructions);
> > diff --git a/src/compiler/glsl/lower_blend_equation_advanced.cpp 
> > b/src/compiler/glsl/lower_blend_equation_advanced.cpp
> > new file mode 100644
> > index 000..5632865
> > --- /dev/null
> > +++ b/src/compiler/glsl/lower_blend_equation_advanced.cpp
> > @@ -0,0 +1,557 @@
> > +/*
> > + * Copyright © 2016 Intel Corporation
> > + *
> > + * 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 "ir.h"
> > +#include "ir_builder.h"
> > +#include "ir_optimization.h"
> > +#include "ir_hierarchical_visitor.h"
> > +#include "program/prog_instruction.h"
> > +#include "program/prog_statevars.h"
> > +#include "util/bitscan.h"
> > +
> > +using namespace ir_builder;
> > +
> > +#define imm1(x) new(mem_ctx) ir_constant((float) x, 1)
> > +#define imm3(x) new(mem_ctx) ir_constant((float) x, 3)
> > +
> 
> The x argument should 

Re: [Mesa-dev] [PATCH v2 12/16] glsl: Add a lowering pass to handle advanced blending modes.

2016-08-22 Thread Francisco Jerez
Kenneth Graunke  writes:

> Many GPUs cannot handle GL_KHR_blend_equation_advanced natively, and
> need to emulate it in the pixel shader.  This lowering pass implements
> all the necessary math for advanced blending.  It fetches the existing
> framebuffer value using the MESA_shader_framebuffer_fetch built-in
> variables, and the previous commit's state var uniform to select
> which equation to use.
>
> This is done at the GLSL IR level to make it easy for all drivers to
> implement the GL_KHR_blend_equation_advanced extension and share code.
>
> Drivers need to hook up MESA_shader_framebuffer_fetch functionality:
> 1. Hook up the fb_fetch_output variable
> 2. Implement BlendBarrier()
>
> Then to get KHR_blend_equation_advanced, they simply need to:
> 3. Disable hardware blending based on ctx->Color._AdvancedBlendEnabled
> 4. Call this lowering pass.
>
> Very little driver specific code should be required.
>
> v2: Handle multiple output variables per render target (which may exist
> due to ARB_enhanced_layouts), and array variables (even with one
> render target, we might have out vec4 color[1]), and non-vec4
> variables (it's easier than finding spec text to justify not
> handling it).  Thanks to Francisco Jerez for the feedback.
>
> Signed-off-by: Kenneth Graunke 
> ---
>  src/compiler/Makefile.sources  |   1 +
>  src/compiler/glsl/ir_optimization.h|   1 +
>  .../glsl/lower_blend_equation_advanced.cpp | 557 
> +
>  3 files changed, 559 insertions(+)
>  create mode 100644 src/compiler/glsl/lower_blend_equation_advanced.cpp
>
> diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
> index 0ff9b23..fe26132 100644
> --- a/src/compiler/Makefile.sources
> +++ b/src/compiler/Makefile.sources
> @@ -77,6 +77,7 @@ LIBGLSL_FILES = \
>   glsl/loop_analysis.h \
>   glsl/loop_controls.cpp \
>   glsl/loop_unroll.cpp \
> + glsl/lower_blend_equation_advanced.cpp \
>   glsl/lower_buffer_access.cpp \
>   glsl/lower_buffer_access.h \
>   glsl/lower_const_arrays_to_uniforms.cpp \
> diff --git a/src/compiler/glsl/ir_optimization.h 
> b/src/compiler/glsl/ir_optimization.h
> index c29260a..3bd6928 100644
> --- a/src/compiler/glsl/ir_optimization.h
> +++ b/src/compiler/glsl/ir_optimization.h
> @@ -151,6 +151,7 @@ void optimize_dead_builtin_variables(exec_list 
> *instructions,
>  bool lower_tess_level(gl_linked_shader *shader);
>  
>  bool lower_vertex_id(gl_linked_shader *shader);
> +bool lower_blend_equation_advanced(gl_linked_shader *shader);
>  
>  bool lower_subroutine(exec_list *instructions, struct _mesa_glsl_parse_state 
> *state);
>  void propagate_invariance(exec_list *instructions);
> diff --git a/src/compiler/glsl/lower_blend_equation_advanced.cpp 
> b/src/compiler/glsl/lower_blend_equation_advanced.cpp
> new file mode 100644
> index 000..5632865
> --- /dev/null
> +++ b/src/compiler/glsl/lower_blend_equation_advanced.cpp
> @@ -0,0 +1,557 @@
> +/*
> + * Copyright © 2016 Intel Corporation
> + *
> + * 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 "ir.h"
> +#include "ir_builder.h"
> +#include "ir_optimization.h"
> +#include "ir_hierarchical_visitor.h"
> +#include "program/prog_instruction.h"
> +#include "program/prog_statevars.h"
> +#include "util/bitscan.h"
> +
> +using namespace ir_builder;
> +
> +#define imm1(x) new(mem_ctx) ir_constant((float) x, 1)
> +#define imm3(x) new(mem_ctx) ir_constant((float) x, 3)
> +

The x argument should probably be between parentheses to avoid surprises
in the future -- Or even better, make them static inline functions
instead of macros.

> +static ir_rvalue *
> +blend_multiply(ir_variable *src, ir_variable *dst)
> +{
> +   /* f(Cs,Cd) = Cs*Cd */
> +   return mul(src, dst);
> +}
> +
> +static 

[Mesa-dev] [PATCH v2 12/16] glsl: Add a lowering pass to handle advanced blending modes.

2016-08-21 Thread Kenneth Graunke
Many GPUs cannot handle GL_KHR_blend_equation_advanced natively, and
need to emulate it in the pixel shader.  This lowering pass implements
all the necessary math for advanced blending.  It fetches the existing
framebuffer value using the MESA_shader_framebuffer_fetch built-in
variables, and the previous commit's state var uniform to select
which equation to use.

This is done at the GLSL IR level to make it easy for all drivers to
implement the GL_KHR_blend_equation_advanced extension and share code.

Drivers need to hook up MESA_shader_framebuffer_fetch functionality:
1. Hook up the fb_fetch_output variable
2. Implement BlendBarrier()

Then to get KHR_blend_equation_advanced, they simply need to:
3. Disable hardware blending based on ctx->Color._AdvancedBlendEnabled
4. Call this lowering pass.

Very little driver specific code should be required.

v2: Handle multiple output variables per render target (which may exist
due to ARB_enhanced_layouts), and array variables (even with one
render target, we might have out vec4 color[1]), and non-vec4
variables (it's easier than finding spec text to justify not
handling it).  Thanks to Francisco Jerez for the feedback.

Signed-off-by: Kenneth Graunke 
---
 src/compiler/Makefile.sources  |   1 +
 src/compiler/glsl/ir_optimization.h|   1 +
 .../glsl/lower_blend_equation_advanced.cpp | 557 +
 3 files changed, 559 insertions(+)
 create mode 100644 src/compiler/glsl/lower_blend_equation_advanced.cpp

diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
index 0ff9b23..fe26132 100644
--- a/src/compiler/Makefile.sources
+++ b/src/compiler/Makefile.sources
@@ -77,6 +77,7 @@ LIBGLSL_FILES = \
glsl/loop_analysis.h \
glsl/loop_controls.cpp \
glsl/loop_unroll.cpp \
+   glsl/lower_blend_equation_advanced.cpp \
glsl/lower_buffer_access.cpp \
glsl/lower_buffer_access.h \
glsl/lower_const_arrays_to_uniforms.cpp \
diff --git a/src/compiler/glsl/ir_optimization.h 
b/src/compiler/glsl/ir_optimization.h
index c29260a..3bd6928 100644
--- a/src/compiler/glsl/ir_optimization.h
+++ b/src/compiler/glsl/ir_optimization.h
@@ -151,6 +151,7 @@ void optimize_dead_builtin_variables(exec_list 
*instructions,
 bool lower_tess_level(gl_linked_shader *shader);
 
 bool lower_vertex_id(gl_linked_shader *shader);
+bool lower_blend_equation_advanced(gl_linked_shader *shader);
 
 bool lower_subroutine(exec_list *instructions, struct _mesa_glsl_parse_state 
*state);
 void propagate_invariance(exec_list *instructions);
diff --git a/src/compiler/glsl/lower_blend_equation_advanced.cpp 
b/src/compiler/glsl/lower_blend_equation_advanced.cpp
new file mode 100644
index 000..5632865
--- /dev/null
+++ b/src/compiler/glsl/lower_blend_equation_advanced.cpp
@@ -0,0 +1,557 @@
+/*
+ * Copyright © 2016 Intel Corporation
+ *
+ * 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 "ir.h"
+#include "ir_builder.h"
+#include "ir_optimization.h"
+#include "ir_hierarchical_visitor.h"
+#include "program/prog_instruction.h"
+#include "program/prog_statevars.h"
+#include "util/bitscan.h"
+
+using namespace ir_builder;
+
+#define imm1(x) new(mem_ctx) ir_constant((float) x, 1)
+#define imm3(x) new(mem_ctx) ir_constant((float) x, 3)
+
+static ir_rvalue *
+blend_multiply(ir_variable *src, ir_variable *dst)
+{
+   /* f(Cs,Cd) = Cs*Cd */
+   return mul(src, dst);
+}
+
+static ir_rvalue *
+blend_screen(ir_variable *src, ir_variable *dst)
+{
+   /* f(Cs,Cd) = Cs+Cd-Cs*Cd */
+   return sub(add(src, dst), mul(src, dst));
+}
+
+static ir_rvalue *
+blend_overlay(ir_variable *src, ir_variable *dst)
+{
+   void *mem_ctx = ralloc_parent(src);
+
+   /* f(Cs,Cd) = 2*Cs*Cd, if Cd <= 0.5
+*1-2*(1-Cs)*(1-Cd), otherwise
+*/
+   ir_rvalue *rule_1 = mul(imm3(2), mul(src, dst));
+