本文整理汇总了C#中Resource.Map方法的典型用法代码示例。如果您正苦于以下问题:C# Resource.Map方法的具体用法?C# Resource.Map怎么用?C# Resource.Map使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Resource
的用法示例。
在下文中一共展示了Resource.Map方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LoadAssets
private void LoadAssets()
{
DescriptorRange[] ranges = new DescriptorRange[] { new DescriptorRange() { RangeType = DescriptorRangeType.ShaderResourceView, DescriptorCount = 1, OffsetInDescriptorsFromTableStart = int.MinValue, BaseShaderRegister = 0 } };
StaticSamplerDescription sampler = new StaticSamplerDescription()
{
Filter = Filter.MinimumMinMagMipPoint,
AddressU = TextureAddressMode.Border,
AddressV = TextureAddressMode.Border,
AddressW = TextureAddressMode.Border,
MipLODBias = 0,
MaxAnisotropy = 0,
ComparisonFunc = Comparison.Never,
BorderColor = StaticBorderColor.TransparentBlack,
MinLOD = 0.0f,
MaxLOD = float.MaxValue,
ShaderRegister = 0,
RegisterSpace = 0,
ShaderVisibility = ShaderVisibility.Pixel,
};
RootParameter[] rootParameters = new RootParameter[] { new RootParameter(ShaderVisibility.Pixel, ranges) };
RootSignatureDescription rootSignatureDesc = new RootSignatureDescription(RootSignatureFlags.AllowInputAssemblerInputLayout, rootParameters, new StaticSamplerDescription[] { sampler });
rootSignature = device.CreateRootSignature(0, rootSignatureDesc.Serialize());
// Create the pipeline state, which includes compiling and loading shaders.
#if DEBUG
var vertexShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile("shaders.hlsl", "VSMain", "vs_5_0", SharpDX.D3DCompiler.ShaderFlags.Debug));
#else
var vertexShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile("shaders.hlsl", "VSMain", "vs_5_0"));
#endif
#if DEBUG
var pixelShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile("shaders.hlsl", "PSMain", "ps_5_0", SharpDX.D3DCompiler.ShaderFlags.Debug));
#else
var pixelShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile("shaders.hlsl", "PSMain", "ps_5_0"));
#endif
// Define the vertex input layout.
InputElement[] inputElementDescs = new InputElement[]
{
new InputElement("POSITION",0,Format.R32G32B32_Float,0,0),
new InputElement("TEXCOORD",0,Format.R32G32_Float,12,0)
};
// Describe and create the graphics pipeline state object (PSO).
GraphicsPipelineStateDescription psoDesc = new GraphicsPipelineStateDescription()
{
InputLayout = new InputLayoutDescription(inputElementDescs),
RootSignature = rootSignature,
VertexShader = vertexShader,
PixelShader = pixelShader,
RasterizerState = RasterizerStateDescription.Default(),
BlendState = BlendStateDescription.Default(),
DepthStencilFormat = SharpDX.DXGI.Format.D32_Float,
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;
pipelineState = device.CreateGraphicsPipelineState(psoDesc);
// Create the command list.
commandList = device.CreateCommandList(CommandListType.Direct, commandAllocator, pipelineState);
// Create the vertex buffer.
float aspectRatio = viewport.Width / viewport.Height;
// Define the geometry for a triangle.
Vertex[] triangleVertices = new Vertex[]
{
new Vertex() {position=new Vector3(0.0f, 0.25f * aspectRatio, 0.0f ),uv=new Vector2(0.5f, 0.0f) },
new Vertex() {position=new Vector3(0.25f, -0.25f * aspectRatio, 0.0f),uv=new Vector2(1.0f, 1.0f) },
new Vertex() {position=new Vector3(-0.25f, -0.25f * aspectRatio, 0.0f),uv=new Vector2(0.0f, 1.0f) },
};
int vertexBufferSize = Utilities.SizeOf(triangleVertices);
// Note: using upload heaps to transfer static data like vert buffers is not
// recommended. Every time the GPU needs it, the upload heap will be marshalled
// over. Please read up on Default Heap usage. An upload heap is used here for
// code simplicity and because there are very few verts to actually transfer.
vertexBuffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(vertexBufferSize), ResourceStates.GenericRead);
// Copy the triangle data to the vertex buffer.
IntPtr pVertexDataBegin = vertexBuffer.Map(0);
Utilities.Write(pVertexDataBegin, triangleVertices, 0, triangleVertices.Length);
vertexBuffer.Unmap(0);
// Initialize the vertex buffer view.
vertexBufferView = new VertexBufferView();
vertexBufferView.BufferLocation = vertexBuffer.GPUVirtualAddress;
vertexBufferView.StrideInBytes = Utilities.SizeOf<Vertex>();
vertexBufferView.SizeInBytes = vertexBufferSize;
//.........这里部分代码省略.........
示例2: BuildTerrainResouces
private void BuildTerrainResouces()
{
float[] HeightMapData = new float[100 * 100];
for (int i = 0; i < 100; i++)
for (int j = 0; j < 100; j++)
HeightMapData[i * 100 + j] = Convert.ToSingle(Math.Cos(Math.PI * 2 * i) * Math.Sin(Math.PI * 2 * j));
var Tex = Texture.LoadFromFile("../../", "Terrain.jpg");
ResourceDescription HeighMapDesc = ResourceDescription.Texture2D(Format.R32_Float, 100, 100, 1);
ResourceDescription TerrainTextureDesc = ResourceDescription.Texture2D(Tex[0].ColorFormat, Tex[0].Width, Tex[0].Height, 1);
HeightMap = device.CreateCommittedResource(
new HeapProperties(HeapType.Upload),
HeapFlags.None,
HeighMapDesc,
ResourceStates.GenericRead, null);
TerrainTexture = device.CreateCommittedResource(
new HeapProperties(HeapType.Upload),
HeapFlags.None,
HeighMapDesc,
ResourceStates.GenericRead, null);
TerrainCBF = device.CreateCommittedResource(
new HeapProperties(HeapType.Upload),
HeapFlags.None,
ResourceDescription.Buffer(1024 * 64),
ResourceStates.GenericRead);
GCHandle handle = GCHandle.Alloc(HeightMapData, GCHandleType.Pinned);
IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement(HeightMapData, 0);
HeightMap.WriteToSubresource(0, null, ptr, 100 * 4, HeightMapData.Length);
handle.Free();
handle = GCHandle.Alloc(Tex[0].Data, GCHandleType.Pinned);
ptr = Marshal.UnsafeAddrOfPinnedArrayElement(Tex[0].Data, 0);
TerrainTexture.WriteToSubresource(0, null, ptr, Tex[0].Width * Tex[0].PixelWdith, Tex[0].Data.Length);
handle.Free();
device.CreateShaderResourceView(
HeightMap,
new ShaderResourceViewDescription
{
Shader4ComponentMapping = ((((0) & 0x7) | (((1) & 0x7) << 3) | (((2) & 0x7) << (3 * 2)) | (((3) & 0x7) << (3 * 3)) | (1 << (3 * 4)))),
Format = HeighMapDesc.Format,
Dimension = ShaderResourceViewDimension.Texture2D,
Texture2D =
{
MipLevels = 1,
MostDetailedMip = 0,
PlaneSlice = 0,
ResourceMinLODClamp = 0.0f
}
},
terrainHeap.CPUDescriptorHandleForHeapStart);
device.CreateShaderResourceView(
TerrainTexture,
new ShaderResourceViewDescription
{
Shader4ComponentMapping = ((((0) & 0x7) | (((1) & 0x7) << 3) | (((2) & 0x7) << (3 * 2)) | (((3) & 0x7) << (3 * 3)) | (1 << (3 * 4)))),
Format = HeighMapDesc.Format,
Dimension = ShaderResourceViewDimension.Texture2D,
Texture2D =
{
MipLevels = 1,
MostDetailedMip = 0,
PlaneSlice = 0,
ResourceMinLODClamp = 0.0f
}
},
terrainHeap.CPUDescriptorHandleForHeapStart +
device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView));
//==========
TerrainCBF = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(1024 * 64), ResourceStates.GenericRead);
device.CreateConstantBufferView(
new ConstantBufferViewDescription()
{
BufferLocation = TerrainCBF.GPUVirtualAddress,
SizeInBytes = (Utilities.SizeOf<ConstantBufferData>() + 255) & ~255
},
terrainHeap.CPUDescriptorHandleForHeapStart +
device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView) * 2);
TerrainCBFData = new ConstantBufferData
{
World = Matrix.Identity,
View = Matrix.Identity,
Project = Matrix.Identity,
TexsCount = 1
};
TerrainCBFPointer = TerrainCBF.Map(0);
Vertex[] TerrainVertex = new Vertex[100 * 100];
for (int i = 0; i < 100; i++)
for (int j = 0; j < 100; j++)
{
TerrainVertex[i * 100 + j].Position = new Vector3(i, 0, j);
TerrainVertex[i * 100 + j].TexCoord = new Vector2(i/2 == 0? 0 : 1, j/2==0? 0 : 1);
}
TerrainVertexBuffer = device.CreateCommittedResource(
//.........这里部分代码省略.........
示例3: LoadAssets
private void LoadAssets()
{
// Create an empty root signature.
var rootSignatureDesc = new RootSignatureDescription(RootSignatureFlags.AllowInputAssemblerInputLayout);
rootSignature = device.CreateRootSignature(rootSignatureDesc.Serialize());
// Create the pipeline state, which includes compiling and loading shaders.
#if DEBUG
var vertexShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.Compile(SharpDX.IO.NativeFile.ReadAllText("shaders.hlsl"), "VSMain", "vs_5_0", SharpDX.D3DCompiler.ShaderFlags.Debug));
#else
var vertexShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile("shaders.hlsl", "VSMain", "vs_5_0"));
#endif
#if DEBUG
var pixelShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.Compile(SharpDX.IO.NativeFile.ReadAllText("shaders.hlsl"), "PSMain", "ps_5_0", SharpDX.D3DCompiler.ShaderFlags.Debug));
#else
var pixelShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile("shaders.hlsl", "PSMain", "ps_5_0"));
#endif
// Define the vertex input layout.
var inputElementDescs = new []
{
new InputElement("POSITION",0,Format.R32G32B32_Float,0,0),
new InputElement("COLOR",0,Format.R32G32B32A32_Float,12,0)
};
// Describe and create the graphics pipeline state object (PSO).
var psoDesc = new GraphicsPipelineStateDescription()
{
InputLayout = new InputLayoutDescription(inputElementDescs),
RootSignature = rootSignature,
VertexShader = vertexShader,
PixelShader = pixelShader,
RasterizerState = RasterizerStateDescription.Default(),
BlendState = BlendStateDescription.Default(),
DepthStencilFormat = SharpDX.DXGI.Format.D32_Float,
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;
pipelineState = device.CreateGraphicsPipelineState(psoDesc);
// Create the command list.
commandList = device.CreateCommandList(CommandListType.Direct, commandAllocator, pipelineState);
// Command lists are created in the recording state, but there is nothing
// to record yet. The main loop expects it to be closed, so close it now.
commandList.Close();
// Create the vertex buffer.
float aspectRatio = viewport.Width / viewport.Height;
// Define the geometry for a triangle.
var triangleVertices = new []
{
new Vertex() {Position=new Vector3(0.0f, 0.25f * aspectRatio, 0.0f ),Color=new Vector4(1.0f, 0.0f, 0.0f, 1.0f ) },
new Vertex() {Position=new Vector3(0.25f, -0.25f * aspectRatio, 0.0f),Color=new Vector4(0.0f, 1.0f, 0.0f, 1.0f) },
new Vertex() {Position=new Vector3(-0.25f, -0.25f * aspectRatio, 0.0f),Color=new Vector4(0.0f, 0.0f, 1.0f, 1.0f ) },
};
int vertexBufferSize = Utilities.SizeOf(triangleVertices);
// Note: using upload heaps to transfer static data like vert buffers is not
// recommended. Every time the GPU needs it, the upload heap will be marshalled
// over. Please read up on Default Heap usage. An upload heap is used here for
// code simplicity and because there are very few verts to actually transfer.
vertexBuffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(vertexBufferSize), ResourceStates.GenericRead);
// Copy the triangle data to the vertex buffer.
IntPtr pVertexDataBegin = vertexBuffer.Map(0);
Utilities.Write(pVertexDataBegin, triangleVertices, 0, triangleVertices.Length);
vertexBuffer.Unmap(0);
// Initialize the vertex buffer view.
vertexBufferView = new VertexBufferView();
vertexBufferView.BufferLocation = vertexBuffer.GPUVirtualAddress;
vertexBufferView.StrideInBytes = Utilities.SizeOf<Vertex>();
vertexBufferView.SizeInBytes = vertexBufferSize;
// Create and record the bundle.
bundle = device.CreateCommandList(0, CommandListType.Bundle, bundleAllocator, pipelineState);
bundle.SetGraphicsRootSignature(rootSignature);
bundle.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
bundle.SetVertexBuffer(0, vertexBufferView);
bundle.DrawInstanced(3, 1, 0, 0);
bundle.Close();
// Create synchronization objects.
{
fence = device.CreateFence(0, FenceFlags.None);
fenceValue = 1;
// Create an event handle to use for frame synchronization.
//.........这里部分代码省略.........
示例4: BuildModelResources
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,
//.........这里部分代码省略.........
示例5: LoadAssets
private void LoadAssets()
{
DescriptorRange[] ranges = new DescriptorRange[] { new DescriptorRange() { RangeType = DescriptorRangeType.ConstantBufferView, BaseShaderRegister = 0, OffsetInDescriptorsFromTableStart = int.MinValue, DescriptorCount = 1 } };
RootParameter parameter = new RootParameter(ShaderVisibility.Vertex, ranges);
// Create an empty root signature.
RootSignatureDescription rootSignatureDesc = new RootSignatureDescription(RootSignatureFlags.AllowInputAssemblerInputLayout, new RootParameter[] { parameter });
rootSignature = device.CreateRootSignature(rootSignatureDesc.Serialize());
// Create the pipeline state, which includes compiling and loading shaders.
#if DEBUG
var vertexShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile("shaders.hlsl", "VSMain", "vs_5_0", SharpDX.D3DCompiler.ShaderFlags.Debug));
#else
var vertexShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile("shaders.hlsl", "VSMain", "vs_5_0"));
#endif
#if DEBUG
var pixelShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile("shaders.hlsl", "PSMain", "ps_5_0", SharpDX.D3DCompiler.ShaderFlags.Debug));
#else
var pixelShader = new ShaderBytecode(SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile("shaders.hlsl", "PSMain", "ps_5_0"));
#endif
// Define the vertex input layout.
InputElement[] inputElementDescs = new InputElement[]
{
new InputElement("POSITION",0,Format.R32G32B32_Float,0,0),
new InputElement("COLOR",0,Format.R32G32B32A32_Float,12,0)
};
// Describe and create the graphics pipeline state object (PSO).
GraphicsPipelineStateDescription psoDesc = new GraphicsPipelineStateDescription()
{
InputLayout = new InputLayoutDescription(inputElementDescs),
RootSignature = rootSignature,
VertexShader = vertexShader,
PixelShader = pixelShader,
RasterizerState = RasterizerStateDescription.Default(),
BlendState = BlendStateDescription.Default(),
DepthStencilFormat = SharpDX.DXGI.Format.D32_Float,
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;
pipelineState = device.CreateGraphicsPipelineState(psoDesc);
// Create the command list.
commandList = device.CreateCommandList(CommandListType.Direct, commandAllocator, pipelineState);
// Create the vertex buffer.
float aspectRatio = viewport.Width / viewport.Height;
// Define the geometry for a triangle.
Vertex[] triangleVertices = new Vertex[]
{
new Vertex() {position=new Vector3(0.0f, 0.25f * aspectRatio, 0.0f ),color=new Vector4(1.0f, 0.0f, 0.0f, 1.0f ) },
new Vertex() {position=new Vector3(0.25f, -0.25f * aspectRatio, 0.0f),color=new Vector4(0.0f, 1.0f, 0.0f, 1.0f) },
new Vertex() {position=new Vector3(-0.25f, -0.25f * aspectRatio, 0.0f),color=new Vector4(0.0f, 0.0f, 1.0f, 1.0f ) },
};
int vertexBufferSize = Utilities.SizeOf(triangleVertices);
// Note: using upload heaps to transfer static data like vert buffers is not
// recommended. Every time the GPU needs it, the upload heap will be marshalled
// over. Please read up on Default Heap usage. An upload heap is used here for
// code simplicity and because there are very few verts to actually transfer.
vertexBuffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(vertexBufferSize), ResourceStates.GenericRead);
// Copy the triangle data to the vertex buffer.
IntPtr pVertexDataBegin = vertexBuffer.Map(0);
Utilities.Write(pVertexDataBegin, triangleVertices, 0, triangleVertices.Length);
vertexBuffer.Unmap(0);
// Initialize the vertex buffer view.
vertexBufferView = new VertexBufferView();
vertexBufferView.BufferLocation = vertexBuffer.GPUVirtualAddress;
vertexBufferView.StrideInBytes = Utilities.SizeOf<Vertex>();
vertexBufferView.SizeInBytes = vertexBufferSize;
// Command lists are created in the recording state, but there is nothing
// to record yet. The main loop expects it to be closed, so close it now.
commandList.Close();
constantBuffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(1024 * 64), ResourceStates.GenericRead);
//// Describe and create a constant buffer view.
ConstantBufferViewDescription cbvDesc = new ConstantBufferViewDescription()
{
BufferLocation = constantBuffer.GPUVirtualAddress,
SizeInBytes = (Utilities.SizeOf<ConstantBuffer>() + 255) & ~255
};
device.CreateConstantBufferView(cbvDesc, constantBufferViewHeap.CPUDescriptorHandleForHeapStart);
// Initialize and map the constant buffers. We don't unmap this until the
// app closes. Keeping things mapped for the lifetime of the resource is okay.
//.........这里部分代码省略.........
示例6: LoadAssets
//.........这里部分代码省略.........
new Vertex(new Vector3(5,-5,5),new Vector4(1,0,1,1)),
new Vertex(new Vector3(5,-5,-5),new Vector4(1,0,1,1)),
new Vertex(new Vector3(-5,-5,-5),new Vector4(1,0,1,1)),
//LEFT
new Vertex(new Vector3(-5,-5,5),new Vector4(1,0,0,1)),
new Vertex(new Vector3(-5,5,5),new Vector4(1,0,0,1)),
new Vertex(new Vector3(-5,5,-5),new Vector4(1,0,0,1)),
new Vertex(new Vector3(-5,-5,-5),new Vector4(1,0,0,1)),
//RIGHT
new Vertex(new Vector3(5,-5,5),new Vector4(1,1,0,1)),
new Vertex(new Vector3(5,5,5),new Vector4(1,1,0,1)),
new Vertex(new Vector3(5,5,-5),new Vector4(1,1,0,1)),
new Vertex(new Vector3(5,-5,-5),new Vector4(1,1,0,1)),
//FRONT
new Vertex(new Vector3(-5,5,5),new Vector4(0,1,1,1)),
new Vertex(new Vector3(5,5,5),new Vector4(0,1,1,1)),
new Vertex(new Vector3(5,-5,5),new Vector4(0,1,1,1)),
new Vertex(new Vector3(-5,-5,5),new Vector4(0,1,1,1)),
//BACK
new Vertex(new Vector3(-5,5,-5),new Vector4(0,0,1,1)),
new Vertex(new Vector3(5,5,-5),new Vector4(0,0,1,1)),
new Vertex(new Vector3(5,-5,-5),new Vector4(0,0,1,1)),
new Vertex(new Vector3(-5,-5,-5),new Vector4(0,0,1,1))
};
int vertexBufferSize = Utilities.SizeOf(vertices);
// Note: using upload heaps to transfer static data like vert buffers is not
// recommended. Every time the GPU needs it, the upload heap will be marshalled
// over. Please read up on Default Heap usage. An upload heap is used here for
// code simplicity and because there are very few verts to actually transfer.
vertexBuffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(vertexBufferSize), ResourceStates.GenericRead);
// Copy the triangle data to the vertex buffer.
IntPtr pVertexDataBegin = vertexBuffer.Map(0);
Utilities.Write(pVertexDataBegin, vertices, 0, vertices.Length);
vertexBuffer.Unmap(0);
// Initialize the vertex buffer view.
vertexBufferView = new VertexBufferView();
vertexBufferView.BufferLocation = vertexBuffer.GPUVirtualAddress;
vertexBufferView.StrideInBytes = Utilities.SizeOf<Vertex>();
vertexBufferView.SizeInBytes = vertexBufferSize;
//Create Index Buffer
//Indices
int[] indices = new int[]
{
0,1,2,0,2,3,
4,6,5,4,7,6,
8,9,10,8,10,11,
12,14,13,12,15,14,
16,18,17,16,19,18,
20,21,22,20,22,23
};
int indexBufferSize = Utilities.SizeOf(indices);
indexBuffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(indexBufferSize), ResourceStates.GenericRead);
// Copy the triangle data to the vertex buffer.
IntPtr pIndexDataBegin = indexBuffer.Map(0);
Utilities.Write(pIndexDataBegin, indices, 0, indices.Length);
indexBuffer.Unmap(0);
// Initialize the index buffer view.
indexBufferView = new IndexBufferView();
indexBufferView.BufferLocation = indexBuffer.GPUVirtualAddress;
indexBufferView.Format = Format.R32_UInt;
indexBufferView.SizeInBytes = indexBufferSize;
//constant Buffer for each cubes
constantBufferViewHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription()
{
DescriptorCount = NumCubes,
Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView,
Flags = DescriptorHeapFlags.ShaderVisible
});
int constantBufferSize = (Utilities.SizeOf<Transform>() + 255) & ~255;
constantBuffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(constantBufferSize * NumCubes), ResourceStates.GenericRead);
constantBufferDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);
//First cube
ConstantBufferViewDescription cbvDesc = new ConstantBufferViewDescription()
{
BufferLocation = constantBuffer.GPUVirtualAddress,
SizeInBytes = constantBufferSize
};
CpuDescriptorHandle cbHandleHeapStart = constantBufferViewHeap.CPUDescriptorHandleForHeapStart;
for (int i = 0; i < NumCubes; i++)
{
device.CreateConstantBufferView(cbvDesc, cbHandleHeapStart);
cbvDesc.BufferLocation += Utilities.SizeOf<Transform>();
cbHandleHeapStart += constantBufferDescriptorSize;
}
InitBundles();
}
示例7: LoadMesh
private void LoadMesh(CpuDescriptorHandle heapStart)
{
SamplerStateDescription samplerDesc = new SamplerStateDescription()
{
Filter = Filter.ComparisonMinMagMipLinear,
AddressU = TextureAddressMode.Wrap,
AddressV = TextureAddressMode.Wrap,
AddressW = TextureAddressMode.Wrap,
MinimumLod = float.MinValue,
MaximumLod = float.MaxValue,
MipLodBias = 0,
MaximumAnisotropy = 0,
ComparisonFunction = Comparison.Never
};
var heapPosition = heapStart;
// Load model from obj.
var importer = new Assimp.AssimpContext();
var scene = importer.ImportFile(@"../../../Models/lara/lara.obj", PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.FlipUVs | PostProcessSteps.PreTransformVertices);
Vertex[] vertices = new Vertex[scene.Meshes.Sum(m => m.VertexCount)];
int[] indices = new int[scene.Meshes.Sum(m => m.FaceCount * 3)];
faceCounts = new List<int>();
int vertexOffSet = 0;
int indexOffSet = 0;
foreach (var mesh in scene.Meshes)
{
var positions = mesh.Vertices;
var normals = mesh.Normals;
var texs = mesh.TextureCoordinateChannels[0];
for (int i = 0; i < mesh.VertexCount; i++)
{
vertices[vertexOffSet + i] = new Vertex()
{
position = new Vector3(positions[i].X, positions[i].Y, positions[i].Z),
normal = new Vector3(normals[i].X, normals[i].Y, normals[i].Z),
textureCoordinate = new Vector3(texs[i].X, texs[i].Y, texs[i].Z)
};
}
var faces = mesh.Faces;
for (int i = 0; i < mesh.FaceCount; i++)
{
indices[i * 3 + indexOffSet] = (int)faces[i].Indices[0] + vertexOffSet;
indices[i * 3 + 1 + indexOffSet] = (int)faces[i].Indices[1] + vertexOffSet;
indices[i * 3 + 2 + indexOffSet] = (int)faces[i].Indices[2] + vertexOffSet;
}
faceCounts.Add(mesh.FaceCount * 3);
vertexOffSet += mesh.VertexCount;
indexOffSet += mesh.FaceCount * 3;
string textureName = System.IO.Path.GetFileName(scene.Materials[mesh.MaterialIndex].TextureDiffuse.FilePath);
var texResource = TextureUtilities.CreateTextureFromDDS(device, @"../../../Models/lara/" + textureName);
textures.Add(texResource);
int D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING = 5768;
ShaderResourceViewDescription desc = new ShaderResourceViewDescription
{
Dimension = ShaderResourceViewDimension.Texture2D,
Format = texResource.Description.Format,
Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
};
desc.Texture2D.MipLevels = 1;
desc.Texture2D.MostDetailedMip = 0;
desc.Texture2D.ResourceMinLODClamp = 0;
device.CreateShaderResourceView(texResource, desc, heapStart);
heapStart += constantBufferDescriptorSize;
}
int vertexBufferSize = Utilities.SizeOf(vertices);
// Note: using upload heaps to transfer static data like vert buffers is not
// recommended. Every time the GPU needs it, the upload heap will be marshalled
// over. Please read up on Default Heap usage. An upload heap is used here for
// code simplicity and because there are very few verts to actually transfer.
vertexBuffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(vertexBufferSize), ResourceStates.GenericRead);
// Copy the triangle data to the vertex buffer.
IntPtr pVertexDataBegin = vertexBuffer.Map(0);
Utilities.Write(pVertexDataBegin, vertices, 0, vertices.Length);
vertexBuffer.Unmap(0);
// Initialize the vertex buffer view.
vertexBufferView = new VertexBufferView();
vertexBufferView.BufferLocation = vertexBuffer.GPUVirtualAddress;
vertexBufferView.StrideInBytes = Utilities.SizeOf<Vertex>();
vertexBufferView.SizeInBytes = vertexBufferSize;
//Create Index Buffer
int indexBufferSize = Utilities.SizeOf(indices);
indexBuffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(indexBufferSize), ResourceStates.GenericRead);
// Copy the triangle data to the vertex buffer.
IntPtr pIndexDataBegin = indexBuffer.Map(0);
Utilities.Write(pIndexDataBegin, indices, 0, indices.Length);
indexBuffer.Unmap(0);
//.........这里部分代码省略.........
示例8: LoadAssets
//.........这里部分代码省略.........
new Vertex() {Position = new Vector3(1f , 1f , 1f) , TexCoord = new Vector2(0f ,1f)} ,
new Vertex() {Position = new Vector3(1f , 1f ,-1f) , TexCoord = new Vector2(0f ,0f)} ,
new Vertex() {Position = new Vector3(-1f , 1f ,-1f) , TexCoord = new Vector2(1f ,0f)} ,
//BOTTOM
new Vertex() {Position = new Vector3(-1f ,-1f , 1f) , TexCoord = new Vector2(1f ,1f)} ,
new Vertex() {Position = new Vector3(1f ,-1f , 1f) , TexCoord = new Vector2(0f ,1f)} ,
new Vertex() {Position = new Vector3(1f ,-1f ,-1f) , TexCoord = new Vector2(0f ,0f)} ,
new Vertex() {Position = new Vector3(-1f ,-1f ,-1f) , TexCoord = new Vector2(1f ,0f)} ,
//LEFT
new Vertex() {Position = new Vector3(-1f ,-1f , 1f) , TexCoord = new Vector2(0f ,1f)} ,
new Vertex() {Position = new Vector3(-1f , 1f , 1f) , TexCoord = new Vector2(0f ,0f)} ,
new Vertex() {Position = new Vector3(-1f , 1f ,-1f) , TexCoord = new Vector2(1f ,0f)} ,
new Vertex() {Position = new Vector3(-1f ,-1f ,-1f) , TexCoord = new Vector2(1f ,1f)} ,
//RIGHT
new Vertex() {Position = new Vector3(1f ,-1f , 1f) , TexCoord = new Vector2(1f ,1f)} ,
new Vertex() {Position = new Vector3(1f , 1f , 1f) , TexCoord = new Vector2(1f ,0f)} ,
new Vertex() {Position = new Vector3(1f , 1f ,-1f) , TexCoord = new Vector2(0f ,0f)} ,
new Vertex() {Position = new Vector3(1f ,-1f ,-1f) , TexCoord = new Vector2(0f ,1f)} ,
//FRONT
new Vertex() {Position = new Vector3(-1f , 1f , 1f) , TexCoord = new Vector2(1f ,0f)} ,
new Vertex() {Position = new Vector3(1f , 1f , 1f) , TexCoord = new Vector2(0f ,0f)} ,
new Vertex() {Position = new Vector3(1f ,-1f , 1f) , TexCoord = new Vector2(0f ,1f)} ,
new Vertex() {Position = new Vector3(-1f ,-1f , 1f) , TexCoord = new Vector2(1f ,1f)} ,
//BACK
new Vertex() {Position = new Vector3(-1f , 1f ,-1f) , TexCoord = new Vector2(0f ,0f)} ,
new Vertex() {Position = new Vector3(1f , 1f ,-1f) , TexCoord = new Vector2(1f ,0f)} ,
new Vertex() {Position = new Vector3(1f ,-1f ,-1f) , TexCoord = new Vector2(1f ,1f)} ,
new Vertex() {Position = new Vector3(-1f ,-1f ,-1f) , TexCoord = new Vector2(0f ,1f)}
};
int vertexBufferSize = Utilities.SizeOf(triangleVertices);
vertexBuffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(vertexBufferSize), ResourceStates.GenericRead);
IntPtr pVertexDataBegin = vertexBuffer.Map(0);
Utilities.Write(pVertexDataBegin, triangleVertices, 0, triangleVertices.Length);
vertexBuffer.Unmap(0);
vertexBufferView = new VertexBufferView();
vertexBufferView.BufferLocation = vertexBuffer.GPUVirtualAddress;
vertexBufferView.StrideInBytes = Utilities.SizeOf<Vertex>();
vertexBufferView.SizeInBytes = vertexBufferSize;
// build index buffer
var triangleIndexes = new uint[]
{
0,1,2,
0,2,3,
4,6,5,
4,7,6,
8,9,10,
8,10,11,
12,14,13,
12,15,14,
16,18,17,
16,19,18,
20,21,22,
20,22,23
};
int indexBufferSize = Utilities.SizeOf(triangleIndexes);