#include "compat.h"
#include <d3d10.h>
-#include <d3d10shader.h>
+#include "tri_vs_4_0.h"
+#include "tri_ps_4_0.h"
+
+static IDXGIFactory *g_pFactory = NULL;
+static IDXGIAdapter *g_pAdapter = NULL;
static IDXGISwapChain* g_pSwapChain = NULL;
static ID3D10Device * g_pDevice = NULL;
-int main(int argc, char *argv[]){
+int
+main(int argc, char *argv[])
+{
HRESULT hr;
HINSTANCE hInstance = GetModuleHandle(NULL);
ShowWindow(hWnd, SW_SHOW);
+ UINT Flags = 0;
+ if (LoadLibraryA("d3d10sdklayers")) {
+ Flags |= D3D10_CREATE_DEVICE_DEBUG;
+ }
+
+ hr = CreateDXGIFactory(IID_IDXGIFactory, (void**)(&g_pFactory) );
+ if (FAILED(hr)) {
+ return 1;
+ }
+
+ hr = g_pFactory->EnumAdapters(0, &g_pAdapter);
+ if (FAILED(hr)) {
+ return 1;
+ }
+
+ hr = D3D10CreateDevice(g_pAdapter,
+ D3D10_DRIVER_TYPE_HARDWARE,
+ NULL,
+ Flags,
+ D3D10_SDK_VERSION,
+ &g_pDevice);
+ if (FAILED(hr)) {
+ return 1;
+ }
+
DXGI_SWAP_CHAIN_DESC SwapChainDesc;
ZeroMemory(&SwapChainDesc, sizeof SwapChainDesc);
- SwapChainDesc.BufferDesc.Width = WindowWidth;
- SwapChainDesc.BufferDesc.Height = WindowHeight;
SwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;;
SwapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
SwapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
SwapChainDesc.BufferCount = 2;
SwapChainDesc.OutputWindow = hWnd;
SwapChainDesc.Windowed = true;
+ SwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
- hr = D3D10CreateDeviceAndSwapChain(NULL,
- D3D10_DRIVER_TYPE_HARDWARE,
- NULL,
- D3D10_CREATE_DEVICE_DEBUG,
- D3D10_SDK_VERSION,
- &SwapChainDesc,
- &g_pSwapChain,
- &g_pDevice);
+ hr = g_pFactory->CreateSwapChain(g_pDevice, &SwapChainDesc, &g_pSwapChain);
if (FAILED(hr)) {
return 1;
}
if (FAILED(hr)) {
return 1;
}
- hr = g_pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);
+ D3D10_RENDER_TARGET_VIEW_DESC RenderTargetViewDesc;
+ ZeroMemory(&RenderTargetViewDesc, sizeof RenderTargetViewDesc);
+ RenderTargetViewDesc.Format = SwapChainDesc.BufferDesc.Format;
+ RenderTargetViewDesc.ViewDimension = D3D10_RTV_DIMENSION_TEXTURE2D;
+ RenderTargetViewDesc.Texture2D.MipSlice = 0;
+ hr = g_pDevice->CreateRenderTargetView(pBackBuffer, &RenderTargetViewDesc, &pRenderTargetView);
if (FAILED(hr)) {
return 1;
}
const float clearColor[4] = { 0.3f, 0.1f, 0.3f, 1.0f };
g_pDevice->ClearRenderTargetView(pRenderTargetView, clearColor);
- const char *szShader =
- "struct VS_OUTPUT {\n"
- " float4 Pos : SV_POSITION;\n"
- " float4 Color : COLOR0;\n"
- "};\n"
- "\n"
- "VS_OUTPUT VS(float4 Pos : POSITION, float4 Color : COLOR) {\n"
- " VS_OUTPUT Out;\n"
- " Out.Pos = Pos;\n"
- " Out.Color = Color;\n"
- " return Out;\n"
- "}\n"
- "\n"
- "float4 PS(VS_OUTPUT In) : SV_Target {\n"
- " return In.Color;\n"
- "}\n"
- ;
-
- ID3D10Blob *pVertexShaderBlob = NULL;
- ID3D10Blob *pErrorMsgs = NULL;
-
- hr = D3D10CompileShader(szShader, strlen(szShader), NULL, NULL, NULL, "VS", "vs_4_0", 0, &pVertexShaderBlob, &pErrorMsgs);
- if (pErrorMsgs) {
- fprintf(stderr, "%s\n", pErrorMsgs->GetBufferPointer());
- pErrorMsgs->Release();
- }
+ ID3D10VertexShader * pVertexShader;
+ hr = g_pDevice->CreateVertexShader(g_VS, sizeof g_VS, &pVertexShader);
if (FAILED(hr)) {
return 1;
}
- ID3D10VertexShader * pVertexShader;
- hr = g_pDevice->CreateVertexShader((DWORD*)pVertexShaderBlob->GetBufferPointer(), pVertexShaderBlob->GetBufferSize(), &pVertexShader);
-
struct Vertex {
float position[4];
float color[4];
};
- D3D10_INPUT_ELEMENT_DESC InputElementDescs[] = {
- { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, position), D3D10_INPUT_PER_VERTEX_DATA, 0 },
+ static const D3D10_INPUT_ELEMENT_DESC InputElementDescs[] = {
+ { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, position), D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, color), D3D10_INPUT_PER_VERTEX_DATA, 0 }
};
ID3D10InputLayout *pVertexLayout = NULL;
hr = g_pDevice->CreateInputLayout(InputElementDescs,
2,
- pVertexShaderBlob->GetBufferPointer(),
- pVertexShaderBlob->GetBufferSize(),
+ g_VS,
+ sizeof g_VS,
&pVertexLayout);
- pVertexShaderBlob->Release();
+ if (FAILED(hr)) {
+ return 1;
+ }
g_pDevice->IASetInputLayout(pVertexLayout);
-
- ID3D10Blob *pPixelShaderBlob = NULL;
- pErrorMsgs = NULL;
- hr = D3D10CompileShader(szShader, strlen(szShader), NULL, NULL, NULL, "PS", "ps_4_0", 0, &pPixelShaderBlob, &pErrorMsgs);
- if (pErrorMsgs) {
- fprintf(stderr, "%s\n", pErrorMsgs->GetBufferPointer());
- pErrorMsgs->Release();
- }
+ ID3D10PixelShader * pPixelShader;
+ hr = g_pDevice->CreatePixelShader(g_PS, sizeof g_PS, &pPixelShader);
if (FAILED(hr)) {
return 1;
}
- ID3D10PixelShader * pPixelShader;
- hr = g_pDevice->CreatePixelShader((DWORD*)pPixelShaderBlob->GetBufferPointer(), pPixelShaderBlob->GetBufferSize(), &pPixelShader);
- pPixelShaderBlob->Release();
-
-
g_pDevice->VSSetShader(pVertexShader);
g_pDevice->PSSetShader(pPixelShader);
ID3D10Buffer *pVertexBuffer;
hr = g_pDevice->CreateBuffer(&BufferDesc, NULL, &pVertexBuffer);
+ if (FAILED(hr)) {
+ return 1;
+ }
void *pMap = NULL;
pVertexBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pMap);
RasterizerDesc.CullMode = D3D10_CULL_NONE;
RasterizerDesc.FillMode = D3D10_FILL_SOLID;
RasterizerDesc.FrontCounterClockwise = true;
+ RasterizerDesc.DepthClipEnable = true;
ID3D10RasterizerState* pRasterizerState = NULL;
- g_pDevice->CreateRasterizerState(&RasterizerDesc, &pRasterizerState);
+ hr = g_pDevice->CreateRasterizerState(&RasterizerDesc, &pRasterizerState);
+ if (FAILED(hr)) {
+ return 1;
+ }
g_pDevice->RSSetState(pRasterizerState);
g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);