public H1Shader CompileShader(String debugGroupName, H1VertexFactoryType vertexFactoryType, H1ShaderType shaderType, H1ShaderCompileHelper.H1ShaderCompileInput input)
        {
            // compile the shader
            H1ShaderCompileHelper.H1ShaderCompileOutput shaderCompileOutput = new H1ShaderCompileHelper.H1ShaderCompileOutput();
            H1ShaderCompileHelper.CompileD3D12Shader(input, shaderCompileOutput);

            if (!shaderCompileOutput.IsSucceed)
            {
                throw new Exception("failed to compile the shader! please check!");
            }

            H1Shader outShader = null;

            if (shaderType != null)
            {
                // create shader instance
                outShader = shaderType.FinishCompileShader(shaderCompileOutput);

                if (shaderType is H1GlobalShaderType) // only for global shader, add global shaders
                {
                    m_GlobalShaders.Add(outShader);
                }
                else // otherwise add local shaders
                {
                    m_LocalShaders.Add(outShader);
                }
            }

            return(outShader);
        }
        public H1VertexFactoryType CacheVertexFactoryType(H1VertexFactoryType type)
        {
            if (!m_VertexFactoryTypes.ContainsKey(type.Name))
            {
                RegisterVertexFactoryType(type.Name, type);
            }

            return(m_VertexFactoryTypes[type.Name]);
        }
        public H1MeshMaterialShaderMap GetMeshShaderMap(H1VertexFactoryType vertexFactoryType)
        {
            foreach (H1MeshMaterialShaderMap meshMaterialShaderMap in m_MeshMaterialMaps)
            {
                if (meshMaterialShaderMap.VertexFactoryType == vertexFactoryType)
                {
                    return(meshMaterialShaderMap);
                }
            }

            return(null);
        }
        public H1Shader GetShader(String shaderTypeName, H1VertexFactoryType vertexFactoryType)
        {
            H1MeshMaterialShaderType shaderType = H1Global <H1ManagedRenderer> .Instance.ShaderManager.GetShaderType(shaderTypeName) as H1MeshMaterialShaderType;

            if (shaderType != null)
            {
                H1MeshMaterialShaderMap meshShaderMap = m_MaterialShaderMap.GetMeshShaderMap(vertexFactoryType);
                H1Shader shader = meshShaderMap != null ? meshShaderMap.Shaders[shaderType] : null;
                return(shader);
            }
            else
            {
                return(null);
            }
        }
        public void Compile(H1MaterialResource material, H1ShaderCompileHelper.H1ShaderCompilerEnvironment materialEnvironment)
        {
            // iterate over all vertex factory types
            var vertexFactoryTypes = H1VertexFactoryType.GetTypeList();

            foreach (var vertexFactoryType in vertexFactoryTypes)
            {
                H1MeshMaterialShaderMap meshMaterialMap = null;

                // look for existing map for this vertex factory map
                foreach (var shaderMap in m_MeshMaterialMaps)
                {
                    if (shaderMap.VertexFactoryType == vertexFactoryType)
                    {
                        meshMaterialMap = shaderMap;
                        break;
                    }
                }

                if (meshMaterialMap == null)
                {
                    // create a new mesh material shader map
                    meshMaterialMap = new H1MeshMaterialShaderMap(vertexFactoryType);
                    m_MeshMaterialMaps.Add(meshMaterialMap);
                }

                // compile mesh material map
                meshMaterialMap.BeginCompile(0, // @TODO - I need to change this appropriately!
                                             material, materialEnvironment);
            }

            // iterate over all material shader types
            var shaderTypes = H1ShaderType.GetTypeList();

            foreach (var shaderType in shaderTypes)
            {
                H1MaterialShaderType materialShaderType = shaderType.GetMaterialShaderType();
                if (materialShaderType != null &&
                    materialShaderType.ShouldCache(material) &&
                    material.ShouldCache(materialShaderType, null))
                {
                    materialShaderType.BeginCompileShader(0, // @TODO - I need to change this appropriately!
                                                          material, materialEnvironment);
                }
            }
        }
