Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ class UserApp : public sopho::App
{
std::shared_ptr<sopho::GpuWrapper> gpu_wrapper{std::make_shared<sopho::GpuWrapper>()};
sopho::BufferWrapper vertex_buffer{gpu_wrapper->create_buffer(SDL_GPU_BUFFERUSAGE_VERTEX, sizeof(vertices))};
std::optional<sopho::PipelineWrapper> pipeline_wrapper{std::nullopt};

SDL_Window* window{};
sopho::PipelineWrapper pipeline_wrapper{gpu_wrapper->create_pipeline()};

// a list of vertices
std::array<Vertex, 3> vertices{
Expand Down Expand Up @@ -72,17 +70,10 @@ void main()
*/
virtual SDL_AppResult init(int argc, char** argv) override
{
// create a window
window = SDL_CreateWindow("Hello, Triangle!", 960, 540, SDL_WINDOW_RESIZABLE);
gpu_wrapper->set_window(window);

SDL_ClaimWindowForGPUDevice(gpu_wrapper->data(), window);

pipeline_wrapper.emplace(gpu_wrapper);

pipeline_wrapper->set_vertex_shader(vertex_source);
pipeline_wrapper->set_fragment_shader(fragment_source);
pipeline_wrapper->submit();
pipeline_wrapper.set_vertex_shader(vertex_source);
pipeline_wrapper.set_fragment_shader(fragment_source);
pipeline_wrapper.submit();

vertex_buffer.upload(&vertices, sizeof(vertices), 0);

Expand Down Expand Up @@ -110,10 +101,11 @@ void main()
// unnecessary. We leave both here for documentation purpose)

// Setup Platform/Renderer backends
ImGui_ImplSDL3_InitForSDLGPU(window);
ImGui_ImplSDL3_InitForSDLGPU(gpu_wrapper->acquire_window());
ImGui_ImplSDLGPU3_InitInfo init_info = {};
init_info.Device = gpu_wrapper->data();
init_info.ColorTargetFormat = SDL_GetGPUSwapchainTextureFormat(gpu_wrapper->data(), window);
init_info.ColorTargetFormat =
SDL_GetGPUSwapchainTextureFormat(gpu_wrapper->data(), gpu_wrapper->acquire_window());
init_info.MSAASamples = SDL_GPU_SAMPLECOUNT_1; // Only used in multi-viewports mode.
init_info.SwapchainComposition = SDL_GPU_SWAPCHAINCOMPOSITION_SDR; // Only used in multi-viewports mode.
init_info.PresentMode = SDL_GPU_PRESENTMODE_VSYNC;
Expand Down Expand Up @@ -159,7 +151,7 @@ void main()
std::min(ImGui::GetTextLineHeight() * (line_count + 3), ImGui::GetContentRegionAvail().y));
if (ImGui::InputTextMultiline("code editor", &vertex_source, size, ImGuiInputTextFlags_AllowTabInput))
{
pipeline_wrapper->set_vertex_shader(vertex_source);
pipeline_wrapper.set_vertex_shader(vertex_source);
}

ImGui::End();
Expand All @@ -168,15 +160,16 @@ void main()
ImGui::Render();
ImDrawData* draw_data = ImGui::GetDrawData();

pipeline_wrapper->submit();
pipeline_wrapper.submit();

// acquire the command buffer
SDL_GPUCommandBuffer* commandBuffer = SDL_AcquireGPUCommandBuffer(gpu_wrapper->data());

// get the swapchain texture
SDL_GPUTexture* swapchainTexture;
Uint32 width, height;
SDL_WaitAndAcquireGPUSwapchainTexture(commandBuffer, window, &swapchainTexture, &width, &height);
SDL_WaitAndAcquireGPUSwapchainTexture(commandBuffer, gpu_wrapper->acquire_window(), &swapchainTexture, &width,
&height);

// end the frame early if a swapchain texture is not available
if (swapchainTexture == NULL)
Expand All @@ -199,7 +192,7 @@ void main()
SDL_GPURenderPass* renderPass = SDL_BeginGPURenderPass(commandBuffer, &colorTargetInfo, 1, NULL);

// draw calls go here
SDL_BindGPUGraphicsPipeline(renderPass, pipeline_wrapper->data());
SDL_BindGPUGraphicsPipeline(renderPass, pipeline_wrapper.data());

// bind the vertex buffer
SDL_GPUBufferBinding bufferBindings[1];
Expand Down Expand Up @@ -252,11 +245,6 @@ void main()
ImGui_ImplSDL3_Shutdown();
ImGui_ImplSDLGPU3_Shutdown();
ImGui::DestroyContext();

SDL_ReleaseWindowFromGPUDevice(gpu_wrapper->data(), window);

// destroy the window
SDL_DestroyWindow(window);
}
};

Expand Down
21 changes: 17 additions & 4 deletions sdl_wrapper/sdl_wrapper.gpu.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module;
#include <memory>
#include "SDL3/SDL_gpu.h"
#include "SDL3/SDL_log.h"
#include "SDL3/SDL_video.h"
#include "shaderc/shaderc.hpp"
export module sdl_wrapper:gpu;
import :buffer;
Expand All @@ -15,6 +16,7 @@ export namespace sopho
{
SDL_GPUDevice* m_device{};

// TODO: Consider multi window situation
SDL_Window* m_window{};

public:
Expand All @@ -28,6 +30,11 @@ export namespace sopho

~GpuWrapper()
{
if (m_window)
{
SDL_ReleaseWindowFromGPUDevice(m_device, m_window);
SDL_DestroyWindow(m_window);
}
if (m_device)
{
SDL_DestroyGPUDevice(m_device);
Expand Down Expand Up @@ -64,14 +71,20 @@ export namespace sopho
}
}

auto get_texture_format()
auto acquire_window()
{
return SDL_GetGPUSwapchainTextureFormat(m_device, m_window);
if (!m_window)
{
m_window = SDL_CreateWindow("Hello, Triangle!", 960, 540, SDL_WINDOW_RESIZABLE);
SDL_ClaimWindowForGPUDevice(m_device, m_window);
}
return m_window;
}

auto set_window(SDL_Window* p_window)
auto get_texture_format()
{
m_window = p_window;
return SDL_GetGPUSwapchainTextureFormat(m_device, acquire_window());
}

};
} // namespace sopho
4 changes: 1 addition & 3 deletions sdl_wrapper/sdl_wrapper.pipeline.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ export namespace sopho

bool m_modified = false;

public:
PipelineWrapper(std::shared_ptr<GpuWrapper> p_device);
PipelineWrapper(const PipelineWrapper&)=default;
PipelineWrapper(PipelineWrapper&&)=default;
public:
~PipelineWrapper();

auto data() { return m_graphics_pipeline; }
Expand Down