[gtk+/wip/otte/shader: 169/175] gskspv: Reorganize code more



commit 8857772203d132c22aaa72ea308db8a18f5da73a
Author: Benjamin Otte <otte redhat com>
Date:   Sat Oct 7 22:38:25 2017 +0200

    gskspv: Reorganize code more
    
    Now we create IDs in the same way glslang does and we write out code in
    the same order - at least for "void main() { }"

 gsk/gskslfunction.c       |   12 +++--
 gsk/gskslprogram.c        |   21 +++-----
 gsk/gskspvwriter.c        |  122 +++++++++++++++++++++++++++++----------------
 gsk/gskspvwriterprivate.h |    9 ++--
 4 files changed, 98 insertions(+), 66 deletions(-)
---
diff --git a/gsk/gskslfunction.c b/gsk/gskslfunction.c
index e4ba840..3c2e224 100644
--- a/gsk/gskslfunction.c
+++ b/gsk/gskslfunction.c
@@ -408,9 +408,6 @@ gsk_sl_function_declared_write_spv (const GskSlFunction *function,
     }
   function_type_id = gsk_spv_writer_type_function (writer, return_type_id, argument_types, 
declared->n_arguments);
 
-  /* add debug info */
-  /* FIXME */
-
   function_id = gsk_spv_writer_function (writer, declared->return_type, 0, function_type_id);
   /* add function header */
   for (i = 0; i < declared->n_arguments; i++)
@@ -418,7 +415,12 @@ gsk_sl_function_declared_write_spv (const GskSlFunction *function,
       gsk_spv_writer_get_id_for_variable (writer, declared->arguments[i]);
     }
 
+  /* add debug info */
+  gsk_spv_writer_name (writer, function_id, declared->name);
+
   /* add function body */
+  gsk_spv_writer_push_new_code_block (writer);
+
   if (initializer)
     initializer (writer, initializer_data);
 
@@ -430,6 +432,8 @@ gsk_sl_function_declared_write_spv (const GskSlFunction *function,
 
   gsk_spv_writer_function_end (writer);
 
+  gsk_spv_writer_commit_code_block (writer);
+
   return function_id;
 }
 
@@ -447,8 +451,6 @@ gsk_sl_function_declared_write_call_spv (GskSlFunction *function,
                                          arguments,
                                          declared->n_arguments);
 
-  gsk_spv_writer_name (writer, result, declared->name);
-
   return result;
 }
 
diff --git a/gsk/gskslprogram.c b/gsk/gskslprogram.c
index 1885a82..6027d84 100644
--- a/gsk/gskslprogram.c
+++ b/gsk/gskslprogram.c
@@ -266,23 +266,18 @@ gsk_sl_program_write_spv_initializer (GskSpvWriter *writer,
     gsk_spv_writer_get_id_for_variable (writer, l->data);
 }
 
-static void
-gsk_sl_program_write_spv (GskSlProgram *program,
-                          GskSpvWriter *writer)
+static GskSlFunction *
+gsk_sl_program_get_entry_point (GskSlProgram *program)
 {
   GSList *l;
 
   for (l = program->functions; l; l = l->next)
     {
       if (g_str_equal (gsk_sl_function_get_name (l->data), "main"))
-        {
-          gsk_spv_writer_write_function (writer,
-                                         l->data,
-                                         gsk_sl_program_write_spv_initializer,
-                                         program);
-          gsk_spv_writer_set_entry_point (writer, l->data);
-        }
+        return l->data;
     }
+
+  return NULL;
 }
 
 GBytes *
@@ -295,8 +290,10 @@ gsk_sl_program_to_spirv (GskSlProgram *program)
 
   writer = gsk_spv_writer_new ();
 
-  gsk_sl_program_write_spv (program, writer);
-  bytes = gsk_spv_writer_write (writer);
+  bytes = gsk_spv_writer_write (writer,
+                                gsk_sl_program_get_entry_point (program),
+                                gsk_sl_program_write_spv_initializer,
+                                program);
 
   gsk_spv_writer_unref (writer);
 
diff --git a/gsk/gskspvwriter.c b/gsk/gskspvwriter.c
index e949e78..b869fec 100644
--- a/gsk/gskspvwriter.c
+++ b/gsk/gskspvwriter.c
@@ -51,6 +51,21 @@ struct _GskSpvWriter
   GHashTable *functions;
 };
 
