Created
August 5, 2011 09:21
-
-
Save zao/1127183 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace rendering | |
{ | |
namespace detail | |
{ | |
template <typename T> | |
struct format_of; | |
template <> | |
struct format_of<D3DXVECTOR2> | |
{ | |
static DXGI_FORMAT const value = DXGI_FORMAT_R32G32_FLOAT; | |
}; | |
template <> | |
struct format_of<D3DXVECTOR3> | |
{ | |
static DXGI_FORMAT const value = DXGI_FORMAT_R32G32B32_FLOAT; | |
}; | |
template <> | |
struct format_of<D3DXVECTOR4> | |
{ | |
static DXGI_FORMAT const value = DXGI_FORMAT_R32G32B32A32_FLOAT; | |
}; | |
template <> | |
struct format_of<uint16_t> | |
{ | |
static DXGI_FORMAT const value = DXGI_FORMAT_R16_UINT; | |
}; | |
template <> | |
struct format_of<uint32_t> | |
{ | |
static DXGI_FORMAT const value = DXGI_FORMAT_R32_UINT; | |
}; | |
} | |
enum frequency { PerVertex, PerInstance }; | |
template <typename UnderlyingType> | |
D3D11_INPUT_ELEMENT_DESC make_input_element_desc(int slot, char const* semantic, int semantic_index = 0, frequency freq = PerVertex, int freq_rate = 1) | |
{ | |
D3D11_INPUT_ELEMENT_DESC ret; | |
WITH_DESC(ret) | |
{ | |
_.SemanticName = semantic; | |
_.SemanticIndex = semantic_index; | |
_.Format = detail::format_of<UnderlyingType>::value; | |
_.InputSlot = slot; | |
_.AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; | |
_.InputSlotClass = (freq == PerVertex) ? D3D11_INPUT_PER_VERTEX_DATA : D3D11_INPUT_PER_INSTANCE_DATA; | |
_.InstanceDataStepRate = (freq == PerVertex) ? 0 : freq_rate; | |
} | |
return ret; | |
} | |
struct compiled_shaders | |
{ | |
struct source | |
{ | |
CComPtr<ID3D10Blob> buffer, errors; | |
}; | |
source vs_source, ps_source; | |
CComPtr<ID3D11VertexShader> vs; | |
CComPtr<ID3D11PixelShader> ps; | |
}; | |
bool compile_shaders(ID3D11Device* device, fs::path vs_file, fs::path ps_file, compiled_shaders& out) | |
{ | |
auto hr = noisy_hresult(); | |
auto compile_shader = [&](wchar_t const* filename, char const* entrypoint, char const* shader_model) -> compiled_shaders::source | |
{ | |
compiled_shaders::source s; | |
hr = D3DX11CompileFromFileW(filename, NULL, NULL, entrypoint, shader_model, | |
0, 0, NULL, &s.buffer, &s.errors, NULL); | |
return s; | |
}; | |
out.vs_source = compile_shader(vs_file.wstring().c_str(), "terrain_vertex_shader", "vs_5_0"); | |
out.ps_source = compile_shader(ps_file.wstring().c_str(), "terrain_pixel_shader", "ps_5_0"); | |
hr = device->CreateVertexShader(out.vs_source.buffer->GetBufferPointer(), out.vs_source.buffer->GetBufferSize(), NULL, &out.vs); | |
hr = device->CreatePixelShader(out.ps_source.buffer->GetBufferPointer(), out.ps_source.buffer->GetBufferSize(), NULL, &out.ps); | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rendering::shader_pack ret; | |
auto hr = HRESULT();//noisy_hresult(); | |
{ | |
rendering::compiled_shaders cs; | |
compile_shaders(device, L"assets\\terrain.vs", L"assets\\terrain.ps", cs); | |
D3D11_INPUT_ELEMENT_DESC input_desc[2] = { | |
rendering::make_input_element_desc<D3DXVECTOR3>(0, "POSITION"), | |
rendering::make_input_element_desc<D3DXVECTOR3>(0, "NORMAL"), | |
}; | |
ret.vs = cs.vs; | |
ret.ps = cs.ps; | |
auto& buf = cs.vs_source.buffer; | |
hr = device->CreateInputLayout(input_desc, 2, buf->GetBufferPointer(), buf->GetBufferSize(), &ret.layout); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment