Jump to content

  • Log In with Google      Sign In   
  • Create Account






Oh God, It gets even worse!

Posted by Washu, 07 June 2012 · 1,567 views

So if you saw my last entry, it was a basic Win32 app that created a D3D11 device and cleared the display.

Well, I've expanded upon it a wee bit more. If you're familiar with the SlimDX MiniTri11 sample this should look, erm... similar?

Added some Macros to help me twiddle around in COM easier.

.586
.model flat, stdcall
option casemap :none

includelib <d3d11.lib>
includelib <d3dcompiler.lib>

@ArgRev MACRO arglist:REQ
    LOCAL txt, arg
    txt TEXTEQU <>
%   FOR arg, <arglist>
        txt CATSTR <arg>, <,>, txt
    ENDM

    txt SUBSTR  txt, 1, @SizeStr( %txt ) - 1
    txt CATSTR  <!<>, txt, <!>>
    EXITM txt
ENDM

INVOKEC    MACRO comObject, methodNumber, args:VARARG
    LOCAL txt
    IFNB <args>
%       FOR arg, @ArgRev( <args> )
            push arg
        ENDM
    ENDIF
    mov  ecx, dword ptr [comObject]
    push ecx
    mov  eax, [ecx]
    mov  edx, dword ptr [eax + methodNumber];
    call edx
ENDM

RELEASE    MACRO comObject
     LOCAL skip
     cmp  comObject, 0
     jz  skip
     INVOKEC comObject, IUnknown_Release
skip:
ENDM

DEFINEGUID          MACRO name, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11
name    DD _1
        DW _2
        DW _3
        DB _4
        DB _5
        DB _6
        DB _7
        DB _8
        DB _9
        DB _10
        DB _11
ENDM

ExitProcess                     PROTO stdcall :DWORD
MessageBoxA                     PROTO stdcall :DWORD, :DWORD, :DWORD, :DWORD
RegisterClassExA                PROTO stdcall :DWORD
GetModuleHandleA                PROTO stdcall :DWORD
DefWindowProcA                  PROTO stdcall :DWORD, :DWORD, :DWORD, :DWORD
CreateWindowExA                 PROTO stdcall :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD
PostQuitMessage                 PROTO stdcall :DWORD
ShowWindow                      PROTO stdcall :DWORD, :DWORD
UpdateWindow                    PROTO stdcall :DWORD
PeekMessageA                    PROTO stdcall :DWORD, :DWORD, :DWORD, :DWORD, :DWORD
TranslateMessage                PROTO stdcall :DWORD
DispatchMessageA                PROTO stdcall :DWORD
D3D11CreateDeviceAndSwapChain   PROTO stdcall :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD
D3DCompile                      PROTO stdcall :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD, :DWORD
D3DGetInputSignatureBlob        PROTO stdcall :DWORD, :DWORD, :DWORD

POINT                   STRUC 4
    x                      DWORD ?
    y                      DWORD ?
POINT                   ENDS

MSG                     STRUC 4
    hwnd                   DWORD ?
    message                DWORD ?
    wparam                 WORD  ?
    lparam                 DWORD ?
    time                   DWORD ?
    point                  POINT <>
MSG                     ENDS

WNDCLASSEX              STRUC 4
    cbSize                 DWORD SIZEOF(WNDCLASSEX)
    style                  DWORD CW_VREDRAW OR CW_HREDRAW OR CW_OWNDC
    lpfnWndProc            DWORD WndProc
    cbClsExtra             DWORD 0
    cbWndExtra             DWORD 0
    hInstance              DWORD ?
    hIcon                  DWORD 0
    hCursor                DWORD 0
    hbrBackground          DWORD COLOR_BACKGROUND
    lpszMenuName           DWORD 0
    lpszClassName          DWORD className
    hIconSm                DWORD 0
WNDCLASSEX              ENDS

DXGI_RATIONAL  STRUC 4
    numerator              DWORD ?
    denominator            DWORD ?
DXGI_RATIONAL           ENDS

