wined3d: Use wined3d_mask_from_size() in shader_glsl_gather4().
[wine.git] / dlls / wined3d / gl_compat.c
blob99e7cf0fd0e75f91e3a747c91239fb0bf490e0c9
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"
23 #include <stdio.h>
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(gl_compat);
28 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
30 /* Start GL_ARB_multitexture emulation */
31 static void WINE_GLAPI wine_glMultiTexCoord1fARB(GLenum target, GLfloat s)
33 if (target != GL_TEXTURE0)
35 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
36 return;
38 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord1f(s);
41 static void WINE_GLAPI wine_glMultiTexCoord1fvARB(GLenum target, const GLfloat *v)
43 if (target != GL_TEXTURE0)
45 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
46 return;
48 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord1fv(v);
51 static void WINE_GLAPI wine_glMultiTexCoord2fARB(GLenum target, GLfloat s, GLfloat t)
53 if (target != GL_TEXTURE0)
55 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
56 return;
58 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord2f(s, t);
61 static void WINE_GLAPI wine_glMultiTexCoord2fvARB(GLenum target, const GLfloat *v)
63 if (target != GL_TEXTURE0)
65 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
66 return;
68 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord2fv(v);
71 static void WINE_GLAPI wine_glMultiTexCoord3fARB(GLenum target, GLfloat s, GLfloat t, GLfloat r)
73 if (target != GL_TEXTURE0)
75 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
76 return;
78 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord3f(s, t, r);
81 static void WINE_GLAPI wine_glMultiTexCoord3fvARB(GLenum target, const GLfloat *v)
83 if (target != GL_TEXTURE0)
85 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
86 return;
88 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord3fv(v);
91 static void WINE_GLAPI wine_glMultiTexCoord4fARB(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q)
93 if (target != GL_TEXTURE0)
95 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
96 return;
98 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord4f(s, t, r, q);
101 static void WINE_GLAPI wine_glMultiTexCoord4fvARB(GLenum target, const GLfloat *v)
103 if (target != GL_TEXTURE0)
105 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
106 return;
108 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord4fv(v);
111 static void WINE_GLAPI wine_glMultiTexCoord2svARB(GLenum target, const GLshort *v)
113 if (target != GL_TEXTURE0)
115 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
116 return;
118 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord2sv(v);
121 static void WINE_GLAPI wine_glMultiTexCoord4svARB(GLenum target, const GLshort *v)
123 if (target != GL_TEXTURE0)
125 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported.\n");
126 return;
128 wined3d_context_gl_get_current()->gl_info->gl_ops.gl.p_glTexCoord4sv(v);
131 static void WINE_GLAPI wine_glActiveTexture(GLenum texture)
133 if(texture != GL_TEXTURE0) {
134 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported\n");
135 return;
139 static void WINE_GLAPI wine_glClientActiveTextureARB(GLenum texture) {
140 if(texture != GL_TEXTURE0) {
141 ERR("Texture unit > 0 used, but GL_ARB_multitexture is not supported\n");
142 return;
146 static void (WINE_GLAPI *old_multitex_glGetIntegerv) (GLenum pname, GLint* params) = NULL;
147 static void WINE_GLAPI wine_glGetIntegerv(GLenum pname, GLint* params) {
148 switch(pname) {
149 case GL_ACTIVE_TEXTURE: *params = 0; break;
150 case GL_MAX_TEXTURE_UNITS_ARB: *params = 1; break;
151 default: old_multitex_glGetIntegerv(pname, params);
155 static void (WINE_GLAPI *old_multitex_glGetFloatv) (GLenum pname, GLfloat* params) = NULL;
156 static void WINE_GLAPI wine_glGetFloatv(GLenum pname, GLfloat* params) {
157 if (pname == GL_ACTIVE_TEXTURE) *params = 0.0f;
158 else old_multitex_glGetFloatv(pname, params);
161 static void (WINE_GLAPI *old_multitex_glGetDoublev) (GLenum pname, GLdouble* params) = NULL;
162 static void WINE_GLAPI wine_glGetDoublev(GLenum pname, GLdouble* params) {
163 if(pname == GL_ACTIVE_TEXTURE) *params = 0.0;
164 else old_multitex_glGetDoublev(pname, params);
167 /* Start GL_EXT_fogcoord emulation */
168 static void (WINE_GLAPI *old_fogcoord_glEnable)(GLenum cap);
169 static void WINE_GLAPI wine_glEnable(GLenum cap)
171 if (cap == GL_FOG)
173 struct wined3d_context_gl *ctx = wined3d_context_gl_get_current();
175 ctx->fog_enabled = 1;
176 if (ctx->gl_fog_source != GL_FRAGMENT_DEPTH_EXT)
177 return;
179 old_fogcoord_glEnable(cap);
182 static void (WINE_GLAPI *old_fogcoord_glDisable)(GLenum cap);
183 static void WINE_GLAPI wine_glDisable(GLenum cap)
185 if (cap == GL_FOG)
187 struct wined3d_context_gl *ctx = wined3d_context_gl_get_current();
189 ctx->fog_enabled = 0;
190 if (ctx->gl_fog_source != GL_FRAGMENT_DEPTH_EXT)
191 return;
193 old_fogcoord_glDisable(cap);
196 static void (WINE_GLAPI *old_fogcoord_glFogi)(GLenum pname, GLint param);
197 static void WINE_GLAPI wine_glFogi(GLenum pname, GLint param)
199 struct wined3d_context_gl *ctx = wined3d_context_gl_get_current();
201 if (pname == GL_FOG_COORDINATE_SOURCE_EXT)
203 ctx->gl_fog_source = param;
204 if (param == GL_FRAGMENT_DEPTH_EXT)
206 if (ctx->fog_enabled)
207 old_fogcoord_glEnable(GL_FOG);
209 else
211 WARN_(d3d_perf)("Fog coordinates activated, but not supported. Using slow emulation.\n");
212 old_fogcoord_glDisable(GL_FOG);
215 else
217 if (pname == GL_FOG_START)
218 ctx->fog_start = (float)param;
219 else if (pname == GL_FOG_END)
220 ctx->fog_end = (float)param;
221 old_fogcoord_glFogi(pname, param);
225 static void (WINE_GLAPI *old_fogcoord_glFogiv)(GLenum pname, const GLint *param);
226 static void WINE_GLAPI wine_glFogiv(GLenum pname, const GLint *param)
228 struct wined3d_context_gl *ctx = wined3d_context_gl_get_current();
230 if (pname == GL_FOG_COORDINATE_SOURCE_EXT)
232 ctx->gl_fog_source = *param;
233 if (*param == GL_FRAGMENT_DEPTH_EXT)
235 if (ctx->fog_enabled)
236 old_fogcoord_glEnable(GL_FOG);
238 else
240 WARN_(d3d_perf)("Fog coordinates activated, but not supported. Using slow emulation.\n");
241 old_fogcoord_glDisable(GL_FOG);
244 else
246 if (pname == GL_FOG_START)
247 ctx->fog_start = (float)*param;
248 else if (pname == GL_FOG_END)
249 ctx->fog_end = (float)*param;
250 old_fogcoord_glFogiv(pname, param);
254 static void (WINE_GLAPI *old_fogcoord_glFogf)(GLenum pname, GLfloat param);
255 static void WINE_GLAPI wine_glFogf(GLenum pname, GLfloat param)
257 struct wined3d_context_gl *ctx = wined3d_context_gl_get_current();
259 if (pname == GL_FOG_COORDINATE_SOURCE_EXT)
261 ctx->gl_fog_source = (GLint)param;
262 if (param == GL_FRAGMENT_DEPTH_EXT)
264 if (ctx->fog_enabled)
265 old_fogcoord_glEnable(GL_FOG);
267 else
269 WARN_(d3d_perf)("Fog coordinates activated, but not supported. Using slow emulation.\n");
270 old_fogcoord_glDisable(GL_FOG);
273 else
275 if (pname == GL_FOG_START)
276 ctx->fog_start = param;
277 else if (pname == GL_FOG_END)
278 ctx->fog_end = param;
279 old_fogcoord_glFogf(pname, param);
283 static void (WINE_GLAPI *old_fogcoord_glFogfv)(GLenum pname, const GLfloat *param);
284 static void WINE_GLAPI wine_glFogfv(GLenum pname, const GLfloat *param)
286 struct wined3d_context_gl *ctx = wined3d_context_gl_get_current();
288 if (pname == GL_FOG_COORDINATE_SOURCE_EXT)
290 ctx->gl_fog_source = (GLint)*param;
291 if (*param == GL_FRAGMENT_DEPTH_EXT)
293 if (ctx->fog_enabled)
294 old_fogcoord_glEnable(GL_FOG);
296 else
298 WARN_(d3d_perf)("Fog coordinates activated, but not supported. Using slow emulation.\n");
299 old_fogcoord_glDisable(GL_FOG);
302 else
304 if (pname == GL_FOG_COLOR)
306 ctx->fog_colour[0] = param[0];
307 ctx->fog_colour[1] = param[1];
308 ctx->fog_colour[2] = param[2];
309 ctx->fog_colour[3] = param[3];
311 else if (pname == GL_FOG_START)
313 ctx->fog_start = *param;
315 else if (pname == GL_FOG_END)
317 ctx->fog_end = *param;
319 old_fogcoord_glFogfv(pname, param);
323 static void (WINE_GLAPI *old_fogcoord_glVertex4f)(GLfloat x, GLfloat y, GLfloat z, GLfloat w);
324 static void (WINE_GLAPI *old_fogcoord_glColor4f)(GLfloat r, GLfloat g, GLfloat b, GLfloat a);
326 static void WINE_GLAPI wine_glVertex4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
328 const struct wined3d_context_gl *ctx_gl = wined3d_context_gl_get_current();
330 /* This can be called from draw_test_quad() and at that point there is no
331 * wined3d_context current. */
332 if (!ctx_gl)
334 old_fogcoord_glVertex4f(x, y, z, w);
335 return;
338 if (ctx_gl->gl_fog_source == GL_FOG_COORDINATE_EXT && ctx_gl->fog_enabled)
340 GLfloat c[4] = {ctx_gl->colour[0], ctx_gl->colour[1], ctx_gl->colour[2], ctx_gl->colour[3]};
341 GLfloat i;
343 i = (ctx_gl->fog_end - ctx_gl->fog_coord_value) / (ctx_gl->fog_end - ctx_gl->fog_start);
344 c[0] = i * c[0] + (1.0f - i) * ctx_gl->fog_colour[0];
345 c[1] = i * c[1] + (1.0f - i) * ctx_gl->fog_colour[1];
346 c[2] = i * c[2] + (1.0f - i) * ctx_gl->fog_colour[2];
348 old_fogcoord_glColor4f(c[0], c[1], c[2], c[3]);
349 old_fogcoord_glVertex4f(x, y, z, w);
351 else
353 old_fogcoord_glVertex4f(x, y, z, w);
357 static void WINE_GLAPI wine_glVertex4fv(const GLfloat *pos) {
358 wine_glVertex4f(pos[0], pos[1], pos[2], pos[3]);
361 static void WINE_GLAPI wine_glVertex3f(GLfloat x, GLfloat y, GLfloat z) {
362 wine_glVertex4f(x, y, z, 1.0f);
365 static void WINE_GLAPI wine_glVertex3fv(const GLfloat *pos) {
366 wine_glVertex4f(pos[0], pos[1], pos[2], 1.0f);
369 static void WINE_GLAPI wine_glColor4f(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
371 struct wined3d_context_gl *ctx_gl = wined3d_context_gl_get_current();
373 /* This can be called from draw_test_quad() and at that point there is no
374 * wined3d_context current. */
375 if (!ctx_gl)
377 old_fogcoord_glColor4f(r, g, b, a);
378 return;
381 ctx_gl->colour[0] = r;
382 ctx_gl->colour[1] = g;
383 ctx_gl->colour[2] = b;
384 ctx_gl->colour[3] = a;
385 old_fogcoord_glColor4f(r, g, b, a);
388 static void WINE_GLAPI wine_glColor4fv(const GLfloat *c) {
389 wine_glColor4f(c[0], c[1], c[2], c[3]);
392 static void WINE_GLAPI wine_glColor3f(GLfloat r, GLfloat g, GLfloat b) {
393 wine_glColor4f(r, g, b, 1.0f);
396 static void WINE_GLAPI wine_glColor3fv(const GLfloat *c) {
397 wine_glColor4f(c[0], c[1], c[2], 1.0f);
400 static void WINE_GLAPI wine_glColor4ub(GLubyte r, GLubyte g, GLubyte b, GLubyte a) {
401 wine_glColor4f(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
404 /* In D3D the fog coord is a UBYTE, so there's no problem with using the
405 * single precision function. */
406 static void WINE_GLAPI wine_glFogCoordfEXT(GLfloat f)
408 struct wined3d_context_gl *ctx = wined3d_context_gl_get_current();
410 ctx->fog_coord_value = f;
412 static void WINE_GLAPI wine_glFogCoorddEXT(GLdouble f) {
413 wine_glFogCoordfEXT((GLfloat) f);
415 static void WINE_GLAPI wine_glFogCoordfvEXT(const GLfloat *f) {
416 wine_glFogCoordfEXT(*f);
418 static void WINE_GLAPI wine_glFogCoorddvEXT(const GLdouble *f) {
419 wine_glFogCoordfEXT((GLfloat) *f);
422 /* End GL_EXT_fog_coord emulation */
424 void install_gl_compat_wrapper(struct wined3d_gl_info *gl_info, enum wined3d_gl_extension ext)
426 switch (ext)
428 case ARB_MULTITEXTURE:
429 if (gl_info->supported[ARB_MULTITEXTURE])
430 return;
431 if (gl_info->gl_ops.ext.p_glActiveTexture == wine_glActiveTexture)
433 FIXME("ARB_multitexture emulation hooks already applied.\n");
434 return;
436 TRACE("Applying GL_ARB_multitexture emulation hooks.\n");
437 gl_info->gl_ops.ext.p_glActiveTexture = wine_glActiveTexture;
438 gl_info->gl_ops.ext.p_glClientActiveTextureARB = wine_glClientActiveTextureARB;
439 gl_info->gl_ops.ext.p_glMultiTexCoord1fARB = wine_glMultiTexCoord1fARB;
440 gl_info->gl_ops.ext.p_glMultiTexCoord1fvARB = wine_glMultiTexCoord1fvARB;
441 gl_info->gl_ops.ext.p_glMultiTexCoord2fARB = wine_glMultiTexCoord2fARB;
442 gl_info->gl_ops.ext.p_glMultiTexCoord2fvARB = wine_glMultiTexCoord2fvARB;
443 gl_info->gl_ops.ext.p_glMultiTexCoord3fARB = wine_glMultiTexCoord3fARB;
444 gl_info->gl_ops.ext.p_glMultiTexCoord3fvARB = wine_glMultiTexCoord3fvARB;
445 gl_info->gl_ops.ext.p_glMultiTexCoord4fARB = wine_glMultiTexCoord4fARB;
446 gl_info->gl_ops.ext.p_glMultiTexCoord4fvARB = wine_glMultiTexCoord4fvARB;
447 gl_info->gl_ops.ext.p_glMultiTexCoord2svARB = wine_glMultiTexCoord2svARB;
448 gl_info->gl_ops.ext.p_glMultiTexCoord4svARB = wine_glMultiTexCoord4svARB;
449 old_multitex_glGetIntegerv = gl_info->gl_ops.gl.p_glGetIntegerv;
450 gl_info->gl_ops.gl.p_glGetIntegerv = wine_glGetIntegerv;
451 old_multitex_glGetFloatv = gl_info->gl_ops.gl.p_glGetFloatv;
452 gl_info->gl_ops.gl.p_glGetFloatv = wine_glGetFloatv;
453 old_multitex_glGetDoublev = gl_info->gl_ops.gl.p_glGetDoublev;
454 gl_info->gl_ops.gl.p_glGetDoublev = wine_glGetDoublev;
455 gl_info->supported[ARB_MULTITEXTURE] = TRUE;
456 return;
458 case EXT_FOG_COORD:
459 /* This emulation isn't perfect. There are a number of potential problems, but they should
460 * not matter in practise:
462 * Fog vs fragment shader: If we are using GL_ARB_fragment_program with the fog option, the
463 * glDisable(GL_FOG) here won't matter. However, if we have GL_ARB_fragment_program, it is pretty
464 * unlikely that we don't have GL_EXT_fog_coord. Besides, we probably have GL_ARB_vertex_program
465 * too, which would allow fog coord emulation in a fixed function vertex pipeline replacement.
467 * Fog vs texture: We apply the fog in the vertex color. An app could set up texturing settings which
468 * ignore the vertex color, thus effectively disabling our fog. However, in D3D this type of fog is
469 * a per-vertex fog too, so the apps shouldn't do that.
471 * Fog vs lighting: The app could in theory use D3DFOG_NONE table and D3DFOG_NONE vertex fog with
472 * untransformed vertices. That enables lighting and fog coords at the same time, and the lighting
473 * calculations could affect the already blended in fog color. There's nothing we can do against that,
474 * but most apps using fog color do their own lighting too and often even use RHW vertices. So live
475 * with it.
477 if (gl_info->supported[EXT_FOG_COORD])
478 return;
479 if (gl_info->gl_ops.gl.p_glFogi == wine_glFogi)
481 FIXME("EXT_fog_coord emulation hooks already applied.\n");
482 return;
484 TRACE("Applying GL_ARB_fog_coord emulation hooks\n");
486 /* This probably means that the implementation doesn't advertise the extension, but implicitly supports
487 * it via the GL core version, or someone messed around in the extension table in directx.c. Add version-
488 * dependent loading for this extension if we ever hit this situation
490 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
492 FIXME("GL implementation supports GL_ARB_fragment_program but not GL_EXT_fog_coord\n");
493 FIXME("The fog coord emulation will most likely fail\n");
495 else if (gl_info->supported[ARB_FRAGMENT_SHADER])
497 FIXME("GL implementation supports GL_ARB_fragment_shader but not GL_EXT_fog_coord\n");
498 FIXME("The fog coord emulation will most likely fail\n");
501 old_fogcoord_glFogi = gl_info->gl_ops.gl.p_glFogi;
502 gl_info->gl_ops.gl.p_glFogi = wine_glFogi;
503 old_fogcoord_glFogiv = gl_info->gl_ops.gl.p_glFogiv;
504 gl_info->gl_ops.gl.p_glFogiv = wine_glFogiv;
505 old_fogcoord_glFogf = gl_info->gl_ops.gl.p_glFogf;
506 gl_info->gl_ops.gl.p_glFogf = wine_glFogf;
507 old_fogcoord_glFogfv = gl_info->gl_ops.gl.p_glFogfv;
508 gl_info->gl_ops.gl.p_glFogfv = wine_glFogfv;
509 old_fogcoord_glEnable = gl_info->p_glEnableWINE;
510 gl_info->p_glEnableWINE = wine_glEnable;
511 old_fogcoord_glDisable = gl_info->p_glDisableWINE;
512 gl_info->p_glDisableWINE = wine_glDisable;
514 old_fogcoord_glVertex4f = gl_info->gl_ops.gl.p_glVertex4f;
515 gl_info->gl_ops.gl.p_glVertex4f = wine_glVertex4f;
516 gl_info->gl_ops.gl.p_glVertex4fv = wine_glVertex4fv;
517 gl_info->gl_ops.gl.p_glVertex3f = wine_glVertex3f;
518 gl_info->gl_ops.gl.p_glVertex3fv = wine_glVertex3fv;
520 old_fogcoord_glColor4f = gl_info->gl_ops.gl.p_glColor4f;
521 gl_info->gl_ops.gl.p_glColor4f = wine_glColor4f;
522 gl_info->gl_ops.gl.p_glColor4fv = wine_glColor4fv;
523 gl_info->gl_ops.gl.p_glColor3f = wine_glColor3f;
524 gl_info->gl_ops.gl.p_glColor3fv = wine_glColor3fv;
525 gl_info->gl_ops.gl.p_glColor4ub = wine_glColor4ub;
527 gl_info->gl_ops.ext.p_glFogCoordfEXT = wine_glFogCoordfEXT;
528 gl_info->gl_ops.ext.p_glFogCoordfvEXT = wine_glFogCoordfvEXT;
529 gl_info->gl_ops.ext.p_glFogCoorddEXT = wine_glFogCoorddEXT;
530 gl_info->gl_ops.ext.p_glFogCoorddvEXT = wine_glFogCoorddvEXT;
531 gl_info->supported[EXT_FOG_COORD] = TRUE;
532 return;
534 default:
535 FIXME("Extension %u emulation not supported.\n", ext);