Skip to content

Commit

Permalink
Splitting opengl.cpp WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rtoumazet committed Sep 11, 2024
1 parent 0505538 commit b115b38
Show file tree
Hide file tree
Showing 13 changed files with 3,400 additions and 1,266 deletions.
2 changes: 2 additions & 0 deletions saturnin/saturnin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@
<ClCompile Include="src\tests.cpp" />
<ClCompile Include="src\thread_pool.cpp" />
<ClCompile Include="src\timer.cpp" />
<ClCompile Include="src\video\opengl\opengl_shaders.cpp" />
<ClCompile Include="src\video\opengl\opengl_utilities.cpp" />
<ClCompile Include="src\video\texture.cpp" />
<ClCompile Include="src\video\vdp1_part.cpp" />
<ClCompile Include="src\video\vdp2_debug.cpp" />
Expand Down
21 changes: 15 additions & 6 deletions saturnin/saturnin.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
<Filter Include="Fichiers sources\sh2\fast_interpreter">
<UniqueIdentifier>{dab72cf9-6b96-42c9-9a49-b64d257fa361}</UniqueIdentifier>
</Filter>
<Filter Include="Fichiers sources\video\opengl">
<UniqueIdentifier>{789e1ad2-891d-45fa-979a-d33b7c19045e}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\config.cpp">
Expand Down Expand Up @@ -87,9 +90,6 @@
<ClCompile Include="lib\imgui\imgui_custom_controls.cpp">
<Filter>Fichiers sources\imgui</Filter>
</ClCompile>
<ClCompile Include="src\video\opengl.cpp">
<Filter>Fichiers sources\video</Filter>
</ClCompile>
<ClCompile Include="src\smpc.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
Expand Down Expand Up @@ -165,6 +165,15 @@
<ClCompile Include="src\timer.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="src\video\opengl.cpp">
<Filter>Fichiers sources\video\opengl</Filter>
</ClCompile>
<ClCompile Include="src\video\opengl\opengl_shaders.cpp">
<Filter>Fichiers sources\video\opengl</Filter>
</ClCompile>
<ClCompile Include="src\video\opengl\opengl_utilities.cpp">
<Filter>Fichiers sources\video\opengl</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\config.h">
Expand Down Expand Up @@ -230,9 +239,6 @@
<ClInclude Include="lib\imgui\imgui_custom_controls.h">
<Filter>Fichiers sources\imgui</Filter>
</ClInclude>
<ClInclude Include="src\video\opengl.h">
<Filter>Fichiers sources\video</Filter>
</ClInclude>
<ClInclude Include="src\smpc.h">
<Filter>Fichiers sources</Filter>
</ClInclude>
Expand Down Expand Up @@ -347,6 +353,9 @@
<ClInclude Include="src\timer.h">
<Filter>Fichiers sources</Filter>
</ClInclude>
<ClInclude Include="src\video\opengl.h">
<Filter>Fichiers sources\video\opengl</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="src\sh2\fast_interpreter\sh2_opcodes.inc">
Expand Down
2 changes: 1 addition & 1 deletion saturnin/src/emulator_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <saturnin/src/cdrom/cdrom.h>
#include <saturnin/src/cdrom/scsi.h>
#include <saturnin/src/sound/scsp.h>
#include <saturnin/src/video/opengl.h>
#include <saturnin/src/video/opengl/opengl.h>
#include <saturnin/src/video/vdp1.h>
#include <saturnin/src/video/vdp2.h>

Expand Down
2 changes: 1 addition & 1 deletion saturnin/src/video/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include <saturnin/src/thread_pool.h> // ThreadPool
#include <saturnin/src/utilities.h> // stringToVector, format
#include <saturnin/src/cdrom/scsi.h> // ScsiDriveInfo
#include <saturnin/src/video/opengl.h> // Opengl
#include <saturnin/src/video/opengl/opengl.h> // Opengl
#include <saturnin/src/video/texture.h> // Texture
#include <saturnin/src/video/vdp1.h> // Vdp1
#include <saturnin/src/video/vdp2.h> // vram_timing_size
Expand Down
117 changes: 9 additions & 108 deletions saturnin/src/video/opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//

#include <saturnin/src/pch.h>
#include <saturnin/src/video/opengl.h>
#include <saturnin/src/video/opengl/opengl.h>
#include <Windows.h> // removes C4005 warning
#include <algorithm>
#include <fstream> // ifstream
Expand Down Expand Up @@ -188,22 +188,6 @@ void Opengl::postRender() {
glBindFramebuffer(GLenum::GL_FRAMEBUFFER, 0);
};

void Opengl::initializeShaders() {
const auto readFile = [](const std::string& filename) {
std::ifstream input_file("./shaders/" + filename, std::ios::in);
if (!input_file) { Log::exception(Logger::opengl, tr("Could not load shader '{}' !"), filename); }

auto buffer = std::stringstream{};
buffer << input_file.rdbuf();
input_file.close();

return buffer.str();
};

shaders_list_.try_emplace("main.vert", readFile("main.vert"));
shaders_list_.try_emplace("main.frag", readFile("main.frag"));
}