DXGI_MODE_DESC          STRUC 4
    w                      DWORD 800
    h                      DWORD 600
    refreshRate            DXGI_RATIONAL <1, 60>
    format                 DWORD DXGI_FORMAT_R8G8B8A8_UNORM
    scanLineOrder          DWORD 0
    scaling                DWORD 0
DXGI_MODE_DESC          ENDS

DXGI_SWAP_CHAIN_DESC    STRUC 4
    bufferDesc             DXGI_MODE_DESC <>
    sampleDesc             DXGI_RATIONAL <1, 0>
    usage                  DWORD 20h OR 40h
    bufferCount            DWORD 2
    window                 DWORD ?
    windowed               DWORD 1
    swapEffect             DWORD 0
    flags                  DWORD 0
DXGI_SWAP_CHAIN_DESC    ENDS

D3D11_BUFFER_DESC       STRUC 4
    byteWidth               DWORD ?
    usage                   DWORD ?
    bindFlags               DWORD ?
    cpuAccessFlags          DWORD ?
    miscFlags               DWORD ?
    structByteStride        DWORD ?
D3D11_BUFFER_DESC       ENDS

D3D11_SUBRESOURCE_DATA  STRUC 4
    srcData                 DWORD ?
    scrap1                  DWORD 0
    scrap2                  DWORD 0
D3D11_SUBRESOURCE_DATA  ENDS

D3D11_INPUT_ELEMENT_DESC STRUC 4
    semanticName            DWORD ?
    semanticIndex           DWORD ?
    format                  DWORD ?
    slot                    DWORD ?
    byteOffset              DWORD ?
    slotClass               DWORD ?
    dataStepRate            DWORD ?
D3D11_INPUT_ELEMENT_DESC ENDS

D3D11_VIEWPORT          STRUC 4
    topLeftX                REAL4 0.0
    topLeftY                REAL4 0.0
    windowWidth             REAL4 800.0
    windowHeight            REAL4 600.0
    minDepth                REAL4 0.0
    maxDepth                REAL4 1.0
D3D11_VIEWPORT          ENDS

MB_OK                       EQU 0
SW_SHOW                     EQU 5
CW_VREDRAW                  EQU 1
CW_HREDRAW                  EQU 2
CW_OWNDC                    EQU 20h
COLOR_BACKGROUND            EQU 1
WS_OVERLAPPED               EQU 00000000h
WS_MINIMIZEBOX              EQU 00020000h
WS_MAXIMIZEBOX              EQU 00020000h
WS_SYSMENU                  EQU 00080000h
WS_THICKFRAME               EQU 00040000h
WS_CAPTION                  EQU 00C00000h
WS_OVERLAPPEDWINDOW         EQU WS_OVERLAPPED OR WS_MINIMIZEBOX OR WS_MAXIMIZEBOX OR WS_SYSMENU OR WS_THICKFRAME OR WS_CAPTION
WM_QUIT                     EQU 0012h

D3D_DRIVER_TYPE_UNKNOWN             EQU 0
D3D_DRIVER_TYPE_HARDWARE            EQU ( D3D_DRIVER_TYPE_UNKNOWN + 1 )
D3D11_CREATE_DEVICE_SINGLETHREADED  EQU 1
D3D_FEATURE_LEVEL_9_1               EQU 09100h
D3D_FEATURE_LEVEL_9_2               EQU 09200h
D3D_FEATURE_LEVEL_9_3               EQU 09300h
D3D_FEATURE_LEVEL_10_0              EQU 0a000h
D3D_FEATURE_LEVEL_10_1              EQU 0a100h
D3D_FEATURE_LEVEL_11_0              EQU 0b000h
DXGI_FORMAT_R8G8B8A8_UNORM          EQU 28
DXGI_FORMAT_R32G32B32A32_FLOAT      EQU 2
D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST EQU 4

IUnknown_QueryInterface     EQU 0h
IUnknown_AddRef             EQU 4h
IUnknown_Release            EQU 8h

