Ejemplo n.º 1
0
        private void InitializeContextObjects(AssetDatabase ad, RenderContext rc)
        {
            ResourceFactory factory = rc.ResourceFactory;
            MeshData        sphere  = ad.LoadAsset <ObjFile>(new AssetID("Models/Sphere.obj")).GetFirstMesh();

            Vector3[] spherePositions = sphere.GetVertexPositions();
            _sphereGeometryVB = factory.CreateVertexBuffer(spherePositions.Length * 12, false);
            _sphereGeometryVB.SetVertexData(spherePositions, new VertexDescriptor(12, 1));
            _ib = sphere.CreateIndexBuffer(factory, out _indexCount);

            Random r     = new Random();
            int    width = InstanceRows;

            InstanceData[] instanceData = new InstanceData[width * width * width];
            for (int z = 0; z < width; z++)
            {
                for (int y = 0; y < width; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        instanceData[z * width * width + y * width + x] = new InstanceData(
                            new Vector3(x * 10, y * 10, z * 10),
                            new RgbaFloat((float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble()));
                    }
                }
            }

            _instanceVB = factory.CreateVertexBuffer(instanceData.Length * InstanceData.SizeInBytes, false);
            _instanceVB.SetVertexData(instanceData, new VertexDescriptor(InstanceData.SizeInBytes, 2, 0, IntPtr.Zero));

            Shader            vs          = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode("instanced-simple-vertex", ShaderStages.Vertex, rc.ResourceFactory));
            Shader            fs          = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode("instanced-simple-frag", ShaderStages.Fragment, rc.ResourceFactory));
            VertexInputLayout inputLayout = factory.CreateInputLayout(
                new VertexInputDescription(VertexPosition.SizeInBytes, new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3)),
                new VertexInputDescription(
                    InstanceData.SizeInBytes,
                    new VertexInputElement("in_offset", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float3, VertexElementInputClass.PerInstance, 1),
                    new VertexInputElement("in_color", VertexSemanticType.Color, VertexElementFormat.Float4, VertexElementInputClass.PerInstance, 1)));
            ShaderSet shaderSet = factory.CreateShaderSet(inputLayout, vs, fs);
            ShaderResourceBindingSlots constantBindings = factory.CreateShaderResourceBindingSlots(
                shaderSet,
                new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4),
                new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4),
                new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4));

            _material    = new Material(shaderSet, constantBindings);
            _worldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4);
            Matrix4x4 identity = Matrix4x4.Identity;

            _worldBuffer.SetData(ref identity, 64);
        }
Ejemplo n.º 2
0
        public unsafe void ChangeRenderContext(AssetDatabase ad, RenderContext rc)
        {
            var factory = rc.ResourceFactory;

            _vb = factory.CreateVertexBuffer(s_vertices.Length * VertexPosition.SizeInBytes, false);
            _vb.SetVertexData(s_vertices, new VertexDescriptor(VertexPosition.SizeInBytes, 1, IntPtr.Zero));

            _ib = factory.CreateIndexBuffer(s_indices.Length * sizeof(int), false);
            _ib.SetIndices(s_indices);

            Shader            vs          = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode("skybox-vertex", ShaderStages.Vertex, rc.ResourceFactory));
            Shader            fs          = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode("skybox-frag", ShaderStages.Fragment, rc.ResourceFactory));
            VertexInputLayout inputLayout = factory.CreateInputLayout(
                new VertexInputDescription(
                    12,
                    new VertexInputElement("position", VertexSemanticType.Position, VertexElementFormat.Float3)));
            ShaderSet shaderSet = factory.CreateShaderSet(inputLayout, vs, fs);
            ShaderResourceBindingSlots constantSlots = factory.CreateShaderResourceBindingSlots(
                shaderSet,
                new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4),
                new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4),
                new ShaderResourceDescription("Skybox", ShaderResourceType.Texture),
                new ShaderResourceDescription("Skybox", ShaderResourceType.Sampler));

            _material         = new Material(shaderSet, constantSlots);
            _viewMatrixBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4);

            fixed(Rgba32 *frontPin = &_front.ISImage.DangerousGetPinnableReferenceToPixelBuffer())
            fixed(Rgba32 * backPin   = &_back.ISImage.DangerousGetPinnableReferenceToPixelBuffer())
            fixed(Rgba32 * leftPin   = &_left.ISImage.DangerousGetPinnableReferenceToPixelBuffer())
            fixed(Rgba32 * rightPin  = &_right.ISImage.DangerousGetPinnableReferenceToPixelBuffer())
            fixed(Rgba32 * topPin    = &_top.ISImage.DangerousGetPinnableReferenceToPixelBuffer())
            fixed(Rgba32 * bottomPin = &_bottom.ISImage.DangerousGetPinnableReferenceToPixelBuffer())
            {
                var cubemapTexture = factory.CreateCubemapTexture(
                    (IntPtr)frontPin,
                    (IntPtr)backPin,
                    (IntPtr)leftPin,
                    (IntPtr)rightPin,
                    (IntPtr)topPin,
                    (IntPtr)bottomPin,
                    _front.Width,
                    _front.Height,
                    _front.PixelSizeInBytes,
                    _front.Format);

                _cubemapBinding = factory.CreateShaderTextureBinding(cubemapTexture);
            }

            _rasterizerState = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, false, false);

            _viewProvider = new DependantDataProvider <Matrix4x4>(SharedDataProviders.GetProvider <Matrix4x4>("ViewMatrix"), Utilities.ConvertToMatrix3x3);
        }
