News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Aligning .data

Started by Lonewolff, April 12, 2018, 08:16:54 PM

Previous topic - Next topic

Lonewolff

Hey guys,

How do you work out what the ideal alignment is in the .data section?

Mine is a garbled mess at the moment and is most likely far from efficient.

Example


szTitle DB "DirectX11 Game", 0
szClass DB "WindowClass", 0
windowClass WNDCLASSEX <?>
ddWidth DD 1280
ddHeight DD 720
msg MSG <?>
hWnd DD 0
szBuffer DB 10 dup (?)
ddResult DD 0
sd DXGI_SWAP_CHAIN_DESC <>

; Timer
TimeStart DQ 0
TimeEnd DQ 0
TimeElapsed DQ 0
TimeFrequency DQ 0
nCounterFPS DD 0
nCounter DD 0
TimeNanoSecond DQ 1000000000

d3dDevice LPD3D11Device 0
d3dSwapchain LPDXGISwapChain 0
d3dContext LPD3D11DeviceContext 0
d3dBackBufferTexture LPD3D11Texture2D 0
d3dBackBufferRTV LPD3D11RenderTargetView 0
d3dVertexShader LPD3D11VertexShader 0
d3dPixelShader LPD3D11PixelShader 0
d3dInputLayout LPD3D11InputLayout 0
d3dVertexBuffer LPD3D11Buffer 0

vertexShaderData db 68,88,66,67,snipped to save space....
SIZEOFvertexShaderData EQU $-vertexShaderData

pixelShaderData db 68,88,66,67,snipped to save space....
SIZEOFpixelShaderData EQU $-pixelShaderData

szSemanticName db "POSITION", 0
inputDescP D3D11_INPUT_ELEMENT_DESC <szSemanticName, 0 ,DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0>

vertexData real4 -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5
vertexDesc D3D11_BUFFER_DESC <48, D3D11_USAGE_DEFAULT, D3D11_BIND_VERTEX_BUFFER, 0, 0, 0>
resourceData D3D11_SUBRESOURCE_DATA  <vertexData, 0, 0>

dataStride dd 8
dataOffset dd 0

viewport D3D11_VIEWPORT <0.0, 0.0, 1280.0, 720.0, 0.0, 0.0>

BackgroundColor real4 0.129, 0.259, 0.388, 1.0
szError db "Fatal error!",0
szErrorRegisterClass db "Failed to register window class", 0
szErrorWindowCreate db "Failed to create window", 0


How do you know what alignment values to use and should you go largest data to smallest?

As you see, mine is quite randomly placed at present.

(Sorry about the code formatting. Looks fine in the preview window  ::))

Siekmanski

.data
align 16 ; for 128 bit (access 128 bit SIMD with movaps, and matrices etc. )

align 8    ; real8, qword

align 4  ; real4, dword

To save space, go from largest data to smallest
Creative coders use backward thinking techniques as a strategy.

Lonewolff

Cool, I'll do some re-aligning (no pun intended  :lol:) and see if that makes any difference to performance.

Thanks  8)

Lonewolff

Where would you place your data structure declarations? Does it depend on the largest member?

Siekmanski

For example if alignment is important,
This structure needs to be 16 byte aligned because I write to 4 timers at once with movaps.
I usually go from largest data to smallest if possible or rearrange members so they are properly aligned.

.const

MEDIATIMERS struct  ; don't change the order of the members!
    Timer1          real4 ?
    Timer2          real4 ?
    Timer3          real4 ?
    Timer4          real4 ?
    Timer5          real4 ?
    Timer6          real4 ?
    Timer7          real4 ?
    Timer8          real4 ?
    Timer9          real4 ?
    Timer10         real4 ?
    Timer11         real4 ?
    Timer12         real4 ?
    Timer13         real4 ?
    Timer14         real4 ?
    Timer15         real4 ?
    TotalTime       real4 ?
    TimeElapsed     real8 ?
    FramesPerSecond real8 ?
    FrameTimeDelta  real4 ?
MEDIATIMERS ends

.data?
align 16
Timers          MEDIATIMERS <?> ; must be 16 bytes aligned!
Creative coders use backward thinking techniques as a strategy.