summaryrefslogtreecommitdiff
path: root/build/visual-studio/gfx-test-tool
diff options
context:
space:
mode:
Diffstat (limited to 'build/visual-studio/gfx-test-tool')
0 files changed, 0 insertions, 0 deletions
='#n53'>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 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
// main.cpp

#include "../../source/core/slang-io.h"
#include "GFSDK_Aftermath.h"
#include "GFSDK_Aftermath_GpuCrashDump.h"
#include "core/slang-basic.h"
#include "examples/example-base/example-base.h"
#include "gfx-util/shader-cursor.h"
#include "platform/window.h"
#include "slang-com-ptr.h"
#include "slang-gfx.h"
#include "slang.h"

using namespace gfx;
using namespace Slang;

static const ExampleResources resourceBase("nv-aftermath-example");

// This example is based on the "triangle" sample.
//
// This examples purpose is to show how to use the aftermath SDK to capture
// a crash dump.
//
// * [nsight aftermath](https://developer.nvidia.com/nsight-aftermath)
//
// In addition it uses obfuscation and source maps to allow source level
// debugging via aftermath even with obfuscation.
//
// * [obfuscation](https://github.com/shader-slang/slang/blob/master/docs/user-guide/a1-03-obfuscation.md)
// * [source map](https://github.com/source-map/source-map-spec)

struct Vertex
{
    float position[3];
    float color[3];
};

static const int kVertexCount = 3;
static const Vertex kVertexData[kVertexCount] = {
    {{0, 0, 0.5}, {1, 0, 0}},
    {{0, 1, 0.5}, {0, 0, 1}},
    {{1, 0, 0.5}, {0, 1, 0}},
};

struct AftermathCrashExample : public WindowedAppBase
{
    void diagnoseIfNeeded(slang::IBlob* diagnosticsBlob);

    gfx::Result loadShaderProgram(gfx::IDevice* device, gfx::IShaderProgram** outProgram);

    Slang::Result initialize();

    virtual void renderFrame(int frameBufferIndex) override;

    void onAftermathCrash(const void* data, const uint32_t dataSizeInBytes);

    void onAftermathDebugInfo(const void* pGpuCrashDump, const uint32_t gpuCrashDumpSize);

    void onAftermathCrashDescription(PFN_GFSDK_Aftermath_AddGpuCrashDumpDescription description);

    void onAftermathMarker(const void* pMarker, void** resolvedMarkerData, uint32_t* markerSize);

    // Create accessors so we don't have to use g prefixed variables.
    gfx::IDevice* getDevice() { return gDevice; }
    gfx::ICommandQueue* getQueue() { return gQueue; }
    gfx::IFramebufferLayout* getFrameBufferLayout() { return gFramebufferLayout; }
    gfx::ISwapchain* getSwapChain() { return gSwapchain; }
    gfx::IRenderPassLayout* getRenderPassLayout() { return gRenderPass; }
    Slang::List<Slang::ComPtr<gfx::IFramebuffer>>& getFrameBuffers() { return gFramebuffers; }
    Slang::List<Slang::ComPtr<gfx::ITransientResourceHeap>>& getTransientHeaps()
    {
        return gTransientHeaps;
    }

    ComPtr<gfx::IPipelineState> m_pipelineState;
    ComPtr<gfx::IBufferResource> m_vertexBuffer;

    /// A counter such that we can make aftermath dump file names unique
    std::atomic<int> m_uniqueId = 0;
};

void AftermathCrashExample::diagnoseIfNeeded(slang::IBlob* diagnosticsBlob)
{
    if (diagnosticsBlob != nullptr)
    {
        printf("%s", (const char*)diagnosticsBlob->getBufferPointer());
    }
}

void AftermathCrashExample::onAftermathCrash(const void* data, const uint32_t dataSizeInBytes)
{
    // NOTE! This method can be called from *any* thread.
    const auto id = m_uniqueId++;

    // Dump out as a file
    Slang::StringBuilder filename;
    filename << "aftermath-dump-" << id << ".bin";

    File::writeAllBytes(filename, data, dataSizeInBytes);

    // SLANG_BREAKPOINT(0);
}

void AftermathCrashExample::onAftermathDebugInfo(
    const void* gpuCrashDump,
    const uint32_t gpuCrashDumpSize)
{
    const auto id = m_uniqueId++;

    // Dump out as a file
    Slang::StringBuilder filename;
    filename << "aftermath-debug-info-" << id << ".bin";

    File::writeAllBytes(filename, gpuCrashDump, gpuCrashDumpSize);
}

