我试图将 SimpleTriangle 示例转换为 VB,但效果好坏参半。虽然我可以创建一个窗口来绘制并将其清除为背景颜色,但我似乎无法真正绘制我的三角形。我一生都无法说出代码在哪里崩溃。当我运行它时,我得到了窗口(如预期的那样具有红色背景),但现在是三角形。任何人都可以解释一下吗?代码如下 - 从 Sub Main 调用 TEMP.RunMe 方法。
Imports SlimDX.Direct3D11
Imports SlimDX
Imports SlimDX.D3DCompiler
Imports SlimDX.DXGI
Imports SlimDX.Windows
Imports Device = SlimDX.Direct3D11.Device
Imports Resource = SlimDX.Direct3D11.Resource
Namespace Test
Class TEMP
Public form = New RenderForm("MyTest")
Public device As Device
Public swapChain As SwapChain
Public viewport As New Viewport
Public renderTarget As RenderTargetView
Public context As DeviceContext
Public Sub New()
Dim Vertices As DataStream
Dim vertexBuffer As Buffer
Dim vertexShader As VertexShader
Dim pixelShader As PixelShader
Dim inputSignature As ShaderSignature
Dim description As New SwapChainDescription()
Dim elements() As InputElement
Dim layout As InputLayout
With description
.BufferCount = 1
.Usage = Usage.RenderTargetOutput
.OutputHandle = form.handle
.IsWindowed = True
.ModeDescription = New ModeDescription(0, 0, New Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm)
.SampleDescription = New SampleDescription(1, 0)
.Flags = DXGI.SwapChainFlags.AllowModeSwitch
.SwapEffect = DXGI.SwapEffect.Discard
End With
Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, device, swapChain)
viewport = New Viewport(0.0F, 0.0F, form.clientsize.width, form.clientsize.height)
context = device.ImmediateContext
context.OutputMerger.SetTargets(renderTarget)
context.Rasterizer.SetViewports(viewport)
Using resource As Resource = Direct3D11.Resource.FromSwapChain(Of Texture2D)(swapChain, 0)
renderTarget = New RenderTargetView(device, resource)
End Using
Using bytecode = ShaderBytecode.CompileFromFile("triangle.fx", "VShader", "vs_4_0", ShaderFlags.None, EffectFlags.None)
inputSignature = ShaderSignature.GetInputSignature(bytecode)
vertexShader = New VertexShader(device, bytecode)
End Using
Using bytecode = ShaderBytecode.CompileFromFile("triangle.fx", "PShader", "ps_4_0", ShaderFlags.None, EffectFlags.None)
pixelShader = New PixelShader(device, bytecode)
End Using
Vertices = New DataStream(12 * 3, True, True)
With Vertices
.Write(New Vector3(0.0F, 0.5F, 0.5F))
.Write(New Vector3(0.5F, -0.5F, 0.5F))
.Write(New Vector3(-0.5F, -0.5F, 0.5F))
.Position = 0
End With
elements = {New InputElement("POSITION", 0, Format.R32G32B32_Float, 0)}
layout = New InputLayout(device, inputSignature, elements)
vertexBuffer = New Buffer(device, Vertices, 12 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0)
With context
.InputAssembler.InputLayout = layout
.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList
.InputAssembler.SetVertexBuffers(0, New VertexBufferBinding(vertexBuffer, 12, 0))
.VertexShader.Set(vertexShader)
.PixelShader.Set(pixelShader)
End With
vertexBuffer.Dispose()
layout.Dispose()
vertexShader.Dispose()
pixelShader.Dispose()
inputSignature.Dispose()
End Sub
Private Sub mainLp()
With context
.ClearRenderTargetView(renderTarget, New Color4(1.0F, 0.0F, 0.0F))
.Draw(3, 0)
End With
swapChain.Present(0, PresentFlags.None)
End Sub
Public Sub RunMe()
MessagePump.Run(form, New MainLoop(AddressOf mainLp))
End Sub
Public Overloads Sub Dispose()
renderTarget.Dispose()
swapChain.Dispose()
device.Dispose()
End Sub
End Class
End Namespace
Traingle.fx 的内容是:
float4 VShader(float4 position : POSITION) : SV_POSITION
{
return position;
}
float4 PShader(float4 position : SV_POSITION) : SV_Target
{
return float4(1.0f, 1.0f, 0.0f, 1.0f);
}