Ejemplo n.º 3
0
        private void InitializeContextObjects(AssetDatabase ad, RenderContext rc)
        {
            ResourceFactory factory = rc.ResourceFactory;

            _vb = factory.CreateVertexBuffer(1024, true);
            _ib = factory.CreateIndexBuffer(1024, true);

            Shader            vs          = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode("wireframe-vertex", ShaderStages.Vertex, rc.ResourceFactory));
            Shader            fs          = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode("wireframe-frag", ShaderStages.Fragment, rc.ResourceFactory));
            VertexInputLayout inputLayout = factory.CreateInputLayout(
                new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3),
                new VertexInputElement("in_color", VertexSemanticType.Color, VertexElementFormat.Byte4));
            ShaderSet shaderSet            = factory.CreateShaderSet(inputLayout, vs, fs);
            ShaderResourceBindingSlots cbs = factory.CreateShaderResourceBindingSlots(
                shaderSet,
                new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4),
                new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4),
                new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4));

            _material = new Material(shaderSet, cbs);

            _worldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4);
            Matrix4x4 identity = Matrix4x4.Identity;

            _worldBuffer.SetData(ref identity, 64);

            _wireframeState = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, true, true);
        }
Ejemplo n.º 4
0
        public static Material CreateMaterial(
            this ResourceFactory factory,
            RenderContext rc,
            string vertexShaderName,
            string fragmentShaderName,
            VertexInputDescription[] vertexInputs,
            ShaderResourceDescription[] resources)

        {
            Shader                     vs               = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode(vertexShaderName, ShaderStages.Vertex, rc.ResourceFactory));
            Shader                     fs               = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode(fragmentShaderName, ShaderStages.Fragment, rc.ResourceFactory));
            VertexInputLayout          inputLayout      = factory.CreateInputLayout(vertexInputs);
            ShaderSet                  shaderSet        = factory.CreateShaderSet(inputLayout, vs, fs);
            ShaderResourceBindingSlots resourceBindings = factory.CreateShaderResourceBindingSlots(shaderSet, resources);

            return(new Material(shaderSet, resourceBindings));
        }
Ejemplo n.º 5
0
        private void InitializeContextObjects(AssetDatabase ad, RenderContext rc)
        {
            ResourceFactory factory = rc.ResourceFactory;

            _vb = factory.CreateVertexBuffer(new[] { new VertexPosition(Vector3.Zero) }, new VertexDescriptor(12, 1), false);
            _ib = factory.CreateIndexBuffer(new ushort[] { 0 }, false);
            Shader            vertexShader   = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode("geometry-vertex", ShaderStages.Vertex, rc.ResourceFactory));
            Shader            geometryShader = factory.CreateShader(ShaderStages.Geometry, ShaderHelper.LoadShaderCode(_geometryShaderName, ShaderStages.Geometry, rc.ResourceFactory));
            Shader            fragmentShader = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode("geometry-frag", ShaderStages.Fragment, rc.ResourceFactory));
            VertexInputLayout inputLayout    = factory.CreateInputLayout(
                new VertexInputDescription(12, new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3)));
            ShaderSet shaderSet = factory.CreateShaderSet(inputLayout, vertexShader, geometryShader, fragmentShader);
            ShaderResourceBindingSlots constantBindings = factory.CreateShaderResourceBindingSlots(
                shaderSet,
                new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), // Global
                new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4),       // Global
                new ShaderResourceDescription("CameraInfoBuffer", Unsafe.SizeOf <Camera.Info>()),      // Global
                new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4));     // Local

            _material          = new Material(shaderSet, constantBindings);
            _worldMatrixBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4);
        }