Skip to content

Commit

Permalink
VDP2 cache WIP (broken)
Browse files Browse the repository at this point in the history
  • Loading branch information
rtoumazet committed Mar 22, 2024
1 parent a32a2a5 commit c21f6ae
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
43 changes: 34 additions & 9 deletions saturnin/src/video/opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,31 @@ auto Opengl::generateTextureFromTextureArrayLayer(const u32 layer, const size_t
}

return texture_array_debug_layer_id_;
}
auto Opengl::getTextureId(const TextureArrayType type) -> u32 {
// There are 2 texture arrays used in the program :
// - one is used as a texture atlas for VDP parts rendering (texture_array_id_)
// - the other as framebuffers linked to the main FBO, handling debug layers, priority rendering, front and back framebuffer
// (fbo_texture_array_id_), etc.

switch (type) {
using enum TextureArrayType;
case saturn_part: {
// Checking if the texture already exists. If not it'll be created.
if (texture_array_debug_layer_id_ == 0) {
texture_array_debug_layer_id_ = generateTexture(texture_array_width, texture_array_height, std::vector<u8>{});
}
// :TODO: update texture data based on the texture array layer specified.

return texture_array_debug_layer_id_;
}
case framebuffer: {
// :TODO: Search in the texture pool the id of the texture ... is it really needed ?
break;
}
}

return u32();
};

void Opengl::onWindowResize(const u16 width, const u16 height) { hostScreenResolution({width, height}); }
Expand Down Expand Up @@ -1651,17 +1676,17 @@ void Opengl::initializeFbo() {
// fbo_pool_status_[i] = FboStatus::unused;
//}

// A FBO (and its related texture) is generated for every FboType.
// Some layers of the texture array are setup as a specific types in the FBO texture pool.
// Front and back buffers are switched every frame : one will be used as the last complete rendering by the GUI while
// the other will be rendered to.
fbo_global_list_.try_emplace(FboType::front_buffer, generateFbo());
fbo_global_list_.try_emplace(FboType::back_buffer, generateFbo());
fbo_global_list_.try_emplace(FboType::vdp1_debug_overlay, generateFbo());
fbo_global_list_.try_emplace(FboType::vdp2_debug_layer, generateFbo());
fbo_global_list_.try_emplace(FboType::priority, generateFbo());

currentRenderedBuffer(FboType::front_buffer);
glBindFramebuffer(GL_FRAMEBUFFER, getFboTextureId(currentRenderedBuffer()));
// Layers not yet linked to a type will be used as a 'priority' type on demand when rendering.
fbo_texture_type_to_layer_.try_emplace(FboTextureType::front_buffer, getAvailableFboTextureIndex());
fbo_texture_type_to_layer_.try_emplace(FboTextureType::back_buffer, getAvailableFboTextureIndex());
fbo_texture_type_to_layer_.try_emplace(FboTextureType::vdp1_debug_overlay, getAvailableFboTextureIndex());
fbo_texture_type_to_layer_.try_emplace(FboTextureType::vdp2_debug_layer, getAvailableFboTextureIndex());

currentRenderedBuffer(FboTextureType::front_buffer);
glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
}

