Files
vibe4_shaders/source/backends/vulkan_renderer.h
Sergio Valor 6a84234265 Implementar arquitectura multi-backend para vibe4_shaders
- Actualizar proyecto de vibe3_physics a vibe4_shaders
- Crear sistema modular de renderizado con RendererInterface
- Añadir WindowManager para gestión de ventana y backends
- Implementar backends: SDL (fallback), Vulkan, Metal
- Añadir soporte para efectos CRT en software
- Migrar sistema de renderizado a batch processing
- Actualizar README con nueva arquitectura

NOTA: Funcionalidad básica necesita restauración (texto y texturas)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-27 23:07:48 +02:00

185 lines
5.2 KiB
C++

#pragma once
#if defined(_WIN32) || defined(__linux__)
#include "renderer_interface.h"
#include <vector>
// Forward declarations para Vulkan
struct VkInstance_T;
struct VkPhysicalDevice_T;
struct VkDevice_T;
struct VkQueue_T;
struct VkSwapchainKHR_T;
struct VkRenderPass_T;
struct VkPipelineLayout_T;
struct VkPipeline_T;
struct VkCommandPool_T;
struct VkCommandBuffer_T;
struct VkBuffer_T;
struct VkDeviceMemory_T;
struct VkImage_T;
struct VkImageView_T;
struct VkFramebuffer_T;
struct VkSemaphore_T;
struct VkFence_T;
struct VkDescriptorSetLayout_T;
struct VkDescriptorPool_T;
struct VkDescriptorSet_T;
typedef VkInstance_T* VkInstance;
typedef VkPhysicalDevice_T* VkPhysicalDevice;
typedef VkDevice_T* VkDevice;
typedef VkQueue_T* VkQueue;
typedef VkSwapchainKHR_T* VkSwapchainKHR;
typedef VkRenderPass_T* VkRenderPass;
typedef VkPipelineLayout_T* VkPipelineLayout;
typedef VkPipeline_T* VkPipeline;
typedef VkCommandPool_T* VkCommandPool;
typedef VkCommandBuffer_T* VkCommandBuffer;
typedef VkBuffer_T* VkBuffer;
typedef VkDeviceMemory_T* VkDeviceMemory;
typedef VkImage_T* VkImage;
typedef VkImageView_T* VkImageView;
typedef VkFramebuffer_T* VkFramebuffer;
typedef VkSemaphore_T* VkSemaphore;
typedef VkFence_T* VkFence;
typedef VkDescriptorSetLayout_T* VkDescriptorSetLayout;
typedef VkDescriptorPool_T* VkDescriptorPool;
typedef VkDescriptorSet_T* VkDescriptorSet;
struct SDL_Window;
namespace vibe4 {
// Implementación usando Vulkan para Windows/Linux
class VulkanRenderer : public RendererInterface {
public:
VulkanRenderer();
~VulkanRenderer() override;
// Implementación de la interfaz
bool initialize(SDL_Window* window, int width, int height) override;
void shutdown() override;
bool beginFrame() override;
void endFrame() override;
void present() override;
void renderGradientBackground(
float top_r, float top_g, float top_b,
float bottom_r, float bottom_g, float bottom_b
) override;
void renderSpriteBatch(
const std::vector<SpriteData>& sprites,
void* texture_data
) override;
void setCRTParams(const CRTParams& params) override;
void enableCRT(bool enable) override;
BackendType getBackendType() const override { return BackendType::VULKAN; }
const char* getBackendName() const override { return "Vulkan"; }
void setVSync(bool enable) override;
void resize(int width, int height) override;
private:
// Core Vulkan objects
VkInstance instance_ = nullptr;
VkPhysicalDevice physical_device_ = nullptr;
VkDevice device_ = nullptr;
VkQueue graphics_queue_ = nullptr;
VkQueue present_queue_ = nullptr;
// Swapchain
VkSwapchainKHR swapchain_ = nullptr;
std::vector<VkImage> swapchain_images_;
std::vector<VkImageView> swapchain_image_views_;
std::vector<VkFramebuffer> swapchain_framebuffers_;
// Render pass y pipelines
VkRenderPass render_pass_ = nullptr;
VkPipelineLayout sprite_pipeline_layout_ = nullptr;
VkPipeline sprite_pipeline_ = nullptr;
VkPipelineLayout gradient_pipeline_layout_ = nullptr;
VkPipeline gradient_pipeline_ = nullptr;
// Command buffers
VkCommandPool command_pool_ = nullptr;
std::vector<VkCommandBuffer> command_buffers_;
// Synchronization
std::vector<VkSemaphore> image_available_semaphores_;
std::vector<VkSemaphore> render_finished_semaphores_;
std::vector<VkFence> in_flight_fences_;
// Buffers
VkBuffer vertex_buffer_ = nullptr;
VkDeviceMemory vertex_buffer_memory_ = nullptr;
VkBuffer index_buffer_ = nullptr;
VkDeviceMemory index_buffer_memory_ = nullptr;
VkBuffer uniform_buffer_ = nullptr;
VkDeviceMemory uniform_buffer_memory_ = nullptr;
// Descriptors
VkDescriptorSetLayout descriptor_set_layout_ = nullptr;
VkDescriptorPool descriptor_pool_ = nullptr;
VkDescriptorSet descriptor_set_ = nullptr;
// Frame data
std::vector<SpriteVertex> current_vertices_;
std::vector<uint16_t> current_indices_;
uint32_t current_frame_ = 0;
uint32_t current_image_index_ = 0;
// Configuración
const int MAX_FRAMES_IN_FLIGHT = 2;
// Estructuras uniformes
struct SpriteUniforms {
float mvp_matrix[16];
float screen_size[2];
};
struct CRTUniforms {
float scanline_intensity;
float curvature_x;
float curvature_y;
float bloom_factor;
float mask_brightness;
float screen_size[2];
int enable_scanlines;
int enable_curvature;
int enable_bloom;
};
// Métodos privados
bool createInstance();
bool selectPhysicalDevice();
bool createLogicalDevice();
bool createSwapchain();
bool createRenderPass();
bool createPipelines();
bool createFramebuffers();
bool createCommandPool();
bool createCommandBuffers();
bool createSyncObjects();
bool createBuffers();
bool createDescriptors();
void cleanupSwapchain();
void recreateSwapchain();
bool findQueueFamilies();
bool isDeviceSuitable(VkPhysicalDevice device);
uint32_t findMemoryType(uint32_t type_filter, uint32_t properties);
void updateUniforms();
void setupProjectionMatrix(float* matrix);
};
} // namespace vibe4
#endif // _WIN32 || __linux__