panfrost/midgard: Use Gallium framebuffer formats
authorAlyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Tue, 2 Jul 2019 13:28:47 +0000 (06:28 -0700)
committerAlyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Wed, 10 Jul 2019 13:12:05 +0000 (06:12 -0700)
Ideally, we would keep Galliumisms far away from the compiler;
unfortunately, Mesa hasn't standardized on system of format codes to be
shared across APIs and across drivers, so using Gallium formats is our
best bet in the short run.

Signed-off-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
src/gallium/drivers/panfrost/midgard/nir_lower_framebuffer.c

index 2986c3c3393004dab61b3b9c6ce7fad6f36adba5..f5182ca73945735fee5263eaff388de2338b5832 100644 (file)
 #include "compiler/nir/nir.h"
 #include "compiler/nir/nir_builder.h"
 #include "nir_lower_blend.h"
+#include "util/u_format.h"
 
 static nir_ssa_def *
-nir_float_to_native(nir_builder *b, nir_ssa_def *c_float)
+nir_float_to_unorm8(nir_builder *b, nir_ssa_def *c_float)
 {
    /* First, we degrade quality to fp16; we don't need the extra bits */
    nir_ssa_def *degraded = nir_f2f16(b, c_float);
@@ -58,7 +59,7 @@ nir_float_to_native(nir_builder *b, nir_ssa_def *c_float)
 }
 
 static nir_ssa_def *
-nir_native_to_float(nir_builder *b, nir_ssa_def *c_native)
+nir_unorm8_to_float(nir_builder *b, nir_ssa_def *c_native)
 {
    /* First, we convert up from u8 to f16 */
    nir_ssa_def *converted = nir_u2f16(b, nir_u2u16(b, c_native));
@@ -69,12 +70,44 @@ nir_native_to_float(nir_builder *b, nir_ssa_def *c_native)
    return scaled;
 }
 
+
+
+static nir_ssa_def *
+nir_float_to_native(nir_builder *b,
+      nir_ssa_def *c_float,
+      const struct util_format_description *desc)
+{
+   if (util_format_is_unorm8(desc))
+      return nir_float_to_unorm8(b, c_float);
+   else {
+      printf("%s\n", desc->name);
+      unreachable("Unknown format name");
+   }
+}
+
+static nir_ssa_def *
+nir_native_to_float(nir_builder *b,
+   nir_ssa_def *c_native,
+   const struct util_format_description *desc)
+{
+   if (util_format_is_unorm8(desc))
+      return nir_unorm8_to_float(b, c_native);
+   else {
+      printf("%s\n", desc->name);
+      unreachable("Unknown format name");
+   }
+}
+
 void
 nir_lower_framebuffer(nir_shader *shader)
 {
    /* Blend shaders are represented as special fragment shaders */
    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
 
+   enum pipe_format format = PIPE_FORMAT_R8G8B8A8_UNORM;
+   const struct util_format_description *format_desc =
+      util_format_description(format);
+
    nir_foreach_function(func, shader) {
       nir_foreach_block(block, func->impl) {
          nir_foreach_instr_safe(instr, block) {
@@ -106,7 +139,7 @@ nir_lower_framebuffer(nir_shader *shader)
                nir_ssa_def *c_nir = nir_ssa_for_src(&b, intr->src[1], 4);
 
                /* Format convert */
-               nir_ssa_def *converted = nir_float_to_native(&b, c_nir);
+               nir_ssa_def *converted = nir_float_to_native(&b, c_nir, format_desc);
 
                /* Rewrite to use a native store by creating a new intrinsic */
                nir_intrinsic_instr *new =
@@ -137,7 +170,7 @@ nir_lower_framebuffer(nir_shader *shader)
 
                /* Convert the raw value */
                nir_ssa_def *raw = &new->dest.ssa;
-               nir_ssa_def *converted = nir_native_to_float(&b, raw);
+               nir_ssa_def *converted = nir_native_to_float(&b, raw, format_desc);
 
                /* Rewrite to use the converted value */
                nir_src rewritten = nir_src_for_ssa(converted);