summaryrefslogtreecommitdiff
path: root/es_2_0/internal.c
blob: 65647d1680fa26300fa39146a57bcbd76a2f13e1 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* 
 * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 * 
 * Contact:
 * DongKyun Yun <dk77.yun@samsung.com>
 * SangJin Kim <sangjin3.kim@samsung.com>
 * HyunGoo Kang <hyungoo1.kang@samsung.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * Contributors:
 * - S-Core Co., Ltd
 *
 */

#include "es2front.h"

#if ! defined(WIN32)
#include <dlfcn.h>
#endif
#include <malloc.h>

static struct es2Context s_defaultContext = {
	GL_FALSE,	
	0,	
	GL_NO_ERROR,	
	NULL, 0, 0,
	NULL, 0,
	NULL, 0,
	NULL, 0,
	(void*)(&(ES2INTER(NotBound))),
};

static struct es2Context s_context = {
	GL_FALSE,	
	0,	
	GL_NO_ERROR,	
	NULL, 0, 0,
	NULL, 0,
	NULL, 0,
	NULL, 0,
	(void*)(&(ES2INTER(NotBound))),
};

struct es2Context* ES2INTER(curContext) = &s_context;

void GL_APIENTRY ES2INTER(NotBound)(void) {
	fprintf(stderr, "ES2 internal error: you are calling not-bound function\n");
}

void GL_APIENTRY ES2INTER(ResetFnptrs)(struct es2Context* pContext) {
#define	ACTION(name) do { \
		*(void**)(&pContext->fp##name) = (void*)(&(ES2INTER(NotBound))); \
	} while (0)
#include "funcaction.inl"
#undef ACTION
}

