Code Monkey home page Code Monkey logo

Comments (3)

alexmac avatar alexmac commented on July 28, 2024

I made some changes recently that should have reduced register usage, if you still have a shader that has problems can you share the code, or a cut down version of the shader that has the same issue so I can investigate it?

from glsl2agal.

racingpht avatar racingpht commented on July 28, 2024

Hi Alex, here's the sample code that doesn't compile with latest swc.


#define float4 vec4
#define float3 vec3
#define float2 vec2
#define frac fract

uniform mat4 g_ViewMatrixInv;
uniform mat4 g_ShadowMapMatrix;
uniform float4 g_CameraPosition;
uniform float4 g_LightDirection;
uniform float4 g_LightColor;
uniform float4 g_AmbientSHR;
uniform float4 g_AmbientSHG;
uniform float4 g_AmbientSHB;
uniform float4 g_ParametersPS;
uniform sampler2D gbuffer0;
uniform sampler2D gbuffer1;
uniform sampler2D shadowmap;
uniform sampler2D randommap;

uniform float4 g_DecodeShadowDepthValues;
uniform float4 g_DecodeGBufferParameters;
uniform float4 g_TexCoordHelpers;
uniform float4 g_Helpers;

#define positionES tempD

void main()
{
// First we read the gbuffer, then we decode it since is attributes are packed tight
float4 gb0= texture2D(gbuffer0, gl_TexCoord[0].xy);
float4 gb1= texture2D(gbuffer1, gl_TexCoord[0].xy);
float4 tempA = texture2D(shadowmap, gl_TexCoord[0].xy);
//float shadowValue = tempA.x;//dot(tempA.xyzw, tempA.xyzw);
float4 tempB;

// GBuffer attributes to decode
float4 albedo;
float4 normalWS;
float4 positionWS;
//float specularPower = 32;
//float specularFactor = 1;

// Lets decode the normal in World Space
normalWS.xyz = gb0.xyz * g_DecodeGBufferParameters.xxx - g_DecodeGBufferParameters.yyy;
normalWS.w= g_DecodeGBufferParameters.y;
// Lets decode the Albedo and specular attributes
// The Albedo is packed using the lower 6 bits of rgb gbuffer1 channel
// The specular factor and power use the higher 2 bits of gbuffer1 rgb channels
// The specular factor is split between rg and the power is on the blue channel
albedo.rgb= fract(gb1.rgb * g_DecodeGBufferParameters.www);

//float3 specAux = tempA.rgb - albedo.rgb;
//specularPower = specAux.r + specAux.g * g_Helpers.w;
//specularFactor = specAux.b;

// Lets reconstruct the fragment world position using the encoded depth
tempA.w = gb0.w + gb1.w * g_DecodeShadowDepthValues.y;
tempB.x = gl_TexCoord[1].x * tempA.w;
tempB.y = gl_TexCoord[1].y * tempA.w;
tempB.z = gl_TexCoord[1].z * tempA.w;
tempB.w = g_DecodeGBufferParameters.y;

positionWS = tempB * g_ViewMatrixInv;

// Calculate Shadows
tempB= positionWS * g_ShadowMapMatrix;
//tempA.xy = texture2D(randommap, gl_TexCoord[0].zw).xy * g_TexCoordHelpers.zw;
tempB.xy = (tempB.xy / tempB.w) * g_TexCoordHelpers.xy + g_TexCoordHelpers.xx;// + tempA.xy;

float4 sm = texture2D(shadowmap, tempB.xy);
float smDepth = dot(sm.xyz, g_DecodeShadowDepthValues.xyz);
float shadowValue = 1;//smDepth - tempB.z;//smDepth;
if(smDepth < tempB.z) shadowValue = 0;

// Now that we have all the GBuffer attributes decoded now lets do the lighting and shading stuff
float NdotL = dot(g_LightDirection.xyz, normalWS.xyz);
NdotL *= shadowValue;
// Accumulate lighting
tempA.xyz = NdotL * g_LightColor.xyz;
tempA.x+= dot(normalWS, g_AmbientSHR);
tempA.y+= dot(normalWS, g_AmbientSHG);
tempA.z+= dot(normalWS, g_AmbientSHB);

float3 finalColor;
finalColor.rgb = albedo.rgb * tempA.xyz;

// Calculate the eye and half vector
tempA.w= dot(normalize(normalize(g_CameraPosition.xyz - positionWS.xyz).xyz + g_LightDirection.xyz), normalWS.xyz);

gl_FragColor = finalColor.rgbg + vec4(pow(tempA.w, 32));
};

from glsl2agal.

alexmac avatar alexmac commented on July 28, 2024

As a temporary workaround you can replace this:

tempA.x+= dot(normalWS, g_AmbientSHR);
tempA.y+= dot(normalWS, g_AmbientSHG);
tempA.z+= dot(normalWS, g_AmbientSHB);

with:

tempA.xyz += vec3(
dot(normalWS, g_AmbientSHR),
dot(normalWS, g_AmbientSHG),
dot(normalWS, g_AmbientSHB));

But some work on the optimizer should be able to fix this

from glsl2agal.

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.