void Opengl::displayFramebuffer(core::EmulatorContext& state) {
// :TODO:
// - Render each layer + priority to one specific FBO
Expand Down Expand Up @@ -343,16 +327,16 @@ void Opengl::clearFboTextures() {
// Log::debug(Logger::opengl, "clearFboTextures() call");
// for (u8 index = 0; auto& status : fbo_texture_pool_status_) {
// if (status == FboTextureStatus::to_clear) {
// // :WIP:
// // Log::debug(Logger::opengl, "- Clearing texture at index {}", index);
// // attachTextureLayerToFbo(fbo_texture_array_id_, index);
// // attachTextureToFbo(getFboTextureId(FboTextureType::vdp2_debug_layer));
// :WIP:
// Log::debug(Logger::opengl, "- Clearing texture at index {}", index);
// attachTextureLayerToFbo(fbo_texture_array_id_, index);
// attachTextureToFbo(getFboTextureId(FboTextureType::vdp2_debug_layer));

// // gl::glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// // glClear(GL_COLOR_BUFFER_BIT);
// gl::glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// glClear(GL_COLOR_BUFFER_BIT);

// // Log::debug(Logger::opengl, "- Changing FBO texture status at index {} to 'unused'", index);
// // status = FboTextureStatus::unused;
// Log::debug(Logger::opengl, "- Changing FBO texture status at index {} to 'unused'", index);
// status = FboTextureStatus::unused;
// }
//}
// Log::debug(Logger::opengl, "clearFbos() return");
Expand Down Expand Up @@ -1214,46 +1198,6 @@ auto Opengl::getTextureId(const TextureArrayType type) -> u32 {

void Opengl::onWindowResize(const u16 width, const u16 height) { hostScreenResolution({width, height}); }

auto Opengl::createVertexShader(std::string_view name) -> u32 {
const auto vertex_shader = glCreateShader(GL_VERTEX_SHADER);
auto source = getShaderSource(name);
glShaderSource(vertex_shader, 1, &source, nullptr);
glCompileShader(vertex_shader);
checkShaderCompilation(vertex_shader);

return vertex_shader;
}

auto Opengl::createFragmentShader(std::string_view name) -> u32 {
const auto fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
auto source = getShaderSource(name);
glShaderSource(fragment_shader, 1, &source, nullptr);
glCompileShader(fragment_shader);

checkShaderCompilation(fragment_shader);

return fragment_shader;
}

// static
auto Opengl::createProgramShader(const u32 vertex_shader, const u32 fragment_shader) -> u32 {
const auto shader_program = glCreateProgram();

glAttachShader(shader_program, vertex_shader);
glAttachShader(shader_program, fragment_shader);
glLinkProgram(shader_program);
checkProgramCompilation(shader_program);

return shader_program;
}

// static
void Opengl::deleteShaders(const std::vector<u32>& shaders) {
for (auto shader : shaders) {
glDeleteShader(shader);
}
}

// static
auto Opengl::generateTexture(const u32 width, const u32 height, const std::vector<u8>& data) -> u32 {
using enum GLenum;
Expand Down Expand Up @@ -1895,49 +1839,6 @@ auto loadPngImage(const u8* data, const size_t size) -> GLFWimage {
return image;
}

void windowSizeCallback(GLFWwindow* window, const int width, const int height) {
const auto state = std::bit_cast<core::EmulatorContext*>(glfwGetWindowUserPointer(window));
state->opengl()->onWindowResize(static_cast<u16>(width), static_cast<u16>(height));
}

void checkShaderCompilation(const u32 shader) {
auto success = GLboolean{};
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
auto length = s32{};
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
if (success == GL_FALSE) {
auto v = std::vector<char>(length);
glGetShaderInfoLog(shader, length, nullptr, v.data());
const auto info = std::string(v.begin(), v.end());

auto type = GLenum{};
auto shader_type = std::string{};
glGetShaderiv(shader, GL_SHADER_TYPE, &type);
switch (type) {
case GL_VERTEX_SHADER: shader_type = "Vertex shader"; break;
case GL_FRAGMENT_SHADER: shader_type = "Fragment shader"; break;
default: shader_type = "Unknown shader"; break;
}

Log::exception(Logger::opengl, "{} compilation failed : {}", shader_type, info);
}
}

void checkProgramCompilation(const u32 program) {
auto success = GLboolean{};
glGetProgramiv(program, GL_LINK_STATUS, &success);
auto length = s32{};
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);

if (success == GL_FALSE) {
auto v = std::vector<char>(length);
glGetProgramInfoLog(program, length, nullptr, v.data());
const auto info = std::string(v.begin(), v.end());

Log::exception(Logger::opengl, "Shader program link failed : {}", info);
}
}

void checkGlError() {
if (check_gl_error) {
const auto error = glGetError();
Expand Down
Loading

0 comments on commit b115b38

Please sign in to comment.