Git Product home page Git Product logo

Comments (8)

flibitijibibo avatar flibitijibibo commented on June 21, 2024

This is very clearly not the full shader source, but I'll see what I can do anyway.

The first thing is an MGFX problem: const values can't be outside of functions. It's perfectly valid to do this, but in MGFX you'll have to move them to the functions where they're used. This can mean duplicating const values in multiple functions, but it'll fix this issue.

The other MGFX problem is that it optimizes things out. This doesn't just happen at the scope of the whole file, it happens at the scope of each individual technique. So if a uniform is used in one technique and not the other, it will be optimized out. In this case you'll need to check if the parameter is null in the game, but functionally it will be no different since those techniques where it's null weren't using those parameters in the first place.

Since this isn't the full shader source I cannot tell, but it looks like there's just a lot of unused stuff overall; might be time to remove all that. Additionally, if you can avoid float comparisons, even for loops, that will help too. There do exist short/int types! Just be sure that when you're running the SetValue call, be sure the type of your value matches the type of the uniform.

from fna-mghistory.

MrBenjammin avatar MrBenjammin commented on June 21, 2024

As I said this is just the include for lighting. It's used in a bunch of shaders which makes it the biggest problem at the moment also the most reproducible case for me. I understand that it will be optimized out for techniques that simply do not use it. However, where the technique does for sure use LightCount it simply does not exist in any form as a parameter in the Effect at runtime or in some changes I've made it just doesn't compile citing a Swizzling error.

Here's an example of that include in use. The shader file is a little messy because it originally encoded a bunch of vertex data into short4 and byte4 but MGFX doesn't support those types so it's using float4 as replacements in the VertexIpunt as a replacement. I've been ripping things out trying to get the Lighting to work and for tex2D to actually display a texture so there is a few unused params.

#include "Includes/Lighting.fx"

float4x4 World;
float4x4 View;
float4x4 Projection;

bool IsWater;
float3 LightTransition;
float Time;

bool Emissive;
bool FogEnable;
bool Scrolling;

float3 ChunkPosition;
float TestOffset;

static float3 NormalLookup[6] = 
{
    float3(0,1,0),
    float3(0,-1,0),
    float3(1,0,0),
    float3(-1,0,0),
    float3(0,0,-1),
    float3(0,0,1)
};  

static float4 ShortOffset = float4(32767,32767,32767,32767);


struct VertexShaderInput
{
    float4 Position : POSITION0;    
    float4 EncodedData : TEXCOORD1;
    float4 EncodedUVs : TEXCOORD0;

};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float Depth : TEXCOORD1;
    float2 TexCoords: TEXCOORD2;
    float2 ShadeUV : TEXCOORD3;
    float2 EmissCoord : TEXCOORD4;
    float3 WorldPosition : TEXCOORD5;
    float3 Light : TEXCOORD6;

};


Texture2D Texture;
sampler TextureSampler = sampler_state { texture = <Texture>; AddressU = clamp; AddressV = clamp;  magfilter = POINT; minfilter = POINT; mipfilter= POINT;};

Texture2D EmissiveTexture;
sampler EmissiveSampler = sampler_state { texture = <EmissiveTexture>; magfilter = LINEAR; minfilter = LINEAR; mipfilter= LINEAR; AddressU = wrap; AddressV = wrap;};


Texture2D ShadeTexture;
sampler ShadeSampler = sampler_state { texture = <ShadeTexture>; magfilter = LINEAR; minfilter = LINEAR; mipfilter= LINEAR; AddressU = WRAP; AddressV = WRAP;};


VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

        float wave = float4(sin(input.Position.x + Time * 4.0f), sin(input.Position.y + Time * 4.0f), sin(input.Position.x + Time * 4.0f), 0) * 0.0005;

    input.Position = lerp(input.Position, input.Position + wave, input.EncodedData.w);


    float4 worldPosition = mul(input.Position, World);



    float4 viewPosition = mul(worldPosition, View);



    output.Position = mul(viewPosition, Projection);

    float4 uvs = input.EncodedUVs;



    output.TexCoords = float2(uvs.x, uvs.y);
    output.Depth = output.Position.z;
    output.ShadeUV = float2(input.EncodedData.y, input.EncodedData.z);
    output.WorldPosition = worldPosition;
    output.EmissCoord = float2(uvs.z, uvs.w);
    output.Light = VSCalculateLighting(worldPosition) * (0.6 + (CalculateLightingFactor(NormalLookup[input.EncodedData.x], World) * 0.4));

    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
        // Get Diffuse
    float4 color = tex2D(TextureSampler, input.TexCoords);  
    clip(color.a - 0.5);

    input.Light *= DayLighting();

    float ao = tex2D(ShadeSampler, input.ShadeUV);

    // Faked AO

    // Add lighting to texture and multiply it by emissive texture 



    color.rgb *= lerp(saturate(input.Light) * ao, float3(1,1,1),(tex2D(TextureSampler, input.EmissCoord).r));

    // Fog
    color = CalculateFogging(input.Depth, color);

    return color;
}

