fork(3) download
  1. using SlimDX.Direct3D11;
  2. using SlimDX.DXGI;
  3. using SlimDX.D3DCompiler;
  4.  
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. using (Game game = new MyGame())
  10. {
  11. game.Run();
  12. }
  13. }
  14. }
  15.  
  16. class MyGame : Game
  17. {
  18. Effect effect;
  19. InputLayout vertexLayout;
  20. Buffer vertexBuffer;
  21.  
  22. protected override void Draw()
  23. {
  24. GraphicsDevice.ImmediateContext.ClearRenderTargetView(
  25. RenderTarget,
  26. new SlimDX.Color4(1, 0, 0, 1)
  27. );
  28.  
  29. initTriangleInputAssembler();
  30. drawTriangle();
  31.  
  32. SwapChain.Present(0, PresentFlags.None);
  33. }
  34.  
  35. private void drawTriangle()
  36. {
  37. effect.GetTechniqueByIndex(0).GetPassByIndex(0).Apply(GraphicsDevice.ImmediateContext);
  38. GraphicsDevice.ImmediateContext.Draw(3, 0);
  39. }
  40.  
  41. private void initTriangleInputAssembler()
  42. {
  43. GraphicsDevice.ImmediateContext.InputAssembler.InputLayout = vertexLayout;
  44. GraphicsDevice.ImmediateContext.InputAssembler.SetVertexBuffers(
  45. 0,
  46. new VertexBufferBinding(vertexBuffer, sizeof(float) * 3, 0)
  47. );
  48. GraphicsDevice.ImmediateContext.InputAssembler.PrimitiveTopology
  49. = PrimitiveTopology.TriangleList;
  50. }
  51.  
  52. protected override void LoadContent()
  53. {
  54. initEffect();
  55. initVertexLayout();
  56. initVertexBuffer();
  57. }
  58.  
  59. private void initEffect()
  60. {
  61. using (ShaderBytecode shaderBytecode = ShaderBytecode.CompileFromFile(
  62. "effect.fx", "fx_5_0",
  63. ShaderFlags.None,
  64. EffectFlags.None
  65. ))
  66. {
  67. effect = new Effect(GraphicsDevice, shaderBytecode);
  68. }
  69. }
  70.  
  71. private void initVertexLayout()
  72. {
  73. vertexLayout = new InputLayout(
  74. GraphicsDevice,
  75. effect.GetTechniqueByIndex(0).GetPassByIndex(0).Description.Signature,
  76. new[] {
  77. new InputElement
  78. {
  79. SemanticName = "SV_Position",
  80. Format = Format.R32G32B32_Float
  81. }
  82. }
  83. );
  84. }
  85.  
  86. private void initVertexBuffer()
  87. {
  88. vertexBuffer = MyDirectXHelper.CreateVertexBuffer(
  89. GraphicsDevice,
  90. new[] {
  91. new SlimDX.Vector3(0, 0.5f, 0),
  92. new SlimDX.Vector3(0.5f, 0, 0),
  93. new SlimDX.Vector3(-0.5f, 0, 0),
  94. });
  95. }
  96.  
  97. protected override void UnloadContent()
  98. {
  99. effect.Dispose();
  100. vertexLayout.Dispose();
  101. vertexBuffer.Dispose();
  102. }
  103. }
  104.  
  105. class Game : System.Windows.Forms.Form
  106. {
  107. public SlimDX.Direct3D11.Device GraphicsDevice;
  108. public SwapChain SwapChain;
  109. public RenderTargetView RenderTarget;
  110.  
  111.  
  112. public void Run()
  113. {
  114. initDevice();
  115. SlimDX.Windows.MessagePump.Run(this, Draw);
  116. disposeDevice();
  117. }
  118.  
  119. private void initDevice()
  120. {
  121. MyDirectXHelper.CreateDeviceAndSwapChain(
  122. this, out GraphicsDevice, out SwapChain
  123. );
  124.  
  125. initRenderTarget();
  126. initViewport();
  127.  
  128. LoadContent();
  129. }
  130.  
  131. private void initRenderTarget()
  132. {
  133. using (Texture2D backBuffer
  134. = SlimDX.Direct3D11.Resource.FromSwapChain<Texture2D>(SwapChain, 0)
  135. )
  136. {
  137. RenderTarget = new RenderTargetView(GraphicsDevice, backBuffer);
  138. GraphicsDevice.ImmediateContext.OutputMerger.SetTargets(RenderTarget);
  139. }
  140. }
  141.  
  142. private void initViewport()
  143. {
  144. GraphicsDevice.ImmediateContext.Rasterizer.SetViewports(
  145. new Viewport
  146. {
  147. Width = ClientSize.Width,
  148. Height = ClientSize.Height,
  149. }
  150. );
  151. }
  152.  
  153. private void disposeDevice()
  154. {
  155. UnloadContent();
  156. RenderTarget.Dispose();
  157. GraphicsDevice.Dispose();
  158. SwapChain.Dispose();
  159. }
  160.  
  161. protected virtual void Draw() { }
  162. protected virtual void LoadContent() { }
  163. protected virtual void UnloadContent() { }
  164. }
  165.  
  166. class MyDirectXHelper
  167. {
  168. public static void CreateDeviceAndSwapChain(
  169. System.Windows.Forms.Form form,
  170. out SlimDX.Direct3D11.Device device,
  171. out SlimDX.DXGI.SwapChain swapChain
  172. )
  173. {
  174. SlimDX.Direct3D11.Device.CreateWithSwapChain(
  175. DriverType.Hardware,
  176. DeviceCreationFlags.None,
  177. new SwapChainDescription
  178. {
  179. BufferCount = 1,
  180. OutputHandle = form.Handle,
  181. IsWindowed = true,
  182. SampleDescription = new SampleDescription
  183. {
  184. Count = 1,
  185. Quality = 0
  186. },
  187. ModeDescription = new ModeDescription
  188. {
  189. Width = form.ClientSize.Width,
  190. Height = form.ClientSize.Height,
  191. RefreshRate = new SlimDX.Rational(60, 1),
  192. Format = Format.R8G8B8A8_UNorm
  193. },
  194. Usage = Usage.RenderTargetOutput
  195. },
  196. out device,
  197. out swapChain
  198. );
  199. }
  200.  
  201. public static Buffer CreateVertexBuffer(
  202. SlimDX.Direct3D11.Device graphicsDevice,
  203. System.Array vertices
  204. )
  205. {
  206. using (SlimDX.DataStream vertexStream
  207. = new SlimDX.DataStream(vertices, true, true))
  208. {
  209. return new Buffer(
  210. graphicsDevice,
  211. vertexStream,
  212. new BufferDescription
  213. {
  214. SizeInBytes = (int)vertexStream.Length,
  215. BindFlags = BindFlags.VertexBuffer,
  216. }
  217. );
  218. }
  219. }
  220. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(105,21): error CS0234: The type or namespace name `Windows' does not exist in the namespace `System'. Are you missing an assembly reference?
Compilation failed: 1 error(s), 0 warnings
stdout
Standard output is empty