1 | /* gskglbufferprivate.h |
2 | * |
3 | * Copyright 2020 Christian Hergert <chergert@redhat.com> |
4 | * |
5 | * This file is free software; you can redistribute it and/or modify it under |
6 | * the terms of the GNU Lesser General Public License as published by the Free |
7 | * Software Foundation; either version 2.1 of the License, or (at your option) |
8 | * any later version. |
9 | * |
10 | * This file is distributed in the hope that it will be useful, but WITHOUT |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
13 | * License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | * |
18 | * SPDX-License-Identifier: LGPL-2.1-or-later |
19 | */ |
20 | |
21 | #include "config.h" |
22 | |
23 | #include <string.h> |
24 | |
25 | #include "gskglbufferprivate.h" |
26 | |
27 | /** |
28 | * gsk_gl_buffer_init: |
29 | * @target: the target buffer such as %GL_ARRAY_BUFFER or %GL_UNIFORM_BUFFER |
30 | * @element_size: the size of elements within the buffer |
31 | * |
32 | * Creates a new `GskGLBuffer` which can be used to deliver data to shaders |
33 | * within a GLSL program. You can use this to store vertices such as with |
34 | * %GL_ARRAY_BUFFER or uniform data with %GL_UNIFORM_BUFFER. |
35 | */ |
36 | void |
37 | gsk_gl_buffer_init (GskGLBuffer *self, |
38 | GLenum target, |
39 | guint element_size) |
40 | { |
41 | memset (s: self, c: 0, n: sizeof *self); |
42 | |
43 | /* Default to 2 pages, power-of-two growth from there */ |
44 | self->buffer_len = 4096 * 2; |
45 | self->buffer = g_malloc (n_bytes: self->buffer_len); |
46 | self->target = target; |
47 | self->element_size = element_size; |
48 | } |
49 | |
50 | GLuint |
51 | gsk_gl_buffer_submit (GskGLBuffer *buffer) |
52 | { |
53 | GLuint id; |
54 | |
55 | glGenBuffers (1, &id); |
56 | glBindBuffer (buffer->target, id); |
57 | glBufferData (buffer->target, buffer->buffer_pos, buffer->buffer, GL_STATIC_DRAW); |
58 | |
59 | buffer->buffer_pos = 0; |
60 | buffer->count = 0; |
61 | |
62 | return id; |
63 | } |
64 | |
65 | void |
66 | gsk_gl_buffer_destroy (GskGLBuffer *buffer) |
67 | { |
68 | g_clear_pointer (&buffer->buffer, g_free); |
69 | } |
70 | |