ID3D10Blob_GetBufferData    EQU IUnknown_Release + 4
ID3D10Blob_GetBufferSize    EQU IUnknown_Release + 8

IDXGISwapChain_GetBuffer    EQU 24h
IDXGISwapChain_Present      EQU 20h

ID3D11Device_CreateRTView   EQU 24h
ID3D11Device_CreateVS       EQU 30h
ID3D11Device_CreatePS       EQU 3Ch
ID3D11Device_CreateBuffer   EQU 0Ch
ID3D11Device_CreateLayout   EQU 2Ch

ID3D11Context_ClearRTView   EQU 0C8h
ID3D11Context_OMSetRT       EQU 84h
ID3D11Context_IASetLayout   EQU 44h
ID3D11Context_VSSetShader   EQU 2Ch
ID3D11Context_PSSetShader   EQU 24h
ID3D11Context_IASetPrimTop  EQU 60h
ID3D11Context_RSSetViewport EQU 0B0h
ID3D11Context_IASetVertBuff EQU 48h
ID3D11Context_Draw          EQU 34h

.data
msgTitle    DB "Oh shit son!", 0
regFail     DB "Failed to register window class.", 0
cwFail      DB "Failed to create window.", 0
showFail    DB "Failed to show window.", 0
updateFail  DB "Failed to update window.", 0
d3d11Fail1  DB "Failed to create D3D11 device.", 0
d3d11Fail2  DB "Failed to get back buffer from swap chain.", 0
d3d11Fail3  DB "Failed to create render target.", 0
d3d11Fail4  DB "Failed to compile pixel shader.", 0
d3d11Fail5  DB "Failed to compile vertex shader.", 0
d3d11Fail6  DB "Failed to get vertex shader layout.", 0
d3d11Fail7  DB "Failed to create vertex buffer.", 0
d3d11Fail8  DB "Failed to create input layout.", 0
d3d11Fail9  DB "Failed to create shader resource.", 0

messageIdx  DD regFail, cwFail, showFail, updateFail, d3d11Fail1, d3d11Fail2, d3d11Fail3, d3d11Fail4, d3d11Fail5, d3d11Fail6, d3d11Fail7, d3d11Fail8, d3d11Fail9

className   DB "TestClass", 0
windowTitle DB "My Window", 0
wndClass    WNDCLASSEX <>
swapDesc    DXGI_SWAP_CHAIN_DESC <>
featureLvl  DD D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3
swapChain   DD 0
device      DD 0
newFeatLvl  DD 0
context     DD 0

blackColor  DD 0, 0, 0, 0
backBuffer  DD 0
view        DD 0
DEFINEGUID  ID3D11Texture2D_GUID, 6f15aaf2h, 0d208h, 04e89h, 09ah, 0b4h, 048h, 095h, 035h, 0d3h, 04fh, 09Ch

viewport    D3D11_VIEWPORT <>

psSource    DB "struct VS_IN { float4 pos : POSITION; float4 col : COLOR; }; struct PS_IN { float4 pos : SV_POSITION; float4 col : COLOR; }; float4 PS( PS_IN input ) : SV_Target { return input.col; }", 0
psSourceLen DD $ - OFFSET psSource
psTarget    DB "ps_5_0", 0
psEntry     DB "PS", 0

vsSource    DB"struct VS_IN { float4 pos : POSITION; float4 col : COLOR; }; struct PS_IN { float4 pos : SV_POSITION; float4 col : COLOR; }; PS_IN VS( VS_IN input ) { PS_IN output = (PS_IN)0; output.pos = input.pos; output.col = input.col; return output; }", 0
vsSourceLen DD $ - OFFSET vsSource
vsTarget    DB "vs_5_0", 0
vsEntry     DB "VS", 0

vertData    REAL4 0.0, 0.5, 0.5, 1.0, 1.0, 0.0, 0.0, 1.0
            REAL4 0.5f, -0.5f, 0.5f, 1.0, 0.0, 1.0, 0.0, 1.0
            REAL4 -0.5f, -0.5f, 0.5f, 1.0, 0.0, 0.0, 1.0, 1.0

