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