[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
public void UpdateSubresource(DataBox source, Resource resource, int subresource);
using SlimDX;using SlimDX.Direct3D11;using SlimDX.DXGI;using SlimDX.D3DCompiler;class Program{static void Main(){using (Game game = new MyGame()){game.Run();}}}struct MyConstantBuffer{public float OffsetX;public float OffsetY;}class MyGame : Game{Effect effect;InputLayout vertexLayout;Buffer vertexBuffer;Buffer constantBuffer;protected override void Draw(){GraphicsDevice.ImmediateContext.ClearRenderTargetView(RenderTarget,new Color4(1, 0, 0, 1));updateConstantBuffer();initTriangleInputAssembler();drawTriangle();SwapChain.Present(0, PresentFlags.None);}private void updateConstantBuffer(){double time = System.Environment.TickCount / 500d;MyConstantBuffer myConstantBuffer = new MyConstantBuffer{OffsetX = (float)System.Math.Sin(time) * 0.4f,OffsetY = (float)System.Math.Sin(time) * 0.8f};GraphicsDevice.ImmediateContext.UpdateSubresource(new DataBox(0, 0, new DataStream(new[] { myConstantBuffer }, true, true)),constantBuffer,0);effect.GetConstantBufferByName("myConstantBuffer").ConstantBuffer = constantBuffer;}private void initTriangleInputAssembler(){GraphicsDevice.ImmediateContext.InputAssembler.InputLayout = vertexLayout;GraphicsDevice.ImmediateContext.InputAssembler.SetVertexBuffers(0,new VertexBufferBinding(vertexBuffer, sizeof(float) * 3, 0));GraphicsDevice.ImmediateContext.InputAssembler.PrimitiveTopology= PrimitiveTopology.TriangleList;}private void drawTriangle(){effect.GetTechniqueByIndex(0).GetPassByIndex(0).Apply(GraphicsDevice.ImmediateContext);GraphicsDevice.ImmediateContext.Draw(3, 0);}protected override void LoadContent(){initEffect();initVertexLayout();initVertexBuffer();initConstantBuffer();}private void initEffect(){using (ShaderBytecode shaderBytecode = ShaderBytecode.CompileFromFile("myEffect.fx", "fx_5_0",ShaderFlags.None,EffectFlags.None)){effect = new Effect(GraphicsDevice, shaderBytecode);}}private void initVertexLayout(){vertexLayout = new InputLayout(GraphicsDevice,effect.GetTechniqueByIndex(0).GetPassByIndex(0).Description.Signature,new[] {new InputElement{SemanticName = "SV_Position",Format = Format.R32G32B32_Float}});}private void initVertexBuffer(){vertexBuffer = MyDirectXHelper.CreateVertexBuffer(GraphicsDevice,new[] {new SlimDX.Vector3(0, 0.5f, 0),new SlimDX.Vector3(0.5f, 0, 0),new SlimDX.Vector3(-0.5f, 0, 0),});}private void initConstantBuffer(){constantBuffer = new Buffer(GraphicsDevice,new BufferDescription{//なぜかsizeof(float) * 4以上でないと例外をスローする。SizeInBytes = sizeof(float) * 4,BindFlags = BindFlags.ConstantBuffer,});}protected override void UnloadContent(){effect.Dispose();vertexLayout.Dispose();vertexBuffer.Dispose();constantBuffer.Dispose();}}class Game : System.Windows.Forms.Form{public SlimDX.Direct3D11.Device GraphicsDevice;public SwapChain SwapChain;public RenderTargetView RenderTarget;public void Run(){initDevice();SlimDX.Windows.MessagePump.Run(this, Draw);disposeDevice();}private void initDevice(){MyDirectXHelper.CreateDeviceAndSwapChain(this, out GraphicsDevice, out SwapChain);initRenderTarget();initViewport();LoadContent();}private void initRenderTarget(){using (Texture2D backBuffer= SlimDX.Direct3D11.Resource.FromSwapChain<Texture2D>(SwapChain, 0)){RenderTarget = new RenderTargetView(GraphicsDevice, backBuffer);GraphicsDevice.ImmediateContext.OutputMerger.SetTargets(RenderTarget);}}private void initViewport(){GraphicsDevice.ImmediateContext.Rasterizer.SetViewports(new Viewport{Width = ClientSize.Width,Height = ClientSize.Height,});}private void disposeDevice(){UnloadContent();RenderTarget.Dispose();GraphicsDevice.Dispose();SwapChain.Dispose();}protected virtual void Draw() { }protected virtual void LoadContent() { }protected virtual void UnloadContent() { }}class MyDirectXHelper{public static void CreateDeviceAndSwapChain(System.Windows.Forms.Form form,out SlimDX.Direct3D11.Device device,out SlimDX.DXGI.SwapChain swapChain){SlimDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware,DeviceCreationFlags.None,new SwapChainDescription{BufferCount = 1,OutputHandle = form.Handle,IsWindowed = true,SampleDescription = new SampleDescription{Count = 1,Quality = 0},ModeDescription = new ModeDescription{Width = form.ClientSize.Width,Height = form.ClientSize.Height,RefreshRate = new SlimDX.Rational(60, 1),Format = Format.R8G8B8A8_UNorm},Usage = Usage.RenderTargetOutput},out device,out swapChain);}public static Buffer CreateVertexBuffer(SlimDX.Direct3D11.Device graphicsDevice,System.Array vertices){using (SlimDX.DataStream vertexStream= new SlimDX.DataStream(vertices, true, true)){return new Buffer(graphicsDevice,vertexStream,new BufferDescription{SizeInBytes= (int)vertexStream.Length,BindFlags = BindFlags.VertexBuffer,});}}}
cbuffer myConstantBuffer{float OffsetX;float OffsetY;}float4 MyVertexShader(float4 position : SV_Position) : SV_Position{return position + float4(OffsetX, OffsetY, 0, 0);}float4 MyPixelShader() : SV_Target{return float4(1, 1, 1, 1);}technique10 MyTechnique{pass MyPass{SetVertexShader( CompileShader( vs_5_0, MyVertexShader() ) );SetPixelShader( CompileShader( ps_5_0, MyPixelShader() ) );}}
[maxvertexcount(VertexCount)] void ShaderName( triangle DataType vertices[3], inout TriangleStream<DataType> resultStream )
class TriangleStream<T> { void Append(T vertex); void RestartStrip(); }
using SlimDX.Direct3D11;using SlimDX.DXGI;using SlimDX.D3DCompiler;class Program{static void Main(){using (Game game = new MyGame()){game.Run();}}}class MyGame : Game{Effect effect;InputLayout vertexLayout;Buffer vertexBuffer;protected override void Draw(){GraphicsDevice.ImmediateContext.ClearRenderTargetView(RenderTarget,new SlimDX.Color4(1, 0, 0, 1));initTriangleInputAssembler();drawTriangle();SwapChain.Present(0, PresentFlags.None);}private void drawTriangle(){effect.GetTechniqueByIndex(0).GetPassByIndex(0).Apply(GraphicsDevice.ImmediateContext);GraphicsDevice.ImmediateContext.Draw(3, 0);}private void initTriangleInputAssembler(){GraphicsDevice.ImmediateContext.InputAssembler.InputLayout = vertexLayout;GraphicsDevice.ImmediateContext.InputAssembler.SetVertexBuffers(0,new VertexBufferBinding(vertexBuffer, sizeof(float) * 3, 0));GraphicsDevice.ImmediateContext.InputAssembler.PrimitiveTopology= PrimitiveTopology.TriangleList;}protected override void LoadContent(){initEffect();initVertexLayout();initVertexBuffer();initWireframeRasterizerState();}private void initEffect(){using (ShaderBytecode shaderBytecode = ShaderBytecode.CompileFromFile("myEffect.fx", "fx_5_0",ShaderFlags.None,EffectFlags.None)){effect = new Effect(GraphicsDevice, shaderBytecode);}}private void initVertexLayout(){vertexLayout = new InputLayout(GraphicsDevice,effect.GetTechniqueByIndex(0).GetPassByIndex(0).Description.Signature,new[] {new InputElement{SemanticName = "SV_Position",Format = Format.R32G32B32_Float}});}private void initVertexBuffer(){vertexBuffer = MyDirectXHelper.CreateVertexBuffer(GraphicsDevice,new[] {new SlimDX.Vector3(0, 0.5f, 0),new SlimDX.Vector3(0.5f, 0, 0),new SlimDX.Vector3(-0.5f, 0, 0),});}private void initWireframeRasterizerState(){var desc = new RasterizerStateDescription{CullMode = CullMode.Back,FillMode = FillMode.Wireframe,};GraphicsDevice.ImmediateContext.Rasterizer.State= RasterizerState.FromDescription(GraphicsDevice,desc);}protected override void UnloadContent(){GraphicsDevice.ImmediateContext.Rasterizer.State.Dispose();effect.Dispose();vertexLayout.Dispose();vertexBuffer.Dispose();}}class Game : System.Windows.Forms.Form{public SlimDX.Direct3D11.Device GraphicsDevice;public SwapChain SwapChain;public RenderTargetView RenderTarget;public void Run(){initDevice();SlimDX.Windows.MessagePump.Run(this, Draw);disposeDevice();}private void initDevice(){MyDirectXHelper.CreateDeviceAndSwapChain(this, out GraphicsDevice, out SwapChain);initRenderTarget();initViewport();LoadContent();}private void initRenderTarget(){using (Texture2D backBuffer= SlimDX.Direct3D11.Resource.FromSwapChain<Texture2D>(SwapChain, 0)){RenderTarget = new RenderTargetView(GraphicsDevice, backBuffer);GraphicsDevice.ImmediateContext.OutputMerger.SetTargets(RenderTarget);}}private void initViewport(){GraphicsDevice.ImmediateContext.Rasterizer.SetViewports(new Viewport{Width = ClientSize.Width,Height = ClientSize.Height,});}private void disposeDevice(){UnloadContent();RenderTarget.Dispose();GraphicsDevice.Dispose();SwapChain.Dispose();}protected virtual void Draw() { }protected virtual void LoadContent() { }protected virtual void UnloadContent() { }}class MyDirectXHelper{public static void CreateDeviceAndSwapChain(System.Windows.Forms.Form form,out SlimDX.Direct3D11.Device device,out SlimDX.DXGI.SwapChain swapChain){SlimDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware,DeviceCreationFlags.None,new SwapChainDescription{BufferCount = 1,OutputHandle = form.Handle,IsWindowed = true,SampleDescription = new SampleDescription{Count = 1,Quality = 0},ModeDescription = new ModeDescription{Width = form.ClientSize.Width,Height = form.ClientSize.Height,RefreshRate = new SlimDX.Rational(60, 1),Format = Format.R8G8B8A8_UNorm},Usage = Usage.RenderTargetOutput},out device,out swapChain);}public static Buffer CreateVertexBuffer(SlimDX.Direct3D11.Device graphicsDevice,System.Array vertices){using (SlimDX.DataStream vertexStream= new SlimDX.DataStream(vertices, true, true)){return new Buffer(graphicsDevice,vertexStream,new BufferDescription{SizeInBytes= (int)vertexStream.Length,BindFlags = BindFlags.VertexBuffer,});}}}
struct VertexPosition{float4 Position : SV_Position;};VertexPosition MyVertexShader(VertexPosition position){return position;}VertexPosition Average(VertexPosition a, VertexPosition b){VertexPosition result = { (a.Position + b.Position) / 2};return result;}[maxvertexcount(6)]void MyGeometryShader(triangle VertexPosition vertices[3],inout TriangleStream<VertexPosition> resultStream){resultStream.Append(vertices[0]);resultStream.Append(vertices[1]);resultStream.Append(Average(vertices[1], vertices[2]));resultStream.RestartStrip();resultStream.Append(vertices[0]);resultStream.Append(Average(vertices[1], vertices[2]));resultStream.Append(vertices[2]);resultStream.RestartStrip();}float4 MyPixelShader() : SV_Target{return float4(1, 1, 1, 1);}technique10 MyTechnique{pass MyPass{SetVertexShader( CompileShader( vs_5_0, MyVertexShader() ) );SetGeometryShader(CompileShader(gs_5_0, MyGeometryShader()));SetPixelShader( CompileShader( ps_5_0, MyPixelShader() ) );}}
public RasterizerState State { get; set; }
public class RasterizerState : DeviceChild { public static RasterizerState FromDescription( Device device, RasterizerStateDescription description ); ... }
public struct RasterizerStateDescription { public CullMode CullMode { get; set; } public FillMode FillMode { get; set; } ... }
using SlimDX.Direct3D11;using SlimDX.DXGI;using SlimDX.D3DCompiler;class Program{static void Main(){using (Game game = new MyGame()){game.Run();}}}class MyGame : Game{Effect effect;InputLayout vertexLayout;Buffer vertexBuffer;protected override void Draw(){GraphicsDevice.ImmediateContext.ClearRenderTargetView(RenderTarget,new SlimDX.Color4(1, 0, 0, 1));initTriangleInputAssembler();drawTriangle();SwapChain.Present(0, PresentFlags.None);}private void drawTriangle(){effect.GetTechniqueByIndex(0).GetPassByIndex(0).Apply(GraphicsDevice.ImmediateContext);GraphicsDevice.ImmediateContext.Draw(3, 0);}private void initTriangleInputAssembler(){GraphicsDevice.ImmediateContext.InputAssembler.InputLayout = vertexLayout;GraphicsDevice.ImmediateContext.InputAssembler.SetVertexBuffers(0,new VertexBufferBinding(vertexBuffer, sizeof(float) * 3, 0));GraphicsDevice.ImmediateContext.InputAssembler.PrimitiveTopology= PrimitiveTopology.TriangleList;}protected override void LoadContent(){initEffect();initVertexLayout();initVertexBuffer();initWireframeRasterizerState();}private void initEffect(){using (ShaderBytecode shaderBytecode = ShaderBytecode.CompileFromFile("myEffect.fx", "fx_5_0",ShaderFlags.None,EffectFlags.None)){effect = new Effect(GraphicsDevice, shaderBytecode);}}private void initVertexLayout(){vertexLayout = new InputLayout(GraphicsDevice,effect.GetTechniqueByIndex(0).GetPassByIndex(0).Description.Signature,new[] {new InputElement{SemanticName = "SV_Position",Format = Format.R32G32B32_Float}});}private void initVertexBuffer(){vertexBuffer = MyDirectXHelper.CreateVertexBuffer(GraphicsDevice,new[] {new SlimDX.Vector3(0, 0.5f, 0),new SlimDX.Vector3(0.5f, 0, 0),new SlimDX.Vector3(-0.5f, 0, 0),});}private void initWireframeRasterizerState(){var desc = new RasterizerStateDescription{CullMode = CullMode.Back,FillMode = FillMode.Wireframe,};GraphicsDevice.ImmediateContext.Rasterizer.State= RasterizerState.FromDescription(GraphicsDevice,desc);}protected override void UnloadContent(){GraphicsDevice.ImmediateContext.Rasterizer.State.Dispose();effect.Dispose();vertexLayout.Dispose();vertexBuffer.Dispose();}}class Game : System.Windows.Forms.Form{public SlimDX.Direct3D11.Device GraphicsDevice;public SwapChain SwapChain;public RenderTargetView RenderTarget;public void Run(){initDevice();SlimDX.Windows.MessagePump.Run(this, Draw);disposeDevice();}private void initDevice(){MyDirectXHelper.CreateDeviceAndSwapChain(this, out GraphicsDevice, out SwapChain);initRenderTarget();initViewport();LoadContent();}private void initRenderTarget(){using (Texture2D backBuffer= SlimDX.Direct3D11.Resource.FromSwapChain<Texture2D>(SwapChain, 0)){RenderTarget = new RenderTargetView(GraphicsDevice, backBuffer);GraphicsDevice.ImmediateContext.OutputMerger.SetTargets(RenderTarget);}}private void initViewport(){GraphicsDevice.ImmediateContext.Rasterizer.SetViewports(new Viewport{Width = ClientSize.Width,Height = ClientSize.Height,});}private void disposeDevice(){UnloadContent();RenderTarget.Dispose();GraphicsDevice.Dispose();SwapChain.Dispose();}protected virtual void Draw() { }protected virtual void LoadContent() { }protected virtual void UnloadContent() { }}class MyDirectXHelper{public static void CreateDeviceAndSwapChain(System.Windows.Forms.Form form,out SlimDX.Direct3D11.Device device,out SlimDX.DXGI.SwapChain swapChain){SlimDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware,DeviceCreationFlags.None,new SwapChainDescription{BufferCount = 1,OutputHandle = form.Handle,IsWindowed = true,SampleDescription = new SampleDescription{Count = 1,Quality = 0},ModeDescription = new ModeDescription{Width = form.ClientSize.Width,Height = form.ClientSize.Height,RefreshRate = new SlimDX.Rational(60, 1),Format = Format.R8G8B8A8_UNorm},Usage = Usage.RenderTargetOutput},out device,out swapChain);}public static Buffer CreateVertexBuffer(SlimDX.Direct3D11.Device graphicsDevice,System.Array vertices){using (SlimDX.DataStream vertexStream= new SlimDX.DataStream(vertices, true, true)){return new Buffer(graphicsDevice,vertexStream,new BufferDescription{SizeInBytes= (int)vertexStream.Length,BindFlags = BindFlags.VertexBuffer,});}}}
float4 MyVertexShader(float4 position : SV_Position) : SV_Position{return position;}float4 MyPixelShader() : SV_Target{return float4(1, 1, 1, 1);}technique10 MyTechnique{pass MyPass{SetVertexShader( CompileShader( vs_5_0, MyVertexShader() ) );SetPixelShader( CompileShader( ps_5_0, MyPixelShader() ) );}}
public class ShaderResourceView : ResourceView
public static ShaderResourceView FromFile(Device device, string fileName);
public Result SetResource(ShaderResourceView view);
float4 Texture2D.Sample( SamplerState samplerState, float2 location );samplerStateはテクスチャから色を取ってくるときにのやり方です。
using SlimDX;using SlimDX.Direct3D11;using SlimDX.DXGI;using SlimDX.D3DCompiler;class Program{static void Main(){using (Game game = new MyGame()){game.Run();}}}class MyGame : Game{Effect effect;InputLayout vertexLayout;Buffer vertexBuffer;ShaderResourceView texture;protected override void Draw(){GraphicsDevice.ImmediateContext.ClearRenderTargetView(RenderTarget,new SlimDX.Color4(1, 0.39f, 0.58f, 0.93f));effect.GetVariableByName("diffuseTexture").AsResource().SetResource(texture);initTriangleInputAssembler();drawTriangle();SwapChain.Present(0, PresentFlags.None);}private void drawTriangle(){effect.GetTechniqueByIndex(0).GetPassByIndex(0).Apply(GraphicsDevice.ImmediateContext);GraphicsDevice.ImmediateContext.Draw(3, 0);}private void initTriangleInputAssembler(){GraphicsDevice.ImmediateContext.InputAssembler.InputLayout = vertexLayout;GraphicsDevice.ImmediateContext.InputAssembler.SetVertexBuffers(0,new VertexBufferBinding(vertexBuffer, VertexPositionTexture.SizeInBytes, 0));GraphicsDevice.ImmediateContext.InputAssembler.PrimitiveTopology= PrimitiveTopology.TriangleList;}protected override void LoadContent(){initEffect();initVertexLayout();initVertexBuffer();initTexture();}private void initEffect(){using (ShaderBytecode shaderBytecode = ShaderBytecode.CompileFromFile("myEffect.fx", "fx_5_0",ShaderFlags.None,EffectFlags.None)){effect = new Effect(GraphicsDevice, shaderBytecode);}}private void initVertexLayout(){vertexLayout = new InputLayout(GraphicsDevice,effect.GetTechniqueByIndex(0).GetPassByIndex(0).Description.Signature,VertexPositionTexture.VertexElements);}private void initVertexBuffer(){vertexBuffer = MyDirectXHelper.CreateVertexBuffer(GraphicsDevice,new[] {new VertexPositionTexture{Position = new Vector3(0, 0.5f, 0),TextureCoordinate = new Vector2(0.5f, 0)},new VertexPositionTexture{Position = new Vector3(0.5f, 0, 0),TextureCoordinate = new Vector2(1, 1)},new VertexPositionTexture{Position = new Vector3(-0.5f, 0, 0),TextureCoordinate = new Vector2(0, 1)},});}private void initTexture(){texture = ShaderResourceView.FromFile(GraphicsDevice, "Penguins.jpg");}protected override void UnloadContent(){effect.Dispose();vertexLayout.Dispose();vertexBuffer.Dispose();texture.Dispose();}}struct VertexPositionTexture{public Vector3 Position;public Vector2 TextureCoordinate;public static readonly InputElement[] VertexElements = new[]{new InputElement{SemanticName = "SV_Position",Format = Format.R32G32B32_Float},new InputElement{SemanticName = "TEXCOORD",Format = Format.R32G32_Float,AlignedByteOffset = InputElement.AppendAligned//自動的にオフセット決定}};public static int SizeInBytes{get{return System.Runtime.InteropServices.Marshal.SizeOf(typeof(VertexPositionTexture));}}}class Game : System.Windows.Forms.Form{public SlimDX.Direct3D11.Device GraphicsDevice;public SwapChain SwapChain;public RenderTargetView RenderTarget;public void Run(){initDevice();SlimDX.Windows.MessagePump.Run(this, Draw);disposeDevice();}private void initDevice(){MyDirectXHelper.CreateDeviceAndSwapChain(this, out GraphicsDevice, out SwapChain);initRenderTarget();initViewport();LoadContent();}private void initRenderTarget(){using (Texture2D backBuffer= SlimDX.Direct3D11.Resource.FromSwapChain<Texture2D>(SwapChain, 0)){RenderTarget = new RenderTargetView(GraphicsDevice, backBuffer);GraphicsDevice.ImmediateContext.OutputMerger.SetTargets(RenderTarget);}}private void initViewport(){GraphicsDevice.ImmediateContext.Rasterizer.SetViewports(new Viewport{Width = ClientSize.Width,Height = ClientSize.Height,});}private void disposeDevice(){UnloadContent();RenderTarget.Dispose();GraphicsDevice.Dispose();SwapChain.Dispose();}protected virtual void Draw() { }protected virtual void LoadContent() { }protected virtual void UnloadContent() { }}class MyDirectXHelper{public static void CreateDeviceAndSwapChain(System.Windows.Forms.Form form,out SlimDX.Direct3D11.Device device,out SlimDX.DXGI.SwapChain swapChain){SlimDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware,DeviceCreationFlags.None,new SwapChainDescription{BufferCount = 1,OutputHandle = form.Handle,IsWindowed = true,SampleDescription = new SampleDescription{Count = 1,Quality = 0},ModeDescription = new ModeDescription{Width = form.ClientSize.Width,Height = form.ClientSize.Height,RefreshRate = new SlimDX.Rational(60, 1),Format = Format.R8G8B8A8_UNorm},Usage = Usage.RenderTargetOutput},out device,out swapChain);}public static Buffer CreateVertexBuffer(SlimDX.Direct3D11.Device graphicsDevice,System.Array vertices){using (SlimDX.DataStream vertexStream= new SlimDX.DataStream(vertices, true, true)){return new Buffer(graphicsDevice,vertexStream,new BufferDescription{SizeInBytes= (int)vertexStream.Length,BindFlags = BindFlags.VertexBuffer,});}}}
Texture2D diffuseTexture;SamplerState mySampler{};struct VertexPositionTexture{float4 Position : SV_Position;float2 TextureCoordinate : TEXCOORD;};VertexPositionTexture MyVertexShader(VertexPositionTexture input){return input;}float4 MyPixelShader(VertexPositionTexture input) : SV_Target{return diffuseTexture.Sample(mySampler, input.TextureCoordinate);}technique10 MyTechnique{pass MyPass{SetVertexShader( CompileShader( vs_5_0, MyVertexShader() ) );SetPixelShader( CompileShader( ps_5_0, MyPixelShader() ) );}}