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 "d3d10app.h" 28 #include "d3d10tri.hlsl.ps.h" 29 #include "d3d10tri.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 d3d10tri : public d3d10_application 53 { 54 ID3D10PixelShader* ps; 55 ID3D10VertexShader* vs; 56 ID3D10InputLayout* layout; 57 ID3D10Buffer* vb; 58 59 virtual bool init(ID3D10Device* dev, int argc, char** argv) 60 { 61 ensure(dev->CreatePixelShader(g_ps, sizeof(g_ps), &ps)); 62 ensure(dev->CreateVertexShader(g_vs, sizeof(g_vs), &vs)); 63 64 D3D10_INPUT_ELEMENT_DESC elements[] = 65 { 66 {"POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0}, 67 {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 16, D3D10_INPUT_PER_VERTEX_DATA, 0}, 68 }; 69 70 ensure(dev->CreateInputLayout(elements, sizeof(elements) / sizeof(elements[0]), g_vs, sizeof(g_vs), &layout)); 71 D3D10_BUFFER_DESC bufferd; 72 bufferd.ByteWidth = sizeof(vertices); 73 bufferd.Usage = D3D10_USAGE_IMMUTABLE; 74 bufferd.BindFlags = D3D10_BIND_VERTEX_BUFFER; 75 bufferd.CPUAccessFlags = 0; 76 bufferd.MiscFlags = 0; 77 78 D3D10_SUBRESOURCE_DATA buffersd; 79 buffersd.pSysMem = vertices; 80 buffersd.SysMemPitch = sizeof(vertices); 81 buffersd.SysMemSlicePitch = sizeof(vertices); 82 83 ensure(dev->CreateBuffer(&bufferd, &buffersd, &vb)); 84 85 return true; 86 } 87 88 virtual void draw(ID3D10Device* ctx, ID3D10RenderTargetView* rtv, unsigned width, unsigned height, double time) 89 { 90 float clear_color[4] = {1, 0, 1, 1}; 91 D3D10_VIEWPORT vp; 92 memset(&vp, 0, sizeof(vp)); 93 vp.Width = (unsigned)width; 94 vp.Height = (unsigned)height; 95 vp.MaxDepth = 1.0f; 96 97 ctx->OMSetRenderTargets(1, &rtv, 0); 98 ctx->RSSetViewports(1, &vp); 99 100 ctx->ClearRenderTargetView(rtv, clear_color); 101 102 ctx->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); 103 ctx->IASetInputLayout(layout); 104 unsigned stride = 2 * 4 * 4; 105 unsigned offset = 0; 106 ctx->IASetVertexBuffers(0, 1, &vb, &stride, &offset); 107 108 ctx->VSSetShader(vs); 109 ctx->PSSetShader(ps); 110 111 ctx->Draw(3, 0); 112 } 113 }; 114 115 d3d10_application* d3d10_application_create() 116 { 117 return new d3d10tri(); 118 } 119