summaryrefslogtreecommitdiff
path: root/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/gallium/test/d3dhlp.cpp
blob: 9c24792bad8f2197c71b86790feefd6173a405e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/* $Id$ */
/** @file
 * Gallium D3D testcase. Various D3D helpers.
 */

/*
 * Copyright (C) 2017-2022 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

#include "d3dhlp.h"
#include <math.h>

/*
 * D3D vector and matrix math helpers.
 */
void d3dMatrixTranspose(D3DMATRIX *pM)
{
    int j; /* Row. Only first 3 because diagonal elements are not swapped, i.e. no need to process [3][3]. */
    for (j = 0; j < 3; ++j)
    {
        int i; /* Column, upper right elements. Skip diagonal element [j][j]. */
        for (i = j + 1; i < 4; ++i)
        {
            /* Swap with corresponding bottom left element. */
            const float tmp = pM->m[j][i];
            pM->m[j][i] = pM->m[i][j];
            pM->m[i][j] = tmp;
        }
    }
}

void d3dMatrixIdentity(D3DMATRIX *pM)
{
    int j; /* Row. */
    for (j = 0; j < 4; ++j)
    {
        int i; /* Column. */
        for (i = 0; i < 4; ++i)
        {
            pM->m[j][i] = j == i ? 1.0f : 0.0f;
        }
    }
}

void d3dMatrixScaleTranslation(D3DMATRIX *pM, const float s, const float dx, const float dy, const float dz)
{
    /*
     * Translation matrix:
     * | s  0  0  0 |
     * | 0  s  0  0 |
     * | 0  0  s  0 |
     * | dx dy dz 1 |
     */
    pM->m[0][0] = s;
    pM->m[0][1] = 0;
    pM->m[0][2] = 0;
    pM->m[0][3] = 0;

    pM->m[1][0] = 0;
    pM->m[1][1] = s;
    pM->m[1][2] = 0;
    pM->m[1][3] = 0;

    pM->m[2][0] = 0;
    pM->m[2][1] = 0;
    pM->m[2][2] = s;
    pM->m[2][3] = 0;

    pM->m[3][0] = dx;
    pM->m[3][1] = dy;
    pM->m[3][2] = dz;
    pM->m[3][3] = 1;
}

void d3dMatrixRotationAxis(D3DMATRIX *pM, const D3DVECTOR *pV, float angle)
{
    /*
     * Rotation matrix:
     * | c+x^2*(1-c)    x*y*(1-c)+z*s  x*z*(1-c)-y*s  0 |
     * | x*y*(1-c)-z*s  c+y^2*(1-c)    y*z*(1-c)+x*s  0 |
     * | x*z*(1-c)+y*s  y*z*(1-c)-x*s  c+z^2*(1-c)    0 |
     * | 0              0              0              1 |
     */
    const float c = cos(angle);
    const float s = sin(angle);
    const float x = pV->x;
    const float y = pV->y;
    const float z = pV->z;

    pM->m[0][0] = c + x*x*(1-c);
    pM->m[0][1] = x*y*(1-c)+z*s;
    pM->m[0][2] = x*z*(1-c)-y*s;
    pM->m[0][3] = 0;

    pM->m[1][0] = x*y*(1-c)-z*s;
    pM->m[1][1] = c+y*y*(1-c);
    pM->m[1][2] = y*z*(1-c)+x*s;
    pM->m[1][3] = 0;

    pM->m[2][0] = x*z*(1-c)+y*s;
    pM->m[2][1] = y*z*(1-c)-x*s;
    pM->m[2][2] = c+z*z*(1-c);
    pM->m[2][3] = 0;

    pM->m[3][0] = 0;
    pM->m[3][1] = 0;
    pM->m[3][2] = 0;
    pM->m[3][3] = 1;
}

