forked from obsproject/obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgl-helpers.c
More file actions
167 lines (136 loc) · 4.67 KB
/
Copy pathgl-helpers.c
File metadata and controls
167 lines (136 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/******************************************************************************
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
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, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "gl-subsystem.h"
bool gl_init_face(GLenum target, GLenum type, uint32_t num_levels,
GLenum format, GLint internal_format, bool compressed,
uint32_t width, uint32_t height, uint32_t size,
const uint8_t ***p_data)
{
bool success = true;
const uint8_t **data = p_data ? *p_data : NULL;
uint32_t i;
for (i = 0; i < num_levels; i++) {
if (compressed) {
glCompressedTexImage2D(target, i, internal_format,
width, height, 0, size,
data ? *data : NULL);
if (!gl_success("glCompressedTexImage2D"))
success = false;
} else {
glTexImage2D(target, i, internal_format, width, height,
0, format, type, data ? *data : NULL);
if (!gl_success("glTexImage2D"))
success = false;
}
if (data)
data++;
size /= 4;
if (width > 1)
width /= 2;
if (height > 1)
height /= 2;
}
if (data)
*p_data = data;
return success;
}
static bool gl_copy_fbo(struct gs_texture *dst, uint32_t dst_x, uint32_t dst_y,
struct gs_texture *src, uint32_t src_x, uint32_t src_y,
uint32_t width, uint32_t height)
{
struct fbo_info *fbo = get_fbo(src, width, height);
GLint last_fbo;
bool success = false;
if (!fbo)
return false;
if (!gl_get_integer_v(GL_READ_FRAMEBUFFER_BINDING, &last_fbo))
return false;
if (!gl_bind_framebuffer(GL_READ_FRAMEBUFFER, fbo->fbo))
return false;
if (!gl_bind_texture(dst->gl_target, dst->texture))
goto fail;
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0,
src->gl_target, src->texture, 0);
if (!gl_success("glFrameBufferTexture2D"))
goto fail;
glReadBuffer(GL_COLOR_ATTACHMENT0 + 0);
if (!gl_success("glReadBuffer"))
goto fail;
glCopyTexSubImage2D(dst->gl_target, 0, dst_x, dst_y, src_x, src_y,
width, height);
if (!gl_success("glCopyTexSubImage2D"))
goto fail;
success = true;
fail:
if (!gl_bind_texture(dst->gl_target, 0))
success = false;
if (!gl_bind_framebuffer(GL_READ_FRAMEBUFFER, last_fbo))
success = false;
return success;
}
bool gl_copy_texture(struct gs_device *device, struct gs_texture *dst,
uint32_t dst_x, uint32_t dst_y, struct gs_texture *src,
uint32_t src_x, uint32_t src_y, uint32_t width,
uint32_t height)
{
bool success = false;
if (device->copy_type == COPY_TYPE_ARB) {
glCopyImageSubData(src->texture, src->gl_target, 0, src_x,
src_y, 0, dst->texture, dst->gl_target, 0,
dst_x, dst_y, 0, width, height, 1);
success = gl_success("glCopyImageSubData");
} else if (device->copy_type == COPY_TYPE_NV) {
glCopyImageSubDataNV(src->texture, src->gl_target, 0, src_x,
src_y, 0, dst->texture, dst->gl_target, 0,
dst_x, dst_y, 0, width, height, 1);
success = gl_success("glCopyImageSubDataNV");
} else if (device->copy_type == COPY_TYPE_FBO_BLIT) {
success = gl_copy_fbo(dst, dst_x, dst_y, src, src_x, src_y,
width, height);
if (!success)
blog(LOG_ERROR, "gl_copy_texture failed");
}
return success;
}
bool gl_create_buffer(GLenum target, GLuint *buffer, GLsizeiptr size,
const GLvoid *data, GLenum usage)
{
bool success;
if (!gl_gen_buffers(1, buffer))
return false;
if (!gl_bind_buffer(target, *buffer))
return false;
glBufferData(target, size, data, usage);
success = gl_success("glBufferData");
gl_bind_buffer(target, 0);
return success;
}
bool update_buffer(GLenum target, GLuint buffer, const void *data, size_t size)
{
void *ptr;
bool success = true;
if (!gl_bind_buffer(target, buffer))
return false;
/* glMapBufferRange with these flags will actually give far better
* performance than a plain glMapBuffer call */
ptr = glMapBufferRange(target, 0, size,
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
success = gl_success("glMapBufferRange");
if (success && ptr) {
memcpy(ptr, data, size);
glUnmapBuffer(target);
}
gl_bind_buffer(target, 0);
return success;
}