本文整理汇总了C#中Device.CreateTexture2D方法的典型用法代码示例。如果您正苦于以下问题:C# Device.CreateTexture2D方法的具体用法?C# Device.CreateTexture2D怎么用?C# Device.CreateTexture2D使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Device
的用法示例。
在下文中一共展示了Device.CreateTexture2D方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Initialize
public override WriteableBitmap Initialize(Device device)
{
const int width = 600;
const int height = 400;
// Create device and swap chain.
var swapChainPresenter = new WpfSwapChainPresenter();
_swapChain = device.CreateSwapChain(width, height, swapChainPresenter);
_deviceContext = device.ImmediateContext;
// Create RenderTargetView from the backbuffer.
var backBuffer = Texture2D.FromSwapChain(_swapChain, 0);
_renderTargetView = device.CreateRenderTargetView(backBuffer);
// Create DepthStencilView.
var depthStencilBuffer = device.CreateTexture2D(new Texture2DDescription
{
ArraySize = 1,
MipLevels = 1,
Width = width,
Height = height,
BindFlags = BindFlags.DepthStencil
});
var depthStencilView = device.CreateDepthStencilView(depthStencilBuffer);
// Prepare all the stages.
_deviceContext.Rasterizer.SetViewports(new Viewport(0, 0, width, height, 0.0f, 1.0f));
_deviceContext.OutputMerger.SetTargets(depthStencilView, _renderTargetView);
return swapChainPresenter.Bitmap;
}
示例2: Texture2D
/// <summary>
/// Initializes a new instance of the <see cref = "T:SharpDX.Direct3D11.Texture2D" /> class.
/// </summary>
/// <param name = "device">The device with which to associate the texture.</param>
/// <param name = "description">The description of the texture.</param>
/// <param name = "data">An array of initial texture data for each subresource.</param>
/// <msdn-id>ff476521</msdn-id>
/// <unmanaged>HRESULT ID3D11Device::CreateTexture2D([In] const D3D11_TEXTURE2D_DESC* pDesc,[In, Buffer, Optional] const D3D11_SUBRESOURCE_DATA* pInitialData,[Out, Fast] ID3D11Texture2D** ppTexture2D)</unmanaged>
/// <unmanaged-short>ID3D11Device::CreateTexture2D</unmanaged-short>
public Texture2D(Device device, Texture2DDescription description, params DataRectangle[] data)
: base(IntPtr.Zero)
{
DataBox[] subResourceDatas = null;
if (data != null && data.Length > 0)
{
subResourceDatas = new DataBox[data.Length];
for (int i = 0; i < subResourceDatas.Length; i++)
{
subResourceDatas[i].DataPointer = data[i].DataPointer;
subResourceDatas[i].RowPitch = data[i].Pitch;
}
}
device.CreateTexture2D(ref description, subResourceDatas, this);
}
示例3: CreateCheckerboard
public static Texture2D CreateCheckerboard(Device device, int width, int height)
{
var texture = device.CreateTexture2D(new Texture2DDescription
{
Width = width,
Height = height,
ArraySize = 1
});
var data = new Color4[width * height];
bool black = false;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
data[(y * width) + x] = (black) ? new Color4(0, 0, 0, 1) : new Color4(1, 1, 1, 1);
if (x % 8 == 0)
black = !black;
}
if (y % 8 == 0)
black = !black;
}
device.ImmediateContext.SetTextureData(texture, 0, data);
return texture;
}
示例4: FromColors
public static Texture2D FromColors(Device device, Color4[] data, int width, int height)
{
var result = device.CreateTexture2D(new Texture2DDescription
{
Width = width,
Height = height,
MipLevels = 0,
ArraySize = 1,
});
device.ImmediateContext.SetTextureData(result, 0, data);
device.ImmediateContext.GenerateMips(result);
return result;
}
示例5: ImageGenerator
public ImageGenerator(IResourceManager resourceLoader)
{
const int width = 400;
const int height = 400;
// Create device and swap chain.
_device = new Device();
var swapChainPresenter = new WpfSwapChainPresenter();
_swapChain = _device.CreateSwapChain(width, height, swapChainPresenter);
_outputBitmap = swapChainPresenter.Bitmap;
_deviceContext = _device.ImmediateContext;
// Create RenderTargetView from the backbuffer.
var backBuffer = Texture2D.FromSwapChain(_swapChain, 0);
_renderTargetView = _device.CreateRenderTargetView(backBuffer);
// Create the depth buffer
var depthBuffer = _device.CreateTexture2D(new Texture2DDescription
{
ArraySize = 1,
MipLevels = 1,
Width = width,
Height = height,
BindFlags = BindFlags.DepthStencil
});
// Create the depth buffer view
_depthView = _device.CreateDepthStencilView(depthBuffer);
// Compile Vertex and Pixel shaders
var vertexShaderByteCode = ShaderCompiler.CompileFromFile("Modules/SampleBrowser/TechDemos/Resources/TextureSampling/Texture.fx", "VS", "vs_4_0");
var vertexShader = _device.CreateVertexShader(vertexShaderByteCode);
var pixelShaderByteCode = ShaderCompiler.CompileFromFile("Modules/SampleBrowser/TechDemos/Resources/TextureSampling/Texture.fx", "PS", "ps_4_1");
var pixelShader = _device.CreatePixelShader(pixelShaderByteCode);
// Layout from VertexShader input signature
var layout = _device.CreateInputLayout(
new[]
{
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
new InputElement("TEXCOORD", 0, Format.R32G32_Float, 0, 16)
}, vertexShaderByteCode);
// Instantiate Vertex buiffer from vertex data
var vertices = _device.CreateBuffer(new BufferDescription(BindFlags.VertexBuffer), new[]
{
// 3D coordinates UV Texture coordinates
-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, // Front
-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f,
1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // BACK
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, // Top
-1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 0.0f, // Bottom
1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 0.0f,
1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, // Left
-1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 0.0f, // Right
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f
});
// Create Constant Buffer
_constantBuffer = _device.CreateBuffer(new BufferDescription
{
SizeInBytes = Utilities.SizeOf<Matrix>(),
BindFlags = BindFlags.ConstantBuffer
});
// Load texture and create sampler
var textureStream = resourceLoader.GetStream(
//.........这里部分代码省略.........
示例6: Initialize
public override WriteableBitmap Initialize(Device device)
{
const int width = 600;
const int height = 400;
// Create device and swap chain.
var swapChainPresenter = new WpfSwapChainPresenter();
_swapChain = device.CreateSwapChain(width, height, swapChainPresenter);
_deviceContext = device.ImmediateContext;
// Create RenderTargetView from the backbuffer.
var backBuffer = Texture2D.FromSwapChain(_swapChain, 0);
_renderTargetView = device.CreateRenderTargetView(backBuffer);
// Create the depth buffer
var depthBuffer = device.CreateTexture2D(new Texture2DDescription
{
ArraySize = 1,
MipLevels = 1,
Width = width,
Height = height,
BindFlags = BindFlags.DepthStencil
});
// Create the depth buffer view
_depthView = device.CreateDepthStencilView(depthBuffer);
// Load model.
var modelLoader = new ModelLoader(device, TextureLoader.CreateTextureFromStream);
_model = modelLoader.Load("Modules/SampleBrowser/Samples/ModelLoading/Car/car.obj");
// Compile Vertex and Pixel shaders
var vertexShaderByteCode = ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/ModelLoading/ModelLoading.fx", "VS", "vs_4_0");
var vertexShader = device.CreateVertexShader(vertexShaderByteCode);
var pixelShaderByteCode = ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/ModelLoading/ModelLoading.fx", "PS", "ps_4_0");
var pixelShader = device.CreatePixelShader(pixelShaderByteCode);
_model.SetInputLayout(device, vertexShaderByteCode.InputSignature);
var sampler = device.CreateSamplerState(new SamplerStateDescription
{
Filter = Filter.MinMagMipPoint,
AddressU = TextureAddressMode.Wrap,
AddressV = TextureAddressMode.Wrap,
AddressW = TextureAddressMode.Wrap,
BorderColor = Color4.Black,
ComparisonFunction = Comparison.Never,
MaximumAnisotropy = 16,
MipLodBias = 0,
MinimumLod = 0,
MaximumLod = 16,
});
// Create constant buffers.
_vertexConstantBuffer = device.CreateBuffer(new BufferDescription
{
SizeInBytes = Utilities.SizeOf<VertexShaderData>(),
BindFlags = BindFlags.ConstantBuffer
});
_pixelConstantBuffer = device.CreateBuffer(new BufferDescription
{
SizeInBytes = Utilities.SizeOf<PixelShaderData>(),
BindFlags = BindFlags.ConstantBuffer
});
// Prepare all the stages
_deviceContext.VertexShader.SetConstantBuffers(0, _vertexConstantBuffer);
_deviceContext.VertexShader.Shader = vertexShader;
_deviceContext.PixelShader.SetConstantBuffers(0, _pixelConstantBuffer);
_deviceContext.PixelShader.Shader = pixelShader;
_deviceContext.PixelShader.SetSamplers(0, sampler);
_vertexShaderData = new VertexShaderData();
_pixelShaderData = new PixelShaderData
{
LightDirection = new Vector4(Vector3.Normalize(new Vector3(0.5f, 0, -1)), 1)
};
_deviceContext.SetBufferData(_pixelConstantBuffer, ref _pixelShaderData);
// Setup targets and viewport for rendering
_deviceContext.Rasterizer.SetViewports(new Viewport(0, 0, width, height, 0.0f, 1.0f));
_deviceContext.OutputMerger.SetTargets(_depthView, _renderTargetView);
//_deviceContext.Rasterizer.State = device.CreateRasterizerState(new Pipeline.Rasterizer.RasterizerStateDescription
//{
// FillMode = Pipeline.Rasterizer.FillMode.Wireframe
//});
// Prepare matrices
_projection = Matrix.PerspectiveFovLH(MathUtil.Pi / 3.0f,
width / (float) height, 1f, 1000.0f);
return swapChainPresenter.Bitmap;
}
示例7: Initialize
public override WriteableBitmap Initialize(Device device)
{
const int width = 600;
const int height = 400;
// Create device and swap chain.
var swapChainPresenter = new WpfSwapChainPresenter();
_swapChain = device.CreateSwapChain(width, height, swapChainPresenter);
_deviceContext = device.ImmediateContext;
// Create RenderTargetView from the backbuffer.
var backBuffer = Texture2D.FromSwapChain(_swapChain, 0);
_renderTargetView = device.CreateRenderTargetView(backBuffer);
// Create the depth buffer
var depthBuffer = device.CreateTexture2D(new Texture2DDescription
{
ArraySize = 1,
MipLevels = 1,
Width = width,
Height = height,
BindFlags = BindFlags.DepthStencil
});
// Create the depth buffer view
_depthView = device.CreateDepthStencilView(depthBuffer);
// Compile Vertex and Pixel shaders
var vertexShaderByteCode = ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/MiniCubeTexture/MiniCubeTexture.fx", "VS", "vs_4_0");
var vertexShader = device.CreateVertexShader(vertexShaderByteCode);
var pixelShaderByteCode = ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/MiniCubeTexture/MiniCubeTexture.fx", "PS", "ps_4_0");
var pixelShader = device.CreatePixelShader(pixelShaderByteCode);
// Layout from VertexShader input signature
var layout = device.CreateInputLayout(
new[]
{
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
new InputElement("TEXCOORD", 0, Format.R32G32_Float, 0, 16)
}, vertexShaderByteCode);
// Instantiate Vertex buiffer from vertex data
var vertices = device.CreateBuffer(new BufferDescription(BindFlags.VertexBuffer), new[]
{
// 3D coordinates UV Texture coordinates
-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, // Front
-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f,
1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // BACK
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, // Top
-1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
-1.0f,-1.0f, -1.0f, 1.0f, 1.0f, 0.0f, // Bottom
1.0f,-1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-1.0f,-1.0f, -1.0f, 1.0f, 1.0f, 0.0f,
1.0f,-1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
1.0f,-1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, // Left
-1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 0.0f, // Right
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f
});
// Create Constant Buffer
_constantBuffer = device.CreateBuffer(new BufferDescription
{
SizeInBytes = Utilities.SizeOf<Matrix>(),
BindFlags = BindFlags.ConstantBuffer
});
// Load texture and create sampler
var textureStream = _resourceLoader.GetStream(
"Modules/SampleBrowser/Samples/MiniCubeTexture/GeneticaMortarlessBlocks.jpg",
GetType().Assembly.FullName);
//.........这里部分代码省略.........
示例8: Initialize
public override WriteableBitmap Initialize(Device device)
{
// Create device and swap chain.
var swapChainPresenter = new WpfSwapChainPresenter();
_swapChain = device.CreateSwapChain(Width, Height, swapChainPresenter);
_deviceContext = device.ImmediateContext;
// Create RenderTargetView from the backbuffer.
var backBuffer = Texture2D.FromSwapChain(_swapChain, 0);
_renderTargetView = device.CreateRenderTargetView(backBuffer);
// Create the depth buffer
var depthBuffer = device.CreateTexture2D(new Texture2DDescription
{
ArraySize = 1,
MipLevels = 1,
Width = Width,
Height = Height,
BindFlags = BindFlags.DepthStencil
});
// Create the depth buffer view
_depthView = device.CreateDepthStencilView(depthBuffer);
// Create cube map resources.
var cubeMapTexture = device.CreateTexture2D(new Texture2DDescription
{
Width = CubeMapSize,
Height= CubeMapSize,
ArraySize = 6,
BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
MipLevels = 1
});
_renderTargetViewCube = device.CreateRenderTargetView(cubeMapTexture);
_resourceViewCube = device.CreateShaderResourceView(cubeMapTexture);
var depthBufferCube = device.CreateTexture2D(new Texture2DDescription
{
ArraySize = 6,
MipLevels = 1,
Width = CubeMapSize,
Height = CubeMapSize,
BindFlags = BindFlags.DepthStencil
});
_depthViewCube = device.CreateDepthStencilView(depthBufferCube);
// Load model.
var modelLoader = new ModelLoader(device, TextureLoader.CreateTextureFromStream);
_model = modelLoader.Load("Modules/SampleBrowser/Samples/EnvironmentMapping/teapot.obj");
// Compile Vertex and Pixel shaders
var vertexShaderByteCode = ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/EnvironmentMapping/EnvironmentMapping.fx", "VS_CubeMap", "vs_4_0");
_vertexShaderCubeMap = device.CreateVertexShader(vertexShaderByteCode);
_vertexShaderStandard = device.CreateVertexShader(ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/EnvironmentMapping/EnvironmentMapping.fx", "VS_Standard", "vs_4_0"));
_geometryShaderCubeMap = device.CreateGeometryShader(ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/EnvironmentMapping/EnvironmentMapping.fx", "GS_CubeMap", "gs_4_0"));
_pixelShaderCubeMap = device.CreatePixelShader(ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/EnvironmentMapping/EnvironmentMapping.fx", "PS_CubeMap", "ps_4_0"));
_pixelShaderStandard = device.CreatePixelShader(ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/EnvironmentMapping/EnvironmentMapping.fx", "PS_Standard", "ps_4_0"));
_pixelShaderReflective = device.CreatePixelShader(ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/EnvironmentMapping/EnvironmentMapping.fx", "PS_Reflective", "ps_4_0"));
_model.SetInputLayout(device, vertexShaderByteCode.InputSignature);
var sampler = device.CreateSamplerState(SamplerStateDescription.Default);
// Create constant buffers.
_vertexConstantBuffer = device.CreateBuffer(new BufferDescription
{
SizeInBytes = Utilities.SizeOf<VertexShaderData>(),
BindFlags = BindFlags.ConstantBuffer
});
_geometryConstantBuffer = device.CreateBuffer(new BufferDescription
{
SizeInBytes = Utilities.SizeOf<GeometryShaderData>(),
BindFlags = BindFlags.ConstantBuffer
});
var pixelConstantBuffer = device.CreateBuffer(new BufferDescription
{
SizeInBytes = Utilities.SizeOf<PixelShaderData>(),
BindFlags = BindFlags.ConstantBuffer
});
// Prepare all the stages
_deviceContext.VertexShader.SetConstantBuffers(0, _vertexConstantBuffer);
_deviceContext.GeometryShader.SetConstantBuffers(0, _geometryConstantBuffer);
_deviceContext.PixelShader.SetConstantBuffers(0, pixelConstantBuffer);
_deviceContext.PixelShader.SetSamplers(0, sampler);
_vertexShaderData = new VertexShaderData();
_view1 = Matrix.LookAtLH(Vector3.Zero, new Vector3(1, 0, 0), Vector3.UnitY);
_view2 = Matrix.LookAtLH(Vector3.Zero, new Vector3(-1, 0, 0), Vector3.UnitY);
_view3 = Matrix.LookAtLH(Vector3.Zero, new Vector3(0, 1, 0), -Vector3.UnitZ);
_view4 = Matrix.LookAtLH(Vector3.Zero, new Vector3(0, -1, 0), Vector3.UnitZ);
_view5 = Matrix.LookAtLH(Vector3.Zero, new Vector3(0, 0, 1), Vector3.UnitY);
_view6 = Matrix.LookAtLH(Vector3.Zero, new Vector3(0, 0, -1), Vector3.UnitY);
var from = new Vector3(0, 30, 70);
var to = new Vector3(0, 0, 0);
// Prepare matrices
//.........这里部分代码省略.........
示例9: Initialize
public override WriteableBitmap Initialize(Device device)
{
const int width = 400;
const int height = 300;
// Create device and swap chain.
var swapChainPresenter = new WpfSwapChainPresenter();
_swapChain = device.CreateSwapChain(width, height, swapChainPresenter);
_deviceContext = device.ImmediateContext;
// Create RenderTargetView from the backbuffer.
var backBuffer = Texture2D.FromSwapChain(_swapChain, 0);
_renderTargetView = device.CreateRenderTargetView(backBuffer);
// Create the depth buffer
var depthBuffer = device.CreateTexture2D(new Texture2DDescription
{
ArraySize = 1,
MipLevels = 1,
Width = width,
Height = height,
BindFlags = BindFlags.DepthStencil
});
// Create the depth buffer view
_depthView = device.CreateDepthStencilView(depthBuffer);
// Compile Vertex and Pixel shaders
var vertexShaderByteCode = ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/MiniCube/MiniCube.fx", "VS", "vs_4_0");
var vertexShader = device.CreateVertexShader(vertexShaderByteCode);
var pixelShaderByteCode = ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/MiniCube/MiniCube.fx", "PS", "ps_4_0");
var pixelShader = device.CreatePixelShader(pixelShaderByteCode);
// Layout from VertexShader input signature
var layout = device.CreateInputLayout(
new[]
{
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0),
new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 0)
}, vertexShaderByteCode);
// Instantiate Vertex buffer from vertex data
var vertices = device.CreateBuffer(new BufferDescription(BindFlags.VertexBuffer), new[]
{
new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), // Front
new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
new Vector4(1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
new Vector4(1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
new Vector4(1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), // BACK
new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
new Vector4(1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), // Top
new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),
new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),
new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),
new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),
new Vector4(1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),
new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), // Bottom
new Vector4(1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
new Vector4(1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
new Vector4(1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), // Left
new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),
new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),
new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),
new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),
new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),
new Vector4(1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), // Right
new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f),
new Vector4(1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f),
new Vector4(1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f),
new Vector4(1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f),
new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f)
});
// Create Constant Buffer
_constantBuffer = device.CreateBuffer(new BufferDescription
{
SizeInBytes = Utilities.SizeOf<Matrix>(),
BindFlags = BindFlags.ConstantBuffer
});
// Prepare all the stages
_deviceContext.InputAssembler.InputLayout = layout;
_deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
_deviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, 0, Utilities.SizeOf<Vector4>() * 2));
_deviceContext.VertexShader.SetConstantBuffers(0, _constantBuffer);
//.........这里部分代码省略.........