summaryrefslogtreecommitdiffstats
path: root/FOSS/Python/_uuid.pyd
diff options
context:
space:
mode:
Diffstat (limited to 'FOSS/Python/_uuid.pyd')
0 files changed, 0 insertions, 0 deletions
n60' href='#n60'>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 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
// This example is out of date and currently disabled from build.
// The `gfx` layer has been refactored with a new shader-object model
// that will greatly simplify shader binding and specialization.
// This example should be updated to use the shader-object API in `gfx`.

// main.cpp

//
// This example is much more involved than the `hello-world` example,
// so readers are encouraged to work through the simpler code first
// before diving into this application. We will gloss over parts of
// the code that are similar to the code in `hello-world`, and
// instead focus on the new code that is required to use Slang in
// more advanced ways.
//

// We still need to include the Slang header to use the Slang API
//
#include <slang.h>
#include "slang-com-helper.h"

// We will again make use of a graphics API abstraction
// layer that implements the shader-object idiom based on Slang's
// `ParameterBlock` and `interface` features to simplify shader specialization
// and parameter binding.
//
#include "slang-gfx.h"
#include "tools/gfx-util/shader-cursor.h"
#include "tools/platform/model.h"
#include "tools/platform/vector-math.h"
#include "tools/platform/window.h"
#include "tools/platform/gui.h"
#include "examples/example-base/example-base.h"

#include <map>
#include <sstream>

using namespace gfx;
using Slang::RefObject;
using Slang::RefPtr;

struct RendererContext
{
    IDevice* device;
    slang::IModule* shaderModule;
    slang::ShaderReflection* slangReflection;
    ComPtr<IShaderProgram> shaderProgram;

    slang::TypeReflection* perViewShaderType;
    slang::TypeReflection* perModelShaderType;

    Result init(IDevice* inDevice)
    {
        device = inDevice;
        ComPtr<ISlangBlob> diagnostic;
        shaderModule = device->getSlangSession()->loadModule("shaders", diagnostic.writeRef());
        diagnoseIfNeeded(diagnostic);

        // Compose the shader program for drawing models by combining the shader module
        // and entry points ("vertexMain" and "fragmentMain").
        char const* vertexEntryPointName = "vertexMain";
        ComPtr<slang::IEntryPoint> vertexEntryPoint;
        SLANG_RETURN_ON_FAIL(
            shaderModule->findEntryPointByName(vertexEntryPointName, vertexEntryPoint.writeRef()));

        char const* fragEntryPointName = "fragmentMain";
        ComPtr<slang::IEntryPoint> fragEntryPoint;
        SLANG_RETURN_ON_FAIL(
            shaderModule->findEntryPointByName(fragEntryPointName, fragEntryPoint.writeRef()));

        // At this point we have a few different Slang API objects that represent
        // pieces of our code: `module`, `vertexEntryPoint`, and `fragmentEntryPoint`.
        //
        // A single Slang module could contain many different entry points (e.g.,
        // four vertex entry points, three fragment entry points, and two compute
        // shaders), and before we try to generate output code for our target API
        // we need to identify which entry points we plan to use together.
        //
        // Modules and entry points are both examples of *component types* in the
        // Slang API. The API also provides a way to build a *composite* out of
        // other pieces, and that is what we are going to do with our module
        // and entry points.
        //
        Slang::List<slang::IComponentType*> componentTypes;
        componentTypes.add(shaderModule);
        componentTypes.add(vertexEntryPoint);
        componentTypes.add(fragEntryPoint);

        // Actually creating the composite component type is a single operation
        // on the Slang session, but the operation could potentially fail if
        // something about the composite was invalid (e.g., you are trying to
        // combine multiple copies of the same module), so we need to deal
        // with the possibility of diagnostic output.
        //
        ComPtr<slang::IComponentType> composedProgram;
        ComPtr<ISlangBlob> diagnosticsBlob;
        SlangResult result = device->getSlangSession()->createCompositeComponentType(
            componentTypes.getBuffer(),
            componentTypes.getCount(),
            composedProgram.writeRef(),
            diagnosticsBlob.writeRef());
        diagnoseIfNeeded(diagnosticsBlob);
        SLANG_RETURN_ON_FAIL(result);
        slangReflection = composedProgram->getLayout();

        // At this point, `composedProgram` represents the shader program
        // we want to run, and the compute shader there have been checked.
        // We can create a `gfx::IShaderProgram` object from `composedProgram`
        // so it may be used by the graphics layer.
        gfx::IShaderProgram::Desc programDesc = {};
        programDesc.slangGlobalScope = composedProgram.get();

        shaderProgram = device->createProgram(programDesc);

        // Get other shader types that we will use for creating shader objects.
        perViewShaderType = slangReflection->findTypeByName("PerView");
        perModelShaderType = slangReflection->findTypeByName("PerModel");

        return SLANG_OK;
    }
};

