本文整理汇总了C#中Resource.WriteToSubresource方法的典型用法代码示例。如果您正苦于以下问题:C# Resource.WriteToSubresource方法的具体用法?C# Resource.WriteToSubresource怎么用?C# Resource.WriteToSubresource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Resource
的用法示例。
在下文中一共展示了Resource.WriteToSubresource方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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,
//.........这里部分代码省略.........
示例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
//.........这里部分代码省略.........
16,18,17,
16,19,18,
20,21,22,
20,22,23
};
int indexBufferSize = Utilities.SizeOf(triangleIndexes);
indexBuffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(indexBufferSize), ResourceStates.GenericRead);
IntPtr pIndexDataBegin = indexBuffer.Map(0);
Utilities.Write(pIndexDataBegin, triangleIndexes, 0, triangleIndexes.Length);
indexBuffer.Unmap(0);
indexBufferView = new IndexBufferView();
indexBufferView.BufferLocation = indexBuffer.GPUVirtualAddress;
indexBufferView.SizeInBytes = indexBufferSize;
indexBufferView.Format = Format.R32_UInt;
// Create the texture.
// Describe and create a Texture2D.
var textureDesc = ResourceDescription.Texture2D(Format.R8G8B8A8_UNorm, TextureWidth, TextureHeight, 1, 1, 1, 0, ResourceFlags.None, TextureLayout.Unknown, 0);
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.
byte[] textureData = Utilities.ReadStream(new FileStream("../../texture1.dds", FileMode.Open));
texture.Name = "Texture";
var handle = GCHandle.Alloc(textureData, GCHandleType.Pinned);
var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(textureData, 0);
texture.WriteToSubresource(0, null, ptr, TextureWidth * 4, textureData.Length);
handle.Free();
// Describe and create a SRV for the texture.
var srvDesc = 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
},
};
device.CreateShaderResourceView(texture, srvDesc, srvCbvHeap.CPUDescriptorHandleForHeapStart);
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
};