Skip to content

Commit

Permalink
Merge branch 'vdp2-cache' of https://github.com/rtoumazet/saturnin in…
Browse files Browse the repository at this point in the history
…to vdp2-cache
  • Loading branch information
rtoumazet committed Oct 31, 2024
2 parents 546221b + efb86cc commit 59f6bf3
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 227 deletions.
28 changes: 3 additions & 25 deletions saturnin/src/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,11 @@ void runTests() {
}

std::vector<u8> texture_data;
// core::Log::info(Logger::test, "{}", arr[0x3f]);
auto os = std::ostringstream{};
auto b = ankerl::nanobench::Bench();
auto os = std::ostringstream{};
auto b = ankerl::nanobench::Bench();
b.output(&os).relative(true);

// ankerl::nanobench::Bench().output(&os).run("Direct copy", [&] {
b.run("Direct copy", [&] {
b.run("Direct copy", [&texture_data, &current_address, &row, &ec] {
texture_data.clear();
current_address = u32{0x25e00000};
for (u32 i = 0; i < 8; ++i) {
Expand All @@ -129,26 +127,6 @@ void runTests() {
texture_data.emplace_back(row.as_8bits >> DataExtraction::As8Bits::dot3_shift);
current_address += 4;
}

// ankerl::nanobench::doNotOptimizeAway(d);
});

b.run("Block copy", [&] {
texture_data.clear();
current_address = u32{0x25e00000};
// for (u32 i = 0; i < 8; ++i) {
// const auto data = ec.memory()->read(core::MemoryMapArea::vdp2_video_ram, current_address, 0x40);

// for (const auto& elem : data) {
// row.as_8bits = elem;
// texture_data.emplace_back(row.as_8bits >> DataExtraction::As8Bits::dot0_shift);
// texture_data.emplace_back(row.as_8bits >> DataExtraction::As8Bits::dot1_shift);
// texture_data.emplace_back(row.as_8bits >> DataExtraction::As8Bits::dot2_shift);
// texture_data.emplace_back(row.as_8bits >> DataExtraction::As8Bits::dot3_shift);
// }
//}

// ankerl::nanobench::doNotOptimizeAway(d);
});

core::Log::info(Logger::test, "{}", os.str());
Expand Down
22 changes: 10 additions & 12 deletions saturnin/src/video/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@
#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/opengl.h> // Opengl
#include <saturnin/src/video/opengl/opengl_render.h> // OpenglRender
#include <saturnin/src/video/opengl/opengl_texturing.h> // OpenglTexturing
#include <saturnin/src/video/opengl/opengl_render.h> // OpenglRender
#include <saturnin/src/video/texture.h> // Texture
#include <saturnin/src/video/vdp1.h> // Vdp1
#include <saturnin/src/video/vdp2/vdp2.h> // vram_timing_size
Expand Down Expand Up @@ -1413,7 +1412,6 @@ void showDebugVdp1Window(core::EmulatorContext& state, bool* opened) {
// Draw list
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2());
const auto child_size = ImVec2(310, 280);
// ImGui::BeginChild("ChildDrawList", child_size, true, window_flags | ImGuiWindowFlags_MenuBar);
ImGui::BeginChild("ChildDrawList", child_size, true);

if (ImGui::BeginMenuBar()) {
Expand Down Expand Up @@ -1478,10 +1476,10 @@ void showDebugVdp1Window(core::EmulatorContext& state, bool* opened) {

static auto opengl_id = 0;
if (texture) {
if (opengl_id != 0) { state.opengl()->texturing()->deleteTexture(opengl_id); }
opengl_id = state.opengl()->texturing()->generateTexture((*texture)->width(),
(*texture)->height(),
(*texture)->rawData());
if (opengl_id != 0) { video::OpenglTexturing::deleteTexture(opengl_id); }
opengl_id = video::OpenglTexturing::generateTexture((*texture)->width(),
(*texture)->height(),
(*texture)->rawData());
ImGui::Image(reinterpret_cast<ImTextureID>(static_cast<uptr>(opengl_id)), preview_size);
}
}
Expand Down Expand Up @@ -1861,10 +1859,10 @@ void showDebugTexturesWindow(core::EmulatorContext& state, bool* opened) {
// Reloading texture data from the new selected entry.
const auto texture = video::Texture::getTexture(texture_key);
if (texture) {
if (opengl_id != 0) { state.opengl()->texturing()->deleteTexture(opengl_id); }
opengl_id = state.opengl()->texturing()->generateTexture((*texture)->width(),
(*texture)->height(),
(*texture)->rawData());
if (opengl_id != 0) { video::OpenglTexturing::deleteTexture(opengl_id); }
opengl_id = video::OpenglTexturing::generateTexture((*texture)->width(),
(*texture)->height(),
(*texture)->rawData());
}
previous_texture_idx = current_texture_idx;
}
Expand Down Expand Up @@ -1904,7 +1902,7 @@ void showDebugTexturesWindow(core::EmulatorContext& state, bool* opened) {
if (ImGui::Combo("Layer", &current_layer, layers)) {}
auto tex_id
= state.opengl()->texturing()->generateTextureFromTextureArrayLayer(video::GuiTextureType::layer_buffer,
current_layer);
static_cast<u8>(current_layer));

ImGui::BeginChild("child_part_texture", child_size, true, window_flags);
const auto preview_size = ImVec2(500, 500);
Expand Down
36 changes: 14 additions & 22 deletions saturnin/src/video/opengl/opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include <saturnin/src/video/opengl/opengl_render.h>
#include <saturnin/src/video/opengl/opengl_texturing.h>
#include <saturnin/src/video/opengl/opengl_utilities.h>
#include <saturnin/src/video/opengl/opengl_texturing.h>
#include <saturnin/src/video/texture.h>
#include <saturnin/src/video/vdp_common.h>
#include <saturnin/src/resource_holder.hpp>
Expand Down Expand Up @@ -98,22 +99,23 @@ void Opengl::shutdown() {
auto Opengl::areFbosInitialized() const -> bool { return opengl_texturing_->getFboId(FboType::general) != 0; };

void Opengl::clearFboTextures() {
Log::debug(Logger::opengl, "clearFboTextures() call");
using enum Logger;
Log::debug(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);
Log::debug(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);

Log::debug(Logger::opengl, "- Changing FBO texture status at index {} to 'unused'", index);
Log::debug(opengl, "- Changing FBO texture status at index {} to 'unused'", index);
status = FboTextureStatus::unused;
}
}
Log::debug(Logger::opengl, "clearFbos() return");
Log::debug(opengl, "clearFbos() return");
}

void Opengl::clearFboKeys() {
Expand All @@ -126,9 +128,9 @@ void Opengl::clearFboKeys() {
// Log::debug(Logger::opengl, "clearFboKeys() return");
}

void Opengl::bindFbo(const u32 fbo_id) { gl33core::glBindFramebuffer(GL_FRAMEBUFFER, fbo_id); }
void Opengl::bindFbo(const u32 fbo_id) const { gl33core::glBindFramebuffer(GL_FRAMEBUFFER, fbo_id); }

void Opengl::unbindFbo() { bindFbo(0); }
void Opengl::unbindFbo() const { bindFbo(0); }

auto Opengl::getAvailableFboTextureIndex() -> std::optional<u8> {
std::array<FboTextureStatus, 1> status = {FboTextureStatus::unused};
Expand Down Expand Up @@ -160,6 +162,10 @@ auto Opengl::getRenderedBufferTextureId(const GuiTextureType type) -> u32 {
layer = texturing()->getFboTextureLayer(FboTextureType::vdp2_debug_layer);
break;
}
case layer_buffer: {
Log::warning(Logger::opengl, "getRenderedBufferTextureId() called with a layer_buffer type !");
break;
}
}

auto texture_id = texturing()->generateTextureFromTextureArrayLayer(type, layer);
Expand Down Expand Up @@ -219,7 +225,7 @@ auto runOpengl(core::EmulatorContext& state) -> s32 {
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE,
GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
// Change call to glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); for 3.0+ only

glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
}
Expand All @@ -232,9 +238,6 @@ auto runOpengl(core::EmulatorContext& state) -> s32 {
if (window == nullptr) { return EXIT_FAILURE; }
state.openglWindow(window);

// glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
// const auto render_window = glfwCreateWindow(1, 1, "invisible", nullptr, ihm_window);

glfwSetWindowCloseCallback(window, windowCloseCallback);
glfwSetWindowSizeCallback(window, windowSizeCallback);

Expand Down Expand Up @@ -271,19 +274,12 @@ auto runOpengl(core::EmulatorContext& state) -> s32 {
IMGUI_CHECKVERSION();
ImGui::CreateContext();

// auto flags = ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
//(void)flags;
ImGuiIO& io = ImGui::GetIO();
(void)io;

// io.ConfigViewportDecorations
// io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
// io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Viewports

// Setup style
ImGui::StyleColorsDark();
// ImGui::StyleColorsClassic();

ImGui_ImplGlfw_InitForOpenGL(window, true);
is_legacy_opengl ? ImGui_ImplOpenGL3_Init() : ImGui_ImplOpenGL3_Init(glsl_version);
Expand All @@ -299,7 +295,6 @@ auto runOpengl(core::EmulatorContext& state) -> s32 {
// io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());

// Adding glyphs that will be used as images in text
// ImGuiIO& io = ImGui::GetIO();
io.Fonts->AddFontDefault();
static const std::array<ImWchar, 3> icons_ranges = {0xe900, 0xe908, 0}; // Will not be copied by AddFont* so keep in scope.
ImFontConfig config;
Expand All @@ -313,23 +308,20 @@ auto runOpengl(core::EmulatorContext& state) -> s32 {
io.Fonts->AddFontFromMemoryTTF((void*)data.data(), static_cast<u32>(data.size()), font_size, &config, icons_ranges.data());
io.Fonts->Build();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
// io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Viewports

const auto clear_color = ImVec4{0.0f, 0.0f, 0.0f, 1.00f};

updateMainWindowSizeAndRatio(window, minimum_window_width, minimum_window_height);

state.opengl()->initialize();

Log::info(Logger::opengl, "Card : {}", (char*)glGetString(GL_RENDERER));
Log::info(Logger::opengl, "Card : {}", reinterpret_cast<const char*>(glGetString(GL_RENDERER)));

if (state.memory()->selectedBinaryFile().full_path.size() > 0) {
state.startEmulation();
if (!state.memory()->selectedBinaryFile().is_auto_started) { state.debugStatus(core::DebugStatus::paused); }
}

// glGetString(GL_RENDERER);

// Main loop
while (glfwWindowShouldClose(window) == GLFW_FALSE) {
// Poll and handle events (inputs, window resize, etc.)
Expand Down
16 changes: 6 additions & 10 deletions saturnin/src/video/opengl/opengl.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class OpenglTexturing;

enum class DrawType : u8;
enum class ScrollScreen;
enum class GuiTextureType : u8;

using saturnin::core::Config;
using utilities::toUnderlying;
Expand All @@ -82,9 +83,6 @@ constexpr auto vertexes_per_line = u32{2};
constexpr auto fbo_texture_array_depth = u16{14};
constexpr auto max_fbo_texture = u8{20};

enum class TextureArrayType : u8 { saturn_part, framebuffer };
enum class FboTextureType : u8 { front_buffer, back_buffer, vdp1_debug_overlay, vdp2_debug_layer, priority };
enum class GuiTextureType : u8 { render_buffer, vdp1_debug_buffer, vdp2_debug_buffer, layer_buffer };
enum class FboType : u8 { general, for_gui, vdp2_debug };
enum class MutexType : u8 { parts_list = 0, textures_link = 1, texture_delete = 2 };

Expand All @@ -95,11 +93,9 @@ enum class FboTextureStatus : u8 {
to_clear ///< FBO will have to be cleared.
};

using FboData = std::pair<u32, u32>; // Describes a framebuffer object. 1st is fbo id, 2nd is texture id.
using FboTextureTypeToLayer = std::array<FboTextureType, fbo_texture_array_depth>; // Defines the type of each FBO texture layer,
// the index of the array being the layer.
using GuiTextureTypeToId = std::unordered_map<GuiTextureType, u32>; // Defines the type of each texture used to render to GUI.
using FboTypeToId = std::unordered_map<FboType, u32>; // Link between a FboType and its id.
using FboData = std::pair<u32, u32>; // Describes a framebuffer object. 1st is fbo id, 2nd is texture id.

using FboTypeToId = std::unordered_map<FboType, u32>; // Link between a FboType and its id.

using FboTexturePool = std::array<u32, max_fbo_texture>; // Pool of textures ids to be used for rendering by priority.
using FboTexturePoolStatus = std::array<FboTextureStatus, max_fbo_texture>; // State of the textures in the pool.
Expand Down Expand Up @@ -232,10 +228,10 @@ class Opengl {
void clearFboKeys();

// Binds a FBO. Passing index 0 unbinds it.
void bindFbo(const u32 fbo_id);
void bindFbo(const u32 fbo_id) const;

// Unbinds current bound FBO.
void unbindFbo();
void unbindFbo() const;

// Returns the next available FBO texture index (with status 'unused').
auto getAvailableFboTextureIndex() -> std::optional<u8>;
Expand Down
11 changes: 1 addition & 10 deletions saturnin/src/video/opengl/opengl_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <saturnin/src/video/opengl/opengl_shaders.h>
#include <saturnin/src/video/opengl/opengl_texturing.h>
#include <saturnin/src/video/opengl/opengl_utilities.h>
#include <saturnin/src/video/opengl/opengl_texturing.h>
#include <saturnin/src/video/vdp1.h>
#include <saturnin/src/video/vdp2/vdp2.h>

Expand Down Expand Up @@ -334,7 +335,6 @@ void OpenglRender::renderTest() {

glActiveTexture(GLenum::GL_TEXTURE0);
const auto sampler_loc = glGetUniformLocation(shaders_.programs[ProgramShader::main], "sampler");
// glUniform1i(sampler_loc, GLenum::GL_TEXTURE0);
glUniform1i(sampler_loc, 0);
glBindTexture(GL_TEXTURE_2D_ARRAY, opengl_->texturing()->getTextureArrayId());

Expand All @@ -348,10 +348,7 @@ void OpenglRender::renderTest() {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elements_buffer);

if constexpr (false) {
// std::array<GLuint, 20> indices = {0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
std::vector<GLuint> indices = {0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
// std::span<GLuint> indices(vindices);
// std::array<GLuint, 5> indices = {0, 1, 2, 3, 0};
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * indices.size(), &indices.front(), GL_STATIC_DRAW);

auto vertexes = std::vector<Vertex>{
Expand Down Expand Up @@ -422,7 +419,6 @@ void OpenglRender::renderTest() {
};
render_part.common_vdp_data_.draw_type = DrawType::line;
render_part.common_vdp_data_.vdp_type = VdpType::vdp1;
// render_part.common_vdp_data_.color_offset = {0x50, 0x50, 0x50};
parts.emplace_back(render_part);

render_part.common_vdp_data_.vertexes = {
Expand All @@ -431,7 +427,6 @@ void OpenglRender::renderTest() {
};
render_part.common_vdp_data_.draw_type = DrawType::line;
render_part.common_vdp_data_.vdp_type = VdpType::vdp1;
// render_part.common_vdp_data_.color_offset = {0x50, 0x50, 0x50};
parts.emplace_back(render_part);

render_part.common_vdp_data_.vertexes = {
Expand All @@ -442,7 +437,6 @@ void OpenglRender::renderTest() {
};
render_part.common_vdp_data_.draw_type = DrawType::polyline;
render_part.common_vdp_data_.vdp_type = VdpType::vdp1;
// render_part.common_vdp_data_.color_offset = {0x50, 0x50, 0x50};
parts.emplace_back(render_part);

const auto&& [indices, draw_ranges] = generateVertexIndicesAndDrawRanges(parts);
Expand All @@ -452,9 +446,6 @@ void OpenglRender::renderTest() {
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertexes.size(), vertexes.data(), GL_STATIC_DRAW);

for (const auto& range : draw_ranges) {
const auto is_texture_used = GLboolean(range.is_textured);
glUniform1i(texture_used_loc, is_texture_used);

glDrawRangeElements(range.primitive,
range.vertex_array_start,
range.vertex_array_end,
Expand Down
1 change: 1 addition & 0 deletions saturnin/src/video/opengl/opengl_render.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <saturnin/src/emulator_defs.h>
#include <saturnin/src/video/opengl/opengl.h>
#include <saturnin/src/video/opengl/opengl_texturing.h>
#include <saturnin/src/video/vdp_common.h>

namespace saturnin::video {
Expand Down
Loading

0 comments on commit 59f6bf3

Please sign in to comment.