+static GskSpvCodeBlock *
+gsk_spv_code_block_new (void)
+{
+  GskSpvCodeBlock *block;
+  guint i;
+
+  block = g_slice_new0 (GskSpvCodeBlock);
+  for (i = 0; i < GSK_SPV_WRITER_N_BLOCK_SECTIONS; i++)
+    {
+      block->code[i] = g_array_new (FALSE, TRUE, sizeof (guint32));
+    }
+
+  return block;
+}
+
 static void
 gsk_spv_code_block_free (GskSpvCodeBlock *block)
 {
@@ -86,8 +101,6 @@ gsk_spv_writer_new (void)
                                              (GDestroyNotify) gsk_sl_variable_unref, NULL);
   writer->functions = g_hash_table_new_full (g_direct_hash, g_direct_equal,
                                              (GDestroyNotify) gsk_sl_function_unref, NULL);
-  /* the ID 1 is reserved for the GLSL instruction set (for now) */
-  writer->last_id = 1;
 
   return writer;
 }
@@ -130,50 +143,96 @@ gsk_spv_writer_unref (GskSpvWriter *writer)
   g_slice_free (GskSpvWriter, writer);
 }
 
-#define STRING(s, offset) ((guint32) ((s)[offset + 0] | ((s)[offset + 1] << 8) | ((s)[offset + 2] << 16) | 
((s)[offset + 3] << 24)))
+static guint32
+gsk_spv_writer_write_function (GskSpvWriter     *writer,
+                               GskSlFunction    *function,
+                               GskSpvWriterFunc  initializer,
+                               gpointer          initializer_data)
+{
+  GskSpvCodeBlock *block;
+  guint32 result;
+
+  g_assert (g_hash_table_lookup (writer->functions, function) == NULL);
+
+  block = gsk_spv_code_block_new ();
+  gsk_spv_writer_push_code_block (writer, block);
+  result = gsk_sl_function_write_spv (function, writer, initializer, initializer_data);
+  g_hash_table_insert (writer->functions, gsk_sl_function_ref (function), GUINT_TO_POINTER (result));
+  if (block != gsk_spv_writer_pop_code_block (writer))
+    {
+      g_assert_not_reached ();
+    }
+  writer->pending_blocks = g_slist_prepend (writer->pending_blocks, block);
+
+  return result;
+}
+
 static void
-gsk_spv_writer_write_header (GskSpvWriter *writer)
+gsk_spv_writer_do_write (GskSpvWriter     *writer,
+                         GskSlFunction    *entry_point,
+                         GskSpvWriterFunc  initializer,
+                         gpointer          initializer_data)
 {
-  guint32 entry_point;
+  guint32 entry_point_id;
 
   gsk_spv_writer_capability (writer, GSK_SPV_CAPABILITY_SHADER);
   gsk_spv_writer_ext_inst_import (writer,
                                   "GLSL.std.450");
+  gsk_spv_writer_source (writer,
+                         GSK_SPV_SOURCE_LANGUAGE_GLSL,
+                         440,
+                         0,
+                         NULL);
   gsk_spv_writer_memory_model (writer,
                                GSK_SPV_ADDRESSING_MODEL_LOGICAL,
                                GSK_SPV_MEMORY_MODEL_GLSL450);
-  entry_point = gsk_spv_writer_get_id_for_function (writer, writer->entry_point);
+
+  entry_point_id = gsk_spv_writer_write_function (writer, entry_point, initializer, initializer_data);
+
   gsk_spv_writer_entry_point (writer,
                               GSK_SPV_EXECUTION_MODEL_FRAGMENT,
-                              entry_point,
+                              entry_point_id,
                               "main",
                               NULL,
                               0);
   gsk_spv_writer_execution_mode (writer,
-                                 entry_point,
+                                 entry_point_id,
                                  GSK_SPV_EXECUTION_MODE_ORIGIN_UPPER_LEFT);
-  gsk_spv_writer_source (writer,
-                         GSK_SPV_SOURCE_LANGUAGE_GLSL,
-                         440,
-                         0,
-                         NULL);
 }
 
 static void
-gsk_spv_writer_clear_header (GskSpvWriter *writer)
+gsk_spv_writer_clear (GskSpvWriter *writer)
 {
-  g_array_set_size (writer->code[GSK_SPV_WRITER_SECTION_HEADER], 0);
+  guint i;
+
+  g_slist_free_full (writer->pending_blocks, (GDestroyNotify) gsk_spv_code_block_free);
+  writer->pending_blocks = NULL;
+
+  for (i = 0; i < GSK_SPV_WRITER_N_GLOBAL_SECTIONS; i++)
+    {
+      g_array_set_size (writer->code[i], 0);
+    }
+
+  g_hash_table_remove_all (writer->pointer_types);
+  g_hash_table_remove_all (writer->types);
+  g_hash_table_remove_all (writer->values);
+  g_hash_table_remove_all (writer->variables);
+  g_hash_table_remove_all (writer->functions);
+
 }
 
 GBytes *
