Release 6.15.
[wine.git] / dlls / wined3d / gl_compat.c
blobb8323b7e226eb85ad3ce29d55d9a981650eb51e7
1 /*
2 * Compatibility functions for older GL implementations
4 * Copyright 2008 Stefan Dösinger for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdio.h>
25 #ifdef HAVE_FLOAT_H
26 # include <float.h>
27 #endif
29 #include "wined3d_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(gl_compat);
32 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
34 /* Start GL_ARB_multitexture emulation */
35 static void WINE_GLAPI wine_glMultiTexCoord1fARB(GLenum target, GLfloat s)
37 if (target != GL_TEXTURE0)
39 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
40 return;
42 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord1f(s);
45 static void WINE_GLAPI wine_glMultiTexCoord1fvARB(GLenum target, const GLfloat *v)
47 if (target != GL_TEXTURE0)
49 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
50 return;
52 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord1fv(v);
55 static void WINE_GLAPI wine_glMultiTexCoord2fARB(GLenum target, GLfloat s, GLfloat t)
57 if (target != GL_TEXTURE0)
59 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
60 return;
62 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord2f(s, t);
65 static void WINE_GLAPI wine_glMultiTexCoord2fvARB(GLenum target, const GLfloat *v)
67 if (target != GL_TEXTURE0)
69 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
70 return;
72 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord2fv(v);
75 static void WINE_GLAPI wine_glMultiTexCoord3fARB(GLenum target, GLfloat s, GLfloat t, GLfloat r)
77 if (target != GL_TEXTURE0)
79 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
80 return;
82 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord3f(s, t, r);
85 static void WINE_GLAPI wine_glMultiTexCoord3fvARB(GLenum target, const GLfloat *v)
87 if (target != GL_TEXTURE0)
89 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
90 return;
92 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord3fv(v);
95 static void WINE_GLAPI wine_glMultiTexCoord4fARB(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q)
97 if (target != GL_TEXTURE0)
99 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
100 return;
102 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord4f(s, t, r, q);
105 static void WINE_GLAPI wine_glMultiTexCoord4fvARB(GLenum target, const GLfloat *v)
107 if (target != GL_TEXTURE0)
109 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
110 return;
112 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord4fv(v);
115 static void WINE_GLAPI wine_glMultiTexCoord2svARB(GLenum target, const GLshort *v)
117 if (target != GL_TEXTURE0)
119 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
120 return;
122 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord2sv(v);
125 static void WINE_GLAPI wine_glMultiTexCoord4svARB(GLenum target, const GLshort *v)
127 if (target != GL_TEXTURE0)
129 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
130 return;
132 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord4sv(v);
135 static void WINE_GLAPI wine_glActiveTexture(GLenum texture)
137 if(texture != GL_TEXTURE0) {
138 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported\n");
139 return;
143 static void WINE_GLAPI wine_glClientActiveTextureARB(GLenum texture) {
144 if(texture != GL_TEXTURE0) {
145 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported\n");
146 return;
150 static void (WINE_GLAPI *old_multitex_glGetIntegerv) (GLenum pname, GLint* params) = NULL;
151 static void WINE_GLAPI wine_glGetIntegerv(GLenum pname, GLint* params) {
152 switch(pname) {
153 case GL_ACTIVE_TEXTURE: *params = 0; break;
154 case GL_MAX_TEXTURE_UNITS_ARB: *params = 1; break;
155 default: old_multitex_glGetIntegerv(pname, params);
159 static void (WINE_GLAPI *old_multitex_glGetFloatv) (GLenum pname, GLfloat* params) = NULL;
160 static void WINE_GLAPI wine_glGetFloatv(GLenum pname, GLfloat* params) {
161 if (pname == GL_ACTIVE_TEXTURE) *params = 0.0f;
162 else old_multitex_glGetFloatv(pname, params);
165 static void (WINE_GLAPI *old_multitex_glGetDoublev) (GLenum pname, GLdouble* params) = NULL;
166 static void WINE_GLAPI wine_glGetDoublev(GLenum pname, GLdouble* params) {
167 if(pname == GL_ACTIVE_TEXTURE) *params = 0.0;
168 else old_multitex_glGetDoublev(pname, params);
171 /* Start GL_EXT_fogcoord emulation */
172 static void (WINE_GLAPI *old_fogcoord_glEnable)(GLenum cap);
173 static void WINE_GLAPI wine_glEnable(GLenum cap)
175 if (cap == GL_FOG)
177 struct wined3d_context_gl *ctx = wined3d_context_gl_get_current();
179 ctx->fog_enabled = 1;
180 if (ctx->gl_fog_source != GL_FRAGMENT_DEPTH_EXT)
181 return;
183 old_fogcoord_glEnable(cap);
186 static void (WINE_GLAPI *old_fogcoord_glDisable)(GLenum cap);
187 static void WINE_GLAPI wine_glDisable(GLenum cap)
189 if (cap == GL_FOG)
191 struct wined3d_context_gl *ctx = wined3d_context_gl_get_current();
193 ctx->fog_enabled = 0;
194 if (ctx->gl_fog_source != GL_FRAGMENT_DEPTH_EXT)
195 return;
197 old_fogcoord_glDisable(cap);
200 static void (WINE_GLAPI *old_fogcoord_glFogi)(GLenum pname, GLint param);
201 static void WINE_GLAPI wine_glFogi(GLenum pname, GLint param)
203 struct wined3d_context_gl *ctx = wined3d_context_gl_get_current();
205 if (pname == GL_FOG_COORDINATE_SOURCE_EXT)
207 ctx->gl_fog_source = param;
208 if (param == GL_FRAGMENT_DEPTH_EXT)
210 if (ctx->fog_enabled)
211 old_fogcoord_glEnable(GL_FOG);
213 else
215 WARN_(d3d_perf)("Fog coordinates activated, but not supported. Using slow emulation.\n");
216 old_fogcoord_glDisable(GL_FOG);
219 else
221 if (pname == GL_FOG_START)
222 ctx->fog_start = (float)param;
223 else if (pname == GL_FOG_END)
224 ctx->fog_end = (float)param;
225 old_fogcoord_glFogi(pname, param);
229 static void (WINE_GLAPI *old_fogcoord_glFogiv)(GLenum pname, const GLint *param);
230 static void WINE_GLAPI wine_glFogiv(GLenum pname, const GLint *param)
232 struct wined3d_context_gl *ctx = wined3d_context_gl_get_current();
234 if (pname == GL_FOG_COORDINATE_SOURCE_EXT)
236 ctx->gl_fog_source = *param;
237 if (*param == GL_FRAGMENT_DEPTH_EXT)
239 if (ctx->fog_enabled)
240 old_fogcoord_glEnable(GL_FOG);
242 else
244 WARN_(d3d_perf)("Fog coordinates activated, but not supported. Using slow emulation.\n");
245 old_fogcoord_glDisable(GL_FOG);
248 else
250 if (pname == GL_FOG_START)
251 ctx->fog_start = (float)*param;
252 else if (pname == GL_FOG_END)
253 ctx->fog_end = (float)*param;
254 old_fogcoord_glFogiv(pname, param);
258 static void (WINE_GLAPI *old_fogcoord_glFogf)(GLenum pname, GLfloat param);
259 static void WINE_GLAPI wine_glFogf(GLenum pname, GLfloat param)
261 struct wined3d_context_gl *ctx = wined3d_context_gl_get_current();
263 if (pname == GL_FOG_COORDINATE_SOURCE_EXT)
265 ctx->gl_fog_source = (GLint)param;
266 if (param == GL_FRAGMENT_DEPTH_EXT)
268 if (ctx->fog_enabled)
269 old_fogcoord_glEnable(GL_FOG);
271 else
273 WARN_(d3d_perf)("Fog coordinates activated, but not supported. Using slow emulation.\n");
274 old_fogcoord_glDisable(GL_FOG);
277 else
279 if (pname == GL_FOG_START)
280 ctx->fog_start = param;
281 else if (pname == GL_FOG_END)
282 ctx->fog_end = param;
283 old_fogcoord_glFogf(pname, param);
287 static void (WINE_GLAPI *old_fogcoord_glFogfv)(GLenum pname, const GLfloat *param);
288 static void WINE_GLAPI wine_glFogfv(GLenum pname, const GLfloat *param)
290 struct wined3d_context_gl *ctx = wined3d_context_gl_get_current();
292 if (pname == GL_FOG_COORDINATE_SOURCE_EXT)
294 ctx->gl_fog_source = (GLint)*param;
295 if (*param == GL_FRAGMENT_DEPTH_EXT)
297 if (ctx->fog_enabled)
298 old_fogcoord_glEnable(GL_FOG);
300 else
302 WARN_(d3d_perf)("Fog coordinates activated, but not supported. Using slow emulation.\n");
303 old_fogcoord_glDisable(GL_FOG);
306 else
308 if (pname == GL_FOG_COLOR)
310 ctx->fog_colour[0] = param[0];
311 ctx->fog_colour[1] = param[1];
312 ctx->fog_colour[2] = param[2];
313 ctx->fog_colour[3] = param[3];
315 else if (pname == GL_FOG_START)
317 ctx->fog_start = *param;
319 else if (pname == GL_FOG_END)
321 ctx->fog_end = *param;
323 old_fogcoord_glFogfv(pname, param);
327 static void (WINE_GLAPI *old_fogcoord_glVertex4f)(GLfloat x, GLfloat y, GLfloat z, GLfloat w);
328 static void (WINE_GLAPI *old_fogcoord_glColor4f)(GLfloat r, GLfloat g, GLfloat b, GLfloat a);
330 static void WINE_GLAPI wine_glVertex4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
332 const struct wined3d_context_gl *ctx_gl = wined3d_context_gl_get_current();
334 /* This can be called from draw_test_quad() and at that point there is no
335 * wined3d_context current. */
336 if (!ctx_gl)
338 old_fogcoord_glVertex4f(x, y, z, w);
339 return;
342 if (ctx_gl->gl_fog_source == GL_FOG_COORDINATE_EXT && ctx_gl->fog_enabled)
344 GLfloat c[4] = {ctx_gl->colour[0], ctx_gl->colour[1], ctx_gl->colour[2], ctx_gl->colour[3]};
345 GLfloat i;
347 i = (ctx_gl->fog_end - ctx_gl->fog_coord_value) / (ctx_gl->fog_end - ctx_gl->fog_start);
348 c[0] = i * c[0] + (1.0f - i) * ctx_gl->fog_colour[0];
349 c[1] = i * c[1] + (1.0f - i) * ctx_gl->fog_colour[1];
350 c[2] = i * c[2] + (1.0f - i) * ctx_gl->fog_colour[2];
352 old_fogcoord_glColor4f(c[0], c[1], c[2], c[3]);
353 old_fogcoord_glVertex4f(x, y, z, w);
355 else
357 old_fogcoord_glVertex4f(x, y, z, w);
361 static void WINE_GLAPI wine_glVertex4fv(const GLfloat *pos) {
362 wine_glVertex4f(pos[0], pos[1], pos[2], pos[3]);
365 static void WINE_GLAPI wine_glVertex3f(GLfloat x, GLfloat y, GLfloat z) {
366 wine_glVertex4f(x, y, z, 1.0f);
369 static void WINE_GLAPI wine_glVertex3fv(const GLfloat *pos) {
370 wine_glVertex4f(pos[0], pos[1], pos[2], 1.0f);
373 static void WINE_GLAPI wine_glColor4f(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
375 struct wined3d_context_gl *ctx_gl = wined3d_context_gl_get_current();
377 /* This can be called from draw_test_quad() and at that point there is no
378 * wined3d_context current. */
379 if (!ctx_gl)
381 old_fogcoord_glColor4f(r, g, b, a);
382 return;
385 ctx_gl->colour[0] = r;
386 ctx_gl->colour[1] = g;
387 ctx_gl->colour[2] = b;
388 ctx_gl->colour[3] = a;
389 old_fogcoord_glColor4f(r, g, b, a);
392 static void WINE_GLAPI wine_glColor4fv(const GLfloat *c) {
393 wine_glColor4f(c[0], c[1], c[2], c[3]);
396 static void WINE_GLAPI wine_glColor3f(GLfloat r, GLfloat g, GLfloat b) {
397 wine_glColor4f(r, g, b, 1.0f);
400 static void WINE_GLAPI wine_glColor3fv(const GLfloat *c) {
401 wine_glColor4f(c[0], c[1], c[2], 1.0f);
404 static void WINE_GLAPI wine_glColor4ub(GLubyte r, GLubyte g, GLubyte b, GLubyte a) {
405 wine_glColor4f(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
408 /* In D3D the fog coord is a UBYTE, so there's no problem with using the
409 * single precision function. */
410 static void WINE_GLAPI wine_glFogCoordfEXT(GLfloat f)
412 struct wined3d_context_gl *ctx = wined3d_context_gl_get_current();
414 ctx->fog_coord_value = f;
416 static void WINE_GLAPI wine_glFogCoorddEXT(GLdouble f) {
417 wine_glFogCoordfEXT((GLfloat) f);
419 static void WINE_GLAPI wine_glFogCoordfvEXT(const GLfloat *f) {
420 wine_glFogCoordfEXT(*f);
422 static void WINE_GLAPI wine_glFogCoorddvEXT(const GLdouble *f) {
423 wine_glFogCoordfEXT((GLfloat) *f);
426 /* End GL_EXT_fog_coord emulation */
428 void install_gl_compat_wrapper(struct wined3d_gl_info *gl_info, enum wined3d_gl_extension ext)
430 switch (ext)
432 case ARB_MULTITEXTURE:
433 if (gl_info->supported[ARB_MULTITEXTURE])
434 return;
435 if (gl_info->gl_ops.ext.p_glActiveTexture == wine_glActiveTexture)
437 FIXME("ARB_multitexture emulation hooks already applied.\n");
438 return;
440 TRACE("Applying GL_ARB_multitexture emulation hooks.\n");
441 gl_info->gl_ops.ext.p_glActiveTexture = wine_glActiveTexture;
442 gl_info->gl_ops.ext.p_glClientActiveTextureARB = wine_glClientActiveTextureARB;
443 gl_info->gl_ops.ext.p_glMultiTexCoord1fARB = wine_glMultiTexCoord1fARB;
444 gl_info->gl_ops.ext.p_glMultiTexCoord1fvARB = wine_glMultiTexCoord1fvARB;
445 gl_info->gl_ops.ext.p_glMultiTexCoord2fARB = wine_glMultiTexCoord2fARB;
446 gl_info->gl_ops.ext.p_glMultiTexCoord2fvARB = wine_glMultiTexCoord2fvARB;
447 gl_info->gl_ops.ext.p_glMultiTexCoord3fARB = wine_glMultiTexCoord3fARB;
448 gl_info->gl_ops.ext.p_glMultiTexCoord3fvARB = wine_glMultiTexCoord3fvARB;
449 gl_info->gl_ops.ext.p_glMultiTexCoord4fARB = wine_glMultiTexCoord4fARB;
450 gl_info->gl_ops.ext.p_glMultiTexCoord4fvARB = wine_glMultiTexCoord4fvARB;
451 gl_info->gl_ops.ext.p_glMultiTexCoord2svARB = wine_glMultiTexCoord2svARB;
452 gl_info->gl_ops.ext.p_glMultiTexCoord4svARB = wine_glMultiTexCoord4svARB;
453 old_multitex_glGetIntegerv = gl_info->gl_ops.gl.p_glGetIntegerv;
454 gl_info->gl_ops.gl.p_glGetIntegerv = wine_glGetIntegerv;
455 old_multitex_glGetFloatv = gl_info->gl_ops.gl.p_glGetFloatv;
456 gl_info->gl_ops.gl.p_glGetFloatv = wine_glGetFloatv;
457 old_multitex_glGetDoublev = gl_info->gl_ops.gl.p_glGetDoublev;
458 gl_info->gl_ops.gl.p_glGetDoublev = wine_glGetDoublev;
459 gl_info->supported[ARB_MULTITEXTURE] = TRUE;
460 return;
462 case EXT_FOG_COORD:
463 /* This emulation isn't perfect. There are a number of potential problems, but they should
464 * not matter in practise:
466 * Fog vs fragment shader: If we are using GL_ARB_fragment_program with the fog option, the
467 * glDisable(GL_FOG) here won't matter. However, if we have GL_ARB_fragment_program, it is pretty
468 * unlikely that we don't have GL_EXT_fog_coord. Besides, we probably have GL_ARB_vertex_program
469 * too, which would allow fog coord emulation in a fixed function vertex pipeline replacement.
471 * Fog vs texture: We apply the fog in the vertex color. An app could set up texturing settings which
472 * ignore the vertex color, thus effectively disabling our fog. However, in D3D this type of fog is
473 * a per-vertex fog too, so the apps shouldn't do that.
475 * Fog vs lighting: The app could in theory use D3DFOG_NONE table and D3DFOG_NONE vertex fog with
476 * untransformed vertices. That enables lighting and fog coords at the same time, and the lighting
477 * calculations could affect the already blended in fog color. There's nothing we can do against that,
478 * but most apps using fog color do their own lighting too and often even use RHW vertices. So live
479 * with it.
481 if (gl_info->supported[EXT_FOG_COORD])
482 return;
483 if (gl_info->gl_ops.gl.p_glFogi == wine_glFogi)
485 FIXME("EXT_fog_coord emulation hooks already applied.\n");
486 return;
488 TRACE("Applying GL_ARB_fog_coord emulation hooks\n");
490 /* This probably means that the implementation doesn't advertise the extension, but implicitly supports
491 * it via the GL core version, or someone messed around in the extension table in directx.c. Add version-
492 * dependent loading for this extension if we ever hit this situation
494 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
496 FIXME("GL implementation supports GL_ARB_fragment_program but not GL_EXT_fog_coord\n");
497 FIXME("The fog coord emulation will most likely fail\n");
499 else if (gl_info->supported[ARB_FRAGMENT_SHADER])
501 FIXME("GL implementation supports GL_ARB_fragment_shader but not GL_EXT_fog_coord\n");
502 FIXME("The fog coord emulation will most likely fail\n");
505 old_fogcoord_glFogi = gl_info->gl_ops.gl.p_glFogi;
506 gl_info->gl_ops.gl.p_glFogi = wine_glFogi;
507 old_fogcoord_glFogiv = gl_info->gl_ops.gl.p_glFogiv;
508 gl_info->gl_ops.gl.p_glFogiv = wine_glFogiv;
509 old_fogcoord_glFogf = gl_info->gl_ops.gl.p_glFogf;
510 gl_info->gl_ops.gl.p_glFogf = wine_glFogf;
511 old_fogcoord_glFogfv = gl_info->gl_ops.gl.p_glFogfv;
512 gl_info->gl_ops.gl.p_glFogfv = wine_glFogfv;
513 old_fogcoord_glEnable = gl_info->p_glEnableWINE;
514 gl_info->p_glEnableWINE = wine_glEnable;
515 old_fogcoord_glDisable = gl_info->p_glDisableWINE;
516 gl_info->p_glDisableWINE = wine_glDisable;
518 old_fogcoord_glVertex4f = gl_info->gl_ops.gl.p_glVertex4f;
519 gl_info->gl_ops.gl.p_glVertex4f = wine_glVertex4f;
520 gl_info->gl_ops.gl.p_glVertex4fv = wine_glVertex4fv;
521 gl_info->gl_ops.gl.p_glVertex3f = wine_glVertex3f;
522 gl_info->gl_ops.gl.p_glVertex3fv = wine_glVertex3fv;
524 old_fogcoord_glColor4f = gl_info->gl_ops.gl.p_glColor4f;
525 gl_info->gl_ops.gl.p_glColor4f = wine_glColor4f;
526 gl_info->gl_ops.gl.p_glColor4fv = wine_glColor4fv;
527 gl_info->gl_ops.gl.p_glColor3f = wine_glColor3f;
528 gl_info->gl_ops.gl.p_glColor3fv = wine_glColor3fv;
529 gl_info->gl_ops.gl.p_glColor4ub = wine_glColor4ub;
531 gl_info->gl_ops.ext.p_glFogCoordfEXT = wine_glFogCoordfEXT;
532 gl_info->gl_ops.ext.p_glFogCoordfvEXT = wine_glFogCoordfvEXT;
533 gl_info->gl_ops.ext.p_glFogCoorddEXT = wine_glFogCoorddEXT;
534 gl_info->gl_ops.ext.p_glFogCoorddvEXT = wine_glFogCoorddvEXT;
535 gl_info->supported[EXT_FOG_COORD] = TRUE;
536 return;
538 default:
539 FIXME("Extension %u emulation not supported.\n", ext);