Author Topic: NWShader  (Read 9323 times)

Legacy_pkpeachykeen

  • Jr. Member
  • **
  • Posts: 89
  • Karma: +0/-0
NWShader
« Reply #90 on: October 10, 2010, 11:32:11 pm »


               The golem is related to vertex colors, I think. I keep trying to fix it, but the broken shader slips back in. I have a working version, so I'll try to make sure that gets into the next update.



As for the Sin City error, it seems to be a minor language change that just makes two of my functions ambiguous. I've updated my Cg install and the error happens here as well, so I'll take care of it.
               
               

               
            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
NWShader
« Reply #91 on: October 11, 2010, 12:50:42 am »


               peachy, I was just wondering if it's easily possible to get shader to give the whole screen a pale tint. If so how would you go about it?



TR
               
               

               
            

Legacy_pkpeachykeen

  • Jr. Member
  • **
  • Posts: 89
  • Karma: +0/-0
NWShader
« Reply #92 on: October 11, 2010, 03:38:51 am »


               Pale as in lighter, or desaturated, or both? Either way, yes.



In general, you'd want something like this:


sampler2D currentScene < string name="thisframe"; >;

float4 MakePale(in float2 screenPos) : COLOR0
{
    float4 originalColor = tex2D(currentScene, screenPos);
    float luminosity = ( originalColor.r * 0.3 ) + ( originalColor.g * 0.6 ) + ( originalColor.b * 0.1 );
    luminosity += 0.2;
    return float4(luminosity.rrr, 1.0);
}

technique main
{
    pass pale < string target="thisframe"; >
    {
        FragmentProgram = compile latest MakePale();
    }
}



That'll completely desaturate the screen and make it a bit brighter. You can lerp between the original color and the result to make it a bright/pale effect, or plenty of other things.
               
               

               
            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
NWShader
« Reply #93 on: October 11, 2010, 11:26:26 pm »


               Thanks for that. What I actually meant was to give the whole screen say a pale red cast or maybe green. You get the idea? Not every planet in the multiverse has a yellow sun, so this could be useful for setting atmosphere.



TR
               
               

               
            

Legacy_pkpeachykeen

  • Jr. Member
  • **
  • Posts: 89
  • Karma: +0/-0
NWShader
« Reply #94 on: October 12, 2010, 06:14:11 pm »


               Ah, sure. Shaders make that sort of thing a breeze. '<img'> If you have any specifics, I can put something together and send over a .cgfx file for you to tweak, but here's the code to tint the screen:




float4 tintColor = { 1.1, 0.7, 1.0, 1.0 }; // format is red, green, blue, alpha. We'll multiply them, so things should be >1 to increase, <1 to decrease. 1 is white, 0 is black.
float4 sceneColor = tex2D(thisFrame, texCoord); // sample the backbuffer
sceneColor *= tintColor; // this changes the colors
return sceneColor; // send it back to the screen





In addition to that, here's a quick patch for the GUI:

http://www.mediafire...7jjrlt3tizjk7q3

It appears to be working correctly, if not, let me know. The mistakes were silly typos, but those always take forever to find (I had it saving the list of selected items, not the list of checked items, for example). It should save shaders properly, and the regex has been changed to strip the shaders/ subfolder off the names. You may need to open your config file by hand to correct that (it uses the built-in regex or whatever is in the config), it should be:

shaders\\\\(?'display'.*\\.cgfx)



(note: just looking at it, it may not be saving the regex properly. I need to test that after lunch, and I'll upload a fixed copy if it isn't. Until then, try this one)
               
               

               
            

Legacy_Nostrebor

  • Newbie
  • *
  • Posts: 43
  • Karma: +0/-0
NWShader
« Reply #95 on: October 15, 2010, 10:02:54 pm »


                Hi,
I think I found a bug (and a fix!) in nwshader.cpp.

Background:
In order to get a better idea on how material shaders work I changed the miteral golem shader to make everything red.  I noticed that only the golem's torso was red, but the arms, legs head, etc were the original texture.

