#include "renderer.h"
#include "backend_api.h"

#include <runtime/runtime.h>
#include <runtime/config.h>

extern rt_cvar r_MaxRenderResources;
extern rt_cvar r_PerFrameCommandListMemory;

RT_DLLEXPORT void rtRegisterRenderCVARs(void) {
    rtRegisterCVAR(&r_MaxRenderResources);
    rtRegisterCVAR(&r_PerFrameCommandListMemory);
}

RT_DLLEXPORT void rtRegisterRenderBackendCVARs(void) {
    g_render_backend.RegisterCVARs();    
}

rt_render_device_i g_device_i;

extern rt_result InitVirtualResourceRegistry(void);
extern void ShutdownVirtualResourceRegistry(void);
extern rt_result InitCommandLists(void);
extern void ShutdownCommandLists(void);

RT_DLLEXPORT rt_result rtInitRenderer(const rt_renderer_window_info *info) {
    rt_render_backend_init_result backend_res = g_render_backend.Init(info);
    if (backend_res.result != RT_SUCCESS)
        return backend_res.result;
    g_device_i = backend_res.device;
    
    rt_result res;
    if ((res = InitVirtualResourceRegistry()) != RT_SUCCESS) {
        g_render_backend.Shutdown();
        return res;
    }
    if ((res = InitCommandLists()) != RT_SUCCESS) {
        ShutdownVirtualResourceRegistry();
        g_render_backend.Shutdown();
        return res;
    }
    return res;
}

RT_DLLEXPORT void rtShutdownRenderer(void) {
    ShutdownCommandLists();
    ShutdownVirtualResourceRegistry();
    g_render_backend.Shutdown();
}
