glsl_shader.c revision b955672b950093ff7416d1269dd4d3b69983bd8f
/*
* GLSL pixel and vertex shader implementation
*
* Copyright 2006 Jason Green
* Copyright 2006-2007 Henri Verbeet
* Copyright 2007-2008 Stefan Dösinger for CodeWeavers
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
* Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
* other than GPL or LGPL is available it will apply instead, Sun elects to use only
* the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
* a choice of LGPL license versions is made available with the language indicating
* that LGPLv2 or any later version may be used, or where a choice of which version
* of the LGPL is applied is otherwise unspecified.
*/
/*
* D3D shader asm has swizzles on source parameters, and write masks for
* destination parameters. GLSL uses swizzles for both. The result of this is
* that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
* Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
* mask for the destination parameter into account.
*/
#include "config.h"
#include <stdio.h>
#include "wined3d_private.h"
#define GLINFO_LOCATION (*gl_info)
typedef struct {
char reg_name[150];
char mask_str[6];
typedef struct {
char reg_name[150];
char param_str[100];
typedef struct {
const char *name;
/* GLSL shader private data */
struct shader_glsl_priv {
struct hash_table_t *glsl_program_lookup;
const struct glsl_shader_prog_link *glsl_program;
};
/* Struct to maintain data about a linked GLSL program */
struct glsl_shader_prog_link {
struct list vshader_entry;
struct list pshader_entry;
struct ps_compile_args ps_args;
};
typedef struct {
struct ps_compile_args ps_args;
/** Prints the GLSL info log which will contain error messages if they exist */
{
int infologLength = 0;
char *infoLog;
unsigned int i;
const char *spam[] = {
"Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
"Fragment shader was successfully compiled to run on hardware.\n", /* fglrx */
"Fragment shader(s) linked, vertex shader(s) linked. \n ", /* fglrx, with \n */
"Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
"Vertex shader(s) linked, no fragment shader(s) defined. \n ", /* fglrx, with \n */
"Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
"Fragment shader was successfully compiled to run on hardware.\nWARNING: 0:1: extension 'GL_ARB_draw_buffers' is not supported",
"Fragment shader(s) linked, no vertex shader(s) defined.", /* fglrx, no \n */
"Fragment shader(s) linked, no vertex shader(s) defined. \n ", /* fglrx, with \n */
"WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported\n" /* MacOS ati */
};
&infologLength));
/* A size of 1 is just a null-terminated string, so the log should be bigger than
* that if there are errors. */
if (infologLength > 1)
{
/* Fglrx doesn't terminate the string properly, but it tells us the proper length.
* So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
*/
break;
}
}
if(is_spam) {
} else {
}
}
}
/**
* Loads (pixel shader) samplers
*/
static void shader_glsl_load_psamplers(const WineD3D_GL_Info *gl_info, IWineD3DStateBlock *iface, GLhandleARB programId)
{
int i;
char sampler_name[20];
for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
if (name_loc != -1) {
checkGLcall("glUniform1iARB");
} else {
}
}
}
}
static void shader_glsl_load_vsamplers(const WineD3D_GL_Info *gl_info, IWineD3DStateBlock *iface, GLhandleARB programId)
{
char sampler_name[20];
int i;
for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
if (name_loc != -1) {
checkGLcall("glUniform1iARB");
} else {
}
}
}
}
/**
* Loads floating point constants (aka uniforms) into the currently set GLSL program.
* When constant_list == NULL, it will load all the constants.
*/
static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
const struct list *constant_list)
{
const constants_entry *constant;
const local_constant *lconst;
DWORD i, j, k;
if (TRACE_ON(d3d_shader)) {
while (j--) {
i = *idx++;
tmp_loc = constant_locations[i];
if (tmp_loc != -1) {
}
}
}
}
/* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
float lcl_const[4];
while (j--) {
i = *idx++;
tmp_loc = constant_locations[i];
if (tmp_loc != -1) {
/* We found this uniform name in the program - go ahead and send the data */
k = i * 4;
}
}
}
} else {
while (j--) {
i = *idx++;
tmp_loc = constant_locations[i];
if (tmp_loc != -1) {
/* We found this uniform name in the program - go ahead and send the data */
}
}
}
}
checkGLcall("glUniform4fvARB()");
TRACE("No need to load local float constants for this shader\n");
return;
}
/* Load immediate constants */
if (TRACE_ON(d3d_shader)) {
if (tmp_loc != -1) {
}
}
}
/* Immediate constants are clamped to [-1;1] at shader creation time if needed */
if (tmp_loc != -1) {
/* We found this uniform name in the program - go ahead and send the data */
}
}
checkGLcall("glUniform4fvARB()");
}
/* Loads integer constants (aka uniforms) into the currently set GLSL program. */
static void shader_glsl_load_constantsI(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
{
unsigned int i;
{
if (!(constants_set & 1)) continue;
/* We found this uniform name in the program - go ahead and send the data */
checkGLcall("glUniform4ivARB");
}
/* Load immediate constants */
while (ptr) {
/* We found this uniform name in the program - go ahead and send the data */
checkGLcall("glUniform4ivARB");
}
}
/* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
static void shader_glsl_load_constantsB(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
{
unsigned int i;
char tmp_name[8];
/* TODO: Benchmark and see if it would be beneficial to store the
* locations of the constants to avoid looking up each time */
{
if (!(constants_set & 1)) continue;
/* TODO: Benchmark and see if it would be beneficial to store the
* locations of the constants to avoid looking up each time */
if (tmp_loc != -1)
{
/* We found this uniform name in the program - go ahead and send the data */
checkGLcall("glUniform1ivARB");
}
}
/* Load immediate constants */
while (ptr) {
if (tmp_loc != -1) {
/* We found this uniform name in the program - go ahead and send the data */
checkGLcall("glUniform1ivARB");
}
}
}
/**
* Loads the app-supplied constants into the currently set GLSL program.
*/
static void shader_glsl_load_constants(
char usePixelShader,
char useVertexShader) {
const GLhandleARB *constant_locations;
const struct list *constant_list;
int i;
if (!prog) {
/* No GLSL program set - nothing to do. */
return;
}
if (useVertexShader) {
/* Upload the position fixup params */
checkGLcall("glUniform4fvARB");
}
if (usePixelShader) {
/* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
* It can't be 0 for a valid texbem instruction.
*/
const float *data = (const float *)&stateBlock->textureState[(int)ps->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
checkGLcall("glUniformMatrix2fvARB");
/* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
* is set too, so we can check that in the needsbumpmat check
*/
const GLfloat *offset = (const GLfloat *)&stateBlock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET];
checkGLcall("glUniform1fvARB");
checkGLcall("glUniform1fvARB");
}
}
float correction_params[4];
if(deviceImpl->render_offscreen) {
correction_params[0] = 0.0;
} else {
/* position is window relative, not viewport relative */
}
}
}
}
/** Generate the variable & register declarations for the GLSL output target */
static void shader_generate_glsl_declarations(IWineD3DBaseShader *iface, const shader_reg_maps *reg_maps,
{
unsigned int i, extra_constants_needed = 0;
const local_constant *lconst;
/* There are some minor differences between pixel and vertex shaders */
/* Prototype the subroutines */
}
/* Declare the constants (aka uniforms) */
}
if(!pshader) {
/* Predeclaration; This function is added at link time based on the pixel shader.
* VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
* that. We know the input to the reorder function at vertex shader compile time, so
* we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
* read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
* 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
* it will write to the varying array. Here we depend on the shader optimizer on sorting that
* out. The nvidia driver only does that if the parameter is inout instead of out, hence the
* inout.
*/
} else {
}
} else {
ps_impl->numbumpenvmatconsts = 0;
continue;
}
if(reg_maps->luminanceparams) {
} else {
}
}
}
if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
} else {
/* This happens because we do not have proper tracking of the constant registers that are
* actually used, only the max limit of the shader version
*/
FIXME("Cannot find a free uniform for vpos correction params\n");
device->render_offscreen ? 0.0 : ((IWineD3DSurfaceImpl *) device->render_targets[0])->currentDesc.Height,
}
}
}
/* Declare texture samplers */
switch (stype) {
case WINED3DSTT_1D:
break;
case WINED3DSTT_2D:
IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[i]) == GL_TEXTURE_RECTANGLE_ARB) {
} else {
}
break;
case WINED3DSTT_CUBE:
break;
case WINED3DSTT_VOLUME:
break;
default:
break;
}
}
}
/* Declare address variables */
}
/* Declare texture coordinate temporaries and initialize them */
}
/* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
* helper function shader that is linked in at link time
*/
} else {
/* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
* For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
* pixel shader that reads the fixed function color into the packed input registers.
*/
}
}
/* Declare output register temporaries */
}
/* Declare temporary variables */
}
/* Declare attributes */
if (reg_maps->attributes[i])
}
/* Declare loop registers aLx */
for (i = 0; i < reg_maps->loop_depth; i++) {
}
/* Temporary variables for matrix operations */
/* Local constants use a different name so they can be loaded once at shader link time
* They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
* float -> string conversion can cause precision loss.
*/
}
}
/* Start the main program */
/* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
* off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
* not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
* precision troubles when we just substract 0.5.
*
* To deal with that just floor() the position. This will eliminate the fraction on all cards.
*
* TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
*
* An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
* questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
* coordinates specify the pixel centers instead of the pixel corners. This code will behave
* correctly on drivers that returns integer values.
*/
shader_addline(buffer, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
}
}
/*****************************************************************************
* Functions to generate GLSL strings from DirectX Shader bytecode begin here.
*
* For more information, see http://wiki.winehq.org/DirectX-Shaders
****************************************************************************/
/* Prototypes */
/** Used for opcode modifiers - They multiply the result by the specified amount */
static const char * const shift_glsl_tab[] = {
"", /* 0 (none) */
"2.0 * ", /* 1 (x2) */
"4.0 * ", /* 2 (x4) */
"8.0 * ", /* 3 (x8) */
"16.0 * ", /* 4 (x16) */
"32.0 * ", /* 5 (x32) */
"", /* 6 (x64) */
"", /* 7 (x128) */
"", /* 8 (d256) */
"", /* 9 (d128) */
"", /* 10 (d64) */
"", /* 11 (d32) */
"0.0625 * ", /* 12 (d16) */
"0.125 * ", /* 13 (d8) */
"0.25 * ", /* 14 (d4) */
"0.5 * " /* 15 (d2) */
};
/* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
static void shader_glsl_gen_modifier (
const char *in_reg,
const char *in_regswizzle,
char *out_str) {
out_str[0] = 0;
if (instr == WINED3DSIO_TEXKILL)
return;
switch (instr & WINED3DSP_SRCMOD_MASK) {
case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
case WINED3DSPSM_DW:
case WINED3DSPSM_NONE:
break;
case WINED3DSPSM_NEG:
break;
case WINED3DSPSM_NOT:
break;
case WINED3DSPSM_BIAS:
break;
case WINED3DSPSM_BIASNEG:
break;
case WINED3DSPSM_SIGN:
break;
case WINED3DSPSM_SIGNNEG:
break;
case WINED3DSPSM_COMP:
break;
case WINED3DSPSM_X2:
break;
case WINED3DSPSM_X2NEG:
break;
case WINED3DSPSM_ABS:
break;
case WINED3DSPSM_ABSNEG:
break;
default:
}
}
/** Writes the GLSL variable name that corresponds to the register that the
* DX opcode parameter is trying to access */
{
/* oPos, oFog and oPts in D3D */
static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
char tmpStr[150];
switch (regtype) {
case WINED3DSPR_TEMP:
break;
case WINED3DSPR_INPUT:
if (pshader) {
/* Pixel shaders >= 3.0 */
if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
/* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
* operation there
*/
} else {
sprintf(tmpStr, "IN[%s + %u]", rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg]);
}
} else {
} else {
}
}
} else {
} else {
}
}
} else {
if (reg==0)
else
}
} else {
}
break;
case WINED3DSPR_CONST:
{
/* Relative addressing */
if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
/* Relative addressing on shaders 2.0+ have a relative address token,
* prior to that, it was hard-coded as "A0.x" because there's only 1 register */
if(reg) {
} else {
}
} else {
if(reg) {
} else {
}
}
} else {
} else {
}
}
break;
}
case WINED3DSPR_CONSTINT:
if (pshader)
else
break;
case WINED3DSPR_CONSTBOOL:
if (pshader)
else
break;
case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
if (pshader) {
} else {
}
break;
case WINED3DSPR_LOOP:
break;
case WINED3DSPR_SAMPLER:
if (pshader)
else
break;
case WINED3DSPR_COLOROUT:
}
if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
} else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
}
break;
case WINED3DSPR_RASTOUT:
break;
case WINED3DSPR_DEPTHOUT:
break;
case WINED3DSPR_ATTROUT:
if (reg == 0) {
} else {
}
break;
case WINED3DSPR_TEXCRDOUT:
/* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
else
break;
case WINED3DSPR_MISCTYPE:
if (reg == 0) {
/* vPos */
} else if (reg == 1){
/* Note that gl_FrontFacing is a bool, while vFace is
*/
} else {
}
break;
default:
break;
}
}
/* Get the GLSL write mask for the destination register */
char *ptr = write_mask;
if (shader_is_scalar(param)) {
} else {
*ptr++ = '.';
}
*ptr = '\0';
return mask;
}
unsigned int size = 0;
return size;
}
/* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
* but addressed as "rgba". To fix this we need to swap the register's x
* and z components. */
char *ptr = swizzle_str;
if (!shader_is_scalar(param)) {
*ptr++ = '.';
/* swizzle bits fields: wwzzyyxx */
}
*ptr = '\0';
}
/* From a given parameter token, generate the corresponding GLSL string.
* Also, return the actual register name and swizzle in case the
* caller needs this information as well. */
{
char swizzle_str[6];
swizzle_str[0] = '\0';
}
/* From a given parameter token, generate the corresponding GLSL string.
* Also, return the actual register name and swizzle in case the
* caller needs this information as well. */
{
}
/* Append the destination part of the instruction to the buffer, return the effective write mask */
static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer, const SHADER_OPCODE_ARG *arg, const DWORD param)
{
int shift;
if(mask) {
shader_addline(buffer, "%s%s = %s(", dst_param.reg_name, dst_param.mask_str, shift_glsl_tab[shift]);
}
return mask;
}
/* Append the destination part of the instruction to the buffer, return the effective write mask */
{
}
/** Process GLSL instruction modifiers */
{
if (mask & WINED3DSPDM_SATURATE) {
/* _SAT means to clamp the value of the register to between 0 and 1 */
}
if (mask & WINED3DSPDM_MSAMPCENTROID) {
FIXME("_centroid modifier not handled\n");
}
if (mask & WINED3DSPDM_PARTIALPRECISION) {
/* MSDN says this modifier can be safely ignored, so that's what we'll do. */
}
}
}
static inline const char* shader_get_comp_op(
switch (op) {
case COMPARISON_GT: return ">";
case COMPARISON_EQ: return "==";
case COMPARISON_GE: return ">=";
case COMPARISON_LT: return "<";
case COMPARISON_NE: return "!=";
case COMPARISON_LE: return "<=";
default:
return "(\?\?)";
}
}
static void shader_glsl_get_sample_function(DWORD sampler_type, BOOL projected, BOOL texrect, glsl_sample_function_t *sample_function) {
/* Note that there's no such thing as a projected cube texture. */
switch(sampler_type) {
case WINED3DSTT_1D:
break;
case WINED3DSTT_2D:
if(texrect) {
} else {
}
break;
case WINED3DSTT_CUBE:
sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
break;
case WINED3DSTT_VOLUME:
sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
break;
default:
sample_function->coord_mask = 0;
break;
}
}
{
switch(channel_source)
{
case CHANNEL_SOURCE_ZERO:
break;
case CHANNEL_SOURCE_ONE:
break;
case CHANNEL_SOURCE_X:
break;
case CHANNEL_SOURCE_Y:
break;
case CHANNEL_SOURCE_Z:
break;
case CHANNEL_SOURCE_W:
break;
default:
break;
}
}
static void shader_glsl_color_correction(const struct SHADER_OPCODE_ARG *arg, struct color_fixup_desc fixup)
{
char arguments[256];
mask = 0;
if (!mask) return; /* Nothing to do */
if (is_yuv_fixup(fixup))
{
return;
}
arguments[0] = '\0';
if (mask & WINED3DSP_WRITEMASK_0)
{
}
if (mask & WINED3DSP_WRITEMASK_1)
{
}
if (mask & WINED3DSP_WRITEMASK_2)
{
}
if (mask & WINED3DSP_WRITEMASK_3)
{
}
if (mask_size > 1)
{
}
else
{
}
}
/*****************************************************************************
*
* Begin processing individual instruction opcodes
*
****************************************************************************/
/* Generate GLSL arithmetic functions (dst = src1 + src2) */
{
char op;
/* Determine the GLSL operator to use based on the opcode */
default:
op = ' ';
break;
}
}
/* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
{
/* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
* shader versions WINED3DSIO_MOVA is used for this. */
/* This is a simple floor() */
if (mask_size > 1) {
} else {
}
/* We need to *round* to the nearest int here. */
if (mask_size > 1) {
shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n", mask_size, src0_param.param_str, mask_size, src0_param.param_str);
} else {
shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
}
} else {
}
}
/* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
{
unsigned int dst_size = 0;
/* dp3 works on vec3, dp4 on vec4 */
} else {
}
if (dst_size > 1) {
shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
} else {
}
}
/* Note that this instruction has some restrictions. The destination write mask
* can't contain the w component, and the source swizzles have to be .xyzw */
{
char dst_mask[6];
shader_addline(arg->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
}
/* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
* Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
* GLSL uses the value as-is. */
{
unsigned int dst_size;
if (dst_size > 1) {
shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
} else {
}
}
/* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
* Src0 is a scalar. Note that D3D uses the absolute of src0, while
* GLSL uses the value as-is. */
{
unsigned int dst_size;
if (dst_size > 1) {
} else {
}
}
/* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
{
const char *instruction;
char arguments[256];
unsigned i;
/* Determine the GLSL function to use based on the opcode */
/* TODO: Possibly make this a table for faster lookups */
default: instruction = "";
break;
}
arguments[0] = '\0';
if (curOpcode->num_params > 0) {
}
}
}
/** Process the WINED3DSIO_EXPP instruction in GLSL:
* For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
* dst.x = 2^(floor(src))
* dst.y = src - floor(src)
* dst.z = 2^src (partial precision is allowed, but optional)
* dst.w = 1.0;
* For 2.0 shaders, just do this (honoring writemask and swizzle):
* dst = 2^src; (partial precision is allowed, but optional)
*/
{
char dst_mask[6];
shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
} else {
unsigned int mask_size;
if (mask_size > 1) {
} else {
}
}
}
/** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
{
unsigned int mask_size;
if (mask_size > 1) {
} else {
}
}
{
unsigned int mask_size;
if (mask_size > 1) {
} else {
}
}
/** Process signed comparison opcodes in GLSL. */
{
unsigned int mask_size;
if (mask_size > 1) {
const char *compare;
default: compare = "";
}
} else {
case WINED3DSIO_SLT:
/* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
* to return 0.0 but step returns 1.0 because step is not < x
* An alternative is a bvec compare padded with an unused second component.
* step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
* issue. Playing with not() is not possible either because not() does not accept
* a scalar.
*/
shader_addline(arg->buffer, "(%s < %s) ? 1.0 : 0.0);\n", src0_param.param_str, src1_param.param_str);
break;
case WINED3DSIO_SGE:
/* Here we can use the step() function and safe a conditional */
break;
default:
}
}
}
/** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
{
unsigned int i, j;
char mask_char[6];
shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
} else {
/* Cycle through all source0 channels */
for (i=0; i<4; i++) {
write_mask = 0;
/* Find the destination channels which use the current source0 channel */
for (j=0; j<4; j++) {
write_mask |= WINED3DSP_WRITEMASK_0 << j;
cmp_channel = WINED3DSP_WRITEMASK_0 << j;
}
}
/* Splitting the cmp instruction up in multiple lines imposes a problem:
* The first lines may overwrite source parameters of the following lines.
* Deal with that by using a temporary destination register if needed
*/
write_mask = shader_glsl_get_write_mask(arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask), mask_char);
if (!write_mask) continue;
} else {
write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
if (!write_mask) continue;
}
}
if(temp_destination) {
}
}
}
/** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
/* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
* the compare is done per component of src0. */
{
unsigned int i, j;
/* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
} else {
}
return;
}
/* Cycle through all source0 channels */
for (i=0; i<4; i++) {
write_mask = 0;
/* Find the destination channels which use the current source0 channel */
for (j=0; j<4; j++) {
write_mask |= WINED3DSP_WRITEMASK_0 << j;
cmp_channel = WINED3DSP_WRITEMASK_0 << j;
}
}
write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
if (!write_mask) continue;
}
}
/** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
{
}
/** Handles transforming all WINED3DSIO_M?x? opcodes for
Vertex shaders to GLSL codes */
{
int i;
int nComponents = 0;
/* Set constants for the temporary argument */
case WINED3DSIO_M4x4:
nComponents = 4;
break;
case WINED3DSIO_M4x3:
nComponents = 3;
break;
case WINED3DSIO_M3x4:
nComponents = 4;
break;
case WINED3DSIO_M3x3:
nComponents = 3;
break;
case WINED3DSIO_M3x2:
nComponents = 2;
break;
default:
break;
}
for (i = 0; i < nComponents; i++) {
}
}
/**
The LRP instruction performs a component-wise linear interpolation
between the second and third operands using the first operand as the
blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
This is equivalent to mix(src2, src1, src0);
*/
{
}
/** Process the WINED3DSIO_LIT instruction in GLSL:
* dst.x = dst.w = 1.0
* dst.y = (src0.x > 0) ? src0.x
* dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
* where src.w is clamped at +- 128
*/
{
char dst_mask[6];
/* The sdk specifies the instruction like this
* dst.x = 1.0;
* if(src.x > 0.0) dst.y = src.x
* else dst.y = 0.0.
* if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
* else dst.z = 0.0;
* dst.w = 1.0;
*
* Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
* dst.x = 1.0 ... No further explanation needed
* dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
* dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
* dst.w = 1.0. ... Nothing fancy.
*
* So we still have one conditional in there. So do this:
* dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
*
* step(0.0, x) will return 1 if src.x > 0.0, and 0 otherwise. So if y is 0 we get pow(0.0 * 1.0, power),
* which sets dst.z to 0. If y > 0, but x = 0.0, we get pow(y * 0.0, power), which results in 0 too.
* if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
*/
shader_addline(arg->buffer, "vec4(1.0, max(%s, 0.0), pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
}
/** Process the WINED3DSIO_DST instruction in GLSL:
* dst.x = 1.0
* dst.y = src0.x * src0.y
* dst.z = src0.z
* dst.w = src1.w
*/
{
char dst_mask[6];
src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
}
/** Process the WINED3DSIO_SINCOS instruction in GLSL:
* VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
* can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
*
* dst.x = cos(src0.?)
* dst.y = sin(src0.?)
* dst.z = dst.z
* dst.w = dst.w
*/
{
switch (write_mask) {
case WINED3DSP_WRITEMASK_0:
break;
case WINED3DSP_WRITEMASK_1:
break;
case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
break;
default:
ERR("Write mask should be .x, .y or .xy\n");
break;
}
}
/** Process the WINED3DSIO_LOOP instruction in GLSL:
* Start a for() loop where src1.y is the initial value of aL,
* increment aL by src1.z for a total of src1.x iterations.
* Need to use a temporary variable for this operation.
*/
/* FIXME: I don't think nested loops will work correctly this way. */
{
const local_constant *constant;
shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
/* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
* varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
* known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
* addressing.
*/
if(regtype == WINED3DSPR_CONSTINT) {
break;
}
}
}
if(control_values) {
if(control_values[2] > 0) {
} else if(control_values[2] == 0) {
} else {
}
} else {
shader_addline(arg->buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
}
}
{
}
}
}
{
}
{
}
{
}
{
}
{
}
/* FIXME: According to MSDN the compare is done per component. */
{
}
{
}
{
}
{
}
/*********************************************
* Pixel Shader Specific Code begins here
********************************************/
{
char dst_swizzle[6];
/* All versions have a destination register */
/* 1.0-1.4: Use destination register as sampler source.
* 2.0+: Use provided sampler source. */
} else {
}
/* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
switch (flags & ~WINED3DTTFF_PROJECTED) {
case WINED3DTTFF_COUNT4:
}
} else {
}
if (src_mod == WINED3DSPSM_DZ) {
} else if (src_mod == WINED3DSPSM_DW) {
} else {
}
} else {
/* ps 2.0 texldp instruction always divides by the fourth component. */
} else {
}
}
IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
}
} else {
}
/* 1.0-1.3: Use destination register as coordinate source.
1.4+: Use provided coordinate source register. */
char coord_mask[6];
} else {
} else {
}
}
}
{
char dst_swizzle[6];
IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
}
shader_glsl_get_sample_function(sampler_type, FALSE, texrect, &sample_function); shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &coord_param);
/* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
* However, they seem to work just fine in fragment shaders as well. */
} else {
}
}
{
/* FIXME: Make this work for more than just 2D textures */
char dst_mask[6];
} else {
char dst_swizzle[6];
if (src_mod == WINED3DSPSM_DZ) {
if (mask_size > 1) {
shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
} else {
}
} else if (src_mod == WINED3DSPSM_DW) {
if (mask_size > 1) {
shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
} else {
}
} else {
}
}
}
/** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
* Take a 3-component dot product of the TexCoord[dstreg] and src,
* then perform a 1D texture lookup from stage dstregnum, place into dst. */
{
char dst_mask[6];
/* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
* scalar, and projected sampling would require 4.
*
* It is a dependent read - not valid with conditional NP2 textures
*/
case 1:
break;
case 2:
break;
case 3:
break;
default:
}
}
/** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
* Take a 3-component dot product of the TexCoord[dstreg] and src. */
{
unsigned int mask_size;
if (mask_size > 1) {
shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
} else {
}
}
/** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
* Calculate the depth as dst.x / dst.y */
{
/* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
* Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
* this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
* too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
* >= 1.0 or < 0.0
*/
shader_addline(arg->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n", dst_param.reg_name, dst_param.reg_name);
}
/** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
* Last row of a 3x2 matrix multiply, use the result to calculate the depth:
* Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
* depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
*/
{
shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
}
/** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
* Calculate the 1st of a 2-row matrix multiplication. */
{
}
/** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
* Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
{
shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
}
{
char dst_mask[6];
/* Sample the texture using the calculated coordinates */
}
/** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
* Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
{
char dst_mask[6];
/* Dependent read, not valid with conditional NP2 */
/* Sample the texture using the calculated coordinates */
current_state->current_row = 0;
}
/** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
* Perform the 3rd row of a 3x3 matrix multiply */
{
char dst_mask[6];
shader_addline(arg->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
current_state->current_row = 0;
}
/** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
* Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
{
char dst_mask[6];
/* Perform the last matrix multiply operation */
/* Reflection calculation */
/* Dependent read, not valid with conditional NP2 */
/* Sample the texture */
current_state->current_row = 0;
}
/** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
* Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
{
char dst_mask[6];
/* Perform the last matrix multiply operation */
/* Construct the eye-ray vector from w coordinates */
shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
/* Dependent read, not valid with conditional NP2 */
/* Sample the texture using the calculated coordinates */
current_state->current_row = 0;
}
/** Process the WINED3DSIO_TEXBEM instruction in GLSL.
* Apply a fake bump map transform.
* texbem is pshader <= 1.3 only, this saves a few version checks
*/
{
char dst_swizzle[6];
char coord_mask[6];
/* Dependent read, not valid with conditional NP2 */
/* with projective textures, texbem only divides the static texture coord, not the displacement,
* so we can't let the GL handle this.
*/
if (flags & WINED3DTTFF_PROJECTED) {
char coord_div_mask[3];
switch (flags & ~WINED3DTTFF_PROJECTED) {
case WINED3DTTFF_COUNT4:
}
shader_addline(arg->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
}
shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &coord_param);
shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &luminance_param);
shader_addline(arg->buffer, "(%s(Psampler%u, T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s )*(%s * luminancescale%d + luminanceoffset%d))%s);\n",
sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask,
} else {
sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask, dst_swizzle);
}
}
{
shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src0_param);
shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src1_param);
}
/** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
* Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
{
char dst_mask[6];
shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
}
/** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
* Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
{
char dst_mask[6];
shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
}
/** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
* Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
{
char dst_mask[6];
/* Dependent read, not valid with conditional NP2 */
shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
}
/** Process the WINED3DSIO_TEXKILL instruction in GLSL.
* If any of the first 3 components are < 0, discard this pixel */
{
/* The argument is a destination parameter, and no writemasks are allowed */
/* 2.0 shaders compare all 4 components in texkill */
shader_addline(arg->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
} else {
/* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
* instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
* 4 components are defined, only the first 3 are used
*/
shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
}
}
/** Process the WINED3DSIO_DP2ADD instruction in GLSL.
* dst = dot2(src0, src1) + src2 */
{
unsigned int mask_size;
shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
if (mask_size > 1) {
shader_addline(arg->buffer, "vec%d(dot(%s, %s) + %s));\n", mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
} else {
shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
}
}
{
unsigned int i;
for (i = 0; i < MAX_REG_INPUT; i++) {
char reg_mask[6];
/* Uninitialized */
if (!usage_token) continue;
switch(usage) {
} else {
}
break;
case WINED3DDECLUSAGE_COLOR:
if (usage_idx == 0)
else if (usage_idx == 1)
else
break;
default:
}
}
}
/*********************************************
* Vertex Shader Specific Code begins here
********************************************/
static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry) {
}
}
static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const WineD3D_GL_Info *gl_info,
struct glsl_shader_prog_link *entry)
{
}
{
unsigned int i, j;
char destination[50];
if (!semantics_out) {
/* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
}
for(i = 0; i < MAX_REG_INPUT; i++) {
if (!usage_token) continue;
FIXME("More input varyings declared than supported, expect issues\n");
continue;
} else if(map[i] == -1) {
/* Declared, but not read register */
continue;
}
} else {
}
if(!semantics_out) {
switch(usage) {
case WINED3DDECLUSAGE_COLOR:
if (usage_idx == 0)
else if (usage_idx == 1)
else
break;
if (usage_idx < 8) {
} else {
}
break;
case WINED3DDECLUSAGE_FOG:
break;
default:
}
} else {
for(j = 0; j < MAX_REG_OUTPUT; j++) {
if (!usage_token_out) continue;
usage_idx_out = (usage_token_out & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
usage_idx == usage_idx_out) {
}
}
if(!found) {
}
}
}
/* This is solely to make the compiler / linker happy and avoid warning about undefined
* varyings. It shouldn't result in any real code executed on the GPU, since all read
* input varyings are assigned above, if the optimizer works properly.
*/
for(i = 0; i < in_count + 2; i++) {
if(set[i] != WINED3DSP_WRITEMASK_ALL) {
unsigned int size = 0;
if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
size++;
}
if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
size++;
}
if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
size++;
}
if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
size++;
}
if (i == in_count) {
} else if (i == in_count + 1) {
} else {
}
if (size == 1) {
} else {
}
}
}
}
{
GLhandleARB ret = 0;
unsigned int i;
char reg_mask[6];
/* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
* Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
*/
}
}
} else {
}
/* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
for(i = 0; i < MAX_REG_OUTPUT; i++) {
if (!usage_token) continue;
switch(usage) {
case WINED3DDECLUSAGE_COLOR:
if (usage_idx == 0)
else if (usage_idx == 1)
break;
break;
if (usage_idx < 8) {
if(!(writemask & WINED3DSP_WRITEMASK_3)) {
}
}
break;
case WINED3DDECLUSAGE_PSIZE:
break;
case WINED3DDECLUSAGE_FOG:
break;
default:
break;
}
}
/* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
/* First, sort out position and point size. Those are not passed to the pixel shader */
for(i = 0; i < MAX_REG_OUTPUT; i++) {
if (!usage_token) continue;
switch(usage) {
break;
case WINED3DDECLUSAGE_PSIZE:
break;
default:
break;
}
}
/* Then, fix the pixel shader input */
/* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
* point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
* read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
*/
} else {
}
checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
checkGLcall("glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL)");
checkGLcall("glCompileShaderARB(ret)");
return ret;
}
static void hardcode_local_constants(IWineD3DBaseShaderImpl *shader, const WineD3D_GL_Info *gl_info,
{
const local_constant *lconst;
const float *value;
char glsl_name[8];
}
checkGLcall("Hardcoding local constants\n");
}
/** Sets the GLSL program ID for the given pixel and vertex shader combination.
* It sets the programId on the current StateBlock (because it should be called
* inside of the DrawPrimitive() part of the render loop).
*
* If a program for the given combination does not exist, create one, and store
* the program in the hash table. If it creates a program, it will link the
* given objects, too.
*/
GLhandleARB programId = 0;
int i;
char glsl_name[8];
struct ps_compile_args compile_args;
if(use_vs) {
} else {
vshader_id = 0;
}
if(use_ps) {
find_ps_compile_args((IWineD3DPixelShaderImpl*)This->stateBlock->pixelShader, This->stateBlock, &compile_args);
} else {
/* FIXME: Do we really have to spend CPU cycles to generate a few zeroed bytes? */
}
if (entry) {
return;
}
/* If we get to this point, then no matching program exists, so we create one */
/* Create the entry */
/* Add the hash table entry */
/* Set the current program */
/* Attach GLSL vshader */
if (vshader_id) {
char tmp_name[10];
checkGLcall("glAttachObjectARB");
/* Flag the reorder function for deletion, then it will be freed automatically when the program
* is destroyed
*/
checkGLcall("glAttachObjectARB");
/* Bind vertex attributes to a corresponding index number to match
* the same index numbers as ARB_vertex_programs (makes loading
* vertex attributes simpler). With this method, we can use the
* exact same code to load the attributes later for both ARB and
* GLSL shaders.
*
* We have to do this here because we need to know the Program ID
* in order to make the bindings work, and it has to be done prior
* to linking the GLSL program. */
for (i = 0; i < max_attribs; ++i) {
}
}
checkGLcall("glBindAttribLocationARB");
list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
}
if(use_ps) {
} else {
pshader_id = 0;
}
/* Attach GLSL pshader */
if (pshader_id) {
checkGLcall("glAttachObjectARB");
list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
}
/* Link the program */
entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
}
for (i = 0; i < MAX_CONST_I; ++i) {
}
entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
}
for (i = 0; i < MAX_CONST_I; ++i) {
}
if(pshader) {
char name[32];
}
}
checkGLcall("Find glsl program uniform locations");
if (pshader && WINED3DSHADER_VERSION_MAJOR(((IWineD3DPixelShaderImpl *)pshader)->baseShader.hex_version) >= 3
} else {
}
/* Set the shader to allow uniform loading on it */
checkGLcall("glUseProgramObjectARB(programId)");
/* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
* that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
* a pshader with fixed function pipeline is used there are no vertex samplers, and if a
* vertex shader with fixed function pixel processing is used we make sure that the card
* supports enough samplers to allow the max number of vertex samplers with all possible
* fixed function fragment processing setups. So once the program is linked these samplers
* won't change.
*/
if(vshader_id) {
/* Load vertex shader samplers */
}
if(pshader_id) {
/* Load pixel shader samplers */
}
/* If the local constants do not have to be loaded with the environment constants,
* load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
* later
*/
}
}
}
{
const char *blt_vshader[] = {
"#version 120\n"
"void main(void)\n"
"{\n"
" gl_Position = gl_Vertex;\n"
" gl_FrontColor = vec4(1.0);\n"
" gl_TexCoord[0] = gl_MultiTexCoord0;\n"
"}\n"
};
const char *blt_pshaders[tex_type_count] = {
/* tex_1d */
NULL,
/* tex_2d */
"#version 120\n"
"uniform sampler2D sampler;\n"
"void main(void)\n"
"{\n"
" gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
"}\n",
/* tex_3d */
NULL,
/* tex_cube */
"#version 120\n"
"uniform samplerCube sampler;\n"
"void main(void)\n"
"{\n"
" gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
"}\n",
/* tex_rect */
"#version 120\n"
"#extension GL_ARB_texture_rectangle : enable\n"
"uniform sampler2DRect sampler;\n"
"void main(void)\n"
"{\n"
" gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
"}\n",
};
if (!blt_pshaders[tex_type])
{
}
/* Once linked we can mark the shaders for deletion. They will be deleted once the program
* is destroyed
*/
return program_id;
}
GLhandleARB program_id = 0;
old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT)) {
checkGLcall("glClampColorARB");
} else {
FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
}
}
checkGLcall("glUseProgramObjectARB");
}
if (!*blt_program) {
} else {
}
}
checkGLcall("glUseProgramObjectARB");
}
}
const struct list *linked_programs;
/* Note: Do not use QueryInterface here to find out which shader type this is because this code
* can be called from IWineD3DBaseShader::Release
*/
if(pshader) {
if(ps->num_gl_shaders == 0) return;
} else {
}
TRACE("Deleting linked programs\n");
if (linked_programs->next) {
if(pshader) {
LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
}
} else {
LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
}
}
}
if(pshader) {
UINT i;
ENTER_GL();
for(i = 0; i < ps->num_gl_shaders; i++) {
checkGLcall("glDeleteObjectARB");
}
LEAVE_GL();
ps->num_gl_shaders = 0;
} else {
ENTER_GL();
checkGLcall("glDeleteObjectARB");
LEAVE_GL();
}
}
static unsigned int glsl_program_key_hash(const void *key)
{
return hash;
}
{
}
struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
return WINED3D_OK;
}
int i;
for (i = 0; i < tex_type_count; ++i)
{
if (priv->depth_blt_program[i])
{
}
}
}
/* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
return FALSE;
}
const char *fragcolor;
const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
/* Create the hw GLSL shader object and assign it as the shader->prgId */
if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
}
if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
/* The spec says that it doesn't have to be explicitly enabled, but the nvidia
* drivers write a warning if we don't do so
*/
}
/* Base Declarations */
/* Pack 3.0 inputs */
}
}
/* Base Shader Body */
/* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
/* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
else
}
if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
fragcolor = "gl_FragData[0]";
} else {
fragcolor = "gl_FragColor";
}
if(((IWineD3DDeviceImpl *)This->baseShader.device)->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
shader_addline(buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
shader_addline(buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
shader_addline(buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
shader_addline(buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
}
/* Pixel shader < 3.0 do not replace the fog stage.
* This implements linear fog computation and blending.
* TODO: non linear fog
* NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
* -1/(e-s) and e/(e-s) respectively.
*/
shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
}
/* Store the shader object */
return shader_obj;
}
const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
/* Create the hw GLSL shader program and assign it as the shader->prgId */
/* Base Declarations */
/* Base Shader Body */
/* Unpack 3.0 outputs */
} else {
}
/* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
/* Write the final position.
*
* OpenGL coordinates specify the center of the pixel while d3d coords specify
* the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
* 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
* contains 1.0 to allow a mad.
*/
/* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
*
* Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
* before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
* which is the same as z = z * 2 - w.
*/
/* Store the shader object */
}
static void shader_glsl_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
{
* models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
* vs_nv_version which is based on NV_vertex_program.
* For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
* using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
* supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
* of native instructions, so use that here. For more info see the pixel shader versioning code below.
*/
if((GLINFO_LOCATION.vs_nv_version == VS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
else
TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
/* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
* In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
* to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
* program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
* in max native instructions. Intel and others also offer the info in this extension but they
* don't support GLSL (at least on Windows).
*
* PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
* of instructions is 512 or less we have to do with ps2.0 hardware.
* NOTE: ps3.0 hardware requires 512 or more instructions but ati and nvidia offer 'enough' (1024 vs 4096) on their most basic ps3.0 hardware.
*/
if((GLINFO_LOCATION.ps_nv_version == PS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
else
/* FIXME: The following line is card dependent. -8.0 to 8.0 is the
* Direct3D minimum requirement.
*
* Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
* of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
*
* The problem is that the refrast clamps temporary results in the shader to
* [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
* then applications may miss the clamping behavior. On the other hand, if it is smaller,
* the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
* offer a way to query this.
*/
TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
}
{
{
TRACE("Checking support for fixup:\n");
}
/* We support everything except YUV conversions. */
if (!is_yuv_fixup(fixup))
{
TRACE("[OK]\n");
return TRUE;
}
TRACE("[FAILED]\n");
return FALSE;
}
{
/* WINED3DSIH_ABS */ shader_glsl_map2gl,
/* WINED3DSIH_ADD */ shader_glsl_arith,
/* WINED3DSIH_BEM */ pshader_glsl_bem,
/* WINED3DSIH_BREAK */ shader_glsl_break,
/* WINED3DSIH_BREAKC */ shader_glsl_breakc,
/* WINED3DSIH_BREAKP */ NULL,
/* WINED3DSIH_CALL */ shader_glsl_call,
/* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
/* WINED3DSIH_CMP */ shader_glsl_cmp,
/* WINED3DSIH_CND */ shader_glsl_cnd,
/* WINED3DSIH_CRS */ shader_glsl_cross,
/* WINED3DSIH_DCL */ NULL,
/* WINED3DSIH_DEF */ NULL,
/* WINED3DSIH_DEFB */ NULL,
/* WINED3DSIH_DEFI */ NULL,
/* WINED3DSIH_DP2ADD */ pshader_glsl_dp2add,
/* WINED3DSIH_DP3 */ shader_glsl_dot,
/* WINED3DSIH_DP4 */ shader_glsl_dot,
/* WINED3DSIH_DST */ shader_glsl_dst,
/* WINED3DSIH_DSX */ shader_glsl_map2gl,
/* WINED3DSIH_DSY */ shader_glsl_map2gl,
/* WINED3DSIH_ELSE */ shader_glsl_else,
/* WINED3DSIH_ENDIF */ shader_glsl_end,
/* WINED3DSIH_ENDLOOP */ shader_glsl_end,
/* WINED3DSIH_ENDREP */ shader_glsl_end,
/* WINED3DSIH_EXP */ shader_glsl_map2gl,
/* WINED3DSIH_EXPP */ shader_glsl_expp,
/* WINED3DSIH_FRC */ shader_glsl_map2gl,
/* WINED3DSIH_IF */ shader_glsl_if,
/* WINED3DSIH_IFC */ shader_glsl_ifc,
/* WINED3DSIH_LABEL */ shader_glsl_label,
/* WINED3DSIH_LIT */ shader_glsl_lit,
/* WINED3DSIH_LOG */ shader_glsl_log,
/* WINED3DSIH_LOGP */ shader_glsl_log,
/* WINED3DSIH_LOOP */ shader_glsl_loop,
/* WINED3DSIH_LRP */ shader_glsl_lrp,
/* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
/* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
/* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
/* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
/* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
/* WINED3DSIH_MAD */ shader_glsl_mad,
/* WINED3DSIH_MAX */ shader_glsl_map2gl,
/* WINED3DSIH_MIN */ shader_glsl_map2gl,
/* WINED3DSIH_MOV */ shader_glsl_mov,
/* WINED3DSIH_MOVA */ shader_glsl_mov,
/* WINED3DSIH_MUL */ shader_glsl_arith,
/* WINED3DSIH_NOP */ NULL,
/* WINED3DSIH_NRM */ shader_glsl_map2gl,
/* WINED3DSIH_PHASE */ NULL,
/* WINED3DSIH_POW */ shader_glsl_pow,
/* WINED3DSIH_RCP */ shader_glsl_rcp,
/* WINED3DSIH_REP */ shader_glsl_rep,
/* WINED3DSIH_RET */ NULL,
/* WINED3DSIH_RSQ */ shader_glsl_rsq,
/* WINED3DSIH_SETP */ NULL,
/* WINED3DSIH_SGE */ shader_glsl_compare,
/* WINED3DSIH_SGN */ shader_glsl_map2gl,
/* WINED3DSIH_SINCOS */ shader_glsl_sincos,
/* WINED3DSIH_SLT */ shader_glsl_compare,
/* WINED3DSIH_SUB */ shader_glsl_arith,
/* WINED3DSIH_TEX */ pshader_glsl_tex,
/* WINED3DSIH_TEXBEM */ pshader_glsl_texbem,
/* WINED3DSIH_TEXBEML */ pshader_glsl_texbem,
/* WINED3DSIH_TEXCOORD */ pshader_glsl_texcoord,
/* WINED3DSIH_TEXDEPTH */ pshader_glsl_texdepth,
/* WINED3DSIH_TEXDP3 */ pshader_glsl_texdp3,
/* WINED3DSIH_TEXDP3TEX */ pshader_glsl_texdp3tex,
/* WINED3DSIH_TEXKILL */ pshader_glsl_texkill,
/* WINED3DSIH_TEXLDD */ NULL,
/* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
/* WINED3DSIH_TEXM3x2DEPTH */ pshader_glsl_texm3x2depth,
/* WINED3DSIH_TEXM3x2PAD */ pshader_glsl_texm3x2pad,
/* WINED3DSIH_TEXM3x2TEX */ pshader_glsl_texm3x2tex,
/* WINED3DSIH_TEXM3x3 */ pshader_glsl_texm3x3,
/* WINED3DSIH_TEXM3x3DIFF */ NULL,
/* WINED3DSIH_TEXM3x3PAD */ pshader_glsl_texm3x3pad,
/* WINED3DSIH_TEXM3x3SPEC */ pshader_glsl_texm3x3spec,
/* WINED3DSIH_TEXM3x3TEX */ pshader_glsl_texm3x3tex,
/* WINED3DSIH_TEXM3x3VSPEC */ pshader_glsl_texm3x3vspec,
/* WINED3DSIH_TEXREG2AR */ pshader_glsl_texreg2ar,
/* WINED3DSIH_TEXREG2GB */ pshader_glsl_texreg2gb,
/* WINED3DSIH_TEXREG2RGB */ pshader_glsl_texreg2rgb,
};
const shader_backend_t glsl_shader_backend = {
};