void GL_APIENTRY ES2INTER(BindFnptrs)(struct es2Context* pContext) {
#if ! defined(WIN32)
	void* dl;
	if ((dl = dlopen("libGL.so", RTLD_NOW | RTLD_GLOBAL)) == 0) {
		ES2INTER(SetError)(GL_INVALID_OPERATION);
		return;
	}
#define	ACTION(name) do { \
		*(void**)(&pContext->fp##name) = dlsym(dl, "gl" #name ); \
		if (pContext->fp##name == NULL) { \
			*(void**)(&pContext->fp##name) = dlsym(dl, "gl" #name "EXT"); \
			assert(pContext->fp##name != NULL) ; \
			if (pContext->fp##name == NULL) { \
				*(void**)(&pContext->fp##name) = (void*)(&(ES2INTER(NotBound))); \
			} \
		} \
	} while (0)
#include "funcaction.inl"
#undef ACTION
	dlclose(dl);
#endif
}


void* GL_APIENTRY EGLCROSS(CreateContext)(void) {
	struct es2Context* pContext = malloc(sizeof(struct es2Context));
	if (pContext == NULL) {
		ES2INTER(SetError)(GL_OUT_OF_MEMORY);
		return NULL;
	}
	memcpy(pContext, &s_defaultContext, sizeof(struct es2Context));
	ES2INTER(ResetFnptrs)(pContext);
	return pContext;
}

void GL_APIENTRY EGLCROSS(MakeCurrent)(void* ptr) {
	int i;
	struct es2Context* pContext = (struct es2Context*)ptr;
	if (ptr == NULL) {
		ES2INTER(curContext) = &s_context;
		return;
	}
	ES2INTER(BindFnptrs)(pContext);
	ES2INTER(curContext) = pContext;
	pContext->bShaderCompiler = GL_TRUE; 
	FNPTR(GetIntegerv)(GL_MAX_VERTEX_ATTRIBS, &pContext->nMaxVertexAttribs);
	assert(8 <= pContext->nMaxVertexAttribs); 
	if (pContext->pVertexAttrib == NULL) {
		pContext->pVertexAttrib = calloc(pContext->nMaxVertexAttribs,
		        sizeof(struct VertexAttribUnit));
		if (pContext->pVertexAttrib == NULL) {
			ES2INTER(SetError)(GL_OUT_OF_MEMORY);
			return;
		}
		for (i = 0; i < pContext->nMaxVertexAttribs; i++) {
			ES2INTER(VertexAttribInit)(&(pContext->pVertexAttrib[i]));
		}
		pContext->uArrayBufferBinding = 0;
		pContext->uElementArrayBufferBinding = 0;
	}
	if (pContext->pBufferObject == NULL) {
		pContext->pBufferObject = calloc(INTERNAL_NUM_BUFFER_OBJECTS,
		        sizeof(struct BufferObjectUnit));
		if (pContext->pBufferObject == NULL) {
			ES2INTER(SetError)(GL_OUT_OF_MEMORY);
			return;
		}
		pContext->nBufferObjectAllocated = INTERNAL_NUM_BUFFER_OBJECTS;
		for (i = 0; i < pContext->nBufferObjectAllocated; i++) {
			ES2INTER(BufferObjectInit)(&(pContext->pBufferObject[i]));
		}
		pContext->pBufferObject[0].bGenerated = GL_TRUE;
	}
	if (pContext->pShaderObject == NULL) {
		pContext->pShaderObject = calloc(INTERNAL_NUM_SHADER_OBJECTS,
		        sizeof(struct ShaderObjectUnit));
		if (pContext->pShaderObject == NULL) {
			ES2INTER(SetError)(GL_OUT_OF_MEMORY);
			return;
		}
		pContext->nShaderObjectAllocated = INTERNAL_NUM_SHADER_OBJECTS;
		for (i = 0; i < pContext->nShaderObjectAllocated; i++) {
			ES2INTER(ShaderObjectInit)(&(pContext->pShaderObject[i]));
		}
	}
	if (pContext->pProgramObject == NULL) {
		pContext->pProgramObject = calloc(INTERNAL_NUM_PROGRAM_OBJECTS,
		        sizeof(struct ProgramObjectUnit));
		if (pContext->pProgramObject == NULL) {
			ES2INTER(SetError)(GL_OUT_OF_MEMORY);
			return;
		}
		pContext->nProgramObjectAllocated = INTERNAL_NUM_PROGRAM_OBJECTS;
		for (i = 0; i < pContext->nProgramObjectAllocated; i++) {
			ES2INTER(ProgramObjectInit)(&(pContext->pProgramObject[i]));
		}
	}
	/* from the sepc.,
	The values in gl_PointCoord are two-dimensional coordinates indicating
	where within a point primitive the current fragment is located, when point
	sprites are enabled. They range from 0.0 to 1.0 across the point. If the
	current primitive is not a point, or if point sprites are not enabled,
	then the values read from gl_PointCoord are undefined.
	*/
#if ! defined(GL_POINT_SPRITE)
#define GL_POINT_SPRITE                   0x8861
#endif
#if ! defined(GL_VERTEX_PROGRAM_POINT_SIZE)
#define GL_VERTEX_PROGRAM_POINT_SIZE      0x8642
#endif
	FNPTR(Enable)(GL_POINT_SPRITE);
	FNPTR(Enable)(GL_VERTEX_PROGRAM_POINT_SIZE);
}

void GL_APIENTRY EGLCROSS(ReleaseContext)(void* ptr) {
	int i;
	struct es2Context* pContext = (struct es2Context*)ptr;
	if (ptr == NULL) return;
	if (pContext == &s_context) return;
	if (pContext->pVertexAttrib != NULL) {
		for (i = 0; i < pContext->nMaxVertexAttribs; i++) {
			ES2INTER(VertexAttribRelease)(&(pContext->pVertexAttrib[i]));
		}
		free(pContext->pVertexAttrib);
		pContext->pVertexAttrib = NULL;
	}
	if (pContext->pBufferObject == NULL) {
		for (i = 0; i < pContext->nBufferObjectAllocated; i++) {
			ES2INTER(BufferObjectRelease)(&(pContext->pBufferObject[i]));
		}
		free(pContext->pBufferObject);
		pContext->pBufferObject = NULL;
	}
	if (pContext->pShaderObject == NULL) {
		for (i = 0; i < pContext->nShaderObjectAllocated; i++) {
			ES2INTER(ShaderObjectRelease)(&(pContext->pShaderObject[i]));
		}
		free(pContext->pShaderObject);
		pContext->pShaderObject = NULL;
	}
	if (pContext->pProgramObject == NULL) {
		for (i = 0; i < pContext->nProgramObjectAllocated; i++) {
			ES2INTER(ProgramObjectRelease)(&(pContext->pProgramObject[i]));
		}
		free(pContext->pProgramObject);
		pContext->pProgramObject = NULL;
	}
	free(pContext);
	if (pContext == ES2INTER(curContext)) {
		ES2INTER(curContext) = &s_context;
	}
}