[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
cl_int clGetDeviceIDs( cl_platform_id platform, cl_device_type device_type, cl_uint num_entries, cl_device_id *devices, cl_uint *num_devices )
cl_device_type | 値 | 解説 |
CL_DEVICE_TYPE_CPU | (1 << 1) | CPUです。 |
CL_DEVICE_TYPE_GPU | (1 << 2) | GPUです。 |
CL_DEVICE_TYPE_ACCELERATOR | (1 << 3) | IBM CELL(PS3に使われている)などです。 |
CL_DEVICE_TYPE_DEFAULT | (1 << 0) | デフォルトのデバイスです。 |
CL_DEVICE_TYPE_ALL | 0xFFFFFFFF | 使用可能な全てのOpenCLデバイスです。 |
名前 | 値 | 解説 |
CL_SUCCESS | 0 | 関数が成功しました。 |
CL_INVALID_PLATFORM | -32 | platformが無効な値です。 |
CL_INVALID_DEVICE_TYPE | -31 | device_typeが無効な値です。 |
CL_INVALID_VALUE | -30 | num_entriesが0なのにdevicesがNULLではありません。あるいは、num_devicesとdevicesが両方共NULLです。 |
CL_DEVICE_NOT_FOUND | -1 | device_typeで指定されたタイプのデバイスが存在しません。 |
CL_OUT_OF_RESOURCES | -5 | デバイスでリソースを確保するのに失敗しました。 |
CL_OUT_OF_HOST_MEMORY | -6 | メモリが足りません。 |
using System;class Program{static void Main(){foreach (var platform in getPlatforms()){foreach (var device in getDevices(platform, DeviceType.Default)){Console.WriteLine(device);}}}private static IntPtr[] getDevices(IntPtr platform, DeviceType deviceType){int deviceCount;OpenCLFunctions.clGetDeviceIDs(platform, deviceType, 0, null, out deviceCount);IntPtr[] result = new IntPtr[deviceCount];OpenCLFunctions.clGetDeviceIDs(platform, deviceType, deviceCount, result, out deviceCount);return result;}private static IntPtr[] getPlatforms(){int platformCount;OpenCLFunctions.clGetPlatformIDs(0, null, out platformCount);IntPtr[] result = new IntPtr[platformCount];OpenCLFunctions.clGetPlatformIDs(platformCount, result, out platformCount);return result;}}
using System;using System.Runtime.InteropServices;static class OpenCLFunctions{[DllImport("OpenCL.dll")]public static extern int clGetPlatformIDs(int entryCount, IntPtr[] platforms, out int platformCount);[DllImport("OpenCL.dll")]public static extern int clGetDeviceIDs(IntPtr platform,DeviceType deviceType,int entryCount,IntPtr[] devices,out int deviceCount);}enum DeviceType:long{Default = (1 << 0),Cpu = (1 << 1),Gpu = (1 << 2),Accelerator = (1 << 3),All = 0xFFFFFFFF}
cl_int clGetDeviceInfo( cl_device_id device, cl_device_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret )
cl_device_info | 型 | 値 | |
CL_DEVICE_ADDRESS_BITS | cl_uint | 0x100D | |
CL_DEVICE_AVAILABLE | cl_bool | 0x1027 | |
CL_DEVICE_COMPILER_AVAILABLE | cl_bool | 0x1028 | |
CL_DEVICE_DOUBLE_FP_CONFIG | cl_device_fp_config | 0x1032 | |
CL_DEVICE_ENDIAN_LITTLE | cl_bool | 0x1026 | |
CL_DEVICE_ERROR_CORRECTION_SUPPORT | cl_bool | 0x1024 | |
CL_DEVICE_EXECUTION_CAPABILITIES | cl_device_exec_capabilities | 0x1029 | |
CL_DEVICE_EXTENSIONS | char[] | 0x1030 | |
CL_DEVICE_GLOBAL_MEM_CACHE_SIZE | cl_ulong | 0x101E | |
CL_DEVICE_GLOBAL_MEM_CACHE_TYPE | cl_device_mem_cache_type | 0x101C | |
CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE | cl_uint | 0x101D | |
CL_DEVICE_GLOBAL_MEM_SIZE | cl_ulong | 0x101F | |
CL_DEVICE_HALF_FP_CONFIG | cl_device_fp_config | 0x1033 | |
CL_DEVICE_HOST_UNIFIED_MEMORY | cl_bool | 0x1035 | |
CL_DEVICE_IMAGE_SUPPORT | cl_bool | 0x1016 | |
CL_DEVICE_IMAGE2D_MAX_HEIGHT | size_t | 0x1012 | |
CL_DEVICE_IMAGE2D_MAX_WIDTH | size_t | 0x1011 | |
CL_DEVICE_IMAGE3D_MAX_DEPTH | size_t | 0x1015 | |
CL_DEVICE_IMAGE3D_MAX_HEIGHT | size_t | 0x1014 | |
CL_DEVICE_IMAGE3D_MAX_WIDTH | size_t | 0x1013 | |
CL_DEVICE_LOCAL_MEM_SIZE | cl_ulong | 0x1023 | |
CL_DEVICE_LOCAL_MEM_TYPE | cl_device_local_mem_type | 0x1022 | |
CL_DEVICE_MAX_CLOCK_FREQUENCY | cl_uint | 0x100C | |
CL_DEVICE_MAX_COMPUTE_UNITS | cl_uint | 0x1002 | |
CL_DEVICE_MAX_CONSTANT_ARGS | cl_uint | 0x1021 | |
CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE | cl_ulong | 0x1020 | |
CL_DEVICE_MAX_MEM_ALLOC_SIZE | cl_ulong | 0x1010 | |
CL_DEVICE_MAX_PARAMETER_SIZE | size_t | 0x1017 | |
CL_DEVICE_MAX_READ_IMAGE_ARGS | cl_uint | 0x100E | |
CL_DEVICE_MAX_SAMPLERS | cl_uint | 0x1018 | |
CL_DEVICE_MAX_WORK_GROUP_SIZE | size_t | 0x1004 | |
CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS | cl_uint | 0x1003 | |
CL_DEVICE_MAX_WORK_ITEM_SIZES | size_t[] | 0x1005 | |
CL_DEVICE_MAX_WRITE_IMAGE_ARGS | cl_uint | 0x100F | |
CL_DEVICE_MEM_BASE_ADDR_ALIGN | cl_uint | 0x1019 | |
CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE | cl_uint | 0x101A | |
CL_DEVICE_NAME | char[] | 0x102B | |
CL_DEVICE_NATIVE_VECTOR_WIDTH_CHAR | cl_uint | 0x1036 | |
CL_DEVICE_NATIVE_VECTOR_WIDTH_SHORT | cl_uint | 0x1037 | |
CL_DEVICE_NATIVE_VECTOR_WIDTH_INT | cl_uint | 0x1038 | |
CL_DEVICE_NATIVE_VECTOR_WIDTH_LONG | cl_uint | 0x1039 | |
CL_DEVICE_NATIVE_VECTOR_WIDTH_FLOAT | cl_uint | 0x103A | |
CL_DEVICE_NATIVE_VECTOR_WIDTH_DOUBLE | cl_uint | 0x103B | |
CL_DEVICE_NATIVE_VECTOR_WIDTH_HALF | cl_uint | 0x103C | |
CL_DEVICE_OPENCL_C_VERSION | char[] | 0x103D | |
CL_DEVICE_PLATFORM | cl_platform_id | 0x1031 | |
CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR | cl_uint | 0x1006 | |
CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT | cl_uint | 0x1007 | |
CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT | cl_uint | 0x1008 | |
CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG | cl_uint | 0x1009 | |
CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT | cl_uint | 0x100A | |
CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE | cl_uint | 0x100B | |
CL_DEVICE_PREFERRED_VECTOR_WIDTH_HALF | cl_uint | 0x1034 | |
CL_DEVICE_PROFILE | char[] | 0x102E | |
CL_DEVICE_PROFILING_TIMER_RESOLUTION | size_t | 0x1025 | |
CL_DEVICE_QUEUE_PROPERTIES | cl_command_queue_properties | 0x102A | |
CL_DEVICE_SINGLE_FP_CONFIG | cl_device_fp_config | 0x101B | |
CL_DEVICE_TYPE | cl_device_type | 0x1000 | |
CL_DEVICE_VENDOR | char[] | 0x102C | |
CL_DEVICE_VENDOR_ID | cl_uint | 0x1001 | |
CL_DEVICE_VERSION | char[] | 0x102F | |
CL_DRIVER_VERSION | char[] | 0x102D |
名前 | 値 | 解説 |
CL_SUCCESS | 0 | 関数は成功しました |
CL_INVALID_DEVICE | -33 | deviceが無効な値です。 |
CL_INVALID_VALUE | -30 | param_nameが無効な値であるか、param_value_sizeが小さすぎ、param_valueがNULLではありません。 |
CL_OUT_OF_RESOURCES | -5 | デバイスでリソースを確保するのに失敗しました。 |
CL_OUT_OF_HOST_MEMORY | -6 | メモリが足りません。 |
using System;class Program{static void Main(){foreach (var platform in getPlatforms()){foreach (var device in getDevices(platform, DeviceType.Default)){Console.WriteLine(getDeviceInfo(device, DeviceInfoString.Name));}}}private static string getDeviceInfo(IntPtr device, DeviceInfoString deviceInfoName){int valueSize;OpenCLFunctions.clGetDeviceInfo(device, deviceInfoName, 0, null, out valueSize);System.Text.StringBuilder result = new System.Text.StringBuilder(valueSize);OpenCLFunctions.clGetDeviceInfo(device, deviceInfoName, valueSize, result, out valueSize);return result.ToString();}private static IntPtr[] getDevices(IntPtr platform, DeviceType deviceType){int deviceCount;OpenCLFunctions.clGetDeviceIDs(platform, deviceType, 0, null, out deviceCount);IntPtr[] result = new IntPtr[deviceCount];OpenCLFunctions.clGetDeviceIDs(platform, deviceType, deviceCount, result, out deviceCount);return result;}private static IntPtr[] getPlatforms(){int platformCount;OpenCLFunctions.clGetPlatformIDs(0, null, out platformCount);IntPtr[] result = new IntPtr[platformCount];OpenCLFunctions.clGetPlatformIDs(platformCount, result, out platformCount);return result;}}
using System;using System.Runtime.InteropServices;static class OpenCLFunctions{[DllImport("OpenCL.dll")]public static extern int clGetPlatformIDs(int entryCount, IntPtr[] platforms, out int platformCount);[DllImport("OpenCL.dll")]public static extern int clGetDeviceIDs(IntPtr platform,DeviceType deviceType,int entryCount,IntPtr[] devices,out int deviceCount);[DllImport("OpenCL.dll")]public static extern int clGetDeviceInfo(IntPtr device,DeviceInfoString paramName,int paramValueSize,System.Text.StringBuilder paramValue,out int paramValueSizeReturn);}enum DeviceType:long{Default = (1 << 0),Cpu = (1 << 1),Gpu = (1 << 2),Accelerator = (1 << 3),All = 0xFFFFFFFF}enum DeviceInfoString{Name = 0x102B,Vendor = 0x102C,DriverVersion = 0x102D,Profile = 0x102E,Version = 0x102F,Extensions = 0x1030}
cl_int clGetPlatformIDs( cl_uint num_entries, cl_platform_id *platforms, cl_uint *num_platforms );
名前 | 数値 | 説明 |
CL_SUCCESS | 0 | 関数が成功しました。 |
CL_INVALID_VALUE | -30 | 関数が失敗しました。num_entriesが0で、platformsがNULLではありません。あるいはnum_platformsとplatformsの両方がNULLです。 |
CL_OUT_OF_HOST_MEMORY | -6 | メモリが足りません。 |
using System;class Program{static void Main(){int platformCount = 0;OpenCLFunctions.clGetPlatformIDs(0, null, out platformCount);Console.WriteLine(platformCount);}}
using System;using System.Runtime.InteropServices;static class OpenCLFunctions{[DllImport("OpenCL.dll")]public static extern int clGetPlatformIDs(int entryCount, IntPtr[] platforms, out int platformCount);}
using System;class Program{static void Main(){foreach (var platform in getPlatforms()){Console.WriteLine(platform);}}private static IntPtr[] getPlatforms(){int platformCount;OpenCLFunctions.clGetPlatformIDs(0, null, out platformCount);IntPtr[] result = new IntPtr[platformCount];OpenCLFunctions.clGetPlatformIDs(platformCount, result, out platformCount);return result;}}
cl_int clGetPlatformInfo( cl_platform_id platform, cl_platform_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret )
cl_platform_info | 値 | 解説 |
CL_PLATFORM_PROFILE | 0x0900 | OpenCLのプロファイル文字列です。実装によってサポートされているプロファイル名を表します。この値は次のうちのどれか一つになります。 FULL_PROFILEはOpenCLの仕様を実装が満たしていることを意味します。 EMBEDDED_PROFILEは、実装がOpenCLのサブセットを満たしているという意味です。 |
CL_PLATFORM_VERSION | 0x0901 | OpenCLのバージョンです。 |
CL_PLATFORM_NAME | 0x0902 | プラットフォームの名前です。 |
CL_PLATFORM_VENDOR | 0x0903 | ベンダー名です。 |
CL_PLATFORM_EXTENSIONS | 0x0904 | 拡張名のリストを返します。それぞれの拡張はスペースで区切られています。 |
名前 | 数値 | 解説 |
CL_SUCCESS | 0 | 関数が成功しました。 |
CL_INVALID_PLATFORM | -32 | platformが有効な値ではありません。 |
CL_INVALID_VALUE | -30 | param_nameが有効な値でないか、あるいはparam_value_sizeが戻り値の型のサイズより小さくparam_valueがNULLではありません。 |
CL_OUT_OF_HOST_MEMORY | -6 | メモリが足りません。 |
using System;class Program{static void Main(){foreach (var platform in getPlatforms()){Console.WriteLine(getPlatformInfo(platform, PlatformInfo.Name));}}private static string getPlatformInfo(IntPtr platform, PlatformInfo platformInfo){int parameterValueSize;OpenCLFunctions.clGetPlatformInfo(platform,platformInfo,0,null,out parameterValueSize);System.Text.StringBuilder result = new System.Text.StringBuilder(parameterValueSize);OpenCLFunctions.clGetPlatformInfo(platform,platformInfo,parameterValueSize,result,out parameterValueSize);return result.ToString();}private static IntPtr[] getPlatforms(){int platformCount;OpenCLFunctions.clGetPlatformIDs(0, null, out platformCount);IntPtr[] result = new IntPtr[platformCount];OpenCLFunctions.clGetPlatformIDs(platformCount, result, out platformCount);return result;}}
using System;using System.Runtime.InteropServices;static class OpenCLFunctions{[DllImport("OpenCL.dll")]public static extern int clGetPlatformIDs(int entryCount, IntPtr[] platforms, out int platformCount);[DllImport("OpenCL.dll")]public static extern int clGetPlatformInfo(IntPtr platform,PlatformInfo parameterName,int parameterValueSize,System.Text.StringBuilder parameterValue,out int parameterValueSizeReturn);}enum PlatformInfo{Profile = 0x0900,Version = 0x0901,Name = 0x0902,Vendor = 0x0903,Extensions = 0x0904}
[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() ) );}}