OpenGL on Windows

Brent Fulgham <[email protected]>
Newsgroups gmane.comp.lib.cairo
Message-ID <CAFX-vuho3euo19ZnuB1K9TTJc76=DHyX0q5ZN6r7wV79GejqKA@mail.gmail.com>
Last night I was able to get an OpenGL-based Cairo test application to
build and run under Windows using the 1.12.2 source base.

Attached is the diff to accomplish this.  I found that the default
function pointer declarations did not work with the Windows GL
implementation because of differences in calling conventions.  I'm not
sure if the approach I took to add the declarations is the right one,
and was hoping someone could review the change and let me know.

To actually build the software, you need the "OpenGL Registry"
versions of wglext.h and glext.h, which do not ship with Windows.

As I write this, I realize that I forgot to update the Makefile to
include the GL and WGL source files; I will follow up with this
missing piece, once I hear back on the other changes.

Thanks,

-Brent

-- 
cairo mailing list
[email protected]
http://lists.cairographics.org/mailman/listinfo/cairo
wgl_updates.patch (application/octet-stream, 9.1 KB)
diff -wNru3 ./cairo-1.12.2/src/cairo-features.h ./cairo/src/cairo-features.h
--- ./cairo-1.12.2/src/cairo-features.h	1969-12-31 16:00:00.000000000 -0800
+++ ./cairo/src/cairo-features.h	2012-06-24 15:51:25.873287000 -0700
@@ -0,0 +1,19 @@
+/* Generated by Makefile.win32.features-h.  Do not edit. */
+#ifndef CAIRO_FEATURES_H
+#define CAIRO_FEATURES_H 1
+#define CAIRO_HAS_WIN32_SURFACE 1
+#define CAIRO_HAS_WIN32_FONT 1
+#define CAIRO_HAS_GL_SURFACE 1
+#define CAIRO_HAS_PNG_FUNCTIONS 1
+#define CAIRO_HAS_SCRIPT_SURFACE 1
+#define CAIRO_HAS_PS_SURFACE 1
+#define CAIRO_HAS_PDF_SURFACE 1
+#define CAIRO_HAS_SVG_SURFACE 1
+#define CAIRO_HAS_IMAGE_SURFACE 1
+#define CAIRO_HAS_MIME_SURFACE 1
+#define CAIRO_HAS_RECORDING_SURFACE 1
+#define CAIRO_HAS_OBSERVER_SURFACE 1
+#define CAIRO_HAS_USER_FONT 1
+#define CAIRO_HAS_INTERPRETER 1
+#define CAIRO_HAS_WGL_FUNCTIONS 1
+#endif
diff -wNru3 ./cairo-1.12.2/src/cairo-gl-private.h ./cairo/src/cairo-gl-private.h
--- ./cairo-1.12.2/src/cairo-gl-private.h	2012-02-29 04:10:30.000000000 -0800
+++ ./cairo/src/cairo-gl-private.h	2012-06-24 19:45:59.438247500 -0700
@@ -203,75 +203,81 @@
 #define cairo_gl_var_type_hash(src,mask,spans,dest) ((spans) << 3) | ((mask) << 2 | (src << 1) | (dest))
 #define CAIRO_GL_VAR_TYPE_MAX ((CAIRO_GL_VAR_TEXCOORDS << 3) | (CAIRO_GL_VAR_TEXCOORDS << 2) | (CAIRO_GL_VAR_TEXCOORDS << 1) | CAIRO_GL_VAR_TEXCOORDS)
 
+#if defined CAIRO_HAS_WGL_FUNCTIONS
+typedef int (WINAPI *cairo_gl_generic_func_t)(void);
+typedef WINGDIAPI cairo_gl_generic_func_t (WINAPI *cairo_gl_get_proc_addr_func_t)(LPCSTR);
+#else
+#define APIENTRY
 typedef void (*cairo_gl_generic_func_t)(void);
 typedef cairo_gl_generic_func_t (*cairo_gl_get_proc_addr_func_t)(const char *procname);
