project-navigation
Personal tools

Author Topic: Shadersupport  (Read 13867 times)

oxyXen

  • Guest
Shadersupport
« on: March 25, 2006, 04:34:58 pm »
Hi

I could take some work on shadersupport and shaders in a few days. What shaders should the project have[assembler|Cg|GLSlang].
As i messed around with shaders, i needed the glew library and devs. These were assemblercode-shaders.
For Cg the Cg-libs would be needed, which are not in the kubuntu deposit afaik.

ox

Hoehrer

  • Guest
Re: Shaders
« Reply #1 on: March 25, 2006, 05:05:33 pm »
Quote from: "oxyXen"
I could take some work on shadersupport and shaders in a few days. What shaders should the project have[assembler|Cg|GLSlang].
As i messed around with shaders, i needed the glew library and devs. These were assemblercode-shaders.
For Cg the Cg-libs would be needed, which are not in the kubuntu deposit afaik.


I do not know much about the different shader languages [1], but in any case i suggest to choose the one that is the most-supported and _open_ language.

Maybe looking into the other opened quake2-engines [2] (UFO:AI was based on the opensource'd Q2 engine) will save us alot of effort, since they might already have shader-support.

Werner

[1] I think there are some more out there than you mentioned?
[2] There already was a discussions about integrating changes from another quake2-engine project into this... fogot the name. MAybe it supports shaders already.

Offline Mattn

  • Administrator
  • PHALANX Commander
  • *****
  • Posts: 4831
  • https://github.com/mgerhardy/vengi
    • View Profile
    • Vengi Voxel Tools
Shadersupport
« Reply #2 on: March 25, 2006, 05:22:03 pm »
have a look at gl_arb_shaders.c in ref_gl directory. maybe it's a start.
some will cry that we don't need shaders but gameplay.....
imo we need shaders, too - so stop crying.

would be cool to have shaders - for water and stuff like this.

oxyXen

  • Guest
Shadersupport
« Reply #3 on: March 25, 2006, 05:44:19 pm »
Ah, thanx mattn

Who wrote this, id?
Is it used in q2-3.20?

Hehe, in this file the last func has a false name (imho), bindblabal would be better.

Well this file gives some shader support for assembler shaders. Pretty the same as i used in my engine. I can live with that and tage a deeper look in.

Quote
I do not know much about the different shader languages [1], but in any case i suggest to choose the one that is the most-supported and _open_ language.


The arbfp1 and arbvp1 are not that easy to read, of course, but are right now the easiest way. Internally we can write the shaders in Cg and then compile with Cgc to arbfp1 and arbvp1

Cg is as easy to understand as C.

Of course there are more shaderlanguages. But i think these 3 mentioned are the best fitting for the project, because:
arb: standard for opengl
Cg: very much (commercial) documentation and tuts, which i can access for free, but i don't know how this works with ati cards, cause Cg comes from nVidia.
GLSLang: It's "pretty new", even better to read than Cg and comes directly from OpenGL.

I think the most popular language not mentioned is HLSL, which is for DirectX which is pöhse ;)

ox

edit: gl_arb_shader.c can load and link shaders, now there are some funcs needed to "use" the shaders in game (and probably to delete if not needed anymore for performance sake ;) ), but maybe they are implemented somewhere else.

Offline Mattn

  • Administrator
  • PHALANX Commander
  • *****
  • Posts: 4831
  • https://github.com/mgerhardy/vengi
    • View Profile
    • Vengi Voxel Tools
Shadersupport
« Reply #4 on: March 25, 2006, 08:05:18 pm »
no the functions to use the shaders are not in right now

oxyXen

  • Guest
Shadersupport
« Reply #5 on: March 26, 2006, 11:02:48 am »
Hi

Ok, today i got a bit forward with understanding the code, in the drawing section.

So for shaders i could insert some lines into the func R_DrawEntitiesOnList in gl_rmain.c .
Code: [Select]
if ( currententity->shader )
   UseShader(currententity->shader);
else
   UseShader(0);


But this means many statechanges ... probably. Or use a standard shader for all objects without any shader.

ox

oxyXen

  • Guest
Shadersupport
« Reply #6 on: March 26, 2006, 12:41:30 pm »
Hi again.

Probably i should take out the vertex-program support, because, it seems to me, that it is not needed, and would rather make problems or confuse.

ox

Hoehrer

  • Guest
