From 1e548a84d69dcfba28ca705f2eb9889d0789836b Mon Sep 17 00:00:00 2001 From: Stephan Unverwerth Date: Sat, 17 Sep 2022 17:50:43 +0200 Subject: [PATCH] LibSoftGPU: Define a simple shader instruction set This adds a simple instruction set with basic operations and adds an instruction list to the shader class. --- Userland/Libraries/LibSoftGPU/Device.cpp | 2 +- Userland/Libraries/LibSoftGPU/ISA.h | 63 ++++++++++++++++++++++++ Userland/Libraries/LibSoftGPU/Shader.cpp | 3 +- Userland/Libraries/LibSoftGPU/Shader.h | 9 +++- 4 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 Userland/Libraries/LibSoftGPU/ISA.h diff --git a/Userland/Libraries/LibSoftGPU/Device.cpp b/Userland/Libraries/LibSoftGPU/Device.cpp index e971ad01a71..356b078bbe4 100644 --- a/Userland/Libraries/LibSoftGPU/Device.cpp +++ b/Userland/Libraries/LibSoftGPU/Device.cpp @@ -1638,7 +1638,7 @@ NonnullRefPtr Device::create_image(GPU::PixelFormat const& pixel_for ErrorOr> Device::create_shader(GPU::IR::Shader const&) { - return adopt_ref(*new Shader(this)); + return adopt_ref(*new Shader(this, {})); } void Device::set_sampler_config(unsigned sampler, GPU::SamplerConfig const& config) diff --git a/Userland/Libraries/LibSoftGPU/ISA.h b/Userland/Libraries/LibSoftGPU/ISA.h new file mode 100644 index 00000000000..4f34cfae1d8 --- /dev/null +++ b/Userland/Libraries/LibSoftGPU/ISA.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2022, Stephan Unverwerth + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace SoftGPU { + +constexpr u8 swizzle_pattern(u8 a, u8 b, u8 c, u8 d) +{ + return a | (b << 2) | (c << 4) | (d << 6); +} + +constexpr u8 swizzle_index(u8 pattern, u8 element) +{ + return (pattern & (3 << (element * 2))) >> (element * 2); +} + +enum class Opcode : u8 { + Input, + Output, + Sample2D, + Swizzle, + Add, + Sub, + Mul, + Div, +}; + +struct Instruction final { + union Arguments { + struct { + u16 target_register; + u8 input_index; + } input; + struct { + u16 source_register; + u8 output_index; + } output; + struct { + u16 target_register; + u16 coordinates_register; + u8 sampler_index; + } sample; + struct { + u16 target_register; + u16 source_register; + u8 pattern; + } swizzle; + struct { + u16 target_register; + u16 source_register1; + u16 source_register2; + } binop; + } arguments; + Opcode operation; +}; + +} diff --git a/Userland/Libraries/LibSoftGPU/Shader.cpp b/Userland/Libraries/LibSoftGPU/Shader.cpp index 44bb9635eae..ae68fcfd22c 100644 --- a/Userland/Libraries/LibSoftGPU/Shader.cpp +++ b/Userland/Libraries/LibSoftGPU/Shader.cpp @@ -8,8 +8,9 @@ namespace SoftGPU { -Shader::Shader(void const* ownership_token) +Shader::Shader(void const* ownership_token, Vector const& instructions) : GPU::Shader(ownership_token) + , m_instructions(instructions) { } diff --git a/Userland/Libraries/LibSoftGPU/Shader.h b/Userland/Libraries/LibSoftGPU/Shader.h index 7c6b899d05f..e75c094d464 100644 --- a/Userland/Libraries/LibSoftGPU/Shader.h +++ b/Userland/Libraries/LibSoftGPU/Shader.h @@ -6,13 +6,20 @@ #pragma once +#include #include +#include namespace SoftGPU { class Shader final : public GPU::Shader { public: - Shader(void const* ownership_token); + Shader(void const* ownership_token, Vector const&); + + Vector const& instructions() const { return m_instructions; } + +private: + Vector m_instructions; }; }