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 "d3d11u.h"
29 #include "d3d11tex.hlsl.ps.h"
30 #include "d3d11tex.hlsl.vs.h"
31 #include "../data/cornell_box_image.h"
32 #include "../data/tux_image.h"
33
34 struct d3d11tex : public d3d11_application
35 {
36 ID3D11PixelShader* ps;
37 ID3D11VertexShader* vs;
38 mesh* quad;
39 ID3D11ShaderResourceView* srv[2];
40 ID3D11SamplerState* samp[2];
41
initd3d11tex42 virtual bool init(ID3D11Device* dev, int argc, char** argv)
43 {
44 ensure(dev->CreatePixelShader(g_ps, sizeof(g_ps), NULL, &ps));
45 ensure(dev->CreateVertexShader(g_vs, sizeof(g_vs), NULL, &vs));
46
47 quad = create_tex_quad(dev, g_vs, sizeof(g_vs));
48
49 D3D11_TEXTURE2D_DESC texd;
50 memset(&texd, 0, sizeof(texd));
51 texd.BindFlags = D3D11_BIND_SHADER_RESOURCE;
52 texd.Usage = D3D11_USAGE_IMMUTABLE;
53 texd.SampleDesc.Count = 1;
54 texd.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
55 texd.Width = 32;
56 texd.Height = 32;
57 texd.ArraySize = 1;
58 texd.MipLevels = 1;
59
60 D3D11_SUBRESOURCE_DATA texsd;
61 texsd.SysMemPitch = 32 * 4;
62 texsd.SysMemSlicePitch = 32 * 32 * 4;
63
64 ID3D11Texture2D* tex;
65
66 texsd.pSysMem = g_cornell_box_image;
67 ensure(dev->CreateTexture2D(&texd, &texsd, &tex));
68 ensure(dev->CreateShaderResourceView(tex, 0, &srv[0]));
69 tex->Release();
70
71 texsd.pSysMem = g_tux_image;
72 ensure(dev->CreateTexture2D(&texd, &texsd, &tex));
73 ensure(dev->CreateShaderResourceView(tex, 0, &srv[1]));
74 tex->Release();
75
76 D3D11_SAMPLER_DESC sampd;
77 memset(&sampd, 0, sizeof(sampd));
78 sampd.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
79 sampd.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
80 sampd.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
81 sampd.MinLOD = -FLT_MAX;
82 sampd.MaxLOD = FLT_MAX;
83
84 sampd.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
85 dev->CreateSamplerState(&sampd, &samp[0]);
86
87 sampd.Filter = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
88 dev->CreateSamplerState(&sampd, &samp[1]);
89 return true;
90 }
91
drawd3d11tex92 virtual void draw(ID3D11DeviceContext* ctx, ID3D11RenderTargetView* rtv, unsigned width, unsigned height, double time)
93 {
94 D3D11_VIEWPORT vp;
95 memset(&vp, 0, sizeof(vp));
96 vp.Width = (float)width;
97 vp.Height = (float)height;
98 vp.MaxDepth = 1.0f;
99
100 ctx->OMSetRenderTargets(1, &rtv, 0);
101 ctx->RSSetViewports(1, &vp);
102
103 ctx->VSSetShader(vs, NULL, 0);
104 ctx->PSSetShader(ps, NULL, 0);
105
106 ctx->PSSetShaderResources(0, 2, srv);
107 ctx->PSSetSamplers(0, 2, samp);
108
109 quad->bind_and_draw(ctx);
110 }
111 };
112
d3d11_application_create()113 d3d11_application* d3d11_application_create()
114 {
115 return new d3d11tex();
116 }
117