auto Opengl::generateFbo() -> u32 {
Expand Down
31 changes: 18 additions & 13 deletions saturnin/src/video/opengl.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ constexpr auto texture_array_depth = u16{64};
constexpr auto fbo_texture_array_depth = u16{20};
constexpr auto max_fbo_by_priority = u8{16};

enum class TextureArrayType : u8 { saturn_part, framebuffer };
enum class FboTextureType : u8 { front_buffer, back_buffer, vdp1_debug_overlay, vdp2_debug_layer, priority };

////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -80,8 +81,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 FboTextureList = std::unordered_map<FboType, FboData>;
using FboData = std::pair<u32, u32>; // Describes a framebuffer object. 1st is fbo id, 2nd is texture id.
using FboTextureTypeToFboTextureLayer
= std::unordered_map<FboTextureType, u32>; // Defines the type of the FBO texture array layer.

using FboKey = std::pair<u8, VdpLayer>; // First is priority number (1 to 7, last on being the highest), second is linked layer.
using FboKeyToFbo = std::map<FboKey, u8>; // Link between a priority to display and its relative FBO index in the FBO pool.
Expand Down Expand Up @@ -187,8 +189,8 @@ class Opengl {
/// Accessors / Mutators
[[nodiscard]] auto currentRenderedBuffer() const { return current_rendered_buffer_; };
void currentRenderedBuffer(const FboTextureType type) { current_rendered_buffer_ = type; };
[[nodiscard]] auto vdp1DebugOverlayTextureId() const { return getFboTextureId(FboType::vdp1_debug_overlay); };
[[nodiscard]] auto vdp2DebugLayerTextureId() const -> u32 { return getFboTextureId(FboType::vdp2_debug_layer); };
[[nodiscard]] auto vdp1DebugOverlayTextureId() const { return getFboTextureId(FboTextureType::vdp1_debug_overlay); };
[[nodiscard]] auto vdp2DebugLayerTextureId() const -> u32 { return getFboTextureId(FboTextureType::vdp2_debug_layer); };
[[nodiscard]] auto fps() const { return fps_; };
void fps(std::string_view fps) { fps_ = fps; };
void saturnScreenResolution(const ScreenResolution& res) { saturn_screen_resolution_ = res; };
Expand Down Expand Up @@ -542,6 +544,8 @@ class Opengl {

auto generateTextureFromTextureArrayLayer(const u32 layer, const size_t texture_key) -> u32;

auto getTextureId(const TextureArrayType type) -> u32;

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn auto Opengl::getOpenglTextureDetails(const size_t key) -> std::string;
///
Expand Down Expand Up @@ -690,8 +694,8 @@ class Opengl {

auto initializeTextureArray(const u32 width, const u32 height, const u32 depth) const -> u32;

auto getFboId(const FboType type) const -> u32 { return fbo_global_list_.at(type).first; };
auto getFboTextureId(const FboType type) const -> u32 { return fbo_global_list_.at(type).second; };
auto getFboTextureLayer(const FboTextureType type) const -> u32 { return fbo_texture_type_to_layer_.at(type); };

void switchRenderedBuffer();

auto calculateViewportPosAndSize() const -> std::tuple<u32, u32, u32, u32>;
Expand Down Expand Up @@ -887,14 +891,15 @@ class Opengl {

core::Config* config_; ///< Configuration object.

u32 fbo_; ///< The main framebuffer object.
u32 fbo_texture_array_id_; ///< Identifier for the texture array used by the FBO.
FboGlobalList fbo_global_list_; ///< List of framebuffer objects used in the program.
FboTextureType current_rendered_buffer_; ///< The current rendered buffer (front or back)
u32 fbo_; ///< The main framebuffer object.
u32 fbo_texture_array_id_; ///< Identifier for the texture array used by the FBO.
FboTextureTypeToFboTextureLayer fbo_texture_type_to_layer_; ///< Links the used FBO texture array layer to a texture type.
FboTextureType current_rendered_buffer_; ///< The current rendered buffer (front or back)

FboKeyToFbo fbo_key_to_fbo_pool_index_; ///< Link between a FBO key and its relative FBO index in the pool.
FboTexturePool fbo_texture_pool_; ///< Pool of textures to be used for by priority rendering.
FboTexturePoolStatus fbo_texture_pool_status_; ///< State of each texture in the pool.
FboKeyToFbo fbo_key_to_fbo_pool_index_; ///< Link between a FBO key and its relative FBO index in the pool.
// FboTexturePool fbo_texture_pool_; ///< Pool of textures to be used by the FBO. Not using a texture array to ease
// ImGUI interaction.
FboTexturePoolStatus fbo_texture_pool_status_; ///< State of each texture in the pool.

bool is_legacy_opengl_{}; ///< True if rendering in legacy opengl.

Expand Down

0 comments on commit c21f6ae

Please sign in to comment.