vertDesc    D3D11_BUFFER_DESC < 96, 0, 1, 0, 0, 0 >
vertSub     D3D11_SUBRESOURCE_DATA < vertData >
vertDescPos DB "POSITION", 0
vertDescCol DB "COLOR", 0
vertLayout  D3D11_INPUT_ELEMENT_DESC < OFFSET vertDescPos, 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, 0, 0 >
            D3D11_INPUT_ELEMENT_DESC < OFFSET vertDescCol, 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 16, 0, 0 >
vertStride  DD 32
vertOffset  DD 0

vertBuff    DD 0
inputLayout DD 0
psShader    DD 0
psBlob      DD 0
psBlobErr   DD 0
vsShader    DD 0
vsBlob      DD 0
vsBlobErr   DD 0
layoutBlob  DD 0
vsError     DD 0
blobPtr     DD 0
blobSize    DD 0

.code
WndProc  PROC stdcall hwnd:DWORD, msg:DWORD, wparam:WORD, lparam:DWORD
    cmp    msg, 2
    jnz    done
    INVOKE PostQuitMessage, 0
    xor    eax, eax
    ret
done:
    INVOKE DefWindowProcA, hwnd, msg, wparam, lparam
    ret;
WndProc  ENDP

main  PROC
    LOCAL  hwnd:DWORD
    LOCAL  hmodule:DWORD
    LOCAL  msg:MSG

    INVOKE GetModuleHandleA, 0
    mov    hmodule, eax
    mov    wndClass.hInstance, eax

    INVOKE RegisterClassExA, OFFSET wndClass
    cmp    eax, 0
    mov    eax, 0
    jz     error

    INVOKE CreateWindowExA, 0, OFFSET className, OFFSET windowTitle, WS_OVERLAPPEDWINDOW, 0, 0, 800, 600, 0, 0, hmodule, 0
    mov    hwnd, eax
    cmp    eax, 0
    mov    eax, 1
    jz     error

    INVOKE ShowWindow, hwnd, SW_SHOW
    INVOKE UpdateWindow, hwnd
    cmp    eax, 0
    mov    eax, 3
    jz     error

    mov    eax, hwnd
    mov    swapDesc.window, eax

    INVOKE D3D11CreateDeviceAndSwapChain, 0, D3D_DRIVER_TYPE_HARDWARE, 0, 0, OFFSET featureLvl, 4, 7, OFFSET swapDesc, OFFSET swapChain, OFFSET device, OFFSET newFeatLvl, OFFSET context
    cmp    eax, 0
    mov    eax, 4
    jnz    error

    INVOKEC swapChain, IDXGISwapChain_GetBuffer, 0, OFFSET ID3D11Texture2D_GUID, OFFSET backBuffer
    cmp    eax, 0
    mov    eax, 5
    jnz    error

    INVOKEC device, ID3D11Device_CreateRTView, dword ptr [backBuffer], 0, OFFSET view
    cmp    eax, 0
    mov    eax, 6
    jnz    error

    INVOKE D3DCompile, OFFSET psSource, psSourceLen, 0, 0, 0, OFFSET psEntry, OFFSET psTarget, 0, 0, OFFSET psBlob, OFFSET psBlobErr
    cmp    eax, 0
    mov    eax, 7
    jnz    error

    INVOKE D3DCompile, OFFSET vsSource, vsSourceLen, 0, 0, 0, OFFSET vsEntry, OFFSET vsTarget, 0, 0, OFFSET vsBlob, OFFSET vsBlobErr
    cmp    eax, 0
    mov    eax, 8
    jnz    error

    INVOKEC vsBlob, ID3D10Blob_GetBufferData
    mov    blobPtr, eax

    INVOKEC vsBlob, ID3D10Blob_GetBufferSize
    mov    blobSize, eax

    INVOKE D3DGetInputSignatureBlob, blobPtr, blobSize, OFFSET layoutBlob
    cmp    eax, 0
    mov    eax, 9
    jnz    error

    INVOKEC device, ID3D11Device_CreateBuffer, OFFSET vertDesc, OFFSET vertSub, OFFSET vertBuff
    cmp    eax, 0
    mov    eax, 10
    jnz    error

    INVOKEC layoutBlob, ID3D10Blob_GetBufferSize
    mov    blobSize, eax

    INVOKEC layoutBlob, ID3D10Blob_GetBufferData

    INVOKEC device, ID3D11Device_CreateLayout, OFFSET vertLayout, 2, eax, blobSize, OFFSET inputLayout
    cmp    eax, 0
    mov    eax, 11
    jnz    error

    INVOKEC vsBlob, ID3D10Blob_GetBufferData
    mov    blobPtr, eax

    INVOKEC vsBlob, ID3D10Blob_GetBufferSize

    INVOKEC device, ID3D11Device_CreateVS, blobPtr, eax, 0, OFFSET vsShader
    cmp    eax, 0
    mov    eax, 12
    jnz    error

    INVOKEC psBlob, ID3D10Blob_GetBufferData
    mov    blobPtr, eax

    INVOKEC psBlob, ID3D10Blob_GetBufferSize

    INVOKEC device, ID3D11Device_CreatePS, blobPtr, eax, 0, OFFSET psShader
    cmp    eax, 0
    mov    eax, 12
    jnz    error

    RELEASE psBlob
    RELEASE vsBlob
    RELEASE layoutBlob

    INVOKEC context, ID3D11Context_OMSetRT, 1, OFFSET view, 0
    INVOKEC context, ID3D11Context_IASetLayout, inputLayout
    INVOKEC context, ID3D11Context_VSSetShader, vsShader, 0, 0
    INVOKEC context, ID3D11Context_PSSetShader, psShader, 0, 0
    INVOKEC context, ID3D11Context_IASetPrimTop, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST
    INVOKEC context, ID3D11Context_RSSetViewport, 1, OFFSET viewport
    INVOKEC context, ID3D11Context_IASetVertBuff, 0, 1, OFFSET vertBuff, OFFSET vertStride, OFFSET vertOffset
