d3d10.1 sample.
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Wed, 9 May 2012 09:28:25 +0000 (10:28 +0100)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Wed, 9 May 2012 09:28:25 +0000 (10:28 +0100)
app_driver.py
apps/CMakeLists.txt
apps/d3d10_1/CMakeLists.txt [new file with mode: 0644]
apps/d3d10_1/tri.cpp [new file with mode: 0644]
apps/d3d10_1/tri.ref.txt [new file with mode: 0644]

index 9d3b48bc815fee18a8567295f947ae9383638fdf..9d7a9f01f7c996e27c582198e1f919be688ce24c 100755 (executable)
@@ -179,6 +179,7 @@ class AppDriver(Driver):
         'd3d8': 'd3d8',
         'd3d9': 'd3d9',
         'd3d10': 'd3d10',
+        'd3d10_1': 'd3d10_1',
         'd3d11': 'd3d11',
     }
 
index 6412f68fca0ee8d942d3b70fb4ea78ff4976a9f0..9b1e886ddff63f320fb94b7e7bcf07388d046581 100644 (file)
@@ -40,6 +40,10 @@ if (DirectX_D3D11_FOUND)
     add_subdirectory (d3d11)
 endif ()
 
+if (DirectX_D3D10_1_FOUND)
+    add_subdirectory (d3d10_1)
+endif ()
+
 if (DirectX_D3D10_FOUND)
     add_subdirectory (d3d10)
 endif ()