Looking in nwshader.cpp I noticed a check in "glBindTexture" that checks to see if the material should be unbound.  It looked like:

 
            if (           
                           nwshader->shaders != NULL
                   &&          nwshader->shaders->materials != NULL          )   
              {
                     nwshader->shaders->materials->unbindMaterial();    
              }



This was unbinding the material even if the material being unbound was the same material which this call to bind was requesting (i.e. just drew the golem's torso, now drawing a leg).

I changed it to:

               if (
                            texture != lastTexture &&            //jer
                            nwshader->shaders != NULL &&    
                            nwshader->shaders->materials != NULL          )
               {
                           nwshader->shaders->materials->unbindMaterial();    
               }


Now the golum is all red.  Unfortuantly sometimes the shadows are red and other times really weird textures are mapped to the tiles, and information screens.  This occurs when the golem is just  off the bottom of the screen.  I fixed the shadow problem by modifing "glend"

At the end of "glend" I placed:

      //   
// JER   
//    Unbinding the material keeps it from showing up in shadows.   
//
   {
    nwshader->shaders->materials->unbindMaterial();
    textureLock = false;    // this was already in the code JER   
}


Now only the weird textures remain.  I fixed this back in "glBindTexture" by commenting out some code (the first fix mentioned above is included for context.

        // JER
       // This code caused a weird problem when the model   
       // which contained the texture scrolled off the bottom of the   
       // screen.  It caused the player portrait to be mapped   
       // on several tiles and the caracter texture to be in   
      // other locations.   
      //
#if 0   
    if ( textureLock && ( texture != 0 ) )       {   return;      }
#endif

      //
      // JER Adding the condition 
      //   texture != lastTexture
      // keeps the texture active for all nodes in the model
      // previously the texture was only active for the first node.
      //
      if ( 
                        texture != lastTexture &&            //jer
                        nwshader->shaders != NULL && 
                        nwshader->shaders->materials != NULL 
)
      {
          nwshader->shaders->materials->unbindMaterial(); 
      }


I could send you the modified source code, if there is a good way to do that.
               
               

               


                     Modifié par Nostrebor, 15 octobre 2010 - 09:09 .
                     
                  


            

Legacy_pkpeachykeen

  • Jr. Member
  • **
  • Posts: 89
  • Karma: +0/-0
NWShader
« Reply #96 on: October 15, 2010, 10:59:17 pm »


               Awesome! That actually explains an oddity the popped up. I noticed, and "fixed" the shadows/weird textures issue, which is what broke the application of the shader to all portions. If your fix fixes both problems, that's great then.



As far as the source-code, there are two options. Either way, I'll need an emailed note saying you have full copyright ownership over the code (if you go to a university or work for a programming-related company, they'll need to give you permission) and are willing to contribute this snippets to the project under the terms of the GPL. It's a bother, I know, but I'd rather not face the legal issues that could otherwise arise. '<img'>



After that, I can add it or give you write-access to the SVN repository on SourceForge, where you could upload it yourself. I'm updating the repo right now with the GUI fix, so you'll need to merge your changes in (that file shouldn't be changed, so I doubt there will be any problems).



Once your changes get in, I'll build them into the next release and give you a line in the credits. '<img'>
               
               

               
            

Legacy_Gonzo_og

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
NWShader
« Reply #97 on: October 16, 2010, 12:05:32 am »


               Just wanted to say I love NWShader! Thanks Peachy!
               
               

               
            

Legacy_Nostrebor

  • Newbie
  • *
  • Posts: 43
  • Karma: +0/-0
NWShader
« Reply #98 on: October 16, 2010, 02:17:12 am »


               

pkpeachykeen wrote...

Awesome! That actually explains an oddity the popped up. I noticed, and "fixed" the shadows/weird textures issue, which is what broke the application of the shader to all portions. If your fix fixes both problems, that's great then.

As far as the source-code, there are two options. Either way, I'll need an emailed note saying you have full copyright ownership over the code (if you go to a university or work for a programming-related company, they'll need to give you permission) and are willing to contribute this snippets to the project under the terms of the GPL. It's a bother, I know, but I'd rather not face the legal issues that could otherwise arise. '<img'>

After that, I can add it or give you write-access to the SVN repository on SourceForge, where you could upload it yourself. I'm updating the repo right now with the GUI fix, so you'll need to merge your changes in (that file shouldn't be changed, so I doubt there will be any problems).

Once your changes get in, I'll build them into the next release and give you a line in the credits. '<img'>



I sent a note via bioware social with my real email address and acknowledging the GPL license.
               
               

               
            

Legacy_Nostrebor

  • Newbie
  • *
  • Posts: 43
  • Karma: +0/-0
NWShader
« Reply #99 on: October 17, 2010, 02:27:27 am »


               I emailed the updated source file to you.



Does openGL (and therefore nwn) automatically use the gpu for vertex skinning, or is all of the matrix multiplication done by the PC?
               
               

               
            

Legacy_pkpeachykeen

  • Jr. Member
  • **
  • Posts: 89
  • Karma: +0/-0
NWShader
« Reply #100 on: October 17, 2010, 03:40:19 am »


               I got the file, I'll integrate it into the source tonight or tomorrow morning. Thanks again. '<img'>



As for the skinning, NWN has some control over that, I'm not entirely sure (I haven't thoroughly read the code there). The GPU is capable of doing it, and the limitations on GPU skinning and NWN skinning are identical, so there's a chance NWN is offloading it to the GPU. However, jiggly meshes seem like they might be done in CPU (not sure exactly, since adding a vertex shader can break them, which usually suggests GPU). NWN doesn't seem to use vertex buffers in the GPU (the most significant reason for why it runs so slowly), which would mean that if it does GPU skinning, it's download the data every frame.
               
               

               
            

Legacy_Nostrebor

  • Newbie
  • *
  • Posts: 43
  • Karma: +0/-0
NWShader
« Reply #101 on: October 29, 2010, 05:48:55 pm »


               I think there is a memory leak in "CAurTextureBasic::glImage(bool create)" .  This function creates a new "nwshader_texture".  The call to glImage is often (maybe always) bracketed with a call to



"EXPORT void GLAPIENTRY PLATFORMNAME(glDeleteTextures)(GLsizei n, const GLuint *textures)"



in nwshader.cpp.



I think a delete of the recently created nwshader_texture needs to go in here (I do not see anyplace else it is deleted.
               
               

               
            

Legacy_pkpeachykeen

  • Jr. Member
  • **
  • Posts: 89
  • Karma: +0/-0
NWShader
« Reply #102 on: October 30, 2010, 08:04:47 pm »


               I think you may be correct. I know the material is deleted, but the texture may not be deleted. Doing that easily may require making the texture map into a multi-index map as well, since the shader binding depends on names and the delete function uses indices.
               
               

               
            

Legacy_Nostrebor

  • Newbie
  • *
  • Posts: 43
  • Karma: +0/-0
NWShader
« Reply #103 on: October 31, 2010, 12:46:22 am »


               

pkpeachykeen wrote...

I think you may be correct. I know the material is deleted, but the texture may not be deleted. Doing that easily may require making the texture map into a multi-index map as well, since the shader binding depends on names and the delete function uses indices.



I found this because I was working on a way to map the texture index back to the name.  As a result I think I have at a rough cut at a fix.  I will email it to you after I do some checking.
               
               

               
            

Legacy_pkpeachykeen

  • Jr. Member
  • **
  • Posts: 89
  • Karma: +0/-0
NWShader
« Reply #104 on: October 31, 2010, 05:18:35 pm »


               You may want to take a look at the map used to store the materials, if you haven't already. With a slight tweak, the same class can be used as a map<int, string> and keep track of the texture name/ID relationship from both sides. It may also be a chance to forcibly optimize textures, as the map can be checked for an already-loaded texture and simply return that ID, instead of loading a new one.