无法绘制DeviceContext内容

时间:2013-09-19 08:02:23

标签: vb.net visual-studio-2012 visual-studio-express slimdx

我试图将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);
   }

1 个答案:

答案 0 :(得分:0)

我刚刚发现了我的问题。我尝试在初始化renderTarget之前在语句context.OutputMerger.SetTargets(renderTarget)中使用renderTarget变量。在renderTarget = New RenderTargetView(device,resource)之后移动语句解决了这个问题。

不知道我错过了什么!