5 Replies Latest reply: Sep 26, 2011 11:55 AM by AIF Bob RSS

    input image levels

    h.delirium Community Member

      How many sampler stages available?

      In my project works only 2 input images.

      Will more levels available?

        • 1. Re: input image levels
          CoreTech::Chuck Adobe Employee

          Molehill version 1 should be able to handle 8 active samplers at a time.  Pixel Bender 3D should be able to handle the same number.  Are you seeing a bug trying to use more than 2 textures at a time?  If so, are you seeing the bug when rendering or when compiling?

           

          Thanks,

          Chuck.

          • 2. Re: input image levels
            h.delirium Community Member

            i have figured already, but have strange thing:

            In Shader Code input images have next sequence:

                 input image4 diffuseMap;

                 input image4 normalMap;

                 input image4 heightGlossMap;

             

            but In AS3 code, we should make this sequence to have a right indexing of input images in shader:

                 p_context.setProgram(shader.molehillInstance);

                 p_context.setTextureAt(0, diffuseMap.molehillInstance); //first input image

                 p_context.setTextureAt(2, normalMap.molehillInstance); //second input image (must be 1)

                 p_context.setTextureAt(1, heightGlossMap.molehillInstance); //third input image (must be 2)

             

            _____________________________________________________

            And another strange thing. I has made big shader (i have any problems only in big shaders, in small all right), this part of it:

             

            float shadowBias = cameraPosition.w;

                    float receiverHeightSample = sample(heightGlossMap, float2(texCoord.x + dxdy.x, texCoord.y + dxdy.y), PB3D_NEAREST | PB3D_MIPDISABLE | PB3D_CLAMP | PB3D_2D).z;

                .............................................(have a 4 sample reading here)

                    receiverHeightSample = sample(heightGlossMap, float2(texCoord.x + shadowSmoothDxDy + dxdy.x, texCoord.y + shadowSmoothDxDy + dxdy.y), PB3D_NEAREST | PB3D_MIPDISABLE | PB3D_CLAMP | PB3D_2D).z;

                    casterHeightSample = sample(heightGlossMap, float2(texCoord.x + shadowSmoothDxDy + dxdy.x + sdxdy.x, texCoord.y + shadowSmoothDxDy + dxdy.y + sdxdy.y), PB3D_NEAREST | PB3D_MIPDISABLE | PB3D_CLAMP | PB3D_2D).z;

                    shadow += (receiverHeightSample < casterHeightSample - shadowBias ? 0.0 : 1.0);

                    shadow = shadow * 0.5;

                    shadow = shadow * 0.5;

             

             

            shadow *= 0.25; not equal this expression:

                    shadow = shadow * 0.5;

                    shadow = shadow * 0.5;

            Very strange o_O

            ________________________________________________________________________

            And third bad thing. If we have a big shader only. If we assign value to variable in starting of  evaluateFragment method then this variable will have wrong value. Example:

            I calculated attenuation term in starting of shader, and use this variable in ending if shader:

             

                    float lightRadius = lightPosition.w;

                   float lightDistance = length(lightPosition.xyz - position.xyz);

                    float d = max(lightDistance - lightRadius, 0.0) / lightRadius + 1.0;

                    float attenuationTerm = 1.0 / (d * d);

            ............(big code here)...........................

                    float diffuseTerm = shadow * lambertTerm * attenuationTerm;

             

            And we have bad result, i don't know what happening, but have black triangle or wrong parallax offset (parallax calculated in middle of shader), but if i make this expression in ending of shader, then i will have right result:

            ............(big code here)...........................

             

                    float lightRadius = lightPosition.w;

                   float lightDistance = length(lightPosition.xyz - position.xyz);

                    float d = max(lightDistance - lightRadius, 0.0) / lightRadius + 1.0;

                    float attenuationTerm = 1.0 / (d * d);

                    float diffuseTerm = shadow * lambertTerm * attenuationTerm;

            ___________________________________________________

            Have this Error (Error #3668: AGAL validation failed: Unknown special flag used in sampler), if i try use PB3D_REPEAT instead PB3D_CLAMP in sample reading.

             

            I can send my shader to you if you need it for tests.

            • 3. Re: input image levels
              CoreTech::Chuck Adobe Employee

              Thank you for the problem report.  We're going to be dropping a new beta soon which will hopefully address some of your problems.  I'm not certain they will all be handled.  If you could send the shader along, I can have a deeper look at it.

               

              As for the texture orders.....

               

              One of the things that is done is that the registers are ordered during a phase of register allocation.  We compact things together when possible, such as

               

              parameter float x;

              parameter float3 y;

               

              Might all go into constant buffer 0.  In general, we make no guarantees about where things go because we don't want to waste any register space available to us.  Also, numerical constants need to be handed in as if they were parameters as there are no immediate constants down in the AGAL level (or on the cards). We abstract alot of this down in the RegisterMap and ParameterBufferHelper classes.

               

              When it comes to textures, which aren't packable in that same way, leaving them in their original order is probably a good idea.  We'll have a think about that when we're deciding on upcoming features.

               

              Thanks,

              Chuck.

              • 4. Re: input image levels
                h.delirium Community Member

                Thanks for the answer 8)

                I will waiting for beta. There is my shader:

                (AS3 set parameters)

                 

                 

                p_context.setProgram(shader.molehillInstance);
                p_context.setTextureAt(0, diffuseMap.molehillInstance);
                p_context.setTextureAt(2, normalMap.molehillInstance);
                p_context.setTextureAt(1, heightGlossMap.molehillInstance);
                
                
                shader.parametersManager.setMatrixParameterByName(V3DShader.VERTEX, "clipSpaceMatrix", p_modelMatrix, true);
                shader.parametersManager.setNumberParameterByName(V3DShader.FRAGMENT, "cameraPosition", Vector.<Number>([cameraPosition.x, cameraPosition.y, cameraPosition.z, light.shadowBias]));
                shader.parametersManager.setNumberParameterByName(V3DShader.FRAGMENT, "lightPosition", Vector.<Number>([lightPosition.x, lightPosition.y, lightPosition.z, light.radius]));
                shader.parametersManager.setNumberParameterByName(V3DShader.FRAGMENT, "lightColor", Vector.<Number>([light.color.red, light.color.green, light.color.blue, light.ambient]));
                shader.parametersManager.setNumberParameterByName(V3DShader.FRAGMENT, "shininess", Vector.<Number>([light.shininess]));
                shader.parametersManager.setNumberParameterByName(V3DShader.FRAGMENT, "parallaxStrength", Vector.<Number>([0.1]));
                shader.parametersManager.setNumberParameterByName(V3DShader.FRAGMENT, "shadowOffset", Vector.<Number>([0.05]));
                shader.parametersManager.setNumberParameterByName(V3DShader.FRAGMENT, "shadowSmoothDxDy", Vector.<Number>([heightGlossMap.texelSize * 2.0]));
                shader.parametersManager.update();
                
                
                p_context.drawTriangles(p_indexBuffer, drawMap.startIndex, drawMap.numTriangles);
                
                

                 

                (Pixel Bender 3D Material Kernel)

                 

                <languageVersion : 1.0;>
                material kernel complexMaterialProgram
                <
                    namespace : "Venomous Engine";
                    vendor : "Venomous";
                    version : 1;
                >
                {
                    input vertex float4 vertexPosition
                    <
                        id : "PB3D_POSITION";
                    >;
                    
                    input vertex float4 vertexTangent
                    <
                        id : "PB3D_TANGENT";
                    >;
                    
                    input vertex float4 vertexBinormal
                    <
                        id : "PB3D_BINORMAL";
                    >;
                     
                    input vertex float4 vertexNormal
                    <
                        id : "PB3D_NORMAL";
                    >;
                    
                    input vertex float4 vertexTexCoord
                    <
                        id : "PB3D_UV";
                    >;
                    
                        input vertex float4 vertexColor
                    <
                        id : "PB3D_COLOR";
                    >;
                     
                    input image4 diffuseMap;
                    input image4 normalMap;
                    input image4 heightGlossMap;
                     
                    interpolated float4 position;
                    interpolated float4 tangent;
                    interpolated float4 binormal;
                    interpolated float4 normal;
                    interpolated float4 texCoord;
                    interpolated float4 color;
                    //interpolated float4 lightDirection;
                    //interpolated float4 lightDirectionHalf;
                    
                //Contain camera position xyz and shadow bias w
                    parameter float4 cameraPosition
                    <
                        id: "cameraPosition";
                    >;
                    
                //Contain light position xyz and light radius w
                    parameter float4 lightPosition
                    <
                        id: "lightPosition";
                    >;
                    
                //Contain light color rgb and light ambient a
                    parameter float4 lightColor
                    <
                         id: "lightColor";
                    >;
                    
                    parameter float shininess
                    <
                         id: "shininess";
                    >;
                    
                    parameter float parallaxStrength
                    <
                         id: "parallaxStrength";
                    >;
                    
                    parameter float shadowOffset
                    <
                         id: "shadowOffset";
                    >;
                    
                    parameter float shadowSmoothDxDy
                    <
                         id: "shadowSmoothDxDy";
                    >;
                     
                    output float4 result;
                     
                    void evaluateVertex()
                    {
                        position = vertexPosition;
                        tangent = vertexTangent;
                        binormal = vertexBinormal;
                        normal = vertexNormal;
                        texCoord = vertexTexCoord;
                        color = vertexColor;
                        
                        //float3 binormal = normalize(cross(normal.xyz, tangent.xyz));
                    }
                     
                    void evaluateFragment() 
                    {
                         float3 cameraDirection = cameraPosition.xyz - position.xyz;
                         float3 lightDirection = lightPosition.xyz - position.xyz;
                         float3 lightDirectionHalf = normalize((lightDirection + cameraDirection) * 0.5);
                         
                         //float lightDistance = length(lightDirection);
                        //float d = max(lightDistance - lightRadius, 0.0) / lightRadius + 1.0;
                        //float attenuationTerm = 1.0 / (d * d);
                        
                        lightDirection = normalize(lightDirection);
                        
                        /*float3x3 tangentSpaceMatrix = float3x3(
                        
                             tangent.x, binormal.x, normal.x,
                             tangent.y, binormal.y, normal.y, 
                             tangent.z, binormal.z, normal.z
                        );
                        
                        float3 tangentSpaceCameraDirection = cameraDirection * tangentSpaceMatrix;*/
                        
                        float3 tangentSpaceCameraDirection = float3(
                        
                             cameraDirection.x * tangent.x + cameraDirection.y * tangent.y + cameraDirection.z * tangent.z,
                             cameraDirection.x * binormal.x + cameraDirection.y * binormal.y + cameraDirection.z * binormal.z,
                             cameraDirection.x * normal.x + cameraDirection.y * normal.y + cameraDirection.z * normal.z
                        );
                        
                        tangentSpaceCameraDirection = normalize(tangentSpaceCameraDirection);
                        
                        float3 tangentSpaceLightDirection = float3(
                        
                             lightDirection.x * tangent.x + lightDirection.y * tangent.y + lightDirection.z * tangent.z,
                             lightDirection.x * binormal.x + lightDirection.y * binormal.y + lightDirection.z * binormal.z,
                             lightDirection.x * normal.x + lightDirection.y * normal.y + lightDirection.z * normal.z
                        );
                        
                        tangentSpaceLightDirection = -normalize(tangentSpaceLightDirection);
                         
                         float4 heightGlossMapSample = sample(heightGlossMap, float2(texCoord.x, texCoord.y), PB3D_NEAREST | PB3D_MIPDISABLE | PB3D_CLAMP | PB3D_2D);
                         float heightSample = heightGlossMapSample.z;
                         
                         float2 dxdy = float2(tangentSpaceCameraDirection.x * (heightSample - 0.5) * parallaxStrength, tangentSpaceCameraDirection.y * (heightSample - 0.5) * parallaxStrength);
                        float2 sdxdy = float2(tangentSpaceLightDirection.x * (heightSample - 0.5) * shadowOffset, tangentSpaceLightDirection.y * (heightSample - 0.5) * shadowOffset);
                        
                        float shadowBias = cameraPosition.w;
                        float receiverHeightSample = sample(heightGlossMap, float2(texCoord.x + dxdy.x, texCoord.y + dxdy.y), PB3D_NEAREST | PB3D_MIPDISABLE | PB3D_CLAMP | PB3D_2D).z;
                        float casterHeightSample = sample(heightGlossMap, float2(texCoord.x + dxdy.x + sdxdy.x, texCoord.y + dxdy.y + sdxdy.y), PB3D_NEAREST | PB3D_MIPDISABLE | PB3D_CLAMP | PB3D_2D).z;
                        float shadow = receiverHeightSample < casterHeightSample - shadowBias ? 0.0 : 1.0;
                        
                        receiverHeightSample = sample(heightGlossMap, float2(texCoord.x + shadowSmoothDxDy + dxdy.x, texCoord.y + dxdy.y), PB3D_NEAREST | PB3D_MIPDISABLE | PB3D_CLAMP | PB3D_2D).z;
                        casterHeightSample = sample(heightGlossMap, float2(texCoord.x + shadowSmoothDxDy + dxdy.x + sdxdy.x, texCoord.y + dxdy.y + sdxdy.y), PB3D_NEAREST | PB3D_MIPDISABLE | PB3D_CLAMP | PB3D_2D).z;
                        shadow += (receiverHeightSample < casterHeightSample - shadowBias ? 0.0 : 1.0);
                        
                        receiverHeightSample = sample(heightGlossMap, float2(texCoord.x + dxdy.x, texCoord.y + shadowSmoothDxDy + dxdy.y), PB3D_NEAREST | PB3D_MIPDISABLE | PB3D_CLAMP | PB3D_2D).z;
                        casterHeightSample = sample(heightGlossMap, float2(texCoord.x + dxdy.x + sdxdy.x, texCoord.y + shadowSmoothDxDy + dxdy.y + sdxdy.y), PB3D_NEAREST | PB3D_MIPDISABLE | PB3D_CLAMP | PB3D_2D).z;
                        shadow += (receiverHeightSample < casterHeightSample - shadowBias ? 0.0 : 1.0);
                         
                        receiverHeightSample = sample(heightGlossMap, float2(texCoord.x + shadowSmoothDxDy + dxdy.x, texCoord.y + shadowSmoothDxDy + dxdy.y), PB3D_NEAREST | PB3D_MIPDISABLE | PB3D_CLAMP | PB3D_2D).z;
                        casterHeightSample = sample(heightGlossMap, float2(texCoord.x + shadowSmoothDxDy + dxdy.x + sdxdy.x, texCoord.y + shadowSmoothDxDy + dxdy.y + sdxdy.y), PB3D_NEAREST | PB3D_MIPDISABLE | PB3D_CLAMP | PB3D_2D).z;
                        shadow += (receiverHeightSample < casterHeightSample - shadowBias ? 0.0 : 1.0);
                          
                     //hmm, very strange thing shadow *= 0.25 not equal this: shadow = shadow * 0.5; shadow = shadow * 0.5;
                        shadow = shadow * 0.5;
                        shadow = shadow * 0.5;
                        
                        float4 diffuseSample = sample(diffuseMap, float2(texCoord.x + dxdy.x, texCoord.y + dxdy.y), PB3D_NEAREST | PB3D_MIPDISABLE | PB3D_CLAMP | PB3D_2D);
                        float3 normalSample = sample(normalMap, float2(texCoord.x + dxdy.x, texCoord.y + dxdy.y), PB3D_NEAREST | PB3D_MIPDISABLE | PB3D_CLAMP | PB3D_2D).rgb;
                        float4 dstColor = color * diffuseSample;
                        float glossSample = sample(heightGlossMap, float2(texCoord.x + dxdy.x, texCoord.y + dxdy.y), PB3D_NEAREST | PB3D_MIPDISABLE | PB3D_CLAMP | PB3D_2D).g;
                        
                        normalSample = normalSample * 2.0 - 1.0;
                        
                        //float3 texelNormal = normalSample.xyz * tangentSpaceMatrix;
                        //normalSample = normalize(normalSample);
                        
                        float3 texelNormal = float3(
                        
                             normalSample.x * tangent.x + normalSample.y * tangent.y + normalSample.z * tangent.z,
                             normalSample.x * binormal.x + normalSample.y * binormal.y + normalSample.z * binormal.z,
                             normalSample.x * normal.x + normalSample.y * normal.y + normalSample.z * normal.z
                        );
                        
                        float lambertTerm = dot(texelNormal, lightDirection);
                        float specularTerm = pow(max(dot(texelNormal, lightDirectionHalf), 0.0), shininess);
                        
                        float lightRadius = lightPosition.w;
                         float lightDistance = length(lightPosition.xyz - position.xyz);
                        float d = max(lightDistance - lightRadius, 0.0) / lightRadius + 1.0;
                        float attenuationTerm = 1.0 / (d * d);
                        
                        float diffuseTerm = shadow * lambertTerm * attenuationTerm;
                        float ambient = lightColor.a;
                                        
                        dstColor *= lightColor;
                        dstColor = dstColor + specularTerm * glossSample;
                        
                        //diffuseTerm = diffuseTerm + ambient;
                        //diffuseTerm = diffuseTerm > 1.0 ? 1.0 : diffuseTerm;
                        diffuseTerm = max(diffuseTerm, ambient);
                        dstColor = dstColor * diffuseTerm;
                        dstColor.a = 1.0;
                        
                        
                        result = dstColor;
                    }
                }
                

                diffuseMap - just diffuse map (rgb)

                normalMap - normals (xyz)

                heightGlossMap - complex parameters (r - empty, g - gloss term, z - height)

                • 5. Re: input image levels
                  AIF Bob Community Member

                  This problem has been fixed in preview 3 of Pixel Bender 3D

                   

                  Bob