// Our application code has a rudimentary material system,
// to match the `IMaterial` abstraction used in the shade code.
//
struct Material : RefObject
{
    // The key feature of a matrial in our application is that
    // it can provide a shader object that describes it and
    // its parameters. The contents of the shader object will
    // be any colors, textures, etc. that the material needs,
    // while the Slang type that was used to allocate the
    // block will be an implementation of `IMaterial` that
    // provides the evaluation logic for the material.

    // Each subclass of `Material` will provide a routine to
    // create a shader object that stores its shader parameters.
    virtual IShaderObject* createShaderObject(RendererContext* context) = 0;

    // The shader object for a material will be stashed here
    // after it is created.
    ComPtr<IShaderObject> shaderObject;
};

// For now we have only a single implementation of `Material`,
// which corresponds to the `SimpleMaterial` type in our shader
// code.
//
struct SimpleMaterial : Material
{
    glm::vec3   diffuseColor;
    glm::vec3   specularColor;
    float       specularity = 1.0f;

    // Create a shader object that contains the type info and parameter values
    // that represent an instance of `SimpleMaterial`.
    IShaderObject* createShaderObject(RendererContext* context) override
    {
        auto program = context->slangReflection;
        auto shaderType = program->findTypeByName("SimpleMaterial");
        shaderObject = context->device->createShaderObject(shaderType);
        gfx::ShaderCursor cursor(shaderObject);
        cursor["diffuseColor"].setData(&diffuseColor, sizeof(diffuseColor));
        cursor["specularColor"].setData(&specularColor, sizeof(specularColor));
        cursor["specularity"].setData(&specularity, sizeof(specularity));
        return shaderObject.get();
    }
};

// With the `Material` abstraction defined, we can go on to define
// the representation for loaded models that we will use.
//
// A `Model` will own vertex/index buffers, along with a list of meshes,
// while each `Mesh` will own a material and a range of indices.
// For this example we will be loading models from `.obj` files, but
// that is just a simple lowest-common-denominator choice.
//
struct Mesh : RefObject
{
    RefPtr<Material>    material;
    int                 firstIndex;
    int                 indexCount;
};
struct Model : RefObject
{
    typedef platform::ModelLoader::Vertex Vertex;

    ComPtr<IBufferResource>     vertexBuffer;
    ComPtr<IBufferResource>     indexBuffer;
    PrimitiveTopology           primitiveTopology;
    int                         vertexCount;
    int                         indexCount;
    std::vector<RefPtr<Mesh>>   meshes;
};
//
// Loading a model from disk is done with the help of some utility
// code for parsing the `.obj` file format, so that the application
// mostly just registers some callbacks to allocate the objects
// used for its representation.
//
RefPtr<Model> loadModel(
    RendererContext*        context,
    char const*             inputPath,
    platform::ModelLoader::LoadFlags loadFlags = 0,
    float                   scale = 1.0f)
{
    // The model loading interface using a C++ interface of
    // callback functions to handle creating the application-specific
    // representation of meshes, materials, etc.
    //
    struct Callbacks : platform::ModelLoader::ICallbacks
    {
        RendererContext* context;
        // Hold a reference to all material and mesh objects
        // created during loading so that they can be properly
        // freed.
        std::vector<RefPtr<Material>> materials;
        std::vector<RefPtr<Mesh>> meshes;
        void* createMaterial(MaterialData const& data) override
        {
            SimpleMaterial* material = new SimpleMaterial();
            material->diffuseColor = data.diffuseColor;
            material->specularColor = data.specularColor;
            material->specularity = data.specularity;
            material->createShaderObject(context);
            materials.push_back(material);
            return material;
        }

        void* createMesh(MeshData const& data) override
        {
            Mesh* mesh = new Mesh();
            mesh->firstIndex = data.firstIndex;
            mesh->indexCount = data.indexCount;
            mesh->material = (Material*)data.material;
            meshes.push_back(mesh);
            return mesh;
        }

        void* createModel(ModelData const& data) override
        {
            Model* model = new Model();
            model->vertexBuffer = data.vertexBuffer;
            model->indexBuffer = data.indexBuffer;
            model->primitiveTopology = data.primitiveTopology;
            model->vertexCount = data.vertexCount;
            model->indexCount = data.indexCount;

            int meshCount = data.meshCount;
            for (int ii = 0; ii < meshCount; ++ii)
                model->meshes.push_back((Mesh*)data.meshes[ii]);

            return model;
        }
    };
    Callbacks callbacks;
    callbacks.context = context;

    // We instantiate a model loader object and then use it to
    // try and load a model from the chosen path.
    //
    platform::ModelLoader loader;
    loader.device = context->device;
    loader.loadFlags = loadFlags;
    loader.scale = scale;
    loader.callbacks = &callbacks;
    Model* model = nullptr;
    if (SLANG_FAILED(loader.load(inputPath, (void**)&model)))
    {
        log("failed to load '%s'\n", inputPath);
        return nullptr;
    }

    return model;
}