void d3dMatrixView(D3DMATRIX *pM, const D3DVECTOR *pR, const D3DVECTOR *pU, const D3DVECTOR *pL, const D3DVECTOR *pP)
{
    /*
     * Camera coordinate system vectors:
     * *pR = r = right = x
     * *pU = u = up    = y
     * *pL = l = look  = z
     * *pP = p = position
     *
     * View matrix:
     * |  r.x  u.x  l.x 0 |
     * |  r.y  u.y  l.y 0 |
     * |  r.z  u.z  l.z 0 |
     * | -pr  -pu  -pl  1 |
     *
     */

    pM->m[0][0] = pR->x;
    pM->m[0][1] = pU->x;
    pM->m[0][2] = pL->x;
    pM->m[0][3] = 0.0f;

    pM->m[1][0] = pR->y;
    pM->m[1][1] = pU->y;
    pM->m[1][2] = pL->y;
    pM->m[1][3] = 0.0f;

    pM->m[2][0] = pR->z;
    pM->m[2][1] = pU->z;
    pM->m[2][2] = pL->z;
    pM->m[2][3] = 0.0f;

    pM->m[3][0] = -d3dVectorDot(pP, pR);
    pM->m[3][1] = -d3dVectorDot(pP, pU);
    pM->m[3][2] = -d3dVectorDot(pP, pL);
    pM->m[3][3] = 1.0f;
}

void d3dMatrixPerspectiveProjection(D3DMATRIX *pM, float verticalFoV, float aspectRatio, float zNear, float zFar)
{
    /*
     * Perspective projection matrix.
     *
     * a = verticalFoV = vertical field of view angle.
     * R = aspectRatio = width / height of the view window.
     * n = zNear = near Z plane
     * f = zFar = far Z plane
     *
     * | 1/(R*tan(a/2)) 0          0          0 |
     * | 0              1/tan(a/2) 0          0 |
     * | 0              0          f/(f-n)    1 |
     * | 0              0          -f*n/(f-n) 0 |
     */
    const float reciprocalTan2 = 1.0f / tan(verticalFoV / 2.0f);
    const float zRange = zFar - zNear;

    pM->m[0][0] = reciprocalTan2 / aspectRatio;
    pM->m[0][1] = 0;
    pM->m[0][2] = 0;
    pM->m[0][3] = 0;

    pM->m[1][0] = 0;
    pM->m[1][1] = reciprocalTan2;
    pM->m[1][2] = 0;
    pM->m[1][3] = 0;

    pM->m[2][0] = 0;
    pM->m[2][1] = 0;
    pM->m[2][2] = zFar / zRange;
    pM->m[2][3] = 1;

    pM->m[3][0] = 0;
    pM->m[3][1] = 0;
    pM->m[3][2] = - zNear * zFar / zRange;
    pM->m[3][3] = 0;
}

void d3dMatrixMultiply(D3DMATRIX *pM, const D3DMATRIX *pM1, const D3DMATRIX *pM2)
{
    /* *pM = *pM1 * *pM2 */
    int j; /* Row. */
    for (j = 0; j < 4; ++j)
    {
        int i; /* Column. */
        for (i = 0; i < 4; ++i)
        {
            /* Row by Column */
            pM->m[j][i] =   pM1->m[j][0] * pM2->m[0][i]
                          + pM1->m[j][1] * pM2->m[1][i]
                          + pM1->m[j][2] * pM2->m[2][i]
                          + pM1->m[j][3] * pM2->m[3][i];
        }
    }
}

void d3dVectorMatrixMultiply(D3DVECTOR *pR, const D3DVECTOR *pV, float w, const D3DMATRIX *pM)
{
    /* Vector row by matrix columns */
    const float x = pV->x;
    const float y = pV->y;
    const float z = pV->z;
    pR->x = x * pM->m[0][0] + y * pM->m[1][0] + z * pM->m[2][0] + w * pM->m[3][0];
    pR->y = x * pM->m[0][1] + y * pM->m[1][1] + z * pM->m[2][1] + w * pM->m[3][1];
    pR->z = x * pM->m[0][2] + y * pM->m[1][2] + z * pM->m[2][2] + w * pM->m[3][2];
}

