LibGL+LibSoftGPU: Add GPU side shader infrastructure

This adds a shader class to LibSoftGPU and makes use of it when linking
GLSL program in LibGL. Also adds actual rendering code to the shader
tests.
This commit is contained in:
Stephan Unverwerth 2022-09-14 23:48:10 +02:00 committed by Andrew Kaster
parent 4ad41e6680
commit 93ab2db80f
11 changed files with 96 additions and 3 deletions

View file

@ -59,6 +59,18 @@ TEST_CASE(0001_program_creation)
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
glUseProgram(program);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex2i(-1, -1);
glColor3f(0, 1, 0);
glVertex2i(1, -1);
glColor3f(0, 0, 1);
glVertex2i(1, 1);
glEnd();
context->present();
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);

View file

@ -157,7 +157,7 @@ void GLContext::gl_link_program(GLuint program)
// FIXME: implement check "GL_INVALID_OPERATION is generated if program is the currently active program object and transform feedback mode is active."
// NOTE: We are ignoring the link result since this is tracked inside the program object
(void)program_it->value->link();
(void)program_it->value->link(*m_rasterizer);
}
void GLContext::gl_use_program(GLuint program)

View file

@ -48,7 +48,7 @@ ErrorOr<void> Program::attach_shader(Shader& shader)
return {};
}
ErrorOr<void> Program::link()
ErrorOr<void> Program::link(GPU::Device& device)
{
m_info_log = TRY(String::from_utf8(""sv));
@ -86,6 +86,9 @@ ErrorOr<void> Program::link()
m_linked_fragment_shader = linked_fragment_shader_or_error.release_value();
m_gpu_vertex_shader = TRY(device.create_shader({}));
m_gpu_fragment_shader = TRY(device.create_shader({}));
m_link_status = true;
return {};
}

View file

@ -15,6 +15,8 @@
#include <AK/Vector.h>
#include <LibGL/Shaders/Shader.h>
#include <LibGLSL/LinkedShader.h>
#include <LibGPU/Device.h>
#include <LibGPU/Shader.h>
namespace GL {
@ -24,7 +26,7 @@ public:
bool is_shader_attached(Shader const&) const;
ErrorOr<void> attach_shader(Shader&);
ErrorOr<void> link();
ErrorOr<void> link(GPU::Device&);
bool link_status() const { return m_link_status; }
size_t info_log_length() const;
@ -35,6 +37,8 @@ private:
Optional<String> m_info_log;
OwnPtr<GLSL::LinkedShader> m_linked_vertex_shader;
OwnPtr<GLSL::LinkedShader> m_linked_fragment_shader;
RefPtr<GPU::Shader> m_gpu_vertex_shader;
RefPtr<GPU::Shader> m_gpu_fragment_shader;
};
}

View file

@ -12,6 +12,7 @@
#include <AK/Vector.h>
#include <LibGPU/DeviceInfo.h>
#include <LibGPU/Enums.h>
#include <LibGPU/IR.h>
#include <LibGPU/Image.h>
#include <LibGPU/ImageDataLayout.h>
#include <LibGPU/Light.h>
@ -20,6 +21,7 @@
#include <LibGPU/RasterPosition.h>
#include <LibGPU/RasterizerOptions.h>
#include <LibGPU/SamplerConfig.h>
#include <LibGPU/Shader.h>
#include <LibGPU/StencilConfiguration.h>
#include <LibGPU/TextureUnitConfiguration.h>
#include <LibGPU/Vertex.h>
@ -56,6 +58,7 @@ public:
virtual LightModelParameters light_model() const = 0;
virtual NonnullRefPtr<Image> create_image(PixelFormat const&, u32 width, u32 height, u32 depth, u32 max_levels) = 0;
virtual ErrorOr<NonnullRefPtr<Shader>> create_shader(IR::Shader const&) = 0;
virtual void set_sampler_config(unsigned, SamplerConfig const&) = 0;
virtual void set_light_state(unsigned, Light const&) = 0;

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 2022, Stephan Unverwerth <s.unverwerth@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/RefCounted.h>
namespace GPU {
class Shader : public RefCounted<Shader> {
public:
Shader(void const* ownership_token)
: m_ownership_token { ownership_token }
{
}
virtual ~Shader() = default;
void const* ownership_token() const { return m_ownership_token; }
bool has_same_ownership_token(Shader const& other) const { return other.ownership_token() == ownership_token(); }
private:
void const* const m_ownership_token { nullptr };
};
}

View file

@ -4,6 +4,7 @@ set(SOURCES
Image.cpp
PixelConverter.cpp
Sampler.cpp
Shader.cpp
)
add_compile_options(-Wno-psabi)

View file

@ -22,6 +22,7 @@
#include <LibSoftGPU/PixelConverter.h>
#include <LibSoftGPU/PixelQuad.h>
#include <LibSoftGPU/SIMD.h>
#include <LibSoftGPU/Shader.h>
#include <math.h>
namespace SoftGPU {
@ -1626,6 +1627,11 @@ NonnullRefPtr<GPU::Image> Device::create_image(GPU::PixelFormat const& pixel_for
return adopt_ref(*new Image(this, pixel_format, width, height, depth, max_levels));
}
ErrorOr<NonnullRefPtr<GPU::Shader>> Device::create_shader(GPU::IR::Shader const&)
{
return adopt_ref(*new Shader(this));
}
void Device::set_sampler_config(unsigned sampler, GPU::SamplerConfig const& config)
{
VERIFY(config.bound_image.is_null() || config.bound_image->ownership_token() == this);

View file

@ -65,6 +65,7 @@ public:
virtual GPU::LightModelParameters light_model() const override { return m_lighting_model; }
virtual NonnullRefPtr<GPU::Image> create_image(GPU::PixelFormat const&, u32 width, u32 height, u32 depth, u32 max_levels) override;
virtual ErrorOr<NonnullRefPtr<GPU::Shader>> create_shader(GPU::IR::Shader const&) override;
virtual void set_sampler_config(unsigned, GPU::SamplerConfig const&) override;
virtual void set_light_state(unsigned, GPU::Light const&) override;

View file

@ -0,0 +1,16 @@
/*
* Copyright (c) 2022, Stephan Unverwerth <s.unverwerth@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibSoftGPU/Shader.h>
namespace SoftGPU {
Shader::Shader(void const* ownership_token)
: GPU::Shader(ownership_token)
{
}
}

View file

@ -0,0 +1,18 @@
/*
* Copyright (c) 2022, Stephan Unverwerth <s.unverwerth@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibGPU/Shader.h>
namespace SoftGPU {
class Shader final : public GPU::Shader {
public:
Shader(void const* ownership_token);
};
}