nir/builder: Add an init function that creates a simple shader for you
authorJason Ekstrand <jason.ekstrand@intel.com>
Tue, 29 Dec 2015 17:56:44 +0000 (09:56 -0800)
committerJason Ekstrand <jason.ekstrand@intel.com>
Tue, 29 Dec 2015 21:44:05 +0000 (13:44 -0800)
A hugely common case when using nir_builder is to have a shader with a
single function called main.  This adds a helper that gives you just that.
This commit also makes us use it in the NIR control-flow unit tests as well
as tgsi_to_nir and prog_to_nir.

Reviewed-by: Eric Anholt <eric@anholt.net>
Reviewed-by: Connor Abbott <cwabbott0@gmail.com>
Acked-by: Kenneth Graunke <kenneth@whitecape.org>
src/gallium/auxiliary/nir/tgsi_to_nir.c
src/glsl/nir/nir_builder.h
src/glsl/nir/tests/control_flow_tests.cpp
src/mesa/program/prog_to_nir.c

index 01426e86e61dead306dff60cd8c6b8c99ce0e3ba..94d992b00315ba6c3cdcf499ff6fab47dc43fbc9 100644 (file)
@@ -1968,14 +1968,10 @@ tgsi_to_nir(const void *tgsi_tokens,
    tgsi_scan_shader(tgsi_tokens, &scan);
    c->scan = &scan;
 
-   s = nir_shader_create(NULL, tgsi_processor_to_shader_stage(scan.processor),
-                         options);
-
-   nir_function *func = nir_function_create(s, "main");
-   nir_function_impl *impl = nir_function_impl_create(func);
-
-   nir_builder_init(&c->build, impl);
-   c->build.cursor = nir_after_cf_list(&impl->body);
+   nir_builder_init_simple_shader(&c->build, NULL,
+                                  tgsi_processor_to_shader_stage(scan.processor),
+                                  options);
+   s = c->build.shader;
 
    s->num_inputs = scan.file_max[TGSI_FILE_INPUT] + 1;
    s->num_uniforms = scan.const_file_max[0] + 1;
index ee6131a089f5d6fc96a0de57b4b6ffbba2c5543e..cfaaf8e03e3dd756ede7f7cabaf0559f9987a283 100644 (file)
@@ -43,6 +43,17 @@ nir_builder_init(nir_builder *build, nir_function_impl *impl)
    build->shader = impl->function->shader;
 }
 
+static inline void
+nir_builder_init_simple_shader(nir_builder *build, void *mem_ctx,
+                               gl_shader_stage stage,
+                               const nir_shader_compiler_options *options)
+{
+   build->shader = nir_shader_create(mem_ctx, stage, options);
+   nir_function *func = nir_function_create(build->shader, "main");
+   build->impl = nir_function_impl_create(func);
+   build->cursor = nir_after_cf_list(&build->impl->body);
+}
+
 static inline void
 nir_builder_instr_insert(nir_builder *build, nir_instr *instr)
 {
index f142e4434001e6afe6e02f216820ce4108566266..b9379ef3b062b4d201bd841adffe4ff5917a2e4d 100644 (file)
@@ -30,23 +30,17 @@ protected:
    ~nir_cf_test();
 
    nir_builder b;
-   nir_shader *shader;
-   nir_function_impl *impl;
 };
 
 nir_cf_test::nir_cf_test()
 {
    static const nir_shader_compiler_options options = { };
-   shader = nir_shader_create(NULL, MESA_SHADER_VERTEX, &options);
-   nir_function *func = nir_function_create(shader, "main");
-   impl = nir_function_impl_create(func);
-
-   nir_builder_init(&b, impl);
+   nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_VERTEX, &options);
 }
 
 nir_cf_test::~nir_cf_test()
 {
-   ralloc_free(shader);
+   ralloc_free(b.shader);
 }
 
 TEST_F(nir_cf_test, delete_break_in_loop)
