private void BuildModelResources() { constantBuffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(1024 * 64), ResourceStates.GenericRead); var cbDesc = new ConstantBufferViewDescription() { BufferLocation = constantBuffer.GPUVirtualAddress, SizeInBytes = (Utilities.SizeOf<ConstantBufferData>() + 255) & ~255 }; device.CreateConstantBufferView(cbDesc, shaderRenderViewHeap.CPUDescriptorHandleForHeapStart); constantBufferData = new ConstantBufferData { World = Matrix.Identity, View = Matrix.Identity, Project = Matrix.Identity, TexsCount = 1 }; constantBufferPointer = constantBuffer.Map(0); Utilities.Write(constantBufferPointer, ref constantBufferData); //build mesh controll buffer meshCtrBuffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(1024 * 64), ResourceStates.GenericRead); cbDesc = new ConstantBufferViewDescription() { BufferLocation = meshCtrBuffer.GPUVirtualAddress, SizeInBytes = (Utilities.SizeOf<MeshCtrBufferData>() + 255) & ~255 }; device.CreateConstantBufferView(cbDesc, meshCtrBufferViewHeap.CPUDescriptorHandleForHeapStart); meshCtrBufferData = new MeshCtrBufferData { TexsCount = 1 }; meshCtrBufferPointer = meshCtrBuffer.Map(0); Utilities.Write(meshCtrBufferPointer, ref meshCtrBufferData); //model test var modePath = "../../models/MikuDeepSea/"; Model model = Model.LoadFromFile(modePath + "DeepSeaGirl.x"); Vertex[] triangleVertices; int[] triangleIndexes; List<Texture> texs; byte[] textureData; GCHandle handle; IntPtr ptr; ResourceDescription textureDesc; //int viewStep = 0; int viewStep = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView); bufferViews = new List<BufferView>(); foreach (ModelComponent m in model.Components) { if (m.TexturePath != null) { texs = Texture.LoadFromFile(modePath, m.TexturePath); } else { continue; //texs = Texture.LoadFromFile(modePath, "tex/jacket.png"); } int texsCount = 0; foreach (Texture tex in texs) { textureData = tex.Data; textureDesc = ResourceDescription.Texture2D(tex.ColorFormat, tex.Width, tex.Height, 1, 1, 1, 0, ResourceFlags.None, TextureLayout.Unknown, 0); // Create the texture. // Describe and create a Texture2D. texture = device.CreateCommittedResource( new HeapProperties(HeapType.Upload), HeapFlags.None, textureDesc, ResourceStates.GenericRead, null); // Copy data to the intermediate upload heap and then schedule a copy // from the upload heap to the Texture2D. handle = GCHandle.Alloc(textureData, GCHandleType.Pinned); ptr = Marshal.UnsafeAddrOfPinnedArrayElement(textureData, 0); texture.WriteToSubresource(0, null, ptr, tex.Width * tex.PixelWdith, textureData.Length); handle.Free(); // Describe and create a SRV for the texture. device.CreateShaderResourceView( texture, new ShaderResourceViewDescription { Shader4ComponentMapping = ((((0) & 0x7) | (((1) & 0x7) << 3) | (((2) & 0x7) << (3 * 2)) | (((3) & 0x7) << (3 * 3)) | (1 << (3 * 4)))), Format = textureDesc.Format, Dimension = ShaderResourceViewDimension.Texture2D, Texture2D = { MipLevels = 1, MostDetailedMip = 0, PlaneSlice = 0, ResourceMinLODClamp = 0.0f } }, shaderRenderViewHeap.CPUDescriptorHandleForHeapStart + viewStep + texsCount * device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView)); texsCount++; //break; } triangleVertices = (new Func<Vertex[]>(() => { var v = new Vertex[m.Vertices.Length]; for (int i = 0; i < m.Vertices.Length; i++) { v[i].Position = m.Vertices[i]; v[i].TexCoord = m.UV[i]; v[i].Normal = m.Normals[i]; //v[i].Tangent = m.Tangents[i]; //v[i].BiTangent = m.BiTangents[i]; v[i].Diffuse = m.Diffuse; v[i].emissive = m.Emissive; v[i].Specular = m.Specular; } return v; }))(); triangleIndexes = m.Indices; // build vertex buffer vertexBuffer = device.CreateCommittedResource( new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(Utilities.SizeOf(triangleVertices)), ResourceStates.GenericRead); Utilities.Write(vertexBuffer.Map(0), triangleVertices, 0, triangleVertices.Length); vertexBuffer.Unmap(0); // build index buffer indexBuffer = device.CreateCommittedResource( new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(Utilities.SizeOf(triangleIndexes)), ResourceStates.GenericRead); Utilities.Write(indexBuffer.Map(0), triangleIndexes, 0, triangleIndexes.Length); indexBuffer.Unmap(0); bufferViews.Add(new BufferView() { vertexBufferView = new VertexBufferView() { BufferLocation = vertexBuffer.GPUVirtualAddress, StrideInBytes = Utilities.SizeOf<Vertex>(), SizeInBytes = Utilities.SizeOf(triangleVertices) }, indexBufferView = new IndexBufferView() { BufferLocation = indexBuffer.GPUVirtualAddress, SizeInBytes = Utilities.SizeOf(triangleIndexes), Format = Format.R32_UInt }, IndexCount = triangleIndexes.Length, ViewStep = viewStep, TexsCount = texsCount }); viewStep += texsCount * device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView); } SamplerStateDescription samplerDesc = new SamplerStateDescription { Filter = Filter.MinMagMipLinear, AddressU = TextureAddressMode.Clamp, AddressV = TextureAddressMode.Clamp, AddressW = TextureAddressMode.Clamp, MaximumAnisotropy = 0, MaximumLod = float.MaxValue, MinimumLod = -float.MaxValue, MipLodBias = 0, ComparisonFunction = Comparison.Never }; device.CreateSampler(samplerDesc, samplerViewHeap.CPUDescriptorHandleForHeapStart); }
private void BuildModelResources() { constantBuffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(1024 * 64), ResourceStates.GenericRead); var cbDesc = new ConstantBufferViewDescription() { BufferLocation = constantBuffer.GPUVirtualAddress, SizeInBytes = (Utilities.SizeOf <ConstantBufferData>() + 255) & ~255 }; device.CreateConstantBufferView(cbDesc, shaderRenderViewHeap.CPUDescriptorHandleForHeapStart); constantBufferData = new ConstantBufferData { World = Matrix.Identity, View = Matrix.Identity, Project = Matrix.Identity, TexsCount = 1 }; constantBufferPointer = constantBuffer.Map(0); Utilities.Write(constantBufferPointer, ref constantBufferData); //build mesh controll buffer meshCtrBuffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(1024 * 64), ResourceStates.GenericRead); cbDesc = new ConstantBufferViewDescription() { BufferLocation = meshCtrBuffer.GPUVirtualAddress, SizeInBytes = (Utilities.SizeOf <MeshCtrBufferData>() + 255) & ~255 }; device.CreateConstantBufferView(cbDesc, meshCtrBufferViewHeap.CPUDescriptorHandleForHeapStart); meshCtrBufferData = new MeshCtrBufferData { TexsCount = 1 }; meshCtrBufferPointer = meshCtrBuffer.Map(0); Utilities.Write(meshCtrBufferPointer, ref meshCtrBufferData); //model test var modePath = "../../models/MikuDeepSea/"; Model model = Model.LoadFromFile(modePath + "DeepSeaGirl.x"); Vertex[] triangleVertices; int[] triangleIndexes; List <Texture> texs; byte[] textureData; GCHandle handle; IntPtr ptr; ResourceDescription textureDesc; //int viewStep = 0; int viewStep = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView); bufferViews = new List <BufferView>(); foreach (ModelComponent m in model.Components) { if (m.TexturePath != null) { texs = Texture.LoadFromFile(modePath, m.TexturePath); } else { continue; //texs = Texture.LoadFromFile(modePath, "tex/jacket.png"); } int texsCount = 0; foreach (Texture tex in texs) { textureData = tex.Data; textureDesc = ResourceDescription.Texture2D(tex.ColorFormat, tex.Width, tex.Height, 1, 1, 1, 0, ResourceFlags.None, TextureLayout.Unknown, 0); // Create the texture. // Describe and create a Texture2D. texture = device.CreateCommittedResource( new HeapProperties(HeapType.Upload), HeapFlags.None, textureDesc, ResourceStates.GenericRead, null); // Copy data to the intermediate upload heap and then schedule a copy // from the upload heap to the Texture2D. handle = GCHandle.Alloc(textureData, GCHandleType.Pinned); ptr = Marshal.UnsafeAddrOfPinnedArrayElement(textureData, 0); texture.WriteToSubresource(0, null, ptr, tex.Width * tex.PixelWdith, textureData.Length); handle.Free(); // Describe and create a SRV for the texture. device.CreateShaderResourceView( texture, new ShaderResourceViewDescription { Shader4ComponentMapping = ((((0) & 0x7) | (((1) & 0x7) << 3) | (((2) & 0x7) << (3 * 2)) | (((3) & 0x7) << (3 * 3)) | (1 << (3 * 4)))), Format = textureDesc.Format, Dimension = ShaderResourceViewDimension.Texture2D, Texture2D = { MipLevels = 1, MostDetailedMip = 0, PlaneSlice = 0, ResourceMinLODClamp = 0.0f } }, shaderRenderViewHeap.CPUDescriptorHandleForHeapStart + viewStep + texsCount * device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView)); texsCount++; //break; } triangleVertices = (new Func <Vertex[]>(() => { var v = new Vertex[m.Vertices.Length]; for (int i = 0; i < m.Vertices.Length; i++) { v[i].Position = m.Vertices[i]; v[i].TexCoord = m.UV[i]; v[i].Normal = m.Normals[i]; //v[i].Tangent = m.Tangents[i]; //v[i].BiTangent = m.BiTangents[i]; v[i].Diffuse = m.Diffuse; v[i].emissive = m.Emissive; v[i].Specular = m.Specular; } return(v); }))(); triangleIndexes = m.Indices; // build vertex buffer vertexBuffer = device.CreateCommittedResource( new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(Utilities.SizeOf(triangleVertices)), ResourceStates.GenericRead); Utilities.Write(vertexBuffer.Map(0), triangleVertices, 0, triangleVertices.Length); vertexBuffer.Unmap(0); // build index buffer indexBuffer = device.CreateCommittedResource( new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(Utilities.SizeOf(triangleIndexes)), ResourceStates.GenericRead); Utilities.Write(indexBuffer.Map(0), triangleIndexes, 0, triangleIndexes.Length); indexBuffer.Unmap(0); bufferViews.Add(new BufferView() { vertexBufferView = new VertexBufferView() { BufferLocation = vertexBuffer.GPUVirtualAddress, StrideInBytes = Utilities.SizeOf <Vertex>(), SizeInBytes = Utilities.SizeOf(triangleVertices) }, indexBufferView = new IndexBufferView() { BufferLocation = indexBuffer.GPUVirtualAddress, SizeInBytes = Utilities.SizeOf(triangleIndexes), Format = Format.R32_UInt }, IndexCount = triangleIndexes.Length, ViewStep = viewStep, TexsCount = texsCount }); viewStep += texsCount * device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView); } SamplerStateDescription samplerDesc = new SamplerStateDescription { Filter = Filter.MinMagMipLinear, AddressU = TextureAddressMode.Clamp, AddressV = TextureAddressMode.Clamp, AddressW = TextureAddressMode.Clamp, MaximumAnisotropy = 0, MaximumLod = float.MaxValue, MinimumLod = -float.MaxValue, MipLodBias = 0, ComparisonFunction = Comparison.Never }; device.CreateSampler(samplerDesc, samplerViewHeap.CPUDescriptorHandleForHeapStart); }