[86815] | 1 | /* $Id: d3d11render.cpp 103983 2024-03-21 12:06:03Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * Gallium D3D testcase. Simple D3D11 tests.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2017-2023 Oracle and/or its affiliates.
|
---|
[86815] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[86815] | 26 | */
|
---|
| 27 |
|
---|
| 28 | #include "d3d11render.h"
|
---|
| 29 |
|
---|
[99816] | 30 |
|
---|
[103983] | 31 | /*
|
---|
| 32 | * Utilities
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[99816] | 35 | #if 0
|
---|
| 36 | //
|
---|
| 37 | // Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
|
---|
| 38 | //
|
---|
| 39 | //
|
---|
| 40 | // fxc /Fhd3d11color.hlsl.vs.h /Evs_color /Tvs_4_0 d3d11color.hlsl
|
---|
| 41 | //
|
---|
| 42 | //
|
---|
| 43 | //
|
---|
| 44 | // Input signature:
|
---|
| 45 | //
|
---|
| 46 | // Name Index Mask Register SysValue Format Used
|
---|
| 47 | // -------------------- ----- ------ -------- -------- ------ ------
|
---|
| 48 | // POSITION 0 xyz 0 NONE float xyz
|
---|
| 49 | // COLOR 0 xyzw 1 NONE float xyzw
|
---|
| 50 | //
|
---|
| 51 | //
|
---|
| 52 | // Output signature:
|
---|
| 53 | //
|
---|
| 54 | // Name Index Mask Register SysValue Format Used
|
---|
| 55 | // -------------------- ----- ------ -------- -------- ------ ------
|
---|
| 56 | // SV_POSITION 0 xyzw 0 POS float xyzw
|
---|
| 57 | // COLOR 0 xyzw 1 NONE float xyzw
|
---|
| 58 | //
|
---|
| 59 | vs_4_0
|
---|
| 60 | dcl_input v0.xyz
|
---|
| 61 | dcl_input v1.xyzw
|
---|
| 62 | dcl_output_siv o0.xyzw, position
|
---|
| 63 | dcl_output o1.xyzw
|
---|
| 64 | mov o0.xyz, v0.xyzx
|
---|
| 65 | mov o0.w, l(1.000000)
|
---|
| 66 | mov o1.xyzw, v1.xyzw
|
---|
| 67 | ret
|
---|
| 68 | // Approximately 4 instruction slots used
|
---|
| 69 | #endif
|
---|
| 70 |
|
---|
| 71 | static BYTE const g_vs_color[] =
|
---|
| 72 | {
|
---|
| 73 | 68, 88, 66, 67, 109, 138, 105, 83, 86, 190, 83, 125, 72, 102, 194, 136, 46, 69,
|
---|
| 74 | 17, 121, 1, 0, 0, 0, 48, 2, 0, 0, 5, 0, 0, 0, 52, 0, 0, 0,
|
---|
| 75 | 140, 0, 0, 0, 220, 0, 0, 0, 48, 1, 0, 0, 180, 1, 0, 0, 82, 68,
|
---|
| 76 | 69, 70, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 77 | 28, 0, 0, 0, 0, 4, 254, 255, 0, 1, 0, 0, 28, 0, 0, 0, 77, 105,
|
---|
| 78 | 99, 114, 111, 115, 111, 102, 116, 32, 40, 82, 41, 32, 72, 76, 83, 76, 32, 83,
|
---|
| 79 | 104, 97, 100, 101, 114, 32, 67, 111, 109, 112, 105, 108, 101, 114, 32, 57, 46, 50,
|
---|
| 80 | 57, 46, 57, 53, 50, 46, 51, 49, 49, 49, 0, 171, 171, 171, 73, 83, 71, 78,
|
---|
| 81 | 72, 0, 0, 0, 2, 0, 0, 0, 8, 0, 0, 0, 56, 0, 0, 0, 0, 0,
|
---|
| 82 | 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0,
|
---|
| 83 | 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0,
|
---|
| 84 | 0, 0, 15, 15, 0, 0, 80, 79, 83, 73, 84, 73, 79, 78, 0, 67, 79, 76,
|
---|
| 85 | 79, 82, 0, 171, 79, 83, 71, 78, 76, 0, 0, 0, 2, 0, 0, 0, 8, 0,
|
---|
| 86 | 0, 0, 56, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0,
|
---|
| 87 | 0, 0, 0, 0, 15, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 88 | 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 15, 0, 0, 0, 83, 86, 95, 80,
|
---|
| 89 | 79, 83, 73, 84, 73, 79, 78, 0, 67, 79, 76, 79, 82, 0, 171, 171, 83, 72,
|
---|
| 90 | 68, 82, 124, 0, 0, 0, 64, 0, 1, 0, 31, 0, 0, 0, 95, 0, 0, 3,
|
---|
| 91 | 114, 16, 16, 0, 0, 0, 0, 0, 95, 0, 0, 3, 242, 16, 16, 0, 1, 0,
|
---|
| 92 | 0, 0, 103, 0, 0, 4, 242, 32, 16, 0, 0, 0, 0, 0, 1, 0, 0, 0,
|
---|
| 93 | 101, 0, 0, 3, 242, 32, 16, 0, 1, 0, 0, 0, 54, 0, 0, 5, 114, 32,
|
---|
| 94 | 16, 0, 0, 0, 0, 0, 70, 18, 16, 0, 0, 0, 0, 0, 54, 0, 0, 5,
|
---|
| 95 | 130, 32, 16, 0, 0, 0, 0, 0, 1, 64, 0, 0, 0, 0, 128, 63, 54, 0,
|
---|
| 96 | 0, 5, 242, 32, 16, 0, 1, 0, 0, 0, 70, 30, 16, 0, 1, 0, 0, 0,
|
---|
| 97 | 62, 0, 0, 1, 83, 84, 65, 84, 116, 0, 0, 0, 4, 0, 0, 0, 0, 0,
|
---|
| 98 | 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 99 | 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 100 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 101 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0,
|
---|
| 102 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 103 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 104 | 0, 0
|
---|
| 105 | };
|
---|
| 106 |
|
---|
| 107 | #if 0
|
---|
| 108 | //
|
---|
| 109 | // Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
|
---|
| 110 | //
|
---|
| 111 | //
|
---|
| 112 | // fxc /Fhd3d11color.hlsl.ps.h /Eps_color /Tps_4_0 d3d11color.hlsl
|
---|
| 113 | //
|
---|
| 114 | //
|
---|
| 115 | //
|
---|
| 116 | // Input signature:
|
---|
| 117 | //
|
---|
| 118 | // Name Index Mask Register SysValue Format Used
|
---|
| 119 | // -------------------- ----- ------ -------- -------- ------ ------
|
---|
| 120 | // SV_POSITION 0 xyzw 0 POS float
|
---|
| 121 | // COLOR 0 xyzw 1 NONE float xyzw
|
---|
| 122 | //
|
---|
| 123 | //
|
---|
| 124 | // Output signature:
|
---|
| 125 | //
|
---|
| 126 | // Name Index Mask Register SysValue Format Used
|
---|
| 127 | // -------------------- ----- ------ -------- -------- ------ ------
|
---|
| 128 | // SV_TARGET 0 xyzw 0 TARGET float xyzw
|
---|
| 129 | //
|
---|
| 130 | ps_4_0
|
---|
| 131 | dcl_input_ps linear v1.xyzw
|
---|
| 132 | dcl_output o0.xyzw
|
---|
| 133 | mov o0.xyzw, v1.xyzw
|
---|
| 134 | ret
|
---|
| 135 | // Approximately 2 instruction slots used
|
---|
| 136 | #endif
|
---|
| 137 |
|
---|
| 138 | static BYTE const g_ps_color[] =
|
---|
| 139 | {
|
---|
| 140 | 68, 88, 66, 67, 206, 120, 117, 238, 118, 127, 10, 87, 80, 75, 114, 198, 95, 2,
|
---|
| 141 | 120, 102, 1, 0, 0, 0, 208, 1, 0, 0, 5, 0, 0, 0, 52, 0, 0, 0,
|
---|
| 142 | 140, 0, 0, 0, 224, 0, 0, 0, 20, 1, 0, 0, 84, 1, 0, 0, 82, 68,
|
---|
| 143 | 69, 70, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 144 | 28, 0, 0, 0, 0, 4, 255, 255, 0, 1, 0, 0, 28, 0, 0, 0, 77, 105,
|
---|
| 145 | 99, 114, 111, 115, 111, 102, 116, 32, 40, 82, 41, 32, 72, 76, 83, 76, 32, 83,
|
---|
| 146 | 104, 97, 100, 101, 114, 32, 67, 111, 109, 112, 105, 108, 101, 114, 32, 57, 46, 50,
|
---|
| 147 | 57, 46, 57, 53, 50, 46, 51, 49, 49, 49, 0, 171, 171, 171, 73, 83, 71, 78,
|
---|
| 148 | 76, 0, 0, 0, 2, 0, 0, 0, 8, 0, 0, 0, 56, 0, 0, 0, 0, 0,
|
---|
| 149 | 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0,
|
---|
| 150 | 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0,
|
---|
| 151 | 0, 0, 15, 15, 0, 0, 83, 86, 95, 80, 79, 83, 73, 84, 73, 79, 78, 0,
|
---|
| 152 | 67, 79, 76, 79, 82, 0, 171, 171, 79, 83, 71, 78, 44, 0, 0, 0, 1, 0,
|
---|
| 153 | 0, 0, 8, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 154 | 3, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 83, 86, 95, 84, 65, 82,
|
---|
| 155 | 71, 69, 84, 0, 171, 171, 83, 72, 68, 82, 56, 0, 0, 0, 64, 0, 0, 0,
|
---|
| 156 | 14, 0, 0, 0, 98, 16, 0, 3, 242, 16, 16, 0, 1, 0, 0, 0, 101, 0,
|
---|
| 157 | 0, 3, 242, 32, 16, 0, 0, 0, 0, 0, 54, 0, 0, 5, 242, 32, 16, 0,
|
---|
| 158 | 0, 0, 0, 0, 70, 30, 16, 0, 1, 0, 0, 0, 62, 0, 0, 1, 83, 84,
|
---|
| 159 | 65, 84, 116, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 160 | 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
|
---|
| 161 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 162 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 163 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 164 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 165 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
---|
| 166 | };
|
---|
| 167 |
|
---|
[86815] | 168 | /*
|
---|
| 169 | * Clear the backbuffer and display it.
|
---|
| 170 | */
|
---|
| 171 |
|
---|
| 172 | class D3D11RenderClear: public D3D11Render
|
---|
| 173 | {
|
---|
| 174 | public:
|
---|
| 175 | D3D11RenderClear() {}
|
---|
| 176 | virtual ~D3D11RenderClear() {}
|
---|
| 177 | virtual HRESULT InitRender(D3D11DeviceProvider *pDP);
|
---|
| 178 | virtual HRESULT DoRender(D3D11DeviceProvider *pDP);
|
---|
| 179 | };
|
---|
| 180 |
|
---|
| 181 | HRESULT D3D11RenderClear::InitRender(D3D11DeviceProvider *pDP)
|
---|
| 182 | {
|
---|
| 183 | (void)pDP;
|
---|
| 184 | return S_OK;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | HRESULT D3D11RenderClear::DoRender(D3D11DeviceProvider *pDP)
|
---|
| 188 | {
|
---|
| 189 | ID3D11DeviceContext *pImmediateContext = pDP->ImmediateContext();
|
---|
| 190 |
|
---|
| 191 | FLOAT aColor[4] = { 1.0f, 0.0f, 0.0f, 0.0f };
|
---|
| 192 | pImmediateContext->ClearRenderTargetView(pDP->RenderTargetView(), aColor);
|
---|
| 193 |
|
---|
| 194 | return S_OK;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /*
|
---|
| 198 | * Simplest colorful triangle using shaders.
|
---|
| 199 | */
|
---|
| 200 |
|
---|
| 201 | class D3D11RenderTriangleShader: public D3D11Render
|
---|
| 202 | {
|
---|
| 203 | public:
|
---|
| 204 | D3D11RenderTriangleShader();
|
---|
| 205 | virtual ~D3D11RenderTriangleShader();
|
---|
| 206 | virtual HRESULT InitRender(D3D11DeviceProvider *pDP);
|
---|
| 207 | virtual HRESULT DoRender(D3D11DeviceProvider *pDP);
|
---|
| 208 | private:
|
---|
| 209 | ID3D11InputLayout *mpInputLayout;
|
---|
| 210 | ID3D11VertexShader *mpVS;
|
---|
| 211 | ID3D11PixelShader *mpPS;
|
---|
| 212 | ID3D11Buffer *mpVB;
|
---|
| 213 |
|
---|
| 214 | struct Vertex
|
---|
| 215 | {
|
---|
| 216 | float position[3];
|
---|
| 217 | float color[4];
|
---|
| 218 | };
|
---|
| 219 | };
|
---|
| 220 |
|
---|
| 221 |
|
---|
| 222 | D3D11RenderTriangleShader::D3D11RenderTriangleShader()
|
---|
| 223 | : mpInputLayout(0)
|
---|
| 224 | , mpVS(0)
|
---|
| 225 | , mpPS(0)
|
---|
| 226 | , mpVB(0)
|
---|
| 227 | {
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | D3D11RenderTriangleShader::~D3D11RenderTriangleShader()
|
---|
| 231 | {
|
---|
| 232 | D3D_RELEASE(mpInputLayout);
|
---|
| 233 | D3D_RELEASE(mpVS);
|
---|
| 234 | D3D_RELEASE(mpPS);
|
---|
| 235 | D3D_RELEASE(mpVB);
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | HRESULT D3D11RenderTriangleShader::InitRender(D3D11DeviceProvider *pDP)
|
---|
| 239 | {
|
---|
| 240 | ID3D11Device *pDevice = pDP->Device();
|
---|
| 241 |
|
---|
| 242 | static D3D11_INPUT_ELEMENT_DESC const aVertexDesc[] =
|
---|
| 243 | {
|
---|
| 244 | {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
|
---|
| 245 | {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}
|
---|
| 246 | };
|
---|
| 247 |
|
---|
| 248 | static Vertex const aVertices[] =
|
---|
| 249 | {
|
---|
| 250 | { { -0.5f, -0.5f, 0.0f }, { 0.0f, 0.0f, 1.0f, 1.0f }, },
|
---|
| 251 | { { 0.0f, 0.5f, 0.0f }, { 0.0f, 1.0f, 0.0f, 1.0f }, },
|
---|
| 252 | { { 0.5f, -0.5f, 0.0f }, { 1.0f, 0.0f, 0.0f, 1.0f }, },
|
---|
| 253 | };
|
---|
| 254 |
|
---|
| 255 | HRESULT hr = S_OK;
|
---|
| 256 |
|
---|
| 257 | HTEST(pDevice->CreateInputLayout(aVertexDesc, RT_ELEMENTS(aVertexDesc),
|
---|
| 258 | &g_vs_color[0], sizeof(g_vs_color),
|
---|
| 259 | &mpInputLayout));
|
---|
| 260 | HTEST(pDevice->CreateVertexShader(g_vs_color, sizeof(g_vs_color), NULL, &mpVS));
|
---|
| 261 | HTEST(pDevice->CreatePixelShader(g_ps_color, sizeof(g_ps_color), NULL, &mpPS));
|
---|
| 262 |
|
---|
| 263 | D3D11_BUFFER_DESC vbd;
|
---|
| 264 | RT_ZERO(vbd);
|
---|
| 265 | vbd.Usage = D3D11_USAGE_IMMUTABLE;
|
---|
| 266 | vbd.ByteWidth = sizeof(aVertices);
|
---|
| 267 | vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
---|
| 268 | vbd.CPUAccessFlags = 0;
|
---|
| 269 | vbd.MiscFlags = 0;
|
---|
| 270 | vbd.StructureByteStride = 0;
|
---|
| 271 |
|
---|
| 272 | D3D11_SUBRESOURCE_DATA vinitData;
|
---|
| 273 | RT_ZERO(vinitData);
|
---|
| 274 | vinitData.pSysMem = aVertices;
|
---|
| 275 |
|
---|
| 276 | HTEST(pDevice->CreateBuffer(&vbd, &vinitData, &mpVB));
|
---|
| 277 |
|
---|
| 278 | return hr;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | HRESULT D3D11RenderTriangleShader::DoRender(D3D11DeviceProvider *pDP)
|
---|
| 282 | {
|
---|
| 283 | ID3D11DeviceContext *pImmediateContext = pDP->ImmediateContext();
|
---|
| 284 |
|
---|
| 285 | FLOAT aColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
|
---|
| 286 | pImmediateContext->ClearRenderTargetView(pDP->RenderTargetView(), aColor);
|
---|
[95081] | 287 | if (pDP->DepthStencilView())
|
---|
| 288 | pImmediateContext->ClearDepthStencilView(pDP->DepthStencilView(),
|
---|
| 289 | D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
|
---|
[86815] | 290 |
|
---|
| 291 | pImmediateContext->IASetInputLayout(mpInputLayout);
|
---|
| 292 | pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
---|
| 293 |
|
---|
| 294 | UINT stride = sizeof(Vertex);
|
---|
| 295 | UINT offset = 0;
|
---|
| 296 | pImmediateContext->IASetVertexBuffers(0, 1, &mpVB, &stride, &offset);
|
---|
| 297 |
|
---|
| 298 | pImmediateContext->VSSetShader(mpVS, NULL, 0);
|
---|
| 299 | pImmediateContext->PSSetShader(mpPS, NULL, 0);
|
---|
| 300 |
|
---|
| 301 | D3D11_VIEWPORT Viewport;
|
---|
| 302 | Viewport.TopLeftX = 0.0f;
|
---|
| 303 | Viewport.TopLeftY = 0.0f;
|
---|
| 304 | Viewport.Width = (float)800;
|
---|
| 305 | Viewport.Height = (float)600;
|
---|
| 306 | Viewport.MinDepth = 0.0f;
|
---|
| 307 | Viewport.MaxDepth = 1.0f;
|
---|
| 308 | pImmediateContext->RSSetViewports(1, &Viewport);
|
---|
| 309 |
|
---|
| 310 | pImmediateContext->Draw(3, 0);
|
---|
| 311 |
|
---|
| 312 | return S_OK;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
[99816] | 315 |
|
---|
| 316 |
|
---|
[86815] | 317 | /*
|
---|
[99816] | 318 | * Two vertex streams from either a single vertex buffer or two vertex buffers.
|
---|
| 319 | */
|
---|
| 320 |
|
---|
| 321 | class D3D11RenderDoubleVB: public D3D11Render
|
---|
| 322 | {
|
---|
| 323 | public:
|
---|
| 324 | D3D11RenderDoubleVB(bool fUseSameBuffer);
|
---|
| 325 | virtual ~D3D11RenderDoubleVB();
|
---|
| 326 | virtual HRESULT InitRender(D3D11DeviceProvider *pDP);
|
---|
| 327 | virtual HRESULT DoRender(D3D11DeviceProvider *pDP);
|
---|
| 328 | private:
|
---|
| 329 | bool mfUseSameBuffer;
|
---|
| 330 |
|
---|
| 331 | ID3D11InputLayout *mpInputLayout;
|
---|
| 332 | ID3D11VertexShader *mpVS;
|
---|
| 333 | ID3D11PixelShader *mpPS;
|
---|
| 334 | ID3D11Buffer *mapVB[2];
|
---|
| 335 |
|
---|
| 336 | static const size_t mcbGap;
|
---|
| 337 |
|
---|
| 338 | struct Vertex1
|
---|
| 339 | {
|
---|
| 340 | float position[3];
|
---|
| 341 | };
|
---|
| 342 |
|
---|
| 343 | struct Vertex2
|
---|
| 344 | {
|
---|
| 345 | float color[4];
|
---|
| 346 | };
|
---|
| 347 | };
|
---|
| 348 |
|
---|
| 349 | const size_t D3D11RenderDoubleVB::mcbGap = 16;
|
---|
| 350 |
|
---|
| 351 | D3D11RenderDoubleVB::D3D11RenderDoubleVB(bool fUseSameBuffer)
|
---|
| 352 | : mfUseSameBuffer(fUseSameBuffer)
|
---|
| 353 | , mpInputLayout(0)
|
---|
| 354 | , mpVS(0)
|
---|
| 355 | , mpPS(0)
|
---|
| 356 | {
|
---|
| 357 | RT_ZERO(mapVB);
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | D3D11RenderDoubleVB::~D3D11RenderDoubleVB()
|
---|
| 361 | {
|
---|
| 362 | D3D_RELEASE(mpInputLayout);
|
---|
| 363 | D3D_RELEASE(mpVS);
|
---|
| 364 | D3D_RELEASE(mpPS);
|
---|
| 365 | D3D_RELEASE(mapVB[0]);
|
---|
| 366 | D3D_RELEASE(mapVB[1]);
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | HRESULT D3D11RenderDoubleVB::InitRender(D3D11DeviceProvider *pDP)
|
---|
| 370 | {
|
---|
| 371 | ID3D11Device *pDevice = pDP->Device();
|
---|
| 372 |
|
---|
| 373 | static D3D11_INPUT_ELEMENT_DESC const aVertexDesc[] =
|
---|
| 374 | {
|
---|
| 375 | {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
|
---|
| 376 | {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}
|
---|
| 377 | };
|
---|
| 378 |
|
---|
| 379 | static Vertex1 const aVertices1[] =
|
---|
| 380 | {
|
---|
| 381 | { { -0.5f, -0.5f, 0.0f } },
|
---|
| 382 | { { 0.0f, 0.5f, 0.0f } },
|
---|
| 383 | { { 0.5f, -0.5f, 0.0f } },
|
---|
| 384 | };
|
---|
| 385 |
|
---|
| 386 | static Vertex2 const aVertices2[] =
|
---|
| 387 | {
|
---|
| 388 | { { 0.0f, 0.0f, 1.0f, 1.0f } },
|
---|
| 389 | { { 0.0f, 1.0f, 0.0f, 1.0f } },
|
---|
| 390 | { { 1.0f, 0.0f, 0.0f, 1.0f } },
|
---|
| 391 | };
|
---|
| 392 |
|
---|
| 393 | HRESULT hr = S_OK;
|
---|
| 394 |
|
---|
| 395 | HTEST(pDevice->CreateInputLayout(aVertexDesc, RT_ELEMENTS(aVertexDesc),
|
---|
| 396 | &g_vs_color[0], sizeof(g_vs_color),
|
---|
| 397 | &mpInputLayout));
|
---|
| 398 | HTEST(pDevice->CreateVertexShader(g_vs_color, sizeof(g_vs_color), NULL, &mpVS));
|
---|
| 399 | HTEST(pDevice->CreatePixelShader(g_ps_color, sizeof(g_ps_color), NULL, &mpPS));
|
---|
| 400 |
|
---|
| 401 | D3D11_BUFFER_DESC vbd;
|
---|
| 402 | D3D11_SUBRESOURCE_DATA vinitData;
|
---|
| 403 | if (mfUseSameBuffer)
|
---|
| 404 | {
|
---|
| 405 | RT_ZERO(vbd);
|
---|
| 406 | vbd.Usage = D3D11_USAGE_IMMUTABLE;
|
---|
| 407 | vbd.ByteWidth = sizeof(aVertices1) + sizeof(aVertices2);
|
---|
| 408 | vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
---|
| 409 | vbd.CPUAccessFlags = 0;
|
---|
| 410 | vbd.MiscFlags = 0;
|
---|
| 411 | vbd.StructureByteStride = 0;
|
---|
| 412 |
|
---|
| 413 | unsigned char au8TmpInitData[sizeof(aVertices1) + mcbGap + sizeof(aVertices2)];
|
---|
| 414 | memcpy(&au8TmpInitData[0], aVertices1, sizeof(aVertices1));
|
---|
| 415 | memset(&au8TmpInitData[sizeof(aVertices1)], 0, mcbGap);
|
---|
| 416 | memcpy(&au8TmpInitData[sizeof(aVertices1) + mcbGap], aVertices2, sizeof(aVertices2));
|
---|
| 417 |
|
---|
| 418 | RT_ZERO(vinitData);
|
---|
| 419 | vinitData.pSysMem = au8TmpInitData;
|
---|
| 420 |
|
---|
| 421 | HTEST(pDevice->CreateBuffer(&vbd, &vinitData, &mapVB[0]));
|
---|
| 422 |
|
---|
| 423 | mapVB[1] = mapVB[0];
|
---|
| 424 | mapVB[1]->AddRef();
|
---|
| 425 | }
|
---|
| 426 | else
|
---|
| 427 | {
|
---|
| 428 | RT_ZERO(vbd);
|
---|
| 429 | vbd.Usage = D3D11_USAGE_IMMUTABLE;
|
---|
| 430 | vbd.ByteWidth = sizeof(aVertices1);
|
---|
| 431 | vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
---|
| 432 | vbd.CPUAccessFlags = 0;
|
---|
| 433 | vbd.MiscFlags = 0;
|
---|
| 434 | vbd.StructureByteStride = 0;
|
---|
| 435 |
|
---|
| 436 | RT_ZERO(vinitData);
|
---|
| 437 | vinitData.pSysMem = aVertices1;
|
---|
| 438 |
|
---|
| 439 | HTEST(pDevice->CreateBuffer(&vbd, &vinitData, &mapVB[0]));
|
---|
| 440 |
|
---|
| 441 | RT_ZERO(vbd);
|
---|
| 442 | vbd.Usage = D3D11_USAGE_IMMUTABLE;
|
---|
| 443 | vbd.ByteWidth = sizeof(aVertices2);
|
---|
| 444 | vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
---|
| 445 | vbd.CPUAccessFlags = 0;
|
---|
| 446 | vbd.MiscFlags = 0;
|
---|
| 447 | vbd.StructureByteStride = 0;
|
---|
| 448 |
|
---|
| 449 | RT_ZERO(vinitData);
|
---|
| 450 | vinitData.pSysMem = aVertices2;
|
---|
| 451 |
|
---|
| 452 | HTEST(pDevice->CreateBuffer(&vbd, &vinitData, &mapVB[1]));
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | return hr;
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | HRESULT D3D11RenderDoubleVB::DoRender(D3D11DeviceProvider *pDP)
|
---|
| 459 | {
|
---|
| 460 | ID3D11DeviceContext *pImmediateContext = pDP->ImmediateContext();
|
---|
| 461 |
|
---|
| 462 | FLOAT aColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
|
---|
| 463 | pImmediateContext->ClearRenderTargetView(pDP->RenderTargetView(), aColor);
|
---|
| 464 | if (pDP->DepthStencilView())
|
---|
| 465 | pImmediateContext->ClearDepthStencilView(pDP->DepthStencilView(),
|
---|
| 466 | D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
|
---|
| 467 |
|
---|
| 468 | pImmediateContext->IASetInputLayout(mpInputLayout);
|
---|
| 469 | pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
---|
| 470 |
|
---|
| 471 | if (mfUseSameBuffer)
|
---|
| 472 | {
|
---|
| 473 | UINT aStrides[] = { sizeof(Vertex1), sizeof(Vertex2) };
|
---|
| 474 | UINT aOffsets[] = { 0 , 3 * sizeof(Vertex1) + mcbGap };
|
---|
| 475 | pImmediateContext->IASetVertexBuffers(0, 2, mapVB, aStrides, aOffsets);
|
---|
| 476 | }
|
---|
| 477 | else
|
---|
| 478 | {
|
---|
| 479 | UINT aStrides[] = { sizeof(Vertex1), sizeof(Vertex2) };
|
---|
| 480 | UINT aOffsets[] = { 0 , 0 };
|
---|
| 481 | pImmediateContext->IASetVertexBuffers(0, 2, mapVB, aStrides, aOffsets);
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | pImmediateContext->VSSetShader(mpVS, NULL, 0);
|
---|
| 485 | pImmediateContext->PSSetShader(mpPS, NULL, 0);
|
---|
| 486 |
|
---|
| 487 | D3D11_VIEWPORT Viewport;
|
---|
| 488 | Viewport.TopLeftX = 0.0f;
|
---|
| 489 | Viewport.TopLeftY = 0.0f;
|
---|
| 490 | Viewport.Width = (float)800;
|
---|
| 491 | Viewport.Height = (float)600;
|
---|
| 492 | Viewport.MinDepth = 0.0f;
|
---|
| 493 | Viewport.MaxDepth = 1.0f;
|
---|
| 494 | pImmediateContext->RSSetViewports(1, &Viewport);
|
---|
| 495 |
|
---|
| 496 | pImmediateContext->Draw(3, 0);
|
---|
| 497 |
|
---|
| 498 | return S_OK;
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 |
|
---|
| 502 | /*
|
---|
| 503 | * DrawIndexed with various StartIndexLocation, BaseVertexLocation and index buffer offset.
|
---|
| 504 | */
|
---|
| 505 |
|
---|
| 506 | class D3D11RenderDrawIndexed: public D3D11Render
|
---|
| 507 | {
|
---|
| 508 | public:
|
---|
| 509 | D3D11RenderDrawIndexed();
|
---|
| 510 | virtual ~D3D11RenderDrawIndexed();
|
---|
| 511 | virtual HRESULT InitRender(D3D11DeviceProvider *pDP);
|
---|
| 512 | virtual HRESULT DoRender(D3D11DeviceProvider *pDP);
|
---|
| 513 | private:
|
---|
| 514 | ID3D11InputLayout *mpInputLayout;
|
---|
| 515 | ID3D11VertexShader *mpVS;
|
---|
| 516 | ID3D11PixelShader *mpPS;
|
---|
| 517 | ID3D11Buffer *mpVB;
|
---|
| 518 | ID3D11Buffer *mpIB;
|
---|
| 519 |
|
---|
| 520 | struct Vertex
|
---|
| 521 | {
|
---|
| 522 | float position[3];
|
---|
| 523 | float color[4];
|
---|
| 524 | };
|
---|
| 525 | };
|
---|
| 526 |
|
---|
| 527 |
|
---|
| 528 | D3D11RenderDrawIndexed::D3D11RenderDrawIndexed()
|
---|
| 529 | : mpInputLayout(0)
|
---|
| 530 | , mpVS(0)
|
---|
| 531 | , mpPS(0)
|
---|
| 532 | , mpVB(0)
|
---|
| 533 | , mpIB(0)
|
---|
| 534 | {
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | D3D11RenderDrawIndexed::~D3D11RenderDrawIndexed()
|
---|
| 538 | {
|
---|
| 539 | D3D_RELEASE(mpInputLayout);
|
---|
| 540 | D3D_RELEASE(mpVS);
|
---|
| 541 | D3D_RELEASE(mpPS);
|
---|
| 542 | D3D_RELEASE(mpVB);
|
---|
| 543 | D3D_RELEASE(mpIB);
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | HRESULT D3D11RenderDrawIndexed::InitRender(D3D11DeviceProvider *pDP)
|
---|
| 547 | {
|
---|
| 548 | ID3D11Device *pDevice = pDP->Device();
|
---|
| 549 |
|
---|
| 550 | static D3D11_INPUT_ELEMENT_DESC const aVertexDesc[] =
|
---|
| 551 | {
|
---|
| 552 | {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
|
---|
| 553 | {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}
|
---|
| 554 | };
|
---|
| 555 |
|
---|
| 556 | /* A triangle in each quadrant. Need 5 points. */
|
---|
| 557 | static Vertex const aVertices[] =
|
---|
| 558 | {
|
---|
| 559 | { { 0.0f, 0.0f, 0.5f }, { 0.0f, 0.0f, 0.0f, 1.0f }, }, // 0: center
|
---|
| 560 | { { -0.8f, 0.0f, 0.5f }, { 1.0f, 0.0f, 0.0f, 1.0f }, }, // 1: left
|
---|
| 561 | { { 0.0f, 0.8f, 0.5f }, { 0.0f, 1.0f, 0.0f, 1.0f }, }, // 2: top
|
---|
| 562 | { { 0.8f, 0.0f, 0.5f }, { 0.0f, 0.0f, 1.0f, 1.0f }, }, // 3: right
|
---|
| 563 | { { 0.0f, -0.8f, 0.5f }, { 0.5f, 0.5f, 0.5f, 1.0f }, }, // 4: bottom
|
---|
| 564 |
|
---|
| 565 | /* A smaller copy to use BaseVertexLocation. */
|
---|
| 566 | { { 0.0f, 0.0f, 0.0f }, { 0.5f, 0.5f, 0.5f, 1.0f }, }, // 0: center
|
---|
| 567 | { { -0.2f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f, 1.0f }, }, // 1: left
|
---|
| 568 | { { 0.0f, 0.2f, 0.0f }, { 0.0f, 1.0f, 1.0f, 1.0f }, }, // 2: top
|
---|
| 569 | { { 0.2f, 0.0f, 0.0f }, { 1.0f, 1.0f, 0.0f, 1.0f }, }, // 3: right
|
---|
| 570 | { { 0.0f, -0.2f, 0.0f }, { 1.0f, 0.0f, 1.0f, 1.0f }, }, // 4: bottom
|
---|
| 571 | };
|
---|
| 572 |
|
---|
| 573 | /* Four triangles. Indices for each triangle will be accessed using different
|
---|
| 574 | * StartIndexLocation and index buffer offset values.
|
---|
| 575 | */
|
---|
| 576 | static uint16_t const aIndices[] =
|
---|
| 577 | {
|
---|
| 578 | /* Bottom left: left, center, bottom. */ 1, 0, 4,
|
---|
| 579 | /* Top left: top, center, left. */ 2, 0, 1,
|
---|
| 580 | /* Top right: right, center, top. */ 3, 0, 2,
|
---|
| 581 | /* Bottom right: bottom, center, right. */ 4, 0, 3,
|
---|
| 582 | };
|
---|
| 583 |
|
---|
| 584 | HRESULT hr = S_OK;
|
---|
| 585 |
|
---|
| 586 | HTEST(pDevice->CreateInputLayout(aVertexDesc, RT_ELEMENTS(aVertexDesc),
|
---|
| 587 | &g_vs_color[0], sizeof(g_vs_color),
|
---|
| 588 | &mpInputLayout));
|
---|
| 589 | HTEST(pDevice->CreateVertexShader(g_vs_color, sizeof(g_vs_color), NULL, &mpVS));
|
---|
| 590 | HTEST(pDevice->CreatePixelShader(g_ps_color, sizeof(g_ps_color), NULL, &mpPS));
|
---|
| 591 |
|
---|
| 592 | D3D11_BUFFER_DESC bd;
|
---|
| 593 | RT_ZERO(bd);
|
---|
| 594 | bd.Usage = D3D11_USAGE_IMMUTABLE;
|
---|
| 595 | bd.ByteWidth = sizeof(aVertices);
|
---|
| 596 | bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
---|
| 597 | bd.CPUAccessFlags = 0;
|
---|
| 598 | bd.MiscFlags = 0;
|
---|
| 599 | bd.StructureByteStride = 0;
|
---|
| 600 |
|
---|
| 601 | D3D11_SUBRESOURCE_DATA initData;
|
---|
| 602 | RT_ZERO(initData);
|
---|
| 603 | initData.pSysMem = aVertices;
|
---|
| 604 |
|
---|
| 605 | HTEST(pDevice->CreateBuffer(&bd, &initData, &mpVB));
|
---|
| 606 |
|
---|
| 607 | RT_ZERO(bd);
|
---|
| 608 | bd.Usage = D3D11_USAGE_IMMUTABLE;
|
---|
| 609 | bd.ByteWidth = sizeof(aIndices);
|
---|
| 610 | bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
---|
| 611 | bd.CPUAccessFlags = 0;
|
---|
| 612 | bd.MiscFlags = 0;
|
---|
| 613 | bd.StructureByteStride = 0;
|
---|
| 614 |
|
---|
| 615 | RT_ZERO(initData);
|
---|
| 616 | initData.pSysMem = aIndices;
|
---|
| 617 |
|
---|
| 618 | HTEST(pDevice->CreateBuffer(&bd, &initData, &mpIB));
|
---|
| 619 |
|
---|
| 620 | return hr;
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 | static void drawRectangles(ID3D11DeviceContext *pImmediateContext, ID3D11Buffer *pIB, INT const BaseVertexLocation)
|
---|
| 624 | {
|
---|
| 625 | /* Draw each triangle separately. */
|
---|
| 626 | UINT offset;
|
---|
| 627 | UINT StartIndexLocation;
|
---|
| 628 |
|
---|
| 629 | /* Bottom left. */
|
---|
| 630 | offset = 0;
|
---|
| 631 | StartIndexLocation = 0;
|
---|
| 632 | pImmediateContext->IASetIndexBuffer(pIB, DXGI_FORMAT_R16_UINT, offset);
|
---|
| 633 | pImmediateContext->DrawIndexed(3, StartIndexLocation, BaseVertexLocation);
|
---|
| 634 |
|
---|
| 635 | /* Top left. */
|
---|
| 636 | offset = 0;
|
---|
| 637 | StartIndexLocation = 3;
|
---|
| 638 | pImmediateContext->IASetIndexBuffer(pIB, DXGI_FORMAT_R16_UINT, offset);
|
---|
| 639 | pImmediateContext->DrawIndexed(3, StartIndexLocation, BaseVertexLocation);
|
---|
| 640 |
|
---|
| 641 | /* Top right. */
|
---|
| 642 | offset = 6 * sizeof(uint16_t);
|
---|
| 643 | StartIndexLocation = 0;
|
---|
| 644 | pImmediateContext->IASetIndexBuffer(pIB, DXGI_FORMAT_R16_UINT, offset);
|
---|
| 645 | pImmediateContext->DrawIndexed(3, StartIndexLocation, BaseVertexLocation);
|
---|
| 646 |
|
---|
| 647 | /* Bottom right. */
|
---|
| 648 | offset = 6 * sizeof(uint16_t);
|
---|
| 649 | StartIndexLocation = 3;
|
---|
| 650 | pImmediateContext->IASetIndexBuffer(pIB, DXGI_FORMAT_R16_UINT, offset);
|
---|
| 651 | pImmediateContext->DrawIndexed(3, StartIndexLocation, BaseVertexLocation);
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 | HRESULT D3D11RenderDrawIndexed::DoRender(D3D11DeviceProvider *pDP)
|
---|
| 655 | {
|
---|
| 656 | ID3D11DeviceContext *pImmediateContext = pDP->ImmediateContext();
|
---|
| 657 |
|
---|
| 658 | FLOAT aColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
|
---|
| 659 | pImmediateContext->ClearRenderTargetView(pDP->RenderTargetView(), aColor);
|
---|
| 660 | if (pDP->DepthStencilView())
|
---|
| 661 | pImmediateContext->ClearDepthStencilView(pDP->DepthStencilView(),
|
---|
| 662 | D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
|
---|
| 663 |
|
---|
| 664 | pImmediateContext->IASetInputLayout(mpInputLayout);
|
---|
| 665 | pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
---|
| 666 |
|
---|
| 667 | UINT stride = sizeof(Vertex);
|
---|
| 668 | UINT offset = 0;
|
---|
| 669 | pImmediateContext->IASetVertexBuffers(0, 1, &mpVB, &stride, &offset);
|
---|
| 670 |
|
---|
| 671 | pImmediateContext->VSSetShader(mpVS, NULL, 0);
|
---|
| 672 | pImmediateContext->PSSetShader(mpPS, NULL, 0);
|
---|
| 673 |
|
---|
| 674 | INT BaseVertexLocation;
|
---|
| 675 | BaseVertexLocation = 0;
|
---|
| 676 | drawRectangles(pImmediateContext, mpIB, BaseVertexLocation);
|
---|
| 677 | BaseVertexLocation = 5;
|
---|
| 678 | drawRectangles(pImmediateContext, mpIB, BaseVertexLocation);
|
---|
| 679 |
|
---|
| 680 | return S_OK;
|
---|
| 681 | }
|
---|
| 682 |
|
---|
[103983] | 683 | //#include "D3D11RenderVMCloud.txt"
|
---|
[99816] | 684 |
|
---|
| 685 | /*
|
---|
[86815] | 686 | * "Public" interface.
|
---|
| 687 | */
|
---|
| 688 |
|
---|
| 689 | D3D11Render *CreateRender(int iRenderId)
|
---|
| 690 | {
|
---|
| 691 | switch (iRenderId)
|
---|
| 692 | {
|
---|
| 693 | case 0:
|
---|
| 694 | return new D3D11RenderClear();
|
---|
| 695 | case 1:
|
---|
| 696 | return new D3D11RenderTriangleShader();
|
---|
[99816] | 697 | case 2:
|
---|
| 698 | return new D3D11RenderDoubleVB(false);
|
---|
| 699 | case 3:
|
---|
| 700 | return new D3D11RenderDoubleVB(true);
|
---|
| 701 | case 4:
|
---|
| 702 | return new D3D11RenderDrawIndexed();
|
---|
[103983] | 703 | // case 5:
|
---|
| 704 | // return new D3D11RenderTest();
|
---|
[86815] | 705 | default:
|
---|
| 706 | break;
|
---|
| 707 | }
|
---|
| 708 | return 0;
|
---|
| 709 | }
|
---|
| 710 |
|
---|
| 711 | void DeleteRender(D3D11Render *pRender)
|
---|
| 712 | {
|
---|
| 713 | if (pRender)
|
---|
| 714 | delete pRender;
|
---|
| 715 | }
|
---|