예제 #1
0
 private void FinishMaterial(BatchInfo material)
 {
     //DrawTechnique tech = material.Technique.Res;
     this.SetupBlendType(BlendMode.Reset);
     NativeShaderProgram.Bind(null);
     NativeTexture.ResetBinding(this.sharedSamplerBindings);
 }
예제 #2
0
        public static void Bind(NativeTexture tex, int texUnit = 0)
        {
            if (!texInit)
            {
                InitTextureFields();
            }

            if (curBound[texUnit] == tex)
            {
                return;
            }
            if (activeTexUnit != texUnit)
            {
                GL.ActiveTexture(texUnits[texUnit]);
            }
            activeTexUnit = texUnit;

            if (tex == null)
            {
                GL.BindTexture(TextureTarget.Texture2D, 0);
                curBound[texUnit] = null;
            }
            else
            {
                GL.BindTexture(TextureTarget.Texture2D, tex.Handle);
                curBound[texUnit] = tex;
            }
        }
예제 #3
0
        private void SetupNonMultisampled()
        {
            // Generate FBO
            if (this.handleMainFBO == 0)
            {
                GL.GenFramebuffers(1, out this.handleMainFBO);
            }
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, this.handleMainFBO);

            // Attach textures
            int oglWidth  = 0;
            int oglHeight = 0;

            for (int i = 0; i < this.targetInfos.Count; i++)
            {
                NativeTexture tex = this.targetInfos[i].Target;

                FramebufferSlot attachment = (FramebufferSlot)((int)FramebufferSlot.ColorAttachment0 + i);
                GL.FramebufferTexture2D(
                    FramebufferTarget.Framebuffer,
                    attachment,
                    TextureTarget.Texture2D,
                    tex.Handle,
                    0);
                oglWidth  = tex.Width;
                oglHeight = tex.Height;
            }

            // Generate or delete depth renderbuffer
            if (this.depthBuffer)
            {
                if (this.handleDepthRBO == 0)
                {
                    GL.GenRenderbuffers(1, out this.handleDepthRBO);
                }
                GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, this.handleDepthRBO);
                GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferInternalFormat.DepthComponent24, oglWidth, oglHeight);
                GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferSlot.DepthAttachment, RenderbufferTarget.Renderbuffer, this.handleDepthRBO);
            }
            else
            {
                GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferSlot.DepthAttachment, RenderbufferTarget.Renderbuffer, 0);
                if (this.handleDepthRBO != 0)
                {
                    GL.DeleteRenderbuffers(1, ref this.handleDepthRBO);
                }
                this.handleDepthRBO = 0;
            }

            // Check status
            FramebufferErrorCode status = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);

            if (status != FramebufferErrorCode.FramebufferComplete)
            {
                throw new BackendException(string.Format("Incomplete Framebuffer: {0}", status));
            }

            GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        }
예제 #4
0
        private void FinishSharedParameters()
        {
            NativeTexture.ResetBinding();

            this.sharedSamplerBindings = 0;
            this.sharedShaderParameters.Clear();
            this.activeShaders.Clear();
        }
예제 #5
0
        /// <summary>
        /// Applies the specified parameter values to all currently active shaders.
        /// </summary>
        /// <param name="sharedParams"></param>
        /// <seealso cref="RetrieveActiveShaders"/>
        private void SetupSharedParameters(ShaderParameterCollection sharedParams)
        {
            this.sharedSamplerBindings = 0;
            this.sharedShaderParameters.Clear();
            if (sharedParams == null)
            {
                return;
            }

            foreach (NativeShaderProgram shader in this.activeShaders)
            {
                NativeShaderProgram.Bind(shader);

                ShaderFieldInfo[] varInfo   = shader.Fields;
                int[]             locations = shader.FieldLocations;

                // Setup shared sampler bindings and uniform data
                for (int i = 0; i < varInfo.Length; i++)
                {
                    ref ShaderFieldInfo field = ref varInfo[i];

                    if (field.Scope == ShaderFieldScope.Attribute)
                    {
                        continue;
                    }
                    if (field.Type == ShaderFieldType.Sampler2D)
                    {
                        ContentRef <Texture> texRef;
                        if (!sharedParams.TryGetInternal(field.Name, out texRef))
                        {
                            continue;
                        }

                        NativeTexture.Bind(texRef, this.sharedSamplerBindings);
                        GL.Uniform1(locations[i], this.sharedSamplerBindings);

                        this.sharedSamplerBindings++;
                    }
                    else
                    {
                        float[] data;
                        if (!sharedParams.TryGetInternal(field.Name, out data))
                        {
                            continue;
                        }

                        NativeShaderProgram.SetUniform(ref field, locations[i], data);
                    }

                    this.sharedShaderParameters.Add(field.Name);
                }
            }
예제 #6
0
        private void SetupMaterial(BatchInfo material, BatchInfo lastMaterial)
        {
            if (material == lastMaterial)
            {
                return;
            }
            DrawTechnique tech     = material.Technique.Res ?? DrawTechnique.Solid.Res;
            DrawTechnique lastTech = lastMaterial != null ? lastMaterial.Technique.Res : null;

            // Setup BlendType
            if (lastTech == null || tech.Blending != lastTech.Blending)
            {
                this.SetupBlendType(tech.Blending, this.currentDevice.DepthWrite);
            }

            // Bind Shader
            ShaderProgram       shader       = tech.Shader.Res ?? ShaderProgram.Minimal.Res;
            NativeShaderProgram nativeShader = shader.Native as NativeShaderProgram;

            NativeShaderProgram.Bind(nativeShader);

            // Setup shader data
            ShaderFieldInfo[] varInfo   = nativeShader.Fields;
            int[]             locations = nativeShader.FieldLocations;

            // Setup sampler bindings automatically
            int curSamplerIndex = this.sharedSamplerBindings;

            for (int i = 0; i < varInfo.Length; i++)
            {
                if (locations[i] == -1)
                {
                    continue;
                }
                if (varInfo[i].Type != ShaderFieldType.Sampler2D)
                {
                    continue;
                }
                if (this.sharedShaderParameters.Contains(varInfo[i].Name))
                {
                    continue;
                }

                ContentRef <Texture> texRef = material.GetInternalTexture(varInfo[i].Name);
                NativeTexture.Bind(texRef, curSamplerIndex);

                GL.Uniform1(locations[i], curSamplerIndex);

                curSamplerIndex++;
            }
            NativeTexture.ResetBinding(curSamplerIndex);

            // Setup uniform data
            for (int i = 0; i < varInfo.Length; i++)
            {
                if (locations[i] == -1)
                {
                    continue;
                }
                if (varInfo[i].Type == ShaderFieldType.Sampler2D)
                {
                    continue;
                }
                if (this.sharedShaderParameters.Contains(varInfo[i].Name))
                {
                    continue;
                }

                float[] data;
                if (varInfo[i].Name == "ModelView")
                {
                    data = modelViewData;
                }
                else if (varInfo[i].Name == "Projection")
                {
                    data = projectionData;
                }
                else
                {
                    data = material.GetInternalData(varInfo[i].Name);
                    if (data == null)
                    {
                        continue;
                    }
                }

                NativeShaderProgram.SetUniform(ref varInfo[i], locations[i], data);
            }
        }