| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * 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 the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is Copyright (C) 2016 by Mike Erwin. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup gpu | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  |  * GPU vertex buffer | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | #ifndef __GPU_VERTEX_BUFFER_H__
 | 
					
						
							|  |  |  | #define __GPU_VERTEX_BUFFER_H__
 | 
					
						
							| 
									
										
										
										
											2016-09-13 02:41:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | #include "GPU_vertex_format.h"
 | 
					
						
							| 
									
										
										
										
											2016-09-13 02:41:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-02 15:28:47 +01:00
										 |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | extern "C" { | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 17:59:48 +01:00
										 |  |  | #define VRAM_USAGE 1
 | 
					
						
							| 
									
										
										
										
											2019-01-29 08:40:46 +11:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * How to create a #GPUVertBuf: | 
					
						
							|  |  |  |  * 1) verts = GPU_vertbuf_create() or GPU_vertbuf_init(verts) | 
					
						
							|  |  |  |  * 2) GPU_vertformat_attr_add(verts->format, ...) | 
					
						
							|  |  |  |  * 3) GPU_vertbuf_data_alloc(verts, vertex_len) <-- finalizes/packs vertex format | 
					
						
							|  |  |  |  * 4) GPU_vertbuf_attr_fill(verts, pos, application_pos_buffer) | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-09-14 16:28:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | /* Is GPUVertBuf always used as part of a GPUBatch? */ | 
					
						
							| 
									
										
										
										
											2016-09-14 16:28:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-17 16:58:43 +01:00
										 |  |  | typedef enum { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   /* can be extended to support more types */ | 
					
						
							|  |  |  |   GPU_USAGE_STREAM, | 
					
						
							|  |  |  |   GPU_USAGE_STATIC, /* do not keep data in memory */ | 
					
						
							|  |  |  |   GPU_USAGE_DYNAMIC, | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | } GPUUsageType; | 
					
						
							| 
									
										
										
										
											2018-03-17 16:58:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | typedef struct GPUVertBuf { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   GPUVertFormat format; | 
					
						
							| 
									
										
										
										
											2019-05-13 23:31:43 +02:00
										 |  |  |   /** Number of verts we want to draw. */ | 
					
						
							|  |  |  |   uint vertex_len; | 
					
						
							|  |  |  |   /** Number of verts data. */ | 
					
						
							|  |  |  |   uint vertex_alloc; | 
					
						
							|  |  |  |   /** 0 indicates not yet allocated. */ | 
					
						
							|  |  |  |   uint32_t vbo_id; | 
					
						
							|  |  |  |   /** Usage hint for GL optimisation. */ | 
					
						
							| 
									
										
										
										
											2019-05-14 16:31:04 +02:00
										 |  |  |   uint usage : 2; | 
					
						
							| 
									
										
										
										
											2019-05-13 23:31:43 +02:00
										 |  |  |   /** Data has been touched and need to be reuploaded to GPU. */ | 
					
						
							|  |  |  |   uint dirty : 1; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   unsigned char *data; /* NULL indicates data in VRAM (unmapped) */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | } GPUVertBuf; | 
					
						
							| 
									
										
										
										
											2016-09-13 02:41:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | GPUVertBuf *GPU_vertbuf_create(GPUUsageType); | 
					
						
							|  |  |  | GPUVertBuf *GPU_vertbuf_create_with_format_ex(const GPUVertFormat *, GPUUsageType); | 
					
						
							| 
									
										
										
										
											2018-03-17 16:58:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | #define GPU_vertbuf_create_with_format(format) \
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   GPU_vertbuf_create_with_format_ex(format, GPU_USAGE_STATIC) | 
					
						
							| 
									
										
										
										
											2016-09-14 16:28:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-13 17:27:35 +02:00
										 |  |  | void GPU_vertbuf_clear(GPUVertBuf *verts); | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_vertbuf_discard(GPUVertBuf *); | 
					
						
							| 
									
										
										
										
											2016-10-23 23:16:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_vertbuf_init(GPUVertBuf *, GPUUsageType); | 
					
						
							|  |  |  | void GPU_vertbuf_init_with_format_ex(GPUVertBuf *, const GPUVertFormat *, GPUUsageType); | 
					
						
							| 
									
										
										
										
											2018-03-17 16:58:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | #define GPU_vertbuf_init_with_format(verts, format) \
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   GPU_vertbuf_init_with_format_ex(verts, format, GPU_USAGE_STATIC) | 
					
						
							| 
									
										
										
										
											2016-09-14 16:28:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | uint GPU_vertbuf_size_get(const GPUVertBuf *); | 
					
						
							|  |  |  | void GPU_vertbuf_data_alloc(GPUVertBuf *, uint v_len); | 
					
						
							|  |  |  | void GPU_vertbuf_data_resize(GPUVertBuf *, uint v_len); | 
					
						
							| 
									
										
										
										
											2019-01-21 11:28:41 +11:00
										 |  |  | void GPU_vertbuf_data_len_set(GPUVertBuf *, uint v_len); | 
					
						
							| 
									
										
										
										
											2016-09-13 02:41:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-29 07:46:25 +11:00
										 |  |  | /* The most important #set_attr variant is the untyped one. Get it right first.
 | 
					
						
							|  |  |  |  * It takes a void* so the app developer is responsible for matching their app data types | 
					
						
							|  |  |  |  * to the vertex attribute's type and component count. They're in control of both, so this | 
					
						
							|  |  |  |  * should not be a problem. */ | 
					
						
							| 
									
										
										
										
											2016-09-13 02:41:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_vertbuf_attr_set(GPUVertBuf *, uint a_idx, uint v_idx, const void *data); | 
					
						
							| 
									
										
										
										
											2019-08-14 23:29:46 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-02 01:40:58 +01:00
										 |  |  | void GPU_vertbuf_vert_set(GPUVertBuf *verts, uint v_idx, const void *data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 23:29:46 +10:00
										 |  |  | /* Tightly packed, non interleaved input data. */ | 
					
						
							|  |  |  | void GPU_vertbuf_attr_fill(GPUVertBuf *, uint a_idx, const void *data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_vertbuf_attr_fill_stride(GPUVertBuf *, uint a_idx, uint stride, const void *data); | 
					
						
							| 
									
										
										
										
											2016-09-13 02:41:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | /* For low level access only */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | typedef struct GPUVertBufRaw { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   uint size; | 
					
						
							|  |  |  |   uint stride; | 
					
						
							|  |  |  |   unsigned char *data; | 
					
						
							|  |  |  |   unsigned char *data_init; | 
					
						
							| 
									
										
										
										
											2017-06-29 20:09:05 +10:00
										 |  |  | #if TRUST_NO_ONE
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   /* Only for overflow check */ | 
					
						
							|  |  |  |   unsigned char *_data_end; | 
					
						
							| 
									
										
										
										
											2017-06-29 20:09:05 +10:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | } GPUVertBufRaw; | 
					
						
							| 
									
										
										
										
											2017-06-29 20:09:05 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | GPU_INLINE void *GPU_vertbuf_raw_step(GPUVertBufRaw *a) | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   unsigned char *data = a->data; | 
					
						
							|  |  |  |   a->data += a->stride; | 
					
						
							| 
									
										
										
										
											2017-06-29 20:09:05 +10:00
										 |  |  | #if TRUST_NO_ONE
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   assert(data < a->_data_end); | 
					
						
							| 
									
										
										
										
											2017-06-29 20:09:05 +10:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   return (void *)data; | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-06-29 20:09:05 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | GPU_INLINE uint GPU_vertbuf_raw_used(GPUVertBufRaw *a) | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   return ((a->data - a->data_init) / a->stride); | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-14 16:28:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_vertbuf_attr_get_raw_data(GPUVertBuf *, uint a_idx, GPUVertBufRaw *access); | 
					
						
							| 
									
										
										
										
											2017-04-13 13:30:53 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_vertbuf_use(GPUVertBuf *); | 
					
						
							| 
									
										
										
										
											2017-05-15 13:05:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | /* Metrics */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | uint GPU_vertbuf_get_memory_usage(void); | 
					
						
							| 
									
										
										
										
											2017-04-13 13:30:53 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | /* Macros */ | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | #define GPU_VERTBUF_DISCARD_SAFE(verts) \
 | 
					
						
							|  |  |  |   do { \ | 
					
						
							|  |  |  |     if (verts != NULL) { \ | 
					
						
							|  |  |  |       GPU_vertbuf_discard(verts); \ | 
					
						
							|  |  |  |       verts = NULL; \ | 
					
						
							|  |  |  |     } \ | 
					
						
							|  |  |  |   } while (0) | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-02 15:28:47 +01:00
										 |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | #endif /* __GPU_VERTEX_BUFFER_H__ */
 |