void d3dVectorNormalize(D3DVECTOR *pV)
{
    const float Length = sqrt(pV->x * pV->x + pV->y * pV->y + pV->z * pV->z);
    if (Length > 0.0f)
    {
        pV->x /= Length;
        pV->y /= Length;
        pV->z /= Length;
    }
}

void d3dVectorCross(D3DVECTOR *pC, const D3DVECTOR *pV1, const D3DVECTOR *pV2)
{
    /*
     * | i    j    k    |
     * | v1.x v1.y v1.z |
     * | v2.x v2.y v2.z |
     */
    pC->x =  pV1->y * pV2->z - pV2->y  * pV1->z;
    pC->y = -pV1->x * pV2->z + pV2->x  * pV1->z;
    pC->z =  pV1->x * pV2->y - pV2->x  * pV1->y;
}


float d3dVectorDot(const D3DVECTOR *pV1, const D3DVECTOR *pV2)
{
    return pV1->x * pV2->x + pV1->y * pV2->y + pV1->z * pV2->z;
}

void d3dVectorInit(D3DVECTOR *pV, float x, float y, float z)
{
    pV->x = x;
    pV->y = y;
    pV->z = z;
}


/*
 * Helper to compute view and projection matrices for a camera.
 */
D3DCamera::D3DCamera()
{
    d3dMatrixIdentity(&mView);
    d3dMatrixIdentity(&mProjection);
    d3dMatrixIdentity(&mViewProjection);

    d3dVectorInit(&mPosition, 0.0f, 0.0f, 0.0f);
    d3dVectorInit(&mRight,    1.0f, 0.0f, 0.0f);
    d3dVectorInit(&mUp,       0.0f, 1.0f, 0.0f);
    d3dVectorInit(&mLook,     0.0f, 0.0f, 1.0f);

    mTime = 0.0f;
}

const D3DMATRIX *D3DCamera::ViewProjection(void)
{
    return &mViewProjection;
}

void D3DCamera::SetupAt(const D3DVECTOR *pPos, const D3DVECTOR *pAt, const D3DVECTOR *pUp)
{
    mLook.x = pAt->x - pPos->x;
    mLook.y = pAt->y - pPos->y;
    mLook.z = pAt->z - pPos->z;
    d3dVectorNormalize(&mLook);

    d3dVectorCross(&mRight, pUp, &mLook);
    d3dVectorNormalize(&mRight);

    d3dVectorCross(&mUp, &mLook, &mRight);
    d3dVectorNormalize(&mUp);

    mPosition = *pPos;

    computeView();
    d3dMatrixMultiply(&mViewProjection, &mView, &mProjection);
}

void D3DCamera::SetProjection(float verticalFoV, float aspectRatio, float zNear, float zFar)
{
    d3dMatrixPerspectiveProjection(&mProjection, verticalFoV, aspectRatio, zNear, zFar);
    d3dMatrixMultiply(&mViewProjection, &mView, &mProjection);
}

void D3DCamera::TimeAdvance(float dt)
{
    mTime += dt;

    const float xAngleCam = 3.14f / 4.0f * sin(mTime * 3.14f / 9.0f); /* About camera X axis. */
    const float yAngleW = mTime * 3.14f / 4.0f; /* Rotate about world Y axis. */

    const D3DVECTOR xAxis = { 1.0f, 0.0f, 0.0f };
    const D3DVECTOR yAxis = { 0.0f, 1.0f, 0.0f };
    const D3DVECTOR zAxis = { 0.0f, 0.0f, 1.0f };

    /* Start from scratch. */
    mRight = xAxis;
    mUp    = yAxis;
    mLook  = zAxis;

    D3DMATRIX R;

    /* Rotate the camera up and look vectors about the right vector. */
    d3dMatrixRotationAxis(&R, &mRight, xAngleCam);
    d3dVectorMatrixMultiply(&mUp,   &mUp,   0.0f, &R);
    d3dVectorMatrixMultiply(&mLook, &mLook, 0.0f, &R);

    /* Rotate camera axes about the world Y axis. */
    d3dMatrixRotationAxis(&R, &yAxis, yAngleW);
    d3dVectorMatrixMultiply(&mRight, &mRight, 0.0f, &R);
    d3dVectorMatrixMultiply(&mUp,    &mUp,    0.0f, &R);
    d3dVectorMatrixMultiply(&mLook,  &mLook,  0.0f, &R);

    computeView();
    d3dMatrixMultiply(&mViewProjection, &mView, &mProjection);
}