gameLoop:
msgLoop:
    INVOKE PeekMessageA, ADDR msg, 0, 0, 0, 1
    cmp    eax, 0
    jz     gameNext
    cmp    msg.message, WM_QUIT
    jz     done
    INVOKE TranslateMessage, ADDR msg
    INVOKE DispatchMessageA, ADDR msg
    jmp    msgLoop
gameNext:
    INVOKEC context, ID3D11Context_ClearRTView, view, OFFSET blackColor
    INVOKEC context, ID3D11Context_Draw, 3, 0
    INVOKEC swapChain, IDXGISwapChain_Present, 0, 0
    jmp    gameLoop

done:
    RELEASE view
    RELEASE backBuffer
    RELEASE swapChain
    RELEASE device
    RELEASE context
    RELEASE vsShader
    RELEASE psShader
    RELEASE inputLayout
    RELEASE vertBuff
    INVOKE ExitProcess, eax
    ret
error:
    mov    eax, messageIdx[eax*4]
    INVOKE MessageBoxA, 0, eax, ADDR msgTitle, MB_OK
    jmp done
main  ENDP
END main





This time in the correct journal :D
And fixed GameDev.net's formatting.
Damn, you're indeed bored like the previous entry says.

Which I find ironic, given that I'm working on assembly right now (68000 running at 7.67MHz, fun... though nowhere near as bad as Z80 at 3.58MHz and having to parse an entire stream of sound data while also playing a 10650Hz sample at the same time).
Wow. I did a bit of 68k asm back in the day - this looks quite familiar!

PS: COM with ASM, you're a nihilist - although laughably it looks simpler than the C++ versions.

Recent Entries

Recent Comments

July 2014 »

S M T W T F S
  12345
6789101112
13141516171819
20212223242526
272829 30 31  
PARTNERS