technique Technique1
{
    pass Pass1
    {
        // TODO: set renderstates here.

        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader = compile ps_3_0 PixelShaderFunction();
    }
}


////////////////////////////// DEPTH MAP //////////////////////////////
/*
struct DepthVertexShaderOutput
{
    float4 Position : POSITION0;
    float2 TexCoord : TEXCOORD0;
    float Depth : TEXCOORD1;
    // TODO: add vertex shader outputs such as colors and texture
    // coordinates here. These values will automatically be interpolated
    // over the triangle, and provided as input to your pixel shader.
};



DepthVertexShaderOutput DepthVertexShaderFunction(VertexShaderInput input)
{
    DepthVertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);
    output.Depth =  1-(output.Position.z/output.Position.w);    
    output.TexCoord = float2(input.EncodedData.y, input.EncodedData.z) / 255.0;

    return output;
}

float4 DepthPixelShaderFunction(DepthVertexShaderOutput input) : COLOR0
{

    float alpha = tex2D(TextureSampler, input.TexCoord).a;

    return float4(input.Depth, input.Depth, input.Depth, alpha);
}

technique Depthmap
{
    pass Pass1
    {
                VertexShader = compile vs_2_0 DepthVertexShaderFunction();
        PixelShader = compile ps_2_0 DepthPixelShaderFunction();

    }
}*/

from fna-mghistory.

flibitijibibo avatar flibitijibibo commented on June 21, 2024

It's likely because LightCount is a floating point value when it shouldn't be.

EG2 had a lot of files using a generic set of headers as well; I ended up just expanding them all and gutting what wasn't relevant to each shader:

https://github.com/sq/Fracture/tree/master/Squared/RenderLib/Unix

This is something that will be covered in the MGFX replacement, but at the moment this will mostly just be an excuse to do a code audit of your shader source...

from fna-mghistory.

MrBenjammin avatar MrBenjammin commented on June 21, 2024

I've tried LightCount as an int also with the thought that it could of been issue and it still simply doesn't exist. The shader code does need to be neatened up, but I'm just trying to get it running at the moment with things actually displaying as they should before I go about undoing a lot of the dirty bits from the shaders.

from fna-mghistory.

flibitijibibo avatar flibitijibibo commented on June 21, 2024

It really, really sounds like you need to clean up the shader source first. Clean this up in XNA4 first, then come back and try to fix them up for MGFX. There's a good chance that these shaders are borked enough to where they may not even work 100% of the time in a D3D context either, honestly.

Tagging this under [MGFX], in case I do the raw XNB interop before this is resolved.

from fna-mghistory.

MrBenjammin avatar MrBenjammin commented on June 21, 2024

These are really simple shaders and they work in DX for the 15k+ people who own my game just fine. Taking a few lines out I don't think is going to make a big difference, they're down to pretty bare bones for their function as it is. There's really very little to go wrong with them. Anyway, is there a way I can just write GLSL and use that instead of having MGFX middle man? I think I read about it on the main MonoGame branch but I don't think anyone has implemented it? Thanks for the help thus far!

from fna-mghistory.

flibitijibibo avatar flibitijibibo commented on June 21, 2024

It used to be the standard in 2.5, but the result looked a lot like this or worse, due to various things that couldn't be automated:

http://www.flibitijibibo.com/images/blooblurry.png

You're probably just going to want to wait until I do the MojoShader interop if you don't want to fix your shaders. If that's the case, go ahead and close this so I can avoid bug hoarding.

from fna-mghistory.

flibitijibibo avatar flibitijibibo commented on June 21, 2024

Last post seems to have gone missing, ah well.

Closing until MGFX is replaced.

from fna-mghistory.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.