/*
	qf_bsp.h

	Vulkan specific brush model stuff

	Copyright (C) 2012 Bill Currie <bill@taniwha.org>
	Copyright (C) 2021 Bill Currie <bill@taniwha.org>

	Author: Bill Currie <bill@taniwha.org>
	Date: 2012/1/7
	Date: 2021/1/18

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	See the GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to:

		Free Software Foundation, Inc.
		59 Temple Place - Suite 330
		Boston, MA  02111-1307, USA

*/
#ifndef __QF_Vulkan_qf_bsp_h
#define __QF_Vulkan_qf_bsp_h

#include "QF/darray.h"
#include "QF/model.h"
#include "QF/Vulkan/qf_vid.h"
#include "QF/Vulkan/command.h"

#include "QF/simd/types.h"

typedef struct vulktex_s {
	struct texture_s  *texture;
	struct instsurf_s *tex_chain;	// for gl_texsort drawing
	struct instsurf_s **tex_chain_tail;
	struct elechain_s *elechain;
	struct elechain_s **elechain_tail;
	struct qfv_tex_s *tex;
	VkDescriptorSet descriptor;
} vulktex_t;

typedef struct bspvert_s {
	quat_t      vertex;
	quat_t      tlst;
} bspvert_t;

typedef struct elements_s {
	struct elements_s *_next;
	struct elements_s *next;
	uint32_t    first_index;
	uint32_t    index_count;
} elements_t;

typedef struct elechain_s {
	struct elechain_s *_next;
	struct elechain_s *next;
	elements_t *elements;
	vec_t      *transform;
	float      *color;
} elechain_t;

typedef enum {
	qfv_bsp_texture,
	qfv_bsp_glowmap,
	qfv_bsp_lightmap,
	qfv_bsp_skysheet,
	qfv_bsp_skycube,
} qfv_bsp_tex;

typedef enum {
	QFV_bspDepth,
	QFV_bspGBuffer,
	QFV_bspSky,
	QFV_bspTurb,

	QFV_bspNumPasses
} QFV_BspSubpass;

typedef struct bspframe_s {
	uint32_t    *index_data;	// pointer into mega-buffer for this frame (c)
	uint32_t     index_offset;	// offset of index_data within mega-buffer (c)
	uint32_t     index_count;	// number if indices queued (d)
	qfv_cmdbufferset_t cmdSet;
} bspframe_t;

typedef struct bspframeset_s
    DARRAY_TYPE (bspframe_t) bspframeset_t;

typedef struct texchainset_s
    DARRAY_TYPE (vulktex_t *) texchainset_t;

typedef struct bspctx_s {
	struct entity_s *entity;
	vec_t       *transform;
	float       *color;

	instsurf_t  *waterchain;
	instsurf_t **waterchain_tail;
	instsurf_t  *sky_chain;
	instsurf_t **sky_chain_tail;

	texchainset_t texture_chains;

	// for world and non-instance models
	instsurf_t  *static_instsurfs;
	instsurf_t **static_instsurfs_tail;
	instsurf_t  *free_static_instsurfs;

	// for instance models
	elechain_t  *elechains;
	elechain_t **elechains_tail;
	elechain_t  *free_elechains;
	elements_t  *elementss;
	elements_t **elementss_tail;
	elements_t  *free_elementss;
	instsurf_t  *instsurfs;
	instsurf_t **instsurfs_tail;
	instsurf_t  *free_instsurfs;

	struct qfv_tex_s *default_skysheet;
	struct qfv_tex_s *skysheet_tex;

	struct qfv_tex_s *default_skybox;
	struct qfv_tex_s *skybox_tex;
	VkDescriptorSet skybox_descriptor;

	quat_t       default_color;
	quat_t       last_color;

	struct scrap_s *light_scrap;
	struct qfv_stagebuf_s *light_stage;

	struct bsppoly_s *polys;

	VkSampler    sampler;
	VkPipelineLayout layout;

	VkDeviceMemory texture_memory;
	VkPipeline   depth;
	VkPipeline   gbuf;
	VkPipeline   skysheet;
	VkPipeline   skybox;
	VkPipeline   turb;
	size_t       vertex_buffer_size;
	size_t       index_buffer_size;
	VkBuffer     vertex_buffer;
	VkDeviceMemory vertex_memory;
	VkBuffer     index_buffer;
	VkDeviceMemory index_memory;
	bspframeset_t frames;
} bspctx_t;

struct vulkan_ctx_s;
struct qfv_renderframe_s;
void Vulkan_ClearElements (struct vulkan_ctx_s *ctx);
void Vulkan_DrawWorld (struct qfv_renderframe_s *rFrame);
void Vulkan_DrawSky (struct qfv_renderframe_s *rFrame);
void Vulkan_DrawWaterSurfaces (struct qfv_renderframe_s *rFrame);
void Vulkan_Bsp_Flush (struct vulkan_ctx_s *ctx);
void Vulkan_LoadSkys (const char *sky, struct vulkan_ctx_s *ctx);
void Vulkan_RegisterTextures (model_t **models, int num_models,
							  struct vulkan_ctx_s *ctx);
void Vulkan_BuildDisplayLists (model_t **models, int num_models,
							   struct vulkan_ctx_s *ctx);
void Vulkan_Bsp_Init (struct vulkan_ctx_s *ctx);
void Vulkan_Bsp_Shutdown (struct vulkan_ctx_s *ctx);

#endif//__QF_Vulkan_qf_bsp_h
