LoadShader() public static method

public static LoadShader ( string shaderName, ArgumentDependencies modelArguments ) : Shader
shaderName string
modelArguments ValveResourceFormat.Blocks.ResourceEditInfoStructs.ArgumentDependencies
return Shader
Exemplo n.º 1
0
        private void OnRenderModeChange(object sender, EventArgs e)
        {
            // Override placeholder item
            if (renderModeComboBox.SelectedIndex > 0)
            {
                renderModeComboBox.Items[0] = "Default";
            }

            // Rebuild shaders with updated parameters
            foreach (var obj in MeshesToRender)
            {
                foreach (var call in obj.DrawCalls)
                {
                    // Recycle old shader parameters that are not render modes since we are scrapping those anyway
                    var arguments = call.Shader.Parameters.List.Where(arg => !arg.ParameterName.StartsWith("renderMode")).ToList();
                    call.Shader.Parameters.List.Clear();
                    call.Shader.Parameters.List.AddRange(arguments);
                    call.Shader.Parameters.List.Add(new ArgumentDependencies.ArgumentDependency
                    {
                        ParameterName = $"renderMode_{renderModeComboBox.SelectedItem}",
                        Fingerprint   = 1,
                    });
                    call.Shader = ShaderLoader.LoadShader(call.Shader.Name, call.Shader.Parameters);
                }
            }
        }
Exemplo n.º 2
0
        //Set up a draw call
        private DrawCall CreateDrawCall(Dictionary <string, KVValue> drawProperties, uint[] vertexBuffers, uint[] indexBuffers, ArgumentDependencies modelArguments, VBIB block, Material material)
        {
            var drawCall = new DrawCall();

            switch (drawProperties["m_nPrimitiveType"].Value.ToString())
            {
            case "RENDER_PRIM_TRIANGLES":
                drawCall.PrimitiveType = PrimitiveType.Triangles;
                break;

            default:
                throw new Exception("Unknown PrimitiveType in drawCall! (" + drawProperties["m_nPrimitiveType"].Value + ")");
            }

            drawCall.Material = material;

            // Load shader
            drawCall.Shader = ShaderLoader.LoadShader(drawCall.Material.ShaderName, modelArguments);

            //Bind and validate shader
            GL.UseProgram(drawCall.Shader.Program);

            var f = (KVObject)drawProperties["m_indexBuffer"].Value;

            var indexBuffer = default(DrawBuffer);

            indexBuffer.Id       = Convert.ToUInt32(f.Properties["m_hBuffer"].Value);
            indexBuffer.Offset   = Convert.ToUInt32(f.Properties["m_nBindOffsetBytes"].Value);
            drawCall.IndexBuffer = indexBuffer;

            var bufferSize = block.IndexBuffers[(int)drawCall.IndexBuffer.Id].Size;

            drawCall.BaseVertex  = Convert.ToUInt32(drawProperties["m_nBaseVertex"].Value);
            drawCall.VertexCount = Convert.ToUInt32(drawProperties["m_nVertexCount"].Value);
            drawCall.StartIndex  = Convert.ToUInt32(drawProperties["m_nStartIndex"].Value) * bufferSize;
            drawCall.IndexCount  = Convert.ToInt32(drawProperties["m_nIndexCount"].Value);

            if (drawProperties.ContainsKey("m_vTintColor"))
            {
                var tint = (KVObject)drawProperties["m_vTintColor"].Value;
                drawCall.TintColor = new Vector3(
                    Convert.ToSingle(tint.Properties["0"].Value),
                    Convert.ToSingle(tint.Properties["1"].Value),
                    Convert.ToSingle(tint.Properties["2"].Value));
            }

            if (bufferSize == 2)
            {
                //shopkeeper_vr
                drawCall.IndiceType = DrawElementsType.UnsignedShort;
            }
            else if (bufferSize == 4)
            {
                //glados
                drawCall.IndiceType = DrawElementsType.UnsignedInt;
            }
            else
            {
                throw new Exception("Unsupported indice type");
            }

            var g = (KVObject)drawProperties["m_vertexBuffers"].Value;
            var h = (KVObject)g.Properties["0"].Value; // TODO: Not just 0

            var vertexBuffer = default(DrawBuffer);

            vertexBuffer.Id       = Convert.ToUInt32(h.Properties["m_hBuffer"].Value);
            vertexBuffer.Offset   = Convert.ToUInt32(h.Properties["m_nBindOffsetBytes"].Value);
            drawCall.VertexBuffer = vertexBuffer;

            GL.GenVertexArrays(1, out drawCall.VertexArrayObject);

            GL.BindVertexArray(drawCall.VertexArrayObject);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffers[drawCall.VertexBuffer.Id]);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBuffers[drawCall.IndexBuffer.Id]);

            var curVertexBuffer = block.VertexBuffers[(int)drawCall.VertexBuffer.Id];
            var texCoordNum     = 0;

            foreach (var attribute in curVertexBuffer.Attributes)
            {
                var attributeName = "v" + attribute.Name;

                // TODO: other params too?
                if (attribute.Name == "TEXCOORD" && texCoordNum++ > 0)
                {
                    attributeName += texCoordNum;
                }

                BindVertexAttrib(attribute, attributeName, drawCall.Shader.Program, (int)curVertexBuffer.Size);
            }

            GL.BindVertexArray(0);

            return(drawCall);
        }