// Along with materials, our application needs to be able to represent
// multiple light sources in the scene. For this task we will use a C++
// inheritance hierarchy rooted at `Light` to match the `ILight`
// interface in Slang.

struct Light : RefObject
{
    // A light must be able to write its state into a shader parameters
    // of the matching Slang type.
    //
    virtual void writeTo(ShaderCursor const& cursor) = 0;

    // Retrieves the shader type for this light object.
    virtual slang::TypeReflection* getShaderType(RendererContext* context) = 0;

    // The shader object for a light will be stashed here
    // after it is created.
//    ComPtr<IShaderObject> shaderObject;
};

// Helper function to retrieve the underlying shader type of `T`.
template<typename T>
slang::TypeReflection* getShaderType(RendererContext* context)
{
    auto program = context->slangReflection;
    auto shaderType = program->findTypeByName(T::getTypeName());
    return shaderType;
}

// We will provide two nearly trivial implementations of `Light` for now,
// to show the kind of application code needed to line up with the corresponding
// types defined in the Slang shader code for this application.

struct DirectionalLight : Light
{
    glm::vec3 direction = normalize(glm::vec3(1));
    glm::vec3 intensity = glm::vec3(1);

    static const char* getTypeName() { return "DirectionalLight"; }

    virtual void writeTo(ShaderCursor const& cursor) override
    {
        cursor["direction"].setData(&direction, sizeof(direction));
        cursor["intensity"].setData(&intensity, sizeof(intensity));
    }

    virtual slang::TypeReflection* getShaderType(RendererContext* context) override
    {
        return ::getShaderType<DirectionalLight>(context);
    }
};

struct PointLight : Light
{
    glm::vec3 position = glm::vec3(0);
    glm::vec3 intensity = glm::vec3(1);

    static const char* getTypeName() { return "PointLight"; }

    virtual void writeTo(ShaderCursor const& cursor) override
    {
        cursor["position"].setData(&position, sizeof(position));
        cursor["intensity"].setData(&intensity, sizeof(intensity));
    }

    virtual slang::TypeReflection* getShaderType(RendererContext* context) override
    {
        return ::getShaderType<PointLight>(context);
    }
};

// Rendering is usually done with collections of lights rather than single
// lights. This application will use a concept of "light environments" to
// group together lights for rendering.
//
// We want to be *able* to specialize our shader code based on the particular
// types of lights in a scene, but we also do not want to over-specialize
// and, e.g., use differnt specialized shaders for a scene with 99 point
// lights vs. 100.
//
// This particular application will use a notion of a "layout" for a lighting
// environment, which specifies the allowed types of lights, and the maximum
// number of lights of each type. Different lighting environment layouts
// will yield different specialized code.

struct LightEnvLayout : public RefObject
{
    // Our lighting environment layout will track layout
    // information for several different arrays: one
    // for each supported light type.
    //
    struct LightArrayLayout : RefObject
    {
        Int maximumCount = 0;
        std::string typeName;
    };
    std::vector<LightArrayLayout> lightArrayLayouts;
    std::map<slang::TypeReflection*, Int> mapLightTypeToArrayIndex;
    slang::TypeReflection* shaderType = nullptr;

    void addLightType(RendererContext* context, slang::TypeReflection* lightType, Int maximumCount)
    {
        Int arrayIndex = (Int)lightArrayLayouts.size();
        LightArrayLayout layout;
        layout.maximumCount = maximumCount;

        // When the user adds a light type `X` to a light-env layout,
        // we need to compute the corresponding Slang type and
        // layout information to use. If only a single light is
        // supported, this will just be the type `X`, while for
        // any other count this will be a `LightArray<X, maximumCount>`
        //
        if (maximumCount <= 1)
        {
            layout.typeName = lightType->getName();
        }
        else
        {
            auto program = context->slangReflection;
            std::stringstream typeNameBuilder;
            typeNameBuilder << "LightArray<" << lightType->getName() << "," << maximumCount
                            << ">";
            layout.typeName = typeNameBuilder.str();
        }

        lightArrayLayouts.push_back(layout);
        mapLightTypeToArrayIndex.insert(std::make_pair(lightType, arrayIndex));
    }