Shadersupport
« Reply #7 on: March 26, 2006, 12:57:25 pm »
Some of the recent changes to the shader code produces the following error while building:
Code: [Select]
make targets BUILDDIR=debugi386 CFLAGS="-Wall -pipe -Dstricmp=strcasecmp -I/usr/include/SDL -D_REENTRANT -DUSE_SDL -DUSE_ALSA -DJoystick -DHAVE_DGA -DHAVE_VIDMODE -DHAVE_GETTEXT -g -D_FORTIFY_SOURCE=2 -DLINUX_VERSION='\"0.12 Debug\"'"
make[1]: Entering directory `ufoai/trunk/src/linux'
gcc -Wall -pipe -Dstricmp=strcasecmp -I/usr/include/SDL -D_REENTRANT -DUSE_SDL -DUSE_ALSA -DJoystick -DHAVE_DGA -DHAVE_VIDMODE -DHAVE_GETTEXT -g -D_FORTIFY_SOURCE=2 -DLINUX_VERSION='"0.12 Debug"' -fPIC  -o debugi386/ref_gl/gl_rmain.o -c ../ref_gl/gl_rmain.c
In file included from ../ref_gl/gl_rmain.c:21:
../ref_gl/gl_local.h:49:27: error: gl_arb_shader.h: Datei oder Verzeichnis nicht gefunden
../ref_gl/gl_rmain.c: In function 'R_Shutdown':
../ref_gl/gl_rmain.c:1854: warning: implicit declaration of function 'GL_ShutdownShaders'
make[1]: *** [debugi386/ref_gl/gl_rmain.o] Fehler 1
make[1]: Leaving directory `ufoai/trunk/src/linux'
make: *** [build_debug] Fehler 2


There seems to be a missing header file?

Werner

oxyXen

  • Guest
Shadersupport
« Reply #8 on: March 26, 2006, 01:44:16 pm »
Yeah, i know.

I've sent mattn 2 patches, one without and one with the header.
Probably he applied the first one.
Here is the header as it's on my mychine:
Code: [Select]
#ifndef __GL_ARB_SHADER_H__
#define __GL_ARB_SHADER_H__

#define MAX_SHADERS 255
typedef struct shader_s
{
char sname[255];
unsigned int vpid, fpid;
} shader_t;

typedef struct shaderlist_s
{
shader_t* shader[MAX_SHADERS];
} shaderlist_t;

shader_t* CreateShader(char* name);
void UseShader(shader_t* shader);

shaderlist_t* CreateShaderlist();
void UseShaderFromList(char* name, shaderlist_t* shaderlist);
#endif


ox

Hoehrer

  • Guest
Shadersupport
« Reply #9 on: March 26, 2006, 03:40:48 pm »
Thanks, sync'ed up with gl_arb_shader.c and commited.

Werner

oxyXen

  • Guest
Shadersupport
« Reply #10 on: March 26, 2006, 06:12:54 pm »
Hi

Since a few hours i think about implementing GLSL instead of frag/vert_programs.

GLSL is easier to understand (no assembler) and the shaders can be written modular.
ie the modules: refraction and bumpmap. Then a watershader can be built with these two shadermodules, and a bumpmapshader for ie the worldmap with only the bumpmap module.

An .ufo could look like this:
Code: [Select]
shader water
{
   frag mainwater.fp
   frag refraction.fp
   frag bumpmapping.fp
}

shader worldmap
{
   frag worldmapmain.fp
   frag bumpmapping.fp
}


What do you think?
Where to begin, to implement parsing such files?

ox

Hoehrer

  • Guest
ahder parsing
« Reply #11 on: March 26, 2006, 06:34:14 pm »
Ok, mattn has done something similar for me lately and i'm trying to sum it up (this will be incomplete, but hopefully give an overview)
   

in your .c or .h file:
Code: [Select]
#define SHAD(x) (int)&(((shader_t *)0)->x)

value_t valid_shader_values[] =
{
{ "frap", V_STRING, SHAD( sname ) },
{ NULL, 0, 0 }
};




and an entry in src/client/cl_main.c like this:
Code: [Select]
/*
=================
CL_ParseScriptSecond
=================
*/
void CL_ParseScriptSecond( char *type, char *name, char **text )
{
// check for client interpretable scripts
if ( !strcmp( type, "campaign" ) ) CL_ParseCampaign( name, text );
else if ( !strcmp( type, "stage" ) ) CL_ParseStage( name, text );
else if ( !strcmp( type, "up_entry" ) ) MN_ParseUpEntry( name, text );
//TODO: Parse Base
else if ( !strcmp( type, "base" ) ) MN_ParseBases( name, text );
else if ( !strcmp( type, "tech" ) ) MN_ParseTechnologies( name, text );
else if ( !strcmp( type, "tech" ) ) MN_ParseShaders ( name, text );
}


And finally you need the parser in the .c file (just removed some unneeded stuff):