@@ -55,12 +49,12 @@ TEST_F(nir_cf_test, delete_break_in_loop)
     *
     * while (...) { break; }
     */
-   nir_loop *loop = nir_loop_create(shader);
-   nir_cf_node_insert(nir_after_cf_list(&impl->body), &loop->cf_node);
+   nir_loop *loop = nir_loop_create(b.shader);
+   nir_cf_node_insert(nir_after_cf_list(&b.impl->body), &loop->cf_node);
 
    b.cursor = nir_after_cf_list(&loop->body);
 
-   nir_jump_instr *jump = nir_jump_instr_create(shader, nir_jump_break);
+   nir_jump_instr *jump = nir_jump_instr_create(b.shader, nir_jump_break);
    nir_builder_instr_insert(&b, &jump->instr);
 
    /* At this point, we should have:
@@ -81,10 +75,10 @@ TEST_F(nir_cf_test, delete_break_in_loop)
     *         block block_3:
     * }
     */
-   nir_block *block_0 = nir_start_block(impl);
+   nir_block *block_0 = nir_start_block(b.impl);
    nir_block *block_1 = nir_cf_node_as_block(nir_loop_first_cf_node(loop));
    nir_block *block_2 = nir_cf_node_as_block(nir_cf_node_next(&loop->cf_node));
-   nir_block *block_3 = impl->end_block;
+   nir_block *block_3 = b.impl->end_block;
    ASSERT_EQ(nir_cf_node_block, block_0->cf_node.type);
    ASSERT_EQ(nir_cf_node_block, block_1->cf_node.type);
    ASSERT_EQ(nir_cf_node_block, block_2->cf_node.type);
@@ -107,12 +101,12 @@ TEST_F(nir_cf_test, delete_break_in_loop)
    EXPECT_TRUE(_mesa_set_search(block_2->predecessors, block_1));
    EXPECT_TRUE(_mesa_set_search(block_3->predecessors, block_2));
 
-   nir_print_shader(shader, stderr);
+   nir_print_shader(b.shader, stderr);
 
    /* Now remove the break. */
    nir_instr_remove(&jump->instr);
 
-   nir_print_shader(shader, stderr);
+   nir_print_shader(b.shader, stderr);
 
    /* At this point, we should have:
     *
@@ -150,5 +144,5 @@ TEST_F(nir_cf_test, delete_break_in_loop)
    EXPECT_TRUE(_mesa_set_search(block_2->predecessors, block_1));
    EXPECT_TRUE(_mesa_set_search(block_3->predecessors, block_2));
 
-   nir_metadata_require(impl, nir_metadata_dominance);
+   nir_metadata_require(b.impl, nir_metadata_dominance);
 }
index c5d4f273fc82b7ac31cea3e58e1c9d03d1627dc1..ce6f6997d2f07c75c0dfa5a8deb49563293f0285 100644 (file)
@@ -1082,11 +1082,11 @@ prog_to_nir(const struct gl_program *prog,
    c = rzalloc(NULL, struct ptn_compile);
    if (!c)
       return NULL;
-   s = nir_shader_create(NULL, stage, options);
-   if (!s)
-      goto fail;
    c->prog = prog;
 
+   nir_builder_init_simple_shader(&c->build, NULL, stage, options);
+   s = c->build.shader;
+
    if (prog->Parameters->NumParameters > 0) {
       c->parameters = rzalloc(s, nir_variable);
       c->parameters->type =
@@ -1097,13 +1097,6 @@ prog_to_nir(const struct gl_program *prog,
       exec_list_push_tail(&s->uniforms, &c->parameters->node);
    }
 
-   nir_function *func = nir_function_create(s, "main");
-   nir_function_impl *impl = nir_function_impl_create(func);
-
-   c->build.shader = s;
-   c->build.impl = impl;
-   c->build.cursor = nir_after_cf_list(&impl->body);
-
    setup_registers_and_variables(c);
    if (unlikely(c->error))
       goto fail;