    template<typename T> void addLightType(RendererContext* context, Int maximumCount)
    {
        addLightType(context, getShaderType<T>(context), maximumCount);
    }

    Int getArrayIndexForType(slang::TypeReflection* lightType)
    {
        auto iter = mapLightTypeToArrayIndex.find(lightType);
        if (iter != mapLightTypeToArrayIndex.end())
            return iter->second;

        return -1;
    }
};

// A `LightEnv` follows the structure of a `LightEnvLayout`,
// and provides storage for zero or more lights of various
// different types (up to the limits imposed by the layout).
//
struct LightEnv : public RefObject
{
    // A light environment is always created from a fixed layout
    // in this application, so the constructor allocates an array
    // for the per-light-type data.
    //
    // A more complex example might dynamically determine the
    // layout based on the number of lights of each type active
    // in the scene, with some quantization applied to avoid
    // generating too many shader specializations.
    //
    // Note: the kind of specialization going on here would also
    // be applicable to a deferred or "forward+" renderer, insofar
    // as it sets the bounds on the total set of lights for
    // a scene/frame, while per-tile/-cluster light lists would
    // probably just be indices into the global structure.
    //
    RefPtr<LightEnvLayout> layout;
    RendererContext* context;
    LightEnv(RefPtr<LightEnvLayout> layout, RendererContext* inContext)
        : layout(layout)
        , context(inContext)
    {
        for (auto arrayLayout : layout->lightArrayLayouts)
        {
            RefPtr<LightArray> lightArray = new LightArray();
            lightArray->layout = arrayLayout;
            lightArrays.push_back(lightArray);
        }
    }

    // For each light type, we track the layout information,
    // plus the list of active lights of that type.
    //
    struct LightArray : RefObject
    {
        LightEnvLayout::LightArrayLayout layout;
        std::vector<RefPtr<Light>> lights;
    };
    std::vector<RefPtr<LightArray>> lightArrays;

    RefPtr<LightArray> getArrayForType(slang::TypeReflection* type)
    {
        auto index = layout->getArrayIndexForType(type);
        return lightArrays[index];
    }

    void add(RefPtr<Light> light)
    {
        auto array = getArrayForType(light->getShaderType(context));
        array->lights.push_back(light);
    }

    // Get the proper shader type that represents this lighting environment.
    slang::TypeReflection* getShaderType()
    {
        // Given a lighting environment with N light types:
        //
        // L0, L1, ... LN
        //
        // We want to compute the Slang type:
        //
        // LightPair<L0, LightPair<L1, ... LightPair<LN-1, LN>>>
        //
        // This is most easily accomplished by doing a "fold" while
        // walking the array in reverse order.

        std::string currentEnvTypeName;
        auto arrayCount = layout->lightArrayLayouts.size();
        for (size_t ii = arrayCount; ii--;)
        {
            auto arrayInfo = layout->lightArrayLayouts[ii];

            if (!currentEnvTypeName.size())
            {
                // The is the right-most entry, so it is the base case for our "fold".
                currentEnvTypeName = arrayInfo.typeName;
            }
            else
            {
                // Fold one entry: `envLayout = LightPair<a, envLayout>`
                std::stringstream typeBuilder;
                typeBuilder << "LightPair<" << arrayInfo.typeName << "," << currentEnvTypeName
                            << ">";
                currentEnvTypeName = typeBuilder.str();
            }
        }

        if (!currentEnvTypeName.size())
        {
            // Handle the special case of *zero* light types.
            currentEnvTypeName = "EmptyLightEnv";
        }
        return context->slangReflection->findTypeByName(currentEnvTypeName.c_str());
    }

    // Because the lighting environment will often change between frames,
    // we will not try to optimize for the case where it doesn't change,
    // and will instead create a "transient" shader object from
    // scratch every frame.
    //
    ComPtr<IShaderObject> createShaderObject()
    {
        auto specializedType = getShaderType();

        auto shaderObject = context->device->createShaderObject(specializedType);
        ShaderCursor cursor(shaderObject);
        // When filling in the shader object for a lighting
        // environment, we mostly follow the structure of
        // the type that was computed by the `LightEnv::getShaderType`:
        //
        //      LightPair<A, LightPair<B, ... LightPair<Y, Z>>>
        //
        // we will keep `encoder` pointed at the "spine" of this
        // structure (so at an element that represents a `LightPair`,
        // except for the special case of the last item like `Z` above).
        //
        // For each light type, we will then encode the data as
        // needed for the light type (`A` then `B` then ...)
        //
        size_t lightTypeCount = lightArrays.size();
        for (size_t tt = 0; tt < lightTypeCount; ++tt)
        {
            // The encoder for the very last item will