Update ooo320-m1
[ooovba.git] / slideshow / source / engine / OGLTrans / OGLTrans_Shaders.cxx
bloba144790390d6194bf6ee7308506635f0056aeadc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: $
10 * $Revision: $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <osl/diagnose.hxx>
33 #include "OGLTrans_Shaders.hxx"
35 namespace unx
37 #include <GL/glx.h>
38 #include <GL/glxext.h>
41 bool OGLShaders::cbInitialized = false;
43 #ifdef GL_VERSION_2_0
45 PFNGLCREATESHADERPROC OGLShaders::glCreateShader = NULL;
46 PFNGLSHADERSOURCEPROC OGLShaders::glShaderSource = NULL;
47 PFNGLCOMPILESHADERPROC OGLShaders::glCompileShader = NULL;
48 PFNGLGETSHADERIVPROC OGLShaders::glGetShaderiv = NULL;
49 PFNGLGETSHADERINFOLOGPROC OGLShaders::glGetShaderInfoLog = NULL;
50 PFNGLDELETESHADERPROC OGLShaders::glDeleteShader = NULL;
51 PFNGLCREATEPROGRAMPROC OGLShaders::glCreateProgram = NULL;
52 PFNGLATTACHSHADERPROC OGLShaders::glAttachShader = NULL;
53 PFNGLLINKPROGRAMPROC OGLShaders::glLinkProgram = NULL;
54 PFNGLGETPROGRAMIVPROC OGLShaders::glGetProgramiv = NULL;
55 PFNGLGETPROGRAMINFOLOGPROC OGLShaders::glGetProgramInfoLog = NULL;
56 PFNGLUSEPROGRAMPROC OGLShaders::glUseProgram = NULL;
57 PFNGLDELETEPROGRAMPROC OGLShaders::glDeleteProgram = NULL;
58 PFNGLGETUNIFORMLOCATIONPROC OGLShaders::glGetUniformLocation = NULL;
59 PFNGLUNIFORM1IPROC OGLShaders::glUniform1i = NULL;
60 PFNGLUNIFORM1FPROC OGLShaders::glUniform1f = NULL;
61 #endif
63 bool OGLShaders::Initialize()
65 #ifdef GL_VERSION_2_0
66 if( !cbInitialized ) {
67 glCreateShader = (PFNGLCREATESHADERPROC) unx::glXGetProcAddress( (unsigned char *) "glCreateShader" );
68 glShaderSource = (PFNGLSHADERSOURCEPROC) unx::glXGetProcAddress( (unsigned char *) "glShaderSource" );
69 glCompileShader = (PFNGLCOMPILESHADERPROC) unx::glXGetProcAddress( (unsigned char *) "glCompileShader" );
70 glGetShaderiv = (PFNGLGETSHADERIVPROC) unx::glXGetProcAddress( (unsigned char *) "glGetShaderiv" );
71 glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC) unx::glXGetProcAddress( (unsigned char *) "glGetShaderInfoLog" );
72 glDeleteShader = (PFNGLDELETESHADERPROC) unx::glXGetProcAddress( (unsigned char *) "glDeleteShader" );
73 glCreateProgram = (PFNGLCREATEPROGRAMPROC) unx::glXGetProcAddress( (unsigned char *) "glCreateProgram" );
74 glAttachShader = (PFNGLATTACHSHADERPROC) unx::glXGetProcAddress( (unsigned char *) "glAttachShader" );
75 glLinkProgram = (PFNGLLINKPROGRAMPROC) unx::glXGetProcAddress( (unsigned char *) "glLinkProgram" );
76 glGetProgramiv = (PFNGLGETPROGRAMIVPROC) unx::glXGetProcAddress( (unsigned char *) "glGetProgramiv" );
77 glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC) unx::glXGetProcAddress( (unsigned char *) "glGetProgramInfoLog" );
78 glUseProgram = (PFNGLUSEPROGRAMPROC) unx::glXGetProcAddress( (unsigned char *) "glUseProgram" );
79 glDeleteProgram = (PFNGLDELETEPROGRAMPROC) unx::glXGetProcAddress( (unsigned char *) "glDeleteProgram" );
80 glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) unx::glXGetProcAddress( (unsigned char *) "glGetUniformLocation" );
81 glUniform1i = (PFNGLUNIFORM1IPROC) unx::glXGetProcAddress( (unsigned char *) "glUniform1i" );
82 glUniform1f = (PFNGLUNIFORM1FPROC) unx::glXGetProcAddress( (unsigned char *) "glUniform1f" );
83 cbInitialized = true;
86 return glCreateShader != NULL;
87 #else
88 return false;
89 #endif
92 GLuint OGLShaders::LinkProgram( const char *vertexShader, const char *fragmentShader )
94 #ifdef GL_VERSION_2_0
95 if( !Initialize() )
96 return 0;
98 GLhandleARB vertexObject, fragmentObject, programObject;
99 GLint vertexCompiled, fragmentCompiled, programLinked;
100 char log[1024];
102 vertexObject = glCreateShader( GL_VERTEX_SHADER );
103 fragmentObject = glCreateShader( GL_FRAGMENT_SHADER );
104 OSL_TRACE("checkpoint 1: shaders created (%d) vertex: %d fragment: %d", glGetError() == GL_NO_ERROR, vertexObject, fragmentObject );
107 glShaderSource( vertexObject, 1, &vertexShader, NULL );
108 glShaderSource( fragmentObject, 1, &fragmentShader, NULL );
110 glCompileShader( vertexObject );
111 glGetShaderInfoLog( vertexObject, sizeof( log ), NULL, log );
112 OSL_TRACE("vertex compile log: %s", log);
113 glGetShaderiv( vertexObject, GL_COMPILE_STATUS, &vertexCompiled );
114 glCompileShader( fragmentObject );
115 glGetShaderInfoLog( fragmentObject, sizeof( log ), NULL, log );
116 OSL_TRACE("fragment compile log: %s", log);
117 glGetShaderiv( fragmentObject, GL_COMPILE_STATUS, &fragmentCompiled );
119 if( !vertexCompiled || !fragmentCompiled )
120 return 0;
122 OSL_TRACE("checkpoint 2: shaders compiled (%d)", glGetError() == GL_NO_ERROR);
124 programObject = glCreateProgram();
125 glAttachShader( programObject, vertexObject );
126 glAttachShader( programObject, fragmentObject );
128 glLinkProgram( programObject );
129 glGetProgramInfoLog( programObject, sizeof( log ), NULL, log );
130 OSL_TRACE("program link log: %s", log);
131 glGetProgramiv( programObject, GL_LINK_STATUS, &programLinked );
133 if( !programLinked )
134 return 0;
136 OSL_TRACE("checkpoint 3: program linked (%d)", glGetError() == GL_NO_ERROR);
138 return programObject;
139 #else
140 return 0;
141 #endif