-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgl-framebuffer.c
181 lines (133 loc) · 5.07 KB
/
gl-framebuffer.c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* KallistiGL for KallistiOS ##version##
libgl/gl-framebuffer.c
Copyright (C) 2014 Josh Pearson
This file implements Open GL Frame Buffer Object (FBO) functionality, with what
the DC's PVR can directly implement.
Basically, Render-To-Texture using GL_RGB565 is the only native feature of the
PVR, so if you are looking for a depth-buffer, bad news.
This implementation uses a dynamic linked list to implement the data structures needed.
*/
#include "gl.h"
#include "glext.h"
#include "gl-api.h"
#include <malloc.h>
//========================================================================================//
//== Internal KOS Open GL API FBO Structures / Global Variables ==//
static GL_FRAMEBUFFER_OBJECT *FRAMEBUF_OBJ = NULL;
static GLsizei FRAMEBUF_OBJECT = 0;
//========================================================================================//
//== Internal KOS Open GL API FBO Functionality ==//
void _glKosInitFrameBuffers() {
FRAMEBUF_OBJ = malloc(sizeof(GL_FRAMEBUFFER_OBJECT));
FRAMEBUF_OBJ->index = 0;
FRAMEBUF_OBJ->texID = 0;
FRAMEBUF_OBJ->data = NULL;
FRAMEBUF_OBJ->link = NULL;
}
static void _glKosInsertFramebufferObj(GL_FRAMEBUFFER_OBJECT *obj) {
GL_FRAMEBUFFER_OBJECT *ptr = FRAMEBUF_OBJ;
while(ptr->link != NULL)
ptr = (GL_FRAMEBUFFER_OBJECT *)ptr->link;
ptr->link = obj;
}
static GLsizei _glKosGetLastFrameBufferIndex() {
GL_FRAMEBUFFER_OBJECT *ptr = FRAMEBUF_OBJ;
while(ptr->link != NULL)
ptr = (GL_FRAMEBUFFER_OBJECT *)ptr->link;
return ptr->index;
}
static GL_FRAMEBUFFER_OBJECT *_glKosGetFrameBufferObj(GLuint index) {
GL_FRAMEBUFFER_OBJECT *ptr = FRAMEBUF_OBJ;
while(ptr->index != index && ptr->link != NULL)
ptr = (GL_FRAMEBUFFER_OBJECT *)ptr->link;
return ptr;
}
GLsizei _glKosGetFBO() {
return FRAMEBUF_OBJECT;
}
GLuint _glKosGetFBOWidth(GLsizei fbi) {
GL_FRAMEBUFFER_OBJECT *fbo = _glKosGetFrameBufferObj(fbi);
return _glKosTextureWidth(fbo->texID);
}
GLuint _glKosGetFBOHeight(GLsizei fbi) {
GL_FRAMEBUFFER_OBJECT *fbo = _glKosGetFrameBufferObj(fbi);
return _glKosTextureHeight(fbo->texID);
}
GLvoid *_glKosGetFBOData(GLsizei fbi) {
GL_FRAMEBUFFER_OBJECT *fbo = _glKosGetFrameBufferObj(fbi);
return fbo->data;
}
//========================================================================================//
//== Public KOS Open GL API FBO Functionality ==//
GLAPI void APIENTRY glGenFramebuffers(GLsizei n, GLuint *framebuffers) {
GLsizei index = _glKosGetLastFrameBufferIndex();
while(n--) {
GL_FRAMEBUFFER_OBJECT *obj = malloc(sizeof(GL_FRAMEBUFFER_OBJECT));
obj->index = ++index;
obj->texID = 0;
obj->data = NULL;
obj->link = NULL;
_glKosInsertFramebufferObj(obj);
*framebuffers++ = obj->index;
}
}
GLAPI void APIENTRY glDeleteFramebuffers(GLsizei n, GLuint *framebuffers) {
while(n--) {
GL_FRAMEBUFFER_OBJECT *ptr = FRAMEBUF_OBJ->link, * lptr = FRAMEBUF_OBJ;
while(ptr != NULL) {
if(ptr->index == *framebuffers) {
GL_FRAMEBUFFER_OBJECT *cur_node = ptr;
lptr->link = ptr->link;
ptr = (GL_FRAMEBUFFER_OBJECT *)ptr->link;
free(cur_node);
if(*framebuffers == FRAMEBUF_OBJECT)
FRAMEBUF_OBJECT = 0;
break;
}
else
ptr = (GL_FRAMEBUFFER_OBJECT *)ptr->link;
}
++framebuffers;
}
}
GLAPI void APIENTRY glBindFramebuffer(GLenum target, GLuint framebuffer) {
if(target != GL_FRAMEBUFFER) {
_glKosThrowError(GL_INVALID_ENUM, "glBindFramebuffer");
_glKosPrintError();
return;
}
FRAMEBUF_OBJECT = framebuffer;
}
GLAPI void APIENTRY glFramebufferTexture2D(GLenum target, GLenum attachment,
GLenum textarget, GLuint texture, GLint level) {
if(target != GL_FRAMEBUFFER)
_glKosThrowError(GL_INVALID_ENUM, "glFramebufferTexture2D");
if(attachment != GL_COLOR_ATTACHMENT0)
_glKosThrowError(GL_INVALID_OPERATION, "glFramebufferTexture2D");
if(textarget != GL_TEXTURE_2D)
_glKosThrowError(GL_INVALID_OPERATION, "glFramebufferTexture2D");
if(level)
_glKosThrowError(GL_INVALID_ENUM, "glFramebufferTexture2D");
if(!FRAMEBUF_OBJECT)
_glKosThrowError(GL_INVALID_OPERATION, "glFramebufferTexture2D");
if(_glKosGetError()) {
_glKosPrintError();
return;
}
GL_FRAMEBUFFER_OBJECT *fbo = _glKosGetFrameBufferObj(FRAMEBUF_OBJECT);
fbo->texID = texture;
fbo->data = _glKosTextureData(texture);
}
GLAPI GLenum APIENTRY glCheckFramebufferStatus(GLenum target) {
if(target != GL_FRAMEBUFFER) {
_glKosThrowError(GL_INVALID_ENUM, "glCheckFramebufferStatus");
_glKosPrintError();
return 0;
}
if(!FRAMEBUF_OBJECT)
return GL_FRAMEBUFFER_COMPLETE;
GL_FRAMEBUFFER_OBJECT *fbo = _glKosGetFrameBufferObj(FRAMEBUF_OBJECT);
if(!fbo->texID)
return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
return GL_FRAMEBUFFER_COMPLETE;
}