示例#6
0
 public H1VertexFactory(H1VertexFactoryType type)
 {
     m_TypeRef = type;
 }
 private void RegisterVertexFactoryType(String name, H1VertexFactoryType vertexFactoryType)
 {
     m_VertexFactoryTypes.Add(name, vertexFactoryType);
 }
示例#8
0
        public void LoadAssets()
        {
            //@TODO - temporary sampler
            StaticSamplerDescription pointClamp = new StaticSamplerDescription(ShaderVisibility.Pixel, 0, 0);

            pointClamp.Filter   = Filter.ComparisonMinLinearMagMipPoint;
            pointClamp.AddressU = TextureAddressMode.Clamp;
            pointClamp.AddressV = TextureAddressMode.Clamp;
            pointClamp.AddressW = TextureAddressMode.Clamp;

            StaticSamplerDescription[] staticSamArray = new[] { pointClamp };

            // create an empty root signature
            var rootSignatureDesc = new RootSignatureDescription(RootSignatureFlags.AllowInputAssemblerInputLayout,
                                                                 // root parameters
                                                                 new[]
            {
                new RootParameter(ShaderVisibility.Vertex,
                                  new DescriptorRange()
                {
                    RangeType          = DescriptorRangeType.ConstantBufferView,
                    BaseShaderRegister = 0,
                    OffsetInDescriptorsFromTableStart = 0,
                    DescriptorCount = 1
                }),
                new RootParameter(ShaderVisibility.Pixel,
                                  new DescriptorRange()
                {
                    RangeType          = DescriptorRangeType.ShaderResourceView,
                    BaseShaderRegister = 0,
                    OffsetInDescriptorsFromTableStart = 0,
                    DescriptorCount = 1
                })
            }
                                                                 , staticSamArray
                                                                 );

            m_RootSignature = Device.CreateRootSignature(rootSignatureDesc.Serialize());

            // create the pipeline state, which includes compiling and loading shader
            //H1VertexFactoryType vertexFactoryType = ShaderManager.GetVertexFactoryType("H1LocalVertexFactory");
            H1VertexFactoryType vertexFactoryType = ShaderManager.GetVertexFactoryType("H1GpuSkinVertexFactory");

#if DEBUG
            //var vertexShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile("shader.hlsl", "VSMain", "vs_5_1", SharpDX.D3DCompiler.ShaderFlags.Debug));
            var vertexShader = H1Material.DefaultMaterial.MaterialResource.GetShader("H1BasePassVertexShader", vertexFactoryType).ShaderByteCode;
#else
            var vertexShader = H1Material.DefaultMaterial.MaterialResource.GetShader("H1BasePassVertexShader", vertexFactoryType).ShaderByteCode;
#endif

#if DEBUG
            //var pixelShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile("shader.hlsl", "PSMain", "ps_5_1", SharpDX.D3DCompiler.ShaderFlags.Debug));
            var pixelShader = H1Material.DefaultMaterial.MaterialResource.GetShader("H1BasePassPixelShader", vertexFactoryType).ShaderByteCode;
#else
            var pixelShader = H1Material.DefaultMaterial.MaterialResource.GetShader("H1BasePassPixelShader", vertexFactoryType).ShaderByteCode;
#endif

            // set the seperate rasterizerstatedesc
            RasterizerStateDescription rasterizerStateDesc = RasterizerStateDescription.Default();
            //rasterizerStateDesc.FillMode = FillMode.Wireframe;
            //rasterizerStateDesc.CullMode = CullMode.None;
            rasterizerStateDesc.CullMode = CullMode.Front; //@TODO - what the f**k?!... need to solve this urgently~******
            rasterizerStateDesc.FillMode = FillMode.Solid;

            //H1StaticMeshLODResource resource = m_TempStaticMesh.StaticMeshData.GetLODResource(0);
            //H1StaticMeshLODResource resource = H1Global<H1World>.Instance.PersistentLevel.GetActor(0).GetActorComponent<H1StaticMeshComponent>().StaticMesh.StaticMeshData.GetLODResource(1);
            H1SkeletalMeshObjectGPUSkin skeletalMeshObject = H1Global <H1World> .Instance.PersistentLevel.GetActor(0).GetActorComponent <H1SkeletalMeshComponent>().SkeletalMeshObjectGPUSkin;

            // describe and create the graphics pipeline state object (PSO)
            var psoDesc = new GraphicsPipelineStateDescription()
            {
                //InputLayout = resource.LocalVertexFactory.VertexDeclaration.InputLayout,
                InputLayout        = ((Gen2Layer.H1InputLayout)skeletalMeshObject.GetSkeletalMeshObjectLODByIndex(0).GPUSkinVertexFactories.VertexFactories[0].VertexDeclaration.InputLayout).Description,
                RootSignature      = m_RootSignature,
                VertexShader       = vertexShader,
                PixelShader        = pixelShader,
                RasterizerState    = rasterizerStateDesc,
                BlendState         = BlendStateDescription.Default(),
                DepthStencilFormat = SharpDX.DXGI.Format.D32_Float,
                DepthStencilState  = DepthStencilStateDescription.Default(),
                //DepthStencilState = new DepthStencilStateDescription() { IsDepthEnabled = false, IsStencilEnabled = false },
                SampleMask            = int.MaxValue,
                PrimitiveTopologyType = PrimitiveTopologyType.Triangle,
                RenderTargetCount     = 1,
                Flags             = PipelineStateFlags.None,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                StreamOutput      = new StreamOutputDescription()
            };

            psoDesc.RenderTargetFormats[0] = SharpDX.DXGI.Format.R8G8B8A8_UNorm;

            m_PipelineState = Device.CreateGraphicsPipelineState(psoDesc);

            // create the command list
            m_CommandList = new Direct3D12.H1CommandList(m_DeviceContext.Dx12Device, m_DeviceContext.MainCommandListPool);
            m_CommandList.Initialize();

            // create the vertex buffer
            float aspectRatio = m_Viewport.Width / m_Viewport.Height;

            m_ConstantBuffer = Device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(1024 * 64), ResourceStates.GenericRead);

            var cbvDesc = new ConstantBufferViewDescription
            {
                BufferLocation = m_ConstantBuffer.GPUVirtualAddress,
                SizeInBytes    = 256 * 256//(Utilities.SizeOf<TransformationCB>() + 255) & ~255
            };
            Device.CreateConstantBufferView(cbvDesc, m_ConstantBufferViewHeap.CPUDescriptorHandleForHeapStart);

            m_TransformationCBPointer = m_ConstantBuffer.Map(0);
            //Utilities.Write(m_TransformationCBPointer, ref m_TransformationCB);
            List <Matrix> dataToCopy = new List <Matrix>();
            dataToCopy.Add(m_TransformationCB.viewProjectionMatrix);
            foreach (Matrix mtx in m_TransformationCB.BoneMatrices)
            {
                dataToCopy.Add(mtx);
            }
            Utilities.Write(m_TransformationCBPointer, dataToCopy.ToArray(), 0, 101);
            m_ConstantBuffer.Unmap(0);

            // @TODO - temporary so need to delete
            String path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Assets\\");
            m_tempImageLoader = new H1ImageWrapper(path + "alduin.JPG");

            //CpuDescriptorHandle hDescriptor = m_srvDescriptorHeap.CPUDescriptorHandleForHeapStart;
            CpuDescriptorHandle hDescriptor = m_ConstantBufferViewHeap.CPUDescriptorHandleForHeapStart;
            hDescriptor += m_ConstantBufferDescriptorSize;
            //Int32 sizeInBytes = (Utilities.SizeOf<TransformationCB>() + 255) & ~255;
            //hDescriptor += sizeInBytes;

            ShaderResourceViewDescription srvDesc = new ShaderResourceViewDescription()
            {
                Shader4ComponentMapping = 5768, //@TODO - temporary!
                Format    = m_tempImageLoader.m_tempTextureObject.Resource.Description.Format,
                Dimension = ShaderResourceViewDimension.Texture2D
            };
            srvDesc.Texture2D.MostDetailedMip     = 0;
            srvDesc.Texture2D.MipLevels           = 1;// m_tempImageLoader.m_tempTextureObject.Resource.Description.MipLevels;
            srvDesc.Texture2D.ResourceMinLODClamp = 0.0f;

            Device.CreateShaderResourceView(m_tempImageLoader.m_tempTextureObject.Resource, srvDesc, hDescriptor);
        }