-gsk_spv_writer_write (GskSpvWriter *writer)
+gsk_spv_writer_write (GskSpvWriter     *writer,
+                      GskSlFunction    *entry_point,
+                      GskSpvWriterFunc  initializer,
+                      gpointer          initializer_data)
 {
   GArray *array;
   gsize size;
   GSList *l;
   guint i;
 
-  gsk_spv_writer_write_header (writer);
+  gsk_spv_writer_do_write (writer, entry_point, initializer, initializer_data);
 
   array = g_array_new (FALSE, FALSE, sizeof (guint32));
 
@@ -198,7 +257,7 @@ gsk_spv_writer_write (GskSpvWriter *writer)
         }
     }
 
-  gsk_spv_writer_clear_header (writer);
+  gsk_spv_writer_clear (writer);
 
   size = array->len * sizeof (guint32);
   return g_bytes_new_take (g_array_free (array, FALSE), size);
@@ -335,26 +394,6 @@ gsk_spv_writer_get_id_for_function (GskSpvWriter  *writer,
 }
 
 guint32
-gsk_spv_writer_write_function (GskSpvWriter     *writer,
-                               GskSlFunction    *function,
-                               GskSpvWriterFunc  initializer,
-                               gpointer          initializer_data)
-{
-  GskSpvCodeBlock *block;
-  guint32 result;
-
-  g_assert (g_hash_table_lookup (writer->functions, function) == NULL);
-
-  gsk_spv_writer_push_new_code_block (writer);
-  result = gsk_sl_function_write_spv (function, writer, initializer, initializer_data);
-  g_hash_table_insert (writer->functions, gsk_sl_function_ref (function), GUINT_TO_POINTER (result));
-  block = gsk_spv_writer_pop_code_block (writer);
-  writer->pending_blocks = g_slist_prepend (writer->pending_blocks, block);
-
-  return result;
-}
-
-guint32
 gsk_spv_writer_make_id (GskSpvWriter *writer)
 {
   writer->last_id++;
@@ -383,13 +422,8 @@ guint32
 gsk_spv_writer_push_new_code_block (GskSpvWriter *writer)
 {
   GskSpvCodeBlock *block;
-  guint i;
 
-  block = g_slice_new0 (GskSpvCodeBlock);
-  for (i = 0; i < GSK_SPV_WRITER_N_BLOCK_SECTIONS; i++)
-    {
-      block->code[i] = g_array_new (FALSE, TRUE, sizeof (guint32));
-    }
+  block = gsk_spv_code_block_new ();
 
   gsk_spv_writer_push_code_block (writer, block);
 
diff --git a/gsk/gskspvwriterprivate.h b/gsk/gskspvwriterprivate.h
index f411472..28b7743 100644
--- a/gsk/gskspvwriterprivate.h
+++ b/gsk/gskspvwriterprivate.h
@@ -53,7 +53,10 @@ GskSpvWriter *          gsk_spv_writer_new                      (void);
 GskSpvWriter *          gsk_spv_writer_ref                      (GskSpvWriter           *writer);
 void                    gsk_spv_writer_unref                    (GskSpvWriter           *writer);
 
-GBytes *                gsk_spv_writer_write                    (GskSpvWriter           *writer);
+GBytes *                gsk_spv_writer_write                    (GskSpvWriter           *writer,
+                                                                 GskSlFunction          *entry_point,
+                                                                 GskSpvWriterFunc        initializer,
+                                                                 gpointer                initializer_data);
 void                    gsk_spv_writer_set_entry_point          (GskSpvWriter           *writer,
                                                                  GskSlFunction          *function);
 
@@ -71,10 +74,6 @@ guint32                 gsk_spv_writer_get_id_for_variable      (GskSpvWriter
                                                                  GskSlVariable          *variable);
 guint32                 gsk_spv_writer_get_id_for_function      (GskSpvWriter           *writer,
                                                                  GskSlFunction          *function);
-guint32                 gsk_spv_writer_write_function           (GskSpvWriter           *writer,
-                                                                 GskSlFunction          *function,
-                                                                 GskSpvWriterFunc        initializer,
-                                                                 gpointer                intializer_data);
 
 guint32                 gsk_spv_writer_make_id                  (GskSpvWriter           *writer);
 GArray *                gsk_spv_writer_get_bytes                (GskSpvWriter           *writer,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]