1 /************************************************************************** 2 * 3 * Copyright 2010 Luca Barbieri 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial 15 * portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 * 25 **************************************************************************/ 26 27 #include "d3d11app.h" 28 #include "d3d11tri.hlsl.ps.h" 29 #include "d3d11tri.hlsl.vs.h" 30 31 struct vertex { 32 float position[4]; 33 float color[4]; 34 }; 35 36 static struct vertex vertices[3] = 37 { 38 { 39 { 0.0f, 0.9f, 0.5f, 1.0f }, 40 { 1.0f, 0.0f, 0.0f, 1.0f } 41 }, 42 { 43 { 0.9f, -0.9f, 0.5f, 1.0f }, 44 { 0.0f, 0.0f, 1.0f, 1.0f } 45 }, 46 { 47 { -0.9f, -0.9f, 0.5f, 1.0f }, 48 { 0.0f, 1.0f, 0.0f, 1.0f } 49 }, 50 }; 51 52 struct d3d11tri : public d3d11_application 53 { 54 ID3D11PixelShader* ps; 55 ID3D11VertexShader* vs; 56 ID3D11InputLayout* layout; 57 ID3D11Buffer* vb; 58 59 virtual bool init(ID3D11Device* dev, int argc, char** argv) 60 { 61 ensure(dev->CreatePixelShader(g_ps, sizeof(g_ps), NULL, &ps)); 62 ensure(dev->CreateVertexShader(g_vs, sizeof(g_vs), NULL, &vs)); 63 64 D3D11_INPUT_ELEMENT_DESC elements[] = 65 { 66 // inverse order to make sure the implementation can properly parse the vertex shader signature 67 {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0}, 68 {"POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}, 69 }; 70 71 ensure(dev->CreateInputLayout(elements, sizeof(elements) / sizeof(elements[0]), g_vs, sizeof(g_vs), &layout)); 72 D3D11_BUFFER_DESC bufferd; 73 bufferd.ByteWidth = sizeof(vertices); 74 bufferd.Usage = D3D11_USAGE_IMMUTABLE; 75 bufferd.BindFlags = D3D11_BIND_VERTEX_BUFFER; 76 bufferd.CPUAccessFlags = 0; 77 bufferd.MiscFlags = 0; 78 bufferd.StructureByteStride = 0; 79 80 D3D11_SUBRESOURCE_DATA buffersd; 81 buffersd.pSysMem = vertices; 82 buffersd.SysMemPitch = sizeof(vertices); 83 buffersd.SysMemSlicePitch = sizeof(vertices); 84 85 ensure(dev->CreateBuffer(&bufferd, &buffersd, &vb)); 86 87 return true; 88 } 89 90 virtual void draw(ID3D11DeviceContext* ctx, ID3D11RenderTargetView* rtv, unsigned width, unsigned height, double time) 91 { 92 float clear_color[4] = {1, 0, 1, 1}; 93 D3D11_VIEWPORT vp; 94 memset(&vp, 0, sizeof(vp)); 95 vp.Width = (float)width; 96 vp.Height = (float)height; 97 vp.MaxDepth = 1.0f; 98 99 ctx->OMSetRenderTargets(1, &rtv, 0); 100 ctx->RSSetViewports(1, &vp); 101 102 ctx->ClearRenderTargetView(rtv, clear_color); 103 104 ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); 105 ctx->IASetInputLayout(layout); 106 unsigned stride = 2 * 4 * 4; 107 unsigned offset = 0; 108 ctx->IASetVertexBuffers(0, 1, &vb, &stride, &offset); 109 110 ctx->VSSetShader(vs, NULL, 0); 111 ctx->PSSetShader(ps, NULL, 0); 112 113 ctx->Draw(3, 0); 114 } 115 }; 116 117 d3d11_application* d3d11_application_create() 118 { 119 return new d3d11tri(); 120 } 121