diff --git a/apps/d3d10_1/CMakeLists.txt b/apps/d3d10_1/CMakeLists.txt
new file mode 100644 (file)
index 0000000..b290117
--- /dev/null
@@ -0,0 +1,29 @@
+include_directories (
+    ${CMAKE_CURRENT_SOURCE_DIR}/../hlsl
+    ${APITRACE_SOURCE_DIR}/dispatch
+    ${DirectX_D3D10_1_INCLUDE_DIR}
+)
+
+link_libraries (
+    ${DirectX_D3D10_1_LIBRARY}
+)
+
+set (api d3d10_1)
+
+set (targets
+    tri
+)
+
+foreach (target ${targets})
+    add_executable (${api}_${target} ${target}.cpp)
+    set_target_properties (${api}_${target} PROPERTIES OUTPUT_NAME ${target})
+
+    if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${target}.ref.txt)
+        add_app_test(
+            NAME ${api}_${target}
+            TARGET ${api}_${target}
+            REF ${target}.ref.txt
+        )
+    endif ()
+endforeach (target)
+
diff --git a/apps/d3d10_1/tri.cpp b/apps/d3d10_1/tri.cpp
new file mode 100644 (file)
index 0000000..3efa49b
--- /dev/null
@@ -0,0 +1,256 @@
+/**************************************************************************
+ *
+ * Copyright 2012 Jose Fonseca
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#include <stdio.h>
+#include <stddef.h>
+
+#include <initguid.h>
+#include <windows.h>
+
+#include "compat.h"
+
+#include <d3d10_1.h>
+
+#include "tri_vs.h"
+#include "tri_ps.h"
+
+
+static IDXGISwapChain* g_pSwapChain = NULL;
+static ID3D10Device1 * g_pDevice = NULL;
+
+
+int main(int argc, char *argv[]){
+    HRESULT hr;
+
+    HINSTANCE hInstance = GetModuleHandle(NULL);
+
+    WNDCLASSEX wc = {
+        sizeof(WNDCLASSEX),
+        CS_CLASSDC,
+        DefWindowProc,
+        0,
+        0,
+        hInstance,
+        NULL,
+        NULL,
+        NULL,
+        NULL,
+        "SimpleDX10",
+        NULL
+    };
+    RegisterClassEx(&wc);
+
+    const int WindowWidth = 250;
+    const int WindowHeight = 250;
+    BOOL Windowed = TRUE;
+
+    DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
+
+    RECT rect = {0, 0, WindowWidth, WindowHeight};
+    AdjustWindowRect(&rect, dwStyle, FALSE);
+
+    HWND hWnd = CreateWindow(wc.lpszClassName,
+                             "Simple example using DirectX10",
+                             dwStyle,
+                             CW_USEDEFAULT, CW_USEDEFAULT,
+                             rect.right - rect.left,
+                             rect.bottom - rect.top,
+                             NULL,
+                             NULL,
+                             hInstance,
+                             NULL);
+    if (!hWnd) {
+        return 1;
+    }
+
+    ShowWindow(hWnd, SW_SHOW);
+
+    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.SampleDesc.Quality = 0;
+    SwapChainDesc.SampleDesc.Count = 1;
+    SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
+    SwapChainDesc.BufferCount = 2;
+    SwapChainDesc.OutputWindow = hWnd;
+    SwapChainDesc.Windowed = true;
+
+    hr = D3D10CreateDeviceAndSwapChain1(NULL,
+                                        D3D10_DRIVER_TYPE_HARDWARE,
+                                        NULL,
+                                        D3D10_CREATE_DEVICE_DEBUG,
+                                        D3D10_FEATURE_LEVEL_10_0,
+                                        D3D10_1_SDK_VERSION,
+                                        &SwapChainDesc,
+                                        &g_pSwapChain,
+                                        &g_pDevice);
+    if (FAILED(hr)) {
+        return 1;
+    }
+
+    ID3D10RenderTargetView *pRenderTargetView = NULL;
+    ID3D10Texture2D* pBackBuffer;
+    hr = g_pSwapChain->GetBuffer(0, IID_ID3D10Texture2D, (void **)&pBackBuffer);
+    if (FAILED(hr)) {
+        return 1;
+    }
+    hr = g_pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);
+    if (FAILED(hr)) {
+        return 1;
+    }
+    pBackBuffer->Release();
+
+    g_pDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL);
+
+    const float clearColor[4] = { 0.3f, 0.1f, 0.3f, 1.0f };
+    g_pDevice->ClearRenderTargetView(pRenderTargetView, clearColor);
+
+    ID3D10VertexShader * pVertexShader;
+    hr = g_pDevice->CreateVertexShader(g_VS, sizeof g_VS, &pVertexShader);
+    if (FAILED(hr)) {
+        return 1;
+    }
+
+    struct Vertex {
+        float position[4];
+        float color[4];
+    };
+
+    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,
+                                      g_VS,
+                                      sizeof g_VS,
+                                      &pVertexLayout);
+    if (FAILED(hr)) {
+        return 1;
+    }
+
+    g_pDevice->IASetInputLayout(pVertexLayout);
+
+    ID3D10PixelShader * pPixelShader;
+    hr = g_pDevice->CreatePixelShader(g_PS, sizeof g_PS, &pPixelShader);
+    if (FAILED(hr)) {
+        return 1;
+    }
+
+    g_pDevice->VSSetShader(pVertexShader);
+    g_pDevice->PSSetShader(pPixelShader);
+
+    static const Vertex vertices[] = {
+        { { -0.9f, -0.9f, 0.5f, 1.0f}, { 0.8f, 0.0f, 0.0f, 0.1f } },
+        { {  0.9f, -0.9f, 0.5f, 1.0f}, { 0.0f, 0.9f, 0.0f, 0.1f } },
+        { {  0.0f,  0.9f, 0.5f, 1.0f}, { 0.0f, 0.0f, 0.7f, 0.1f } },
+    };
+
+    D3D10_BUFFER_DESC BufferDesc;
+    ZeroMemory(&BufferDesc, sizeof BufferDesc);
+    BufferDesc.Usage = D3D10_USAGE_DYNAMIC;
+    BufferDesc.ByteWidth = sizeof vertices;
+    BufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
+    BufferDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
+    BufferDesc.MiscFlags = 0;
+
+    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);
+    memcpy(pMap, vertices, sizeof vertices);
+    pVertexBuffer->Unmap();
+
+    UINT Stride = sizeof(Vertex);
+    UINT Offset = 0;
+    g_pDevice->IASetVertexBuffers(0, 1, &pVertexBuffer, &Stride, &Offset);
+    
+    D3D10_VIEWPORT ViewPort;
+    ViewPort.TopLeftX = 0;
+    ViewPort.TopLeftY = 0;
+    ViewPort.Width = WindowWidth;
+    ViewPort.Height = WindowHeight;
+    ViewPort.MinDepth = 0.0f;
+    ViewPort.MaxDepth = 1.0f;
+    g_pDevice->RSSetViewports(1, &ViewPort);
+    
+    D3D10_RASTERIZER_DESC RasterizerDesc;
+    ZeroMemory(&RasterizerDesc, sizeof RasterizerDesc);
+    RasterizerDesc.CullMode = D3D10_CULL_NONE;
+    RasterizerDesc.FillMode = D3D10_FILL_SOLID;
+    RasterizerDesc.FrontCounterClockwise = true;
+    ID3D10RasterizerState* pRasterizerState = NULL;
+    g_pDevice->CreateRasterizerState(&RasterizerDesc, &pRasterizerState);
+    g_pDevice->RSSetState(pRasterizerState);
+
+    g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
+    g_pDevice->Draw(3, 0);
+
+    g_pSwapChain->Present(0, 0);
+
+
+    ID3D10Buffer *pNullBuffer = NULL;
+    UINT NullStride = 0;
+    UINT NullOffset = 0;
+    g_pDevice->IASetVertexBuffers(0, 1, &pNullBuffer, &NullStride, &NullOffset);
+    pVertexBuffer->Release();
+
+    g_pDevice->OMSetRenderTargets(0, NULL, NULL);
+    pRenderTargetView->Release();
+
+    g_pDevice->IASetInputLayout(NULL);
+    pVertexLayout->Release();
+
+    g_pDevice->VSSetShader(NULL);
+    pVertexShader->Release();
+
+    g_pDevice->PSSetShader(NULL);
+    pPixelShader->Release();
+
+    g_pDevice->RSSetState(NULL);
+    pRasterizerState->Release();
+
+    g_pSwapChain->Release();
+    g_pSwapChain = NULL;
+
+    g_pDevice->Release();
+    g_pDevice = NULL;
+
+    DestroyWindow(hWnd);
+
+    return 0;
+}
+
diff --git a/apps/d3d10_1/tri.ref.txt b/apps/d3d10_1/tri.ref.txt
new file mode 100644 (file)
index 0000000..e69de29