summaryrefslogtreecommitdiffstats
path: root/src/glsl/nir
diff options
context:
space:
mode:
authorJason Ekstrand <[email protected]>2015-12-29 09:56:44 -0800
committerJason Ekstrand <[email protected]>2015-12-29 13:44:05 -0800
commit0119773ffca586c9e51fa19248c3dfaab0500b25 (patch)
tree8a2fc7dd315a956146e69c41115cf7e17a5b1e2d /src/glsl/nir
parent55ca5b0e74dff2b2f7df57af332b73e2a1b7d081 (diff)
nir/builder: Add an init function that creates a simple shader for you
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 <[email protected]> Reviewed-by: Connor Abbott <[email protected]> Acked-by: Kenneth Graunke <[email protected]>
Diffstat (limited to 'src/glsl/nir')
-rw-r--r--src/glsl/nir/nir_builder.h11
-rw-r--r--src/glsl/nir/tests/control_flow_tests.cpp26
2 files changed, 21 insertions, 16 deletions
diff --git a/src/glsl/nir/nir_builder.h b/src/glsl/nir/nir_builder.h
index ee6131a089f..cfaaf8e03e3 100644
--- a/src/glsl/nir/nir_builder.h
+++ b/src/glsl/nir/nir_builder.h
@@ -44,6 +44,17 @@ nir_builder_init(nir_builder *build, nir_function_impl *impl)
}
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)
{
nir_instr_insert(build->cursor, instr);
diff --git a/src/glsl/nir/tests/control_flow_tests.cpp b/src/glsl/nir/tests/control_flow_tests.cpp
index f142e443400..b9379ef3b06 100644
--- a/src/glsl/nir/tests/control_flow_tests.cpp
+++ b/src/glsl/nir/tests/control_flow_tests.cpp
@@ -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);
}