示例#9
0
        public void SettingForPhysics()
        {
            //@TODO - temporary sampler
            StaticSamplerDescription pointClamp = new StaticSamplerDescription(ShaderVisibility.Pixel, 0, 0);

            pointClamp.Filter   = Filter.ComparisonMinLinearMagMipPoint;
            pointClamp.AddressU = TextureAddressMode.Clamp;
            pointClamp.AddressV = TextureAddressMode.Clamp;
            pointClamp.AddressW = TextureAddressMode.Clamp;

            StaticSamplerDescription[] staticSamArray = new[] { pointClamp };

            // create an empty root signature
            var rootSignatureDesc = new RootSignatureDescription(RootSignatureFlags.AllowInputAssemblerInputLayout,
                                                                 // root parameters
                                                                 new[]
            {
                new RootParameter(ShaderVisibility.Vertex,
                                  new DescriptorRange()
                {
                    RangeType          = DescriptorRangeType.ConstantBufferView,
                    BaseShaderRegister = 0,
                    OffsetInDescriptorsFromTableStart = 0,
                    DescriptorCount = 1
                }),
                new RootParameter(ShaderVisibility.Pixel,
                                  new DescriptorRange()
                {
                    RangeType          = DescriptorRangeType.ShaderResourceView,
                    BaseShaderRegister = 0,
                    OffsetInDescriptorsFromTableStart = 0,
                    DescriptorCount = 1
                })
            }
                                                                 , staticSamArray
                                                                 );

            m_RootSignature = Device.CreateRootSignature(rootSignatureDesc.Serialize());

            // create the pipeline state, which includes compiling and loading shader
            H1VertexFactoryType vertexFactoryType = ShaderManager.GetVertexFactoryType("H1LocalVertexFactory");

            //H1VertexFactoryType vertexFactoryType = ShaderManager.GetVertexFactoryType("H1GpuSkinVertexFactory");
#if DEBUG
            //var vertexShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile("shader.hlsl", "VSMain", "vs_5_1", SharpDX.D3DCompiler.ShaderFlags.Debug));
            var vertexShader = H1Material.DefaultMaterial.MaterialResource.GetShader("H1BasePassVertexShader", vertexFactoryType).ShaderByteCode;
#else
            var vertexShader = H1Material.DefaultMaterial.MaterialResource.GetShader("H1BasePassVertexShader", vertexFactoryType).ShaderByteCode;
#endif

#if DEBUG
            //var pixelShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile("shader.hlsl", "PSMain", "ps_5_1", SharpDX.D3DCompiler.ShaderFlags.Debug));
            var pixelShader = H1Material.DefaultMaterial.MaterialResource.GetShader("H1BasePassPixelShader", vertexFactoryType).ShaderByteCode;
#else
            var pixelShader = H1Material.DefaultMaterial.MaterialResource.GetShader("H1BasePassPixelShader", vertexFactoryType).ShaderByteCode;
#endif

            // set the seperate rasterizerstatedesc
            RasterizerStateDescription rasterizerStateDesc = RasterizerStateDescription.Default();
            rasterizerStateDesc.FillMode = FillMode.Wireframe;
            rasterizerStateDesc.CullMode = CullMode.None;
            //rasterizerStateDesc.CullMode = CullMode.Front; //@TODO - what the f**k?!... need to solve this urgently~******
            //rasterizerStateDesc.FillMode = FillMode.Solid;

            Vector3 position = new Vector3();
            Vector3 size     = new Vector3(1, 1, 1);

            H1RenderUtils.H1DynamicMeshBuilder meshBuilder = H1Global <H1VisualDebugger> .Instance.GetNewDynamicMeshBuilder();

            {
                meshBuilder.AddLine(new Vector4(position + size * new Vector3(1, 0, 0), 1), new Vector4(position - size * new Vector3(1, 0, 0), 1), new Vector4(1));
                meshBuilder.AddLine(new Vector4(position + size * new Vector3(0, 1, 0), 1), new Vector4(position - size * new Vector3(0, 1, 0), 1), new Vector4(1));
                meshBuilder.AddLine(new Vector4(position + size * new Vector3(0, 0, 1), 1), new Vector4(position - size * new Vector3(0, 0, 1), 1), new Vector4(1));
            }

            // generate vertex & index buffers and vertex declaration
            meshBuilder.GenerateVertexIndexBuffersAndVertexDeclaration();

            // describe and create the graphics pipeline state object (PSO)
            var psoDesc = new GraphicsPipelineStateDescription()
            {
                InputLayout        = meshBuilder.VertexFactory.VertexDeclaration.InputLayout.Description,
                RootSignature      = m_RootSignature,
                VertexShader       = vertexShader,
                PixelShader        = pixelShader,
                RasterizerState    = rasterizerStateDesc,
                BlendState         = BlendStateDescription.Default(),
                DepthStencilFormat = SharpDX.DXGI.Format.D32_Float,
                DepthStencilState  = DepthStencilStateDescription.Default(),
                //DepthStencilState = new DepthStencilStateDescription() { IsDepthEnabled = false, IsStencilEnabled = false },
                SampleMask            = int.MaxValue,
                PrimitiveTopologyType = PrimitiveTopologyType.Triangle,
                RenderTargetCount     = 1,
                Flags             = PipelineStateFlags.None,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                StreamOutput      = new StreamOutputDescription()
            };

            psoDesc.RenderTargetFormats[0] = SharpDX.DXGI.Format.R8G8B8A8_UNorm;

            m_PipelineState = Device.CreateGraphicsPipelineState(psoDesc);

            // create the command list
            m_CommandList = new Direct3D12.H1CommandList(m_DeviceContext.Dx12Device, m_DeviceContext.MainCommandListPool);
            m_CommandList.Initialize();

            // create the vertex buffer
            float aspectRatio = m_Viewport.Width / m_Viewport.Height;

            m_ConstantBuffer = Device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(1024 * 64), ResourceStates.GenericRead);

            var cbvDesc = new ConstantBufferViewDescription
            {
                BufferLocation = m_ConstantBuffer.GPUVirtualAddress,
                SizeInBytes    = 256 * 256//(Utilities.SizeOf<TransformationCB>() + 255) & ~255
            };
            Device.CreateConstantBufferView(cbvDesc, m_ConstantBufferViewHeap.CPUDescriptorHandleForHeapStart);
        }
        public H1Shader BeginCompileShader(uint shaderMapId, H1MaterialResource material, H1ShaderCompileHelper.H1ShaderCompilerEnvironment materialEnvironment, H1VertexFactoryType vertexFactoryType)
        {
            H1ShaderCompileHelper.H1ShaderCompilerEnvironment environment       = new H1ShaderCompileHelper.H1ShaderCompilerEnvironment();
            H1ShaderCompileHelper.H1ShaderCompilerEnvironment sharedEnvironment = H1ObjectCopier.Clone(materialEnvironment);

            // set the environment by 'vertex factory type'
            vertexFactoryType.ModifyCompilationEnvironment(material, sharedEnvironment);

            // modify the shader type by the current compile environment
            SetupEnvironment(material, sharedEnvironment);

            // create shader compile input
            H1ShaderCompileHelper.H1ShaderCompileInput input = new H1ShaderCompileHelper.H1ShaderCompileInput();
            input.SourceFileName    = SourceFileName;
            input.EntryPointName    = FunctionName;
            input.Environment       = environment;
            input.SharedEnvironment = sharedEnvironment;
            input.Target            = ShaderTarget;

            // compile shader
            return(H1Global <H1ManagedRenderer> .Instance.ShaderManager.CompileShader(material.FriendlyName, null, this, input));
        }
 public bool ShouldCache(H1MaterialResource material, H1VertexFactoryType vertexFactoryType)
 {
     return(m_ShouldCacheRef(material, vertexFactoryType));
 }
示例#12
0
 public virtual bool ShouldCache(H1ShaderType shaderType, H1VertexFactoryType vertexFactoryType)
 {
     return(true);
 }
示例#13
0
 public H1MeshMaterialShaderMap(H1VertexFactoryType type)
 {
     m_VertexFactoryTypeRef = type;
 }
示例#14
0
 public static bool ShouldCache(H1MaterialResource material, H1VertexFactoryType vertexFactoryType)
 {
     return(true);
 }