void D3DCamera::computeView(void)
{
    /* Vectors of the ñamera coordinate system must be orthonormal. */
    d3dVectorNormalize(&mLook);

    d3dVectorCross(&mUp, &mLook, &mRight);
    d3dVectorNormalize(&mUp);

    d3dVectorCross(&mRight, &mUp, &mLook);
    d3dVectorNormalize(&mRight);

    d3dMatrixView(&mView, &mRight, &mUp, &mLook, &mPosition);
}

/* Create and initialize IDirect3DCubeTexture9. */
HRESULT d3dCreateCubeTexture(IDirect3DDevice9 *pDevice, IDirect3DCubeTexture9 **ppCubeTexture)
{
    HRESULT hr = S_OK;

    /* Create a texture in memory. Test transfer D3DPOOL_SYSTEMMEM -> D3DPOOL_DEFAULT */
    UINT      EdgeLength = 256;
    UINT      Levels = 8; /* Greater than number of faces. */
    DWORD     Usage = 0;
    D3DFORMAT Format = D3DFMT_A8R8G8B8;
    D3DPOOL   Pool = D3DPOOL_SYSTEMMEM;

    IDirect3DCubeTexture9 *pMemTex = NULL;
    HTEST(pDevice->CreateCubeTexture(EdgeLength, Levels, Usage, Format, Pool, &pMemTex, NULL));

    /* Initialize texture content. */
    int iFace;
    for (iFace = 0; iFace < 6; ++iFace)
    {
        DWORD Color;
        D3DCUBEMAP_FACES Face;
        switch (iFace)
        {
        case 0: Face = D3DCUBEMAP_FACE_POSITIVE_X; Color = 0xfff0f0f0; break; /* Almost white */
        case 1: Face = D3DCUBEMAP_FACE_NEGATIVE_X; Color = 0xff7f7f7f; break; /* Gray */
        case 2: Face = D3DCUBEMAP_FACE_POSITIVE_Y; Color = 0xff0000ff; break; /* Blue */
        case 3: Face = D3DCUBEMAP_FACE_NEGATIVE_Y; Color = 0xff00007f; break; /* Darker blue */
        case 4: Face = D3DCUBEMAP_FACE_POSITIVE_Z; Color = 0xff00ff00; break; /* Green */
        default:
        case 5: Face = D3DCUBEMAP_FACE_NEGATIVE_Z; Color = 0xff007f00; break; /* Darker green */
        }

        UINT Level;
        for (Level = 0; Level < Levels; ++Level)
        {
            IDirect3DSurface9 *pCubeMapSurface = NULL;
            HTEST(pMemTex->GetCubeMapSurface(Face, Level, &pCubeMapSurface));

            D3DSURFACE_DESC Desc;
            HTEST(pCubeMapSurface->GetDesc(&Desc));

            D3DLOCKED_RECT LockedRect;
            HTEST(pCubeMapSurface->LockRect(&LockedRect, NULL, D3DLOCK_DISCARD));

            UINT y;
            BYTE *pb = (BYTE *)LockedRect.pBits;
            for (y = 0; y < Desc.Height; ++y)
            {
                UINT x;
                for (x = 0; x < Desc.Width; ++x)
                {
                    DWORD *pdw = (DWORD *)pb;
                    pdw[x] = Color;
                }
                pb += LockedRect.Pitch;
            }

            HTEST(pCubeMapSurface->UnlockRect());

            pCubeMapSurface->Release();
        }
    }

    /* Create actual texture. */
    Pool = D3DPOOL_DEFAULT;

    IDirect3DCubeTexture9 *pCubeTex = NULL;
    HTEST(pDevice->CreateCubeTexture(EdgeLength, Levels, Usage, Format, Pool, &pCubeTex, NULL));

    /* Copy texture content. */
    HTEST(pDevice->UpdateTexture(pMemTex, pCubeTex));

    /* Do not need the bounce texture anymore. */
    pMemTex->Release();

    *ppCubeTexture = pCubeTex;
    return hr;
}