void AftermathCrashExample::onAftermathCrashDescription(
    PFN_GFSDK_Aftermath_AddGpuCrashDumpDescription description)
{
    // Ignore for now
}

void AftermathCrashExample::onAftermathMarker(
    const void* marker,
    void** resolvedMarkerData,
    uint32_t* markerSize)
{
    // Ignore for now
}

struct FileSystemEntry
{
    SlangPathType type; ///< The type of the entry
    String path;        ///< The path to the entr
};

struct CompileProduct
{
    String fileName;         ///< The filename to write the compile product out to
    ComPtr<ISlangBlob> blob; ///< A blob holding the products contents
};

/* Currently the mechanism to access the contents of a compilation that might consist of many
products is through representing the contents as a "file system".

The file system is just a somewhat convenient/simple in memory representation of the compilation
products.

This function transverses the file system and adds everything found into outEntries.
*/
static SlangResult _findFileSystemContents(
    ISlangFileSystemExt* fileSystem,
    const char* rootPath,
    List<FileSystemEntry>& outEntries)
{
    {
        SlangPathType type;
        SLANG_RETURN_ON_FAIL(fileSystem->getPathType(rootPath, &type));
        outEntries.add(FileSystemEntry{type, rootPath});
    }

    // A context used to hold state, when using enumeratePathContents
    struct Context
    {
        List<FileSystemEntry>& entries; // The entries to be accumulated to
        String path;                    // The path being enumerated
    };

    for (Index i = outEntries.getCount() - 1; i < outEntries.getCount(); ++i)
    {
        const auto& entry = outEntries[i];

        // If it's a directory we want to traverse it's contents
        if (entry.type == SLANG_PATH_TYPE_DIRECTORY)
        {
            Context context{outEntries, entry.path};

            fileSystem->enumeratePathContents(
                entry.path.getBuffer(),
                [](SlangPathType pathType, const char* name, void* userData) -> void
                {
                    Context* context = reinterpret_cast<Context*>(userData);

                    const String path = Path::simplify(Path::combine(context->path, name));

                    context->entries.add({pathType, path});
                },
                &context);
        }
    }

    return SLANG_OK;
}

/* This function takes a compile results file system, and finds items that should be written out.

This is somewhat complicated because the names of products from different compilations might have
the same names. So a "prefix" is passed in, and for files that don't have unique names, they are
uniqified via the prefix.

The same product may appear in multiple compilations, for example obfuscated source maps so a
product is not added if there is already a product with the same name */
static SlangResult _addCompileProducts(
    ISlangFileSystemExt* fileSystem,
    const char* prefix,
    List<CompileProduct>& ioProducts)
{
    List<FileSystemEntry> fileSystemEntries;
    SLANG_RETURN_ON_FAIL(_findFileSystemContents(fileSystem, ".", fileSystemEntries));

    for (const auto& fileSystemEntry : fileSystemEntries)
    {
        if (fileSystemEntry.type != SLANG_PATH_TYPE_FILE)
        {
            continue;
        }

        const auto ext = Path::getPathExt(fileSystemEntry.path);

        String outFileName;

        // Some filenames need special handling, and their names are already unique
        // Others will be the same between differen fileSystem that represent the
        // compilation products.
        //
        // Source maps that are obfuscated are unique.
        {
            String inFileName = Path::getFileNameWithoutExt(fileSystemEntry.path);

            // If it's an obfuscated source map, it's name is already unique (it includes the hash)
            const bool isUniqueName =
                (ext == toSlice("map") && inFileName.endsWith(toSlice("-obfuscated")));

            StringBuilder buf;
            // If it's not a uniquename make it unique via the prefix
            if (!isUniqueName)
            {
                // Uniquify with the prefix
                buf << prefix << "-";
            }

            buf << inFileName << "." << ext;
            outFileName = buf;
        }

        // If we have an output filename
        if (outFileName.getLength())
        {
            // And that filename isn't already used
            if (ioProducts.findFirstIndex(
                    [&](const CompileProduct& product) -> bool
                    { return product.fileName == outFileName; }) < 0)
            {
                ComPtr<ISlangBlob> blob;
                SLANG_RETURN_ON_FAIL(
                    fileSystem->loadFile(fileSystemEntry.path.getBuffer(), blob.writeRef()));

                // Add to the results