+#endif
 
 typedef struct _cairo_gl_dispatch {
     /* Buffers */
-    void (*GenBuffers) (GLsizei n, GLuint *buffers);
-    void (*BindBuffer) (GLenum target, GLuint buffer);
-    void (*BufferData) (GLenum target, GLsizeiptr size,
+    void (APIENTRY *GenBuffers) (GLsizei n, GLuint *buffers);
+    void (APIENTRY *BindBuffer) (GLenum target, GLuint buffer);
+    void (APIENTRY *BufferData) (GLenum target, GLsizeiptr size,
 			  const GLvoid* data, GLenum usage);
-    GLvoid *(*MapBuffer) (GLenum target, GLenum access);
-    GLboolean (*UnmapBuffer) (GLenum target);
+    GLvoid *(APIENTRY *MapBuffer) (GLenum target, GLenum access);
+    GLboolean (APIENTRY *UnmapBuffer) (GLenum target);
 
     /* Shaders */
-    GLuint (*CreateShader) (GLenum type);
-    void (*ShaderSource) (GLuint shader, GLsizei count,
+    GLuint (APIENTRY *CreateShader) (GLenum type);
+    void (APIENTRY *ShaderSource) (GLuint shader, GLsizei count,
 			    const GLchar** string, const GLint* length);
-    void (*CompileShader) (GLuint shader);
-    void (*GetShaderiv) (GLuint shader, GLenum pname, GLint *params);
-    void (*GetShaderInfoLog) (GLuint shader, GLsizei bufSize,
+    void (APIENTRY *CompileShader) (GLuint shader);
+    void (APIENTRY *GetShaderiv) (GLuint shader, GLenum pname, GLint *params);
+    void (APIENTRY *GetShaderInfoLog) (GLuint shader, GLsizei bufSize,
 				GLsizei *length, GLchar *infoLog);
-    void (*DeleteShader) (GLuint shader);
+    void (APIENTRY *DeleteShader) (GLuint shader);
 
     /* Programs */
-    GLuint (*CreateProgram) (void);
-    void (*AttachShader) (GLuint program, GLuint shader);
-    void (*DeleteProgram) (GLuint program);
-    void (*LinkProgram) (GLuint program);
-    void (*UseProgram) (GLuint program);
-    void (*GetProgramiv) (GLuint program, GLenum pname, GLint *params);
-    void (*GetProgramInfoLog) (GLuint program, GLsizei bufSize,
+    GLuint (APIENTRY *CreateProgram) (void);
+    void (APIENTRY *AttachShader) (GLuint program, GLuint shader);
+    void (APIENTRY *DeleteProgram) (GLuint program);
+    void (APIENTRY *LinkProgram) (GLuint program);
+    void (APIENTRY *UseProgram) (GLuint program);
+    void (APIENTRY *GetProgramiv) (GLuint program, GLenum pname, GLint *params);
+    void (APIENTRY *GetProgramInfoLog) (GLuint program, GLsizei bufSize,
 				 GLsizei *length, GLchar *infoLog);
 
     /* Uniforms */
-    GLint (*GetUniformLocation) (GLuint program, const GLchar* name);
-    void (*Uniform1f) (GLint location, GLfloat x);
-    void (*Uniform2f) (GLint location, GLfloat x, GLfloat y);
-    void (*Uniform3f) (GLint location, GLfloat x, GLfloat y, GLfloat z);
-    void (*Uniform4f) (GLint location, GLfloat x, GLfloat y, GLfloat z,
+    GLint (APIENTRY *GetUniformLocation) (GLuint program, const GLchar* name);
+    void (APIENTRY *Uniform1f) (GLint location, GLfloat x);
+    void (APIENTRY *Uniform2f) (GLint location, GLfloat x, GLfloat y);
+    void (APIENTRY *Uniform3f) (GLint location, GLfloat x, GLfloat y, GLfloat z);
+    void (APIENTRY *Uniform4f) (GLint location, GLfloat x, GLfloat y, GLfloat z,
 			 GLfloat w);
-    void (*UniformMatrix3fv) (GLint location, GLsizei count,
+    void (APIENTRY *UniformMatrix3fv) (GLint location, GLsizei count,
 				GLboolean transpose, const GLfloat *value);
-    void (*UniformMatrix4fv) (GLint location, GLsizei count,
+    void (APIENTRY *UniformMatrix4fv) (GLint location, GLsizei count,
 			      GLboolean transpose, const GLfloat *value);
-    void (*Uniform1i) (GLint location, GLint x);
+    void (APIENTRY *Uniform1i) (GLint location, GLint x);
 
     /* Attributes */
-    void (*BindAttribLocation) (GLuint program, GLuint index,
+    void (APIENTRY *BindAttribLocation) (GLuint program, GLuint index,
 				const GLchar *name);
-    void (*VertexAttribPointer) (GLuint index, GLint size, GLenum type,
+    void (APIENTRY *VertexAttribPointer) (GLuint index, GLint size, GLenum type,
 				 GLboolean normalized, GLsizei stride,
 				 const GLvoid *pointer);
-    void (*EnableVertexAttribArray) (GLuint index);
-    void (*DisableVertexAttribArray) (GLuint index);
+    void (APIENTRY *EnableVertexAttribArray) (GLuint index);
+    void (APIENTRY *DisableVertexAttribArray) (GLuint index);
 
     /* Framebuffer objects */
-    void (*GenFramebuffers) (GLsizei n, GLuint* framebuffers);
-    void (*BindFramebuffer) (GLenum target, GLuint framebuffer);
-    void (*FramebufferTexture2D) (GLenum target, GLenum attachment,
+    void (APIENTRY *GenFramebuffers) (GLsizei n, GLuint* framebuffers);
+    void (APIENTRY *BindFramebuffer) (GLenum target, GLuint framebuffer);
+    void (APIENTRY *FramebufferTexture2D) (GLenum target, GLenum attachment,
 				    GLenum textarget, GLuint texture,
 				    GLint level);
-    GLenum (*CheckFramebufferStatus) (GLenum target);
-    void (*DeleteFramebuffers) (GLsizei n, const GLuint* framebuffers);
-    void (*GenRenderbuffers) (GLsizei n, GLuint *renderbuffers);
-    void (*BindRenderbuffer) (GLenum target, GLuint renderbuffer);
-    void (*RenderbufferStorage) (GLenum target, GLenum internal_format,
+    GLenum (APIENTRY *CheckFramebufferStatus) (GLenum target);
+    void (APIENTRY *DeleteFramebuffers) (GLsizei n, const GLuint* framebuffers);
+    void (APIENTRY *GenRenderbuffers) (GLsizei n, GLuint *renderbuffers);
+    void (APIENTRY *BindRenderbuffer) (GLenum target, GLuint renderbuffer);
+    void (APIENTRY *RenderbufferStorage) (GLenum target, GLenum internal_format,
 				 GLsizei width, GLsizei height);
-    void (*FramebufferRenderbuffer) (GLenum target, GLenum attachment,
+    void (APIENTRY *FramebufferRenderbuffer) (GLenum target, GLenum attachment,
 				     GLenum renderbuffer_ttarget, GLuint renderbuffer);
-    void (*DeleteRenderbuffers) (GLsizei n, GLuint *renderbuffers);
+    void (APIENTRY *DeleteRenderbuffers) (GLsizei n, GLuint *renderbuffers);
 
 } cairo_gl_dispatch_t;
 
diff -wNru3 ./cairo-1.12.2/src/cairo-wgl-context.c ./cairo/src/cairo-wgl-context.c
--- ./cairo-1.12.2/src/cairo-wgl-context.c	2012-02-01 16:48:14.000000000 -0800
+++ ./cairo/src/cairo-wgl-context.c	2012-06-24 20:01:57.063245100 -0700
@@ -63,6 +63,9 @@
     HDC dc;
 } cairo_wgl_surface_t;
 
+PFNGLACTIVETEXTUREARBPROC _glActiveTextureARB = NULL;
+PFNGLBLENDFUNCSEPARATEPROC _glBlendFuncSeparate = NULL;
+
 static void
 _wgl_acquire (void *abstract_ctx)
 {
@@ -259,3 +262,19 @@
 
     return &surface->base.base;
 }
+
+void InitWGLStuff ()
+{
+    _glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC)wglGetProcAddress((LPCSTR)"glActiveTextureARB");
+    _glBlendFuncSeparate = (PFNGLBLENDFUNCSEPARATEPROC)wglGetProcAddress((LPCSTR)"glBlendFuncSeparate");
+}
+
+GLAPI void APIENTRY glActiveTexture (GLenum texture)
+{
+    (*_glActiveTextureARB)(texture);
+}
+
+GLAPI void APIENTRY glBlendFuncSeparate (GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha)
+{
+    (*_glBlendFuncSeparate)(sfactorRGB, dfactorRGB, sfactorAlpha, dfactorAlpha);
+}
diff -wNru3 ./cairo-1.12.2/src/win32/cairo-win32-system.c ./cairo/src/win32/cairo-win32-system.c
--- ./cairo-1.12.2/src/win32/cairo-win32-system.c	2012-02-15 06:21:01.000000000 -0800
+++ ./cairo/src/win32/cairo-win32-system.c	2012-06-24 16:06:13.251284600 -0700
@@ -61,6 +61,8 @@
 
 #include <windows.h>
 
+void InitWGLStuff ();
+
 /* declare to avoid "no previous prototype for 'DllMain'" warning */
 BOOL WINAPI
 DllMain (HINSTANCE hinstDLL,
@@ -75,6 +77,7 @@
     switch (fdwReason) {
         case DLL_PROCESS_ATTACH:
             CAIRO_MUTEX_INITIALIZE ();
+            InitWGLStuff ();
             break;
 
         case DLL_PROCESS_DETACH:
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.