summaryrefslogtreecommitdiff
path: root/macosx/plugins/DFXVideo/macsrc/GL3Code.m
blob: bc4ea3e0e9887bba98765d2da57b41ffa3afc599 (plain) (blame)
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
//
//  GL3Code.m
//  Pcsxr
//
//  Created by C.W. Betts on 11/26/13.
//
//

#import <Cocoa/Cocoa.h>
#include <OpenGL/gl3.h>
#include <OpenGL/gl3ext.h>
#import <GLKit/GLKit.h>
#import "PluginGLView.h"
#import "SGPUPreferences.h"
#include "externals.h"
#undef BOOL
#include "gpu.h"
#include "swap.h"

static int mylog2(int val)
{
	int i;
	for (i=1; i<31; i++)
		if (val <= (1 << i))
			return (1 << i);
	
	return -1;
}

@implementation PluginGLView (GL3)

- (BOOL)setupOpenGL3
{
	static const NSOpenGLPixelFormatAttribute attrs[] =
	{
		NSOpenGLPFAAccelerated,
		NSOpenGLPFANoRecovery,
		NSOpenGLPFADoubleBuffer,
		NSOpenGLPFAOpenGLProfile,
		NSOpenGLProfileVersion3_2Core,
		0
	};
	
	// Get pixel format from OpenGL
	NSOpenGLPixelFormat* pixFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
	if (!pixFmt)
		return NO;
	
	[self setPixelFormat:pixFmt];

	return NO;
}

- (void)cleanupGL3
{
	
}

- (void)reshapeGL3
{
	NSOpenGLContext *oglContext = [self openGLContext];
	NSRect rect;
	
	[oglContext makeCurrentContext];
	[oglContext update];
	
	rect = [[oglContext view] bounds];
	
	glViewport(0, 0, (int) rect.size.width, (int) rect.size.height);
	
	//glMatrixMode(GL_PROJECTION);
	//glLoadIdentity();
	
	//glMatrixMode(GL_MODELVIEW);
	//glLoadIdentity();
	
	drawBG = YES;
	
	[NSOpenGLContext clearCurrentContext];
}

- (void)renderScreenGL3
{
	NSRect rect = [[[self openGLContext] view] bounds];

	GLKMatrix4 matrix = GLKMatrix4MakeOrtho(0, rect.size.width, 0, rect.size.height, 0, 0);
}

- (void)loadTexturesGL3:(GLboolean)first
{
	
}

- (void)swapBufferGL3
{
	
}

- (GLuint)loadShader:(GLenum)type location:(NSURL*)filename
{
	GLuint myShader = 0;
	
	GLsizei logsize = 0;
	GLint compile_status = GL_TRUE;
	char *log = NULL;
	char *src = NULL;
	
	/* creation d'un shader de sommet */
	myShader = glCreateShader(type);
	if(myShader == 0)
	{
		fprintf(stderr, "impossible de creer le shader\n");
		return 0;
	}
	
	/* chargement du code source */
	src = [self loadSource:filename];
	if(src == NULL)
	{
		/* theoriquement, la fonction LoadSource a deja affiche un message
		 d'erreur, nous nous contenterons de supprimer notre shader
		 et de retourner 0 */
		
		glDeleteShader(myShader);
		return 0;
	}
	
	/* assignation du code source */
	glShaderSource(myShader, 1, (const GLchar**)&src, NULL);
	
	/* compilation du shader */
	glCompileShader(myShader);
	
	/* liberation de la memoire du code source */
	free(src);
	src = NULL;
	
	/* verification du succes de la compilation */
	glGetShaderiv(myShader, GL_COMPILE_STATUS, &compile_status);
	if(compile_status != GL_TRUE)
	{
		/* erreur a la compilation recuperation du log d'erreur */
		
		/* on recupere la taille du message d'erreur */
		glGetShaderiv(myShader, GL_INFO_LOG_LENGTH, &logsize);
		
		/* on alloue un espace memoire dans lequel OpenGL ecrira le message */
		log = malloc(logsize + 1);
		if(log == NULL)
		{
			fprintf(stderr, "impossible d'allouer de la memoire !\n");
			return 0;
		}
		/* initialisation du contenu */
		memset(log, '\0', logsize + 1);
		
		glGetShaderInfoLog(myShader, logsize, &logsize, log);
		fprintf(stderr, "impossible de compiler le shader '%s' :\n%s",
				[[filename path] UTF8String], log);
		
		/* ne pas oublier de liberer la memoire et notre shader */
		free(log);
		glDeleteShader(myShader);
		
		return 0;
	}
    return myShader;
}

@end

void printProgramInfoLog(GLuint obj)
{
	int infologLength = 0;
	int charsWritten  = 0;
	char *infoLog;
	
	glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &infologLength);
	
	if (infologLength > 0)
	{
		infoLog = (char *)malloc(infologLength);
		glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog);
		printf("%s\n",infoLog);
		free(infoLog);
	}
}