0
votes

I'm using vs2015 and studying dx11. I'll show you code first.

cbuffer cbperobject {
    float4x4 gWorldViewProj;
};

struct VertexIn {
    float3 Pos : POSITION;
    float4 Color : COLOR;
};

struct VertexOut {
    float4 PosH : SV_POSITION;
    float4 Color : COLOR;

};

VertexOut main( VertexIn vin ) 
{
    VertexOut vOut;

    vOut.PosH = mul(float4(vin.Pos, 1.0f), gWorldViewProj);
    vOut.Color = vin.Color;
    return vOut;
}

This is my vertex shader code. I rahter copied it from internet.

HRESULT result;
D3D11_MAPPED_SUBRESOURCE mappedResource;
XMMATRIX* dataPtr;
UINT bufferNumber;


// Transpose the matrices to prepare them for the shader.


// Lock the constant buffer so it can be written to.
result = mD3dDContext->Map(contantBuff, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if (FAILED(result))
{
    return false;
}

// Get a pointer to the data in the constant buffer.
dataPtr = (XMMATRIX*)mappedResource.pData;

// Copy the matrices into the constant buffer.
XMMATRIX world = XMLoadFloat4x4(&mWorld);         // 버텍스의 월드변환
XMMATRIX view = XMLoadFloat4x4(&mView);       // 카메라
XMMATRIX proj = XMLoadFloat4x4(&mProj);       // 직교투영
XMMATRIX worldViewProj = world*view*proj;
worldViewProj = XMMatrixTranspose(worldViewProj);
*dataPtr = worldViewProj;

// Unlock the constant buffer.
mD3dDContext->Unmap(contantBuff, 0);

// Set the position of the constant buffer in the vertex shader.
bufferNumber = 0;

// Finanly set the constant buffer in the vertex shader with the updated values.
mD3dDContext->VSSetConstantBuffers(bufferNumber, 1, &contantBuff);

return true;

This is my setting constant buffer in shader code.

First, what is difference between POSITION and SV_POSITION semantic? Would you recommend good HLSL tutorial book? I'm Korean and I'm living in Korea. There is no good book in here; I don't know why, all good book is out of print. What a bad country for studying programming.

Second, why should I transpose my camera matrix(worldviewproj matrix) before CPU gives data to GPU? It's Vertex * matrix = processed Vertex. Why should I transpose it?

1
You don't need a book to learn shaders. There are plenty of tutorials out there on the internet, just google it. Besides, I would rather lean cross platform GLSL instead of platform specific HLSL - Asesh
Thanks for the comment. What I really want is very detailed information about HLSL code like what's the difference between SV_POSITION and POSITION and if I designate register number how do I know how much register i have? like that. - Yunsung Jung
You can find those information on the internet. Anyways, if you want a book then I would recommend Frank Luna's Introduction to 3D game programming with DirectX 12: amazon.com/Introduction-Programming-DirectX-Computer-Science/dp/… It's the best book out there for learning DirectX 12 and HLSL - Asesh

1 Answers

1
votes

Well POSITION(Semantic) gives directive to GPU, that concrete values will be placed as points in coordinate space and SV_POSITION is giving directive for pixel shader. Actually it gives order to GPU about pixels location on screen mainly in range -1 to 1. Look at this https://msdn.microsoft.com/en-us/library/windows/desktop/bb509647(v=vs.85).aspx

Well seems you need Linear Algebra lessons mate. Matrix transposition is the key stone in 3d graphics. With Matrix transpositions(And same time transposed Matrix is inverse Matrix and Inverse Matrix is always Orthogonal) all Matrix Transformations are happening(Translation, Rotation, Scaling). First of all you need Linear Algebra stuff and about Rendering Api be it OpenGL or DirectX(never mind they are just API's) you can grab any book or online documentation you can look at amazon.com. Happy graphics coding pal ;).