/* Create IDirect3DVertexBuffer9 with vertices for a cube. */
HRESULT d3dCreateCubeVertexBuffer(IDirect3DDevice9 *pDevice, float EdgeLength, IDirect3DVertexBuffer9 **ppVertexBuffer)
{
    /* Create vertices of a cube (no indexing). Layout: 3x float.
     * Arbitrary winding order, will use D3DRS_CULLMODE = D3DCULL_NONE.
     */

    static float aVertices[6 * 6 * 3] =
    {
        /* POSITIVE_X */
        /* Triangle 1 */
         1.0f, -1.0f, -1.0f,
         1.0f, -1.0f,  1.0f,
         1.0f,  1.0f,  1.0f,
        /* Triangle 2 */
         1.0f, -1.0f, -1.0f,
         1.0f,  1.0f,  1.0f,
         1.0f,  1.0f, -1.0f,

        /* NEGATIVE_X */
        /* Triangle 1 */
        -1.0f, -1.0f, -1.0f,
        -1.0f, -1.0f,  1.0f,
        -1.0f,  1.0f,  1.0f,
        /* Triangle 2 */
        -1.0f, -1.0f, -1.0f,
        -1.0f,  1.0f,  1.0f,
        -1.0f,  1.0f, -1.0f,

        /* POSITIVE_Y */
        /* Triangle 1 */
        -1.0f,  1.0f, -1.0f,
        -1.0f,  1.0f,  1.0f,
         1.0f,  1.0f,  1.0f,
        /* Triangle 2 */
        -1.0f,  1.0f, -1.0f,
         1.0f,  1.0f,  1.0f,
         1.0f,  1.0f, -1.0f,

        /* NEGATIVE_Y */
        /* Triangle 1 */
        -1.0f, -1.0f, -1.0f,
        -1.0f, -1.0f,  1.0f,
         1.0f, -1.0f,  1.0f,
        /* Triangle 2 */
        -1.0f, -1.0f, -1.0f,
         1.0f, -1.0f,  1.0f,
         1.0f, -1.0f, -1.0f,

        /* POSITIVE_Z */
        /* Triangle 1 */
         1.0f, -1.0f,  1.0f,
        -1.0f, -1.0f,  1.0f,
        -1.0f,  1.0f,  1.0f,
        /* Triangle 2 */
         1.0f, -1.0f,  1.0f,
        -1.0f,  1.0f,  1.0f,
         1.0f,  1.0f,  1.0f,

        /* NEGATIVE_Z */
        /* Triangle 1 */
         1.0f, -1.0f, -1.0f,
        -1.0f, -1.0f, -1.0f,
        -1.0f,  1.0f, -1.0f,
        /* Triangle 2 */
         1.0f, -1.0f, -1.0f,
        -1.0f,  1.0f, -1.0f,
         1.0f,  1.0f, -1.0f,
    };

    HRESULT hr = S_OK;

    IDirect3DVertexBuffer9 *pVB = NULL;
    HTEST(pDevice->CreateVertexBuffer(6 * 6 * 3 * sizeof(float), D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &pVB, 0));

    void *pv = 0;
    HTEST(pVB->Lock(0, 0, &pv, 0));

    const int cFloats = sizeof(aVertices)/sizeof(aVertices[0]);
    float *p = (float *)pv;
    int i;
    for (i = 0; i < cFloats; ++i)
    {
        p[i] = aVertices[i] * EdgeLength / 2.0f;
    }

    HTEST(pVB->Unlock());

    *ppVertexBuffer = pVB;
    return S_OK;
}