예제 #1
0
        private void AddBuffer(Buffer buf, string inputName, Format format, int stride = 0)
        {
            if (stride == 0)
            {
                stride = format.SizeOfInBytes();
            }
            var elementCount = buf.Description.SizeInBytes / stride;

            if (buffers.Count == 0)
            {
                VertexCount = elementCount;
            }
            else
            {
                if (elementCount != VertexCount)
                {
                    throw CargoEngineException.Create("elementCount of Buffer doesn't match Vertexcount");
                }
            }
            InputElements.AddElement(inputName, format, buffers.Count);

            var bufferBinding = new VertexBufferBinding(buf, stride, 0);

            buffers.Add(bufferBinding);
        }
예제 #2
0
        public Renderer()
        {
            if (Instance != null)
            {
                throw CargoEngineException.Create("multiple instances of renderer");
            }
            Instance = this;

            DeviceCreationFlags devFlags = 0;

#if DEBUG
            devFlags |= DeviceCreationFlags.Debug;
#endif
            Device = new Device(SharpDX.Direct3D.DriverType.Hardware, devFlags);

            ImmPipeline = new RenderPipeline(Device.ImmediateContext);

            inputLayouts = new Dictionary <int, InputLayout>();

            for (var i = 0; i < NUM_THREADS; i++)
            {
                var pipeline = new RenderPipeline(Device);
                deferredPipelines[i] = pipeline;
            }

            Shaders = new ShaderLoader(this);
        }
예제 #3
0
 protected object GetParam(string name, RenderParameterType type)
 {
     if (parameters.ContainsKey(name))
     {
         var param = parameters[name];
         if (param.Type != type)
         {
             throw CargoEngineException.Create("Wrong Parametertype expected " + type + " but was " + param.GetType());
         }
         return(param.Value);
     }
     return(null);
 }
예제 #4
0
        private T LoadShader <T, U>(string file, string entryfunction, string profile) where T : ShaderBase <U> where U : DeviceChild
        {
            try {
                ShaderFlags sFlags = ShaderFlags.PackMatrixRowMajor;
#if DEBUG
                sFlags |= ShaderFlags.Debug;
#endif
                using (var bytecode = ShaderBytecode.CompileFromFile(file, entryfunction, profile, sFlags, EffectFlags.None)) {
                    if (bytecode.Message != null)
                    {
                        MessageBox.Show(bytecode.Message);
                        return(null);
                    }
                    return(ReflectBytecode <T, U>(bytecode));
                }
            }
            catch (System.Exception exc) {
                throw CargoEngineException.Create("Error loading Shader", exc);
            }
        }
예제 #5
0
        private List <ConstantBuffer> ReflectConstantBuffers(ShaderReflection reflection)
        {
            var constantBuffers = new List <ConstantBuffer>();

            for (int cBufferIndex = 0; cBufferIndex < reflection.Description.ConstantBuffers; cBufferIndex++)
            {
                var cb             = reflection.GetConstantBuffer(cBufferIndex);
                var bindingDesc    = reflection.GetResourceBindingDescription(cb.Description.Name);
                var buf            = new Buffer(renderer.Device, cb.Description.Size, ResourceUsage.Dynamic, BindFlags.ConstantBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, sizeof(float));
                var constantBuffer = new ConstantBuffer(buf, bindingDesc.BindPoint);
                for (int i = 0; i < cb.Description.VariableCount; i++)
                {
                    var refVar = cb.GetVariable(i);
                    var type   = refVar.GetVariableType();
                    switch (type.Description.Type)
                    {
                    case ShaderVariableType.Float:
                        if (type.Description.RowCount == 4 && type.Description.ColumnCount == 4)
                        {
                            var matParam = new MatrixParameter(refVar.Description.StartOffset);
                            if (matParam.Size != refVar.Description.Size)
                            {
                                throw CargoEngineException.Create("Error ConstantBufferParamtersize");
                            }
                            constantBuffer.AddParameter(refVar.Description.Name, matParam);
                        }
                        if (type.Description.RowCount == 1)
                        {
                            switch (type.Description.ColumnCount)
                            {
                            case 2:
                                var vec2Param = new Vector2Parameter(refVar.Description.StartOffset);
                                if (vec2Param.Size != refVar.Description.Size)
                                {
                                    throw CargoEngineException.Create("Error ConstantBufferParamtersize");
                                }
                                constantBuffer.AddParameter(refVar.Description.Name, vec2Param);
                                break;

                            case 3:
                                var vec3Param = new Vector3Parameter(refVar.Description.StartOffset);
                                if (vec3Param.Size != refVar.Description.Size)
                                {
                                    throw CargoEngineException.Create("Error ConstantBufferParamtersize");
                                }
                                constantBuffer.AddParameter(refVar.Description.Name, vec3Param);
                                break;

                            case 4:
                                var vec4Param = new Vector4Parameter(refVar.Description.StartOffset);
                                if (vec4Param.Size != refVar.Description.Size)
                                {
                                    throw CargoEngineException.Create("Error ConstantBufferParamtersize");
                                }
                                constantBuffer.AddParameter(refVar.Description.Name, vec4Param);
                                break;
                            }
                        }
                        break;
                    }
                    constantBuffers.Add(constantBuffer);
                }
            }
            return(constantBuffers);
        }