Code: [Select]
/*======================
MN_ResetResearch
======================*/
void MN_ParseShaders ( char* id, char** text )
{
value_t *var;
shader_t *shad;
char *errhead = _("MN_ParseShaders: unexptected end of file (names ");
char *tokevn;

// get body
token = COM_Parse( text );
if ( !*text || strcmp( token, "{" ) )
{
Com_Printf( _("MN_ParseShaders: shader def \"%s\" without body ignored\n"), id );
return;
}
if ( numShaders >= MAX_SHADERS )
{
Com_Printf( _("MN_ParseShaders: too many shaders entries\n"), id );
return;
}

// new shader
shad = &theglobalshaderlist[numShaders++]; //TODO

memset( t, 0, sizeof( shader_t ) );

//set standard values
strcpy( shad->sname, id );

do {
// get the name type
token = COM_EParse( text, errhead, id );
if ( !*text ) break;
if ( *token == '}' ) break;
// get values
for ( var = valid_shader_values; var->string; var++ )
if ( !strcmp( token, var->string ) )
{
// found a definition
token = COM_EParse( text, errhead, id );
if ( !*text ) return;

if ( var->ofs && var->type != V_NULL )
Com_ParseValue( shad, token, var->type, var->ofs );
else
// NOTE: do we need a buffer here? for saving or something like that?
Com_Printf(_("MN_ParseShaders Error: - no buffer for shaders - V_NULL not allowed\n"));
break;
}
if ( !var->string )
Com_Printf( _("MN_ParseShaders: unknown token \"%s\" ignored (entry %s)\n"), token, id );

} while ( *text );

}


Note that the "shader" text in the .ufo file will be used globaly, so we need to make sure it hasn't been used elsewhere.

I hope this is not too confusing ... for me it was, but now i think i get at least the basics of parsing.

Werner

PS: the forum will most probably f... up the formatting, so either quote the post and copy&paste it, or ask me - i'll send the original text.

Offline Mattn

  • Administrator
  • PHALANX Commander
  • *****
  • Posts: 4831
  • https://github.com/mgerhardy/vengi
    • View Profile
    • Vengi Voxel Tools
Shadersupport
« Reply #12 on: March 31, 2006, 09:28:37 am »
the current shaders are loaded (base/arb*.vp) and stored in the global shader array

should i make a parser ready for you? no big deal.
but you have to tell me something about the structure of a shader file.

Offline Mattn

  • Administrator
  • PHALANX Commander
  • *****
  • Posts: 4831
  • https://github.com/mgerhardy/vengi
    • View Profile
    • Vengi Voxel Tools
Shadersupport
« Reply #13 on: March 31, 2006, 05:22:56 pm »
ok, i've redone the shader stuff.

have a look at client/cl_shader.c, base/ufos/shaders.ufo, ref_gl/gl_arb_shader.c, client/ref.h and maybe client/cl_view.c

i'm not sure about the shader file sytax, maybe we should be compatible with other quake-shaders
Code: [Select]

shader test
{
filename "arb/test.vp"
frag true
}


in the renderer the shaders are accessable via the refdef_t structure (inited in cl_view.c)

more questions? feel free to ask

oxyXen

  • Guest
Shadersupport
« Reply #14 on: April 01, 2006, 03:12:54 am »
Hi

I had now a look on the new code.

Quote
i'm not sure about the shader file sytax, maybe we should be compatible with other quake-shaders


This depends on the shading language used and the system behind it. We are now using ARB programs which are obsolete, but porting this whole thing to GLSL shouldn't be a problem.

I hoped we could attach a shader to an entity, like a model is attached to an entity.

A shader just changes the properties of vertices (position, color, texturecoordinates etc) and fragments (color).

Quote
Code: [Select]
shader test
{
   filename "arb/test.vp"
   frag true
}


The .vp extension indicates that this is a vertex shader, so
Code: [Select]
shader test
{
   filename "arb/test.vp"
   vertex true
}

would be better. But such a file is always a vertex or a fragment shader.
The actual version allows to have shaders consisting of one vertex- and one fragmentshader. With glsl this amount could be almost arbitrary. better would be something like this:
Code: [Select]
shader test
{
   vertex "arb/test.vp"  
}

or
Code: [Select]
shader test
{
   filename "arb/test.vp" vertex
}


Code: [Select]
shader test
{
   filename "arb/test.vp"
   frag true
}


this would lead to a vert-shader being interpretated as a frag shader which would result in the shaded object not being rendered.

My actual shader.ufo:
Code: [Select]
shader test
{
filename "arb/test.vp"
frag false
vertex true
}


But typing shaderlist into console returns this:
Code: [Select]
[...]
/shaderlist
Shader test
..filename: arb/test.vp
..frag 0
..vertex 1

Shader test
..filename: arb/test.vp
..frag 1
..vertex 0
[...]


The firstone is correct, but where comes the second thingy come from??

I'll play now a bit around, maybe i'll get some objects shaded.

Where is the func, the rendering is managed, outside a mission?

I think, thats it

ox