From: Gert Wollny Date: Fri, 27 Dec 2019 16:49:27 +0000 (+0100) Subject: r600/sfn: Add compute shader skeleton X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=5aadd809d07f6d7ce4d0cae18a410cc111c12c65;p=mesa.git r600/sfn: Add compute shader skeleton This adds some very basic compute shader support. v2: fix compilation with gcc-6 v3: rebase: correct barrier intrinstic Signed-off-by: Gert Wollny Part-of: --- diff --git a/src/gallium/drivers/r600/Makefile.sources b/src/gallium/drivers/r600/Makefile.sources index f16bce16113..f5cf78110a4 100644 --- a/src/gallium/drivers/r600/Makefile.sources +++ b/src/gallium/drivers/r600/Makefile.sources @@ -129,6 +129,8 @@ CXX_SOURCES = \ sfn/sfn_nir_vectorize_vs_inputs.c \ sfn/sfn_shader_base.cpp \ sfn/sfn_shader_base.h \ + sfn/sfn_shader_compute.cpp \ + sfn/sfn_shader_compute.h \ sfn/sfn_shader_fragment.cpp \ sfn/sfn_shader_fragment.h \ sfn/sfn_shader_geometry.cpp \ diff --git a/src/gallium/drivers/r600/meson.build b/src/gallium/drivers/r600/meson.build index 3a4cf25a098..3f85127d9cc 100644 --- a/src/gallium/drivers/r600/meson.build +++ b/src/gallium/drivers/r600/meson.build @@ -146,6 +146,8 @@ files_r600 = files( 'sfn/sfn_nir_vectorize_vs_inputs.c', 'sfn/sfn_shader_base.cpp', 'sfn/sfn_shader_base.h', + 'sfn/sfn_shader_compute.cpp', + 'sfn/sfn_shader_compute.h', 'sfn/sfn_shader_fragment.cpp', 'sfn/sfn_shader_fragment.h', 'sfn/sfn_shader_geometry.cpp', diff --git a/src/gallium/drivers/r600/sfn/sfn_ir_to_assembly.cpp b/src/gallium/drivers/r600/sfn/sfn_ir_to_assembly.cpp index a26054397d8..4909ad498d8 100644 --- a/src/gallium/drivers/r600/sfn/sfn_ir_to_assembly.cpp +++ b/src/gallium/drivers/r600/sfn/sfn_ir_to_assembly.cpp @@ -69,6 +69,8 @@ private: bool copy_dst(r600_bytecode_alu_dst& dst, const Value& src); bool copy_src(r600_bytecode_alu_src& src, const Value& s); + + ConditionalJumpTracker m_jump_tracker; CallStack m_callstack; diff --git a/src/gallium/drivers/r600/sfn/sfn_nir.cpp b/src/gallium/drivers/r600/sfn/sfn_nir.cpp index 845bb6c3b36..206435528a6 100644 --- a/src/gallium/drivers/r600/sfn/sfn_nir.cpp +++ b/src/gallium/drivers/r600/sfn/sfn_nir.cpp @@ -35,6 +35,7 @@ #include "sfn_shader_vertex.h" #include "sfn_shader_fragment.h" #include "sfn_shader_geometry.h" +#include "sfn_shader_compute.h" #include "sfn_nir_lower_fs_out_to_vector.h" #include "sfn_ir_to_assembly.h" @@ -78,6 +79,10 @@ bool ShaderFromNir::lower(const nir_shader *shader, r600_pipe_shader *pipe_shade sfn_log << SfnLog::trans << "Start FS\n"; impl.reset(new FragmentShaderFromNir(*shader, pipe_shader->shader, *sel, key)); break; + case MESA_SHADER_COMPUTE: + sfn_log << SfnLog::trans << "Start CS\n"; + impl.reset(new ComputeShaderFromNir(pipe_shader, *sel, key)); + break; default: return false; } diff --git a/src/gallium/drivers/r600/sfn/sfn_shader_base.cpp b/src/gallium/drivers/r600/sfn/sfn_shader_base.cpp index 6da9a8035c2..96c8e804e2b 100644 --- a/src/gallium/drivers/r600/sfn/sfn_shader_base.cpp +++ b/src/gallium/drivers/r600/sfn/sfn_shader_base.cpp @@ -28,6 +28,7 @@ #include "../r600_shader.h" #include "sfn_shader_vertex.h" +#include "sfn_shader_compute.h" #include "sfn_shader_fragment.h" #include "sfn_shader_geometry.h" #include "sfn_liverange.h" diff --git a/src/gallium/drivers/r600/sfn/sfn_shader_compute.cpp b/src/gallium/drivers/r600/sfn/sfn_shader_compute.cpp new file mode 100644 index 00000000000..6f43533ad1f --- /dev/null +++ b/src/gallium/drivers/r600/sfn/sfn_shader_compute.cpp @@ -0,0 +1,145 @@ +/* -*- mesa-c++ -*- + * + * Copyright (c) 2018 Collabora LTD + * + * Author: Gert Wollny + * + * 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 + * on the rights to use, copy, modify, merge, publish, distribute, sub + * license, 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 NON-INFRINGEMENT. IN NO EVENT SHALL + * THE AUTHOR(S) AND/OR THEIR SUPPLIERS 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 "sfn_shader_compute.h" +#include "sfn_instruction_fetch.h" + +namespace r600 { + +ComputeShaderFromNir::ComputeShaderFromNir(r600_pipe_shader *sh, + r600_pipe_shader_selector& sel, + UNUSED const r600_shader_key& key): + ShaderFromNirProcessor (PIPE_SHADER_COMPUTE, sel, sh->shader, + sh->scratch_space_needed), + m_reserved_registers(0) +{ +} + +bool ComputeShaderFromNir::scan_sysvalue_access(UNUSED nir_instr *instr) +{ + return true; +} +bool ComputeShaderFromNir::allocate_reserved_registers() +{ + int thread_id_sel = m_reserved_registers++; + int wg_id_sel = m_reserved_registers++; + + for (int i = 0; i < 3; ++i) { + auto tmp = new GPRValue(thread_id_sel, i); + tmp->set_as_input(); + m_local_invocation_id[i] = PValue(tmp); + inject_register(tmp->sel(), i, m_local_invocation_id[i], false); + + tmp = new GPRValue(wg_id_sel, i); + tmp->set_as_input(); + m_workgroup_id[i] = PValue(tmp); + inject_register(tmp->sel(), i, m_workgroup_id[i], false); + } + return true; +} + +bool ComputeShaderFromNir::emit_intrinsic_instruction_override(nir_intrinsic_instr* instr) +{ + switch (instr->intrinsic) { + case nir_intrinsic_load_local_invocation_id: + return emit_load_3vec(instr, m_local_invocation_id); + case nir_intrinsic_load_work_group_id: + return emit_load_3vec(instr, m_workgroup_id); + case nir_intrinsic_load_num_work_groups: + return emit_load_num_work_groups(instr); + case nir_intrinsic_control_barrier: + return emit_barrier(instr); + default: + return false; + } +} + +bool ComputeShaderFromNir::emit_load_3vec(nir_intrinsic_instr* instr, + const std::array& src) +{ + AluInstruction *ir = nullptr; + for (int i = 0; i < 3; ++i) { + ir = new AluInstruction(op1_mov, from_nir(instr->dest, i), src[i], {alu_write}); + emit_instruction(ir); + } + ir->set_flag(alu_last_instr); + return true; +} + +bool ComputeShaderFromNir::emit_barrier(UNUSED nir_intrinsic_instr* instr) +{ + AluInstruction *ir = new AluInstruction(op0_group_barrier); + ir->set_flag(alu_last_instr); + emit_instruction(ir); + return true; +} + +bool ComputeShaderFromNir::emit_load_num_work_groups(nir_intrinsic_instr* instr) +{ + int temp = allocate_temp_register(); + PValue a_zero(new GPRValue(temp, 1)); + emit_instruction(new AluInstruction(op1_mov, a_zero, Value::zero, EmitInstruction::last_write)); + GPRVector dest; + for (int i = 0; i < 3; ++i) + dest.set_reg_i(i, from_nir(instr->dest, i)); + dest.set_reg_i(3, from_nir(instr->dest, 7)); + + auto ir = new FetchInstruction(vc_fetch, no_index_offset, + fmt_32_32_32_32, vtx_nf_int, vtx_es_none, a_zero, dest, 16, + false, 16, R600_BUFFER_INFO_CONST_BUFFER, 0, + bim_none, false, false, 0, 0, 0, PValue(), {0,1,2,7}); + ir->set_flag(vtx_srf_mode); + emit_instruction(ir); + return true; +} + +bool ComputeShaderFromNir::do_process_inputs(UNUSED nir_variable *input) +{ + return true; +} + +bool ComputeShaderFromNir::do_process_outputs(UNUSED nir_variable *output) +{ + return true; +} + +bool ComputeShaderFromNir::do_emit_load_deref(UNUSED const nir_variable *in_var, + UNUSED nir_intrinsic_instr* instr) +{ + return true; +} + +bool ComputeShaderFromNir::do_emit_store_deref(UNUSED const nir_variable *out_var, + UNUSED nir_intrinsic_instr* instr) +{ + return true; +} +void ComputeShaderFromNir::do_finalize() +{ + +} + +} diff --git a/src/gallium/drivers/r600/sfn/sfn_shader_compute.h b/src/gallium/drivers/r600/sfn/sfn_shader_compute.h new file mode 100644 index 00000000000..db367760d2d --- /dev/null +++ b/src/gallium/drivers/r600/sfn/sfn_shader_compute.h @@ -0,0 +1,66 @@ +/* -*- mesa-c++ -*- + * + * Copyright (c) 2019 Collabora LTD + * + * Author: Gert Wollny + * + * 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 + * on the rights to use, copy, modify, merge, publish, distribute, sub + * license, 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 NON-INFRINGEMENT. IN NO EVENT SHALL + * THE AUTHOR(S) AND/OR THEIR SUPPLIERS 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. + */ + +#ifndef SFN_COMPUTE_SHADER_FROM_NIR_H +#define SFN_COMPUTE_SHADER_FROM_NIR_H + +#include "sfn_shader_base.h" +#include "sfn_shaderio.h" +#include + +namespace r600 { + +class ComputeShaderFromNir : public ShaderFromNirProcessor +{ +public: + ComputeShaderFromNir(r600_pipe_shader *sh, + r600_pipe_shader_selector& sel, + const r600_shader_key &key); + + bool scan_sysvalue_access(nir_instr *instr) override; + +private: + bool emit_intrinsic_instruction_override(nir_intrinsic_instr* instr) override; + + bool allocate_reserved_registers() override; + bool do_process_inputs(nir_variable *input) override; + bool do_process_outputs(nir_variable *output) override; + bool do_emit_load_deref(const nir_variable *in_var, nir_intrinsic_instr* instr) override; + bool do_emit_store_deref(const nir_variable *out_var, nir_intrinsic_instr* instr) override; + void do_finalize() override; + + bool emit_load_3vec(nir_intrinsic_instr* instr, const std::array& src); + bool emit_load_num_work_groups(nir_intrinsic_instr* instr); + bool emit_barrier(nir_intrinsic_instr* instr); + + int m_reserved_registers; + std::array m_workgroup_id; + std::array m_local_invocation_id; +}; + +} + +#endif // SFN_COMPUTE_SHADER_FROM_NIR_H