# GLSL-> DirectX9 HLSL Port

0

I've a simple DirectX9 HLSL pixel shader that I’ve ported from GLSL, I’ve read that I will need to change the right-handed coordinate system to Direct3D's default left-handed coordinate system. As a result my output is flipped and math's wrong.

Pixel shader commands have been translated correctly from GLSL to HLSL.

MSDN also provides a porting guide for Directx11 vertex buffers. Does anybody have an idea how the code below follows the MSDN rules for DFirectX9:

MSDN states: Flip the order of triangle vertices so that Direct3D traverses them clockwise from the front. For example, if your vertices are indexed as 0, 1, and 2 in your OpenGL pipeline, pass them to Direct3D as 0, 2, 1 instead. Use the view matrix to scale world space by -1.0f in the z direction, effectively reversing the z-axis coordinates. To do this, flip the sign of the values at positions M31, M32, and M33 in your view matrix (when porting it to the Matrix type). If M34 is not 0, flip its sign as well.

Question: Do I need to introduce an HLSL vertex shader? The following code gets the data into the pixel shader without the need for a vertex shader..

``````struct Vertex {  FLOAT x, y, z, rhw;
DWORD diffuse_color;
FLOAT u, v; };

#define MY_VERTEX_FORMAT (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
const int num_vertices = 4;

Vertex vertices[num_vertices] = {
0, 0, 0, 1, 0xFFFF0000, 0.0f, 0.0f,  // left top
w, 0, 0, 1, 0xFFFF0000, 1.0f, 0.0f,  // right top
w, h, 0, 1, 0xFFFF0000, 1.0f, 1.0f,  // right bottom
0, h, 0, 1, 0xFFFF0000, 0.0f, 1.0f   // left bottom };

.....

m_pD3DD9->BeginScene();
hr = m_pD3DD9->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
m_pD3DD9->EndScene();
``````

…..

``````sampler2D rgbaTexture : register(S0);
float4 main(float2 in_Point : TEXCOORD) : COLOR
{
…..
}
``````

Any guidance would be greatly appreciated!

UPDATE 1

Ok, so Ill need to specify a right handed view and projection matrix:

``````    struct Vertex
{
FLOAT x, y, z;
FLOAT u, v;
};
...

->BeginScene()

D3DXMATRIX matView
D3DXVECTOR3 eye(2, 3, 3);  //??
D3DXVECTOR3 Lat(0, 0, 0);  //??
D3DXVECTOR3 up(0, 1, 0);   //??
D3DXMatrixLookAtRH(&matView, &eye, &Lat, &up);
d3ddevice->SetTransform(D3DTS_VIEW, &matView);

D3DXMATRIX matProjection;
D3DXMatrixPerspectiveFovRH(&matProjection, D3DXToRadian(45.0f), float(SrcW / SrcH), 0.1f, 100.0f);
m_pD3DD9->SetTransform(D3DTS_PROJECTION, &matProjection);

``````

What numbers need to be added to these matrices for it to work, I'd like the whole texture through the shader as before but right hand coordinate system?

``````XMFLOAT4X4 matView;
XMFLOAT4X4 matProjection;
FXMVECTOR mEye = XMVectorSet(0.0f, 15.0f, -30.0f, 0.0f);
FXMVECTOR mAt = XMVectorSet(0.0f, 8.0f, 0.0f, 0.0f);
FXMVECTOR mUp = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

float fovInDegrees = 45.0f;
float aspectRatio = (float)m_SrcW / (float)m_SrcH;
float fovAngleY = fovInDegrees * XM_PI / 180.0f;

if (aspectRatio < 1.0f)
fovAngleY /= aspectRatio;

XMStoreFloat4x4(&matView, XMMatrixTranspose(XMMatrixLookAtRH(mEye, mAt, mUp)));
XMStoreFloat4x4(&matProjection, XMMatrixTranspose(XMMatrixPerspectiveFovRH(fovAngleY, aspectRatio, 0.01f, 125.0f)));

hr = m_pD3DD9->SetTransform(D3DTS_VIEW, reinterpret_cast<D3DXMATRIX*>(&matView));
hr = m_pD3DD9->SetTransform(D3DTS_PROJECTION, reinterpret_cast<D3DXMATRIX*>(&matProjection));
``````
glsl
directx
directx-11
hlsl
directx-9
asked on Stack Overflow Jan 11, 2019 by gameOverMan • edited Mar 22, 2019 by gameOverMan