Tuesday, November 1, 2016

DirectX Framework Code generation

When developing a DirectX based framework, after reaching the end of wrapping the base classes & methods which basically cleaned up low-level operations, simple usage still required quite a bit of code.

This is where I put together a convenience hierarchy model. This basically consisted of the inheritance model, and for each level, 
the functionality provided and redundancy removed as well as how constructors would look. The purpose was to get the usage fluidity of the framework from where it was to where to where it could be used sanely. When it came to writing out these structures (which there were dozens of), I assumed it could done manually, but after coding only a fraction, it could not be ignored that management would require multiple times the work necessary. Next were template structures, then macros, and even with dreadful amounts of complexity, both failed due to a few minor inconsistencies within the DirectX APIs paired with the impossibility of programmatic decision making. While a subtle thought in my head the entire time, it eventually became the reality that code generation was obligatory.

The below are the first about 500 lines, minus include guards, of a header generated a while ago covering the top-most levels. The entire header is about 8k lines containing about 120 structures that would have been somewhat stressful to manage. 

That excludes the nested Info types which where thrown in when the constructor parameter count exceeded four, as they would do so often in the aggregation of shader resource, render target, and depth buffer creation. Though most had defaults, the event of having to specify would be (hopefully compile-time) error prone due to having to keep up with argument entry order through change where written code uses generated.


This was done with an earlier, less developed version. In that, the code directing the generation was a bit extensive and not very reader friendly so I did not post it. 
Another generation example alongside the written code can be seen here.


#include <Resources/Views/J3DXViewDimensions.h>
#include <Resources/Gadgets/J3DXPipelineGadgets.h>
#include <Resources/Access/J3DXTextureAccess.h>
#include <Resources/Access/J3DXBufferAccess.h>


//========================================================================================================================
//                    J3DXShaderBuffer
//========================================================================================================================
template <class T>
class J3DXShaderBuffer : public J3DXBuffer::MultiStorage<T> {

public:
    J3DXShaderBuffer(const MultiStoreBrief<T> &mainInfo, const ResourceBriefEx &resourceSpecs = Usage::Basic, uint beginElement = 0, J3DXDevice *parentDevice = J3DXDevice::common()):
        MultiStorage(mainInfo, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, beginElement)  
        {
        }
    
    
    J3DXShaderBufferView<T> *view()
      { return &private__view; }
    
    const J3DXShaderBufferView<T> *view() const
      { return &private__view; }
    
    
private:
    J3DXShaderBufferView<T> private__view;
    
    JS_DISABLE_COPY(J3DXShaderBuffer)
    
    
};








//========================================================================================================================
//                    J3DXShaderTex1D
//========================================================================================================================
class J3DXShaderTex1D : public J3DXTexture1D {

public:
    J3DXShaderTex1D(const Tex1DBrief &mainInfo, const TextureBrief &shaderCommon = Format::R8G8B8A8Unorm, const MipRange &viewsMipRange = Range::All, const ResourceBriefEx &resourceSpecs = Usage::Basic, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTexture1D(mainInfo, shaderCommon, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, viewsMipRange)  
        {
        }
    
    
    J3DXShaderTex1D(const QString &filename, const ResourceBriefEx &resourceSpecs = Usage::Fixed, const MipRange &viewsMipRange = Range::All, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTexture1D(filename, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, viewsMipRange)  
        {
        }
    
    
    J3DXShaderTex1DView *view()
      { return &private__view; }
    
    const J3DXShaderTex1DView *view() const
      { return &private__view; }
    
    struct Info {
    
        Info():
            shaderCommon(Format::R8G8B8A8Unorm),
            viewsMipRange(Range::All),
            resourceSpecs(Usage::Basic)  
            {
            }
        
        
        
        TextureBrief shaderCommon;
        MipRange viewsMipRange;
        ResourceBriefEx resourceSpecs;
        
    };
    
    
    
    J3DXShaderTex1D(const Tex1DBrief &mainInfo, const Info &description, J3DXDevice *parentDevice = J3DXDevice::common())
    :J3DXShaderTex1D(mainInfo, description.shaderCommon, description.viewsMipRange, description.resourceSpecs, parentDevice)  
        {
        }
    
    
private:
    J3DXShaderTex1DView private__view;
    
    JS_DISABLE_COPY(J3DXShaderTex1D)
    
    
};




























//========================================================================================================================
//                    J3DXShaderTex1DArray
//========================================================================================================================
class J3DXShaderTex1DArray : public J3DXTexture1DArray {

public:
    J3DXShaderTex1DArray(const Tex1DArrayBrief &mainInfo, const TextureBrief &shaderCommon = Format::R8G8B8A8Unorm, const ShaderViewArrayBrief &textureAndMipRange = ShaderViewArrayBrief::AllMipsOfAllTexures, const ResourceBriefEx &resourceSpecs = Usage::Basic, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTexture1DArray(mainInfo, shaderCommon, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, textureAndMipRange)  
        {
        }
    
    
    J3DXShaderTex1DArray(const QString &filename, const ResourceBriefEx &resourceSpecs = Usage::Fixed, const ShaderViewArrayBrief &textureAndMipRange = ShaderViewArrayBrief::AllMipsOfAllTexures, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTexture1DArray(filename, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, textureAndMipRange)  
        {
        }
    
    
    J3DXShaderTex1DArrayView *view()
      { return &private__view; }
    
    const J3DXShaderTex1DArrayView *view() const
      { return &private__view; }
    
    struct Info {
    
        Info():
            shaderCommon(Format::R8G8B8A8Unorm),
            textureAndMipRange(ShaderViewArrayBrief::AllMipsOfAllTexures),
            resourceSpecs(Usage::Basic)  
            {
            }
        
        
        
        TextureBrief shaderCommon;
        ShaderViewArrayBrief textureAndMipRange;
        ResourceBriefEx resourceSpecs;
        
    };
    
    
    
    J3DXShaderTex1DArray(const Tex1DArrayBrief &mainInfo, const Info &description, J3DXDevice *parentDevice = J3DXDevice::common())
    :J3DXShaderTex1DArray(mainInfo, description.shaderCommon, description.textureAndMipRange, description.resourceSpecs, parentDevice)  
        {
        }
    
    
private:
    J3DXShaderTex1DArrayView private__view;
    
    JS_DISABLE_COPY(J3DXShaderTex1DArray)
    
    
};



























//========================================================================================================================
//                    J3DXShaderTex2D
//========================================================================================================================
class J3DXShaderTex2D : public J3DXTexture2D {

public:
    J3DXShaderTex2D(const Tex2DBrief &mainInfo, const TextureBrief &shaderCommon = Format::R8G8B8A8Unorm, const MipRange &viewsMipRange = Range::All, const ResourceBriefEx &resourceSpecs = Usage::Basic, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTexture2D(mainInfo, shaderCommon, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, viewsMipRange)  
        {
        }
    
    
    J3DXShaderTex2D(const QString &filename, const ResourceBriefEx &resourceSpecs = Usage::Fixed, const MipRange &viewsMipRange = Range::All, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTexture2D(filename, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, viewsMipRange)  
        {
        }
    
    
    J3DXShaderTex2DView *view()
      { return &private__view; }
    
    const J3DXShaderTex2DView *view() const
      { return &private__view; }
    
    struct Info {
    
        Info():
            shaderCommon(Format::R8G8B8A8Unorm),
            viewsMipRange(Range::All),
            resourceSpecs(Usage::Basic)  
            {
            }
        
        
        
        TextureBrief shaderCommon;
        MipRange viewsMipRange;
        ResourceBriefEx resourceSpecs;
        
    };
    
    
    
    J3DXShaderTex2D(const Tex2DBrief &mainInfo, const Info &description, J3DXDevice *parentDevice = J3DXDevice::common())
    :J3DXShaderTex2D(mainInfo, description.shaderCommon, description.viewsMipRange, description.resourceSpecs, parentDevice)  
        {
        }
    
    
private:
    J3DXShaderTex2DView private__view;
    
    JS_DISABLE_COPY(J3DXShaderTex2D)
    
    
};



























//========================================================================================================================
//                    J3DXShaderTex2DArray
//========================================================================================================================
class J3DXShaderTex2DArray : public J3DXTexture2DArray {

public:
    J3DXShaderTex2DArray(const Tex2DArrayBrief &mainInfo, const TextureBrief &shaderCommon = Format::R8G8B8A8Unorm, const ShaderViewArrayBrief &textureAndMipRange = ShaderViewArrayBrief::AllMipsOfAllTexures, const ResourceBriefEx &resourceSpecs = Usage::Basic, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTexture2DArray(mainInfo, shaderCommon, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, textureAndMipRange)  
        {
        }
    
    
    J3DXShaderTex2DArray(const QString &filename, const ResourceBriefEx &resourceSpecs = Usage::Fixed, const ShaderViewArrayBrief &textureAndMipRange = ShaderViewArrayBrief::AllMipsOfAllTexures, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTexture2DArray(filename, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, textureAndMipRange)  
        {
        }
    
    
    J3DXShaderTex2DArray(const QStringList &filenames, const ResourceBriefEx &resourceSpecs = Usage::Basic, const ShaderViewArrayBrief &textureAndMipRange = ShaderViewArrayBrief::AllMipsOfAllTexures, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTexture2DArray(filenames, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, textureAndMipRange)  
        {
        }
    
    
    J3DXShaderTex2DArrayView *view()
      { return &private__view; }
    
    const J3DXShaderTex2DArrayView *view() const
      { return &private__view; }
    
    struct Info {
    
        Info():
            shaderCommon(Format::R8G8B8A8Unorm),
            textureAndMipRange(ShaderViewArrayBrief::AllMipsOfAllTexures),
            resourceSpecs(Usage::Basic)  
            {
            }
        
        
        
        TextureBrief shaderCommon;
        ShaderViewArrayBrief textureAndMipRange;
        ResourceBriefEx resourceSpecs;
        
    };
    
    
    
    J3DXShaderTex2DArray(const Tex2DArrayBrief &mainInfo, const Info &description, J3DXDevice *parentDevice = J3DXDevice::common())
    :J3DXShaderTex2DArray(mainInfo, description.shaderCommon, description.textureAndMipRange, description.resourceSpecs, parentDevice)  
        {
        }
    
    
private:
    J3DXShaderTex2DArrayView private__view;
    
    JS_DISABLE_COPY(J3DXShaderTex2DArray)
    
    
};



























//========================================================================================================================
//                    J3DXShaderTex2DMS
//========================================================================================================================
class J3DXShaderTex2DMS : public J3DXTexture2DMS {

public:
    J3DXShaderTex2DMS(const Tex2DMSBrief &mainInfo, Format shaderFormat = Format::R8G8B8A8Unorm, const ResourceBriefEx &resourceSpecs = Usage::Basic, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTexture2DMS(mainInfo, shaderFormat, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this)  
        {
        }
    
    
    J3DXShaderTex2DMS(const QString &filename, const ResourceBriefEx &resourceSpecs = Usage::Fixed, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTexture2DMS(filename, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this)  
        {
        }
    
    
    J3DXShaderTex2DMSView *view()
      { return &private__view; }
    
    const J3DXShaderTex2DMSView *view() const
      { return &private__view; }
    
    
private:
    J3DXShaderTex2DMSView private__view;
    
    JS_DISABLE_COPY(J3DXShaderTex2DMS)
    
    
};



























//========================================================================================================================
//                    J3DXShaderTex2DMSArray
//========================================================================================================================
class J3DXShaderTex2DMSArray : public J3DXTexture2DMSArray {

public:
    J3DXShaderTex2DMSArray(const Tex2DMSArrayBrief &mainInfo, Format shaderFormat = Format::R8G8B8A8Unorm, const TextureArrayRange &textureUseRange = Range::All, const ResourceBriefEx &resourceSpecs = Usage::Basic, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTexture2DMSArray(mainInfo, shaderFormat, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, textureUseRange)  
        {
        }
    
    
    J3DXShaderTex2DMSArray(const QString &filename, const ResourceBriefEx &resourceSpecs = Usage::Fixed, const TextureArrayRange &textureUseRange = Range::All, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTexture2DMSArray(filename, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, textureUseRange)  
        {
        }
    
    
    J3DXShaderTex2DMSArrayView *view()
      { return &private__view; }
    
    const J3DXShaderTex2DMSArrayView *view() const
      { return &private__view; }
    
    struct Info {
    
        Info():
            shaderFormat(Format::R8G8B8A8Unorm),
            textureUseRange(Range::All),
            resourceSpecs(Usage::Basic)  
            {
            }
        
        
        
        Format shaderFormat;
        TextureArrayRange textureUseRange;
        ResourceBriefEx resourceSpecs;
        
    };
    
    
    
    J3DXShaderTex2DMSArray(const Tex2DMSArrayBrief &mainInfo, const Info &description, J3DXDevice *parentDevice = J3DXDevice::common())
    :J3DXShaderTex2DMSArray(mainInfo, description.shaderFormat, description.textureUseRange, description.resourceSpecs, parentDevice)  
        {
        }
    
    
private:
    J3DXShaderTex2DMSArrayView private__view;
    
    JS_DISABLE_COPY(J3DXShaderTex2DMSArray)
    
    
};



























//========================================================================================================================
//                    J3DXShaderTexCube
//========================================================================================================================
class J3DXShaderTexCube : public J3DXTextureCube {

public:
    J3DXShaderTexCube(const TexCubeBrief &mainInfo, const TextureBrief &shaderCommon = Format::R8G8B8A8Unorm, const MipRange &viewsMipRange = Range::All, const ResourceBriefEx &resourceSpecs = Usage::Basic, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTextureCube(mainInfo, shaderCommon, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, viewsMipRange)  
        {
        }
    
    
    J3DXShaderTexCube(const QString &filename, const ResourceBriefEx &resourceSpecs = Usage::Fixed, const MipRange &viewsMipRange = Range::All, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTextureCube(filename, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, viewsMipRange)  
        {
        }
    
    
    J3DXShaderTexCube(const QStringList &filenames, const ResourceBriefEx &resourceSpecs = Usage::Basic, const MipRange &viewsMipRange = Range::All, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTextureCube(filenames, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, viewsMipRange)  
        {
        }
    
    
    J3DXShaderTexCubeView *view()
      { return &private__view; }
    
    const J3DXShaderTexCubeView *view() const
      { return &private__view; }
    
    struct Info {
    
        Info():
            shaderCommon(Format::R8G8B8A8Unorm),
            viewsMipRange(Range::All),
            resourceSpecs(Usage::Basic)  
            {
            }
        
        
        
        TextureBrief shaderCommon;
        MipRange viewsMipRange;
        ResourceBriefEx resourceSpecs;
        
    };
    
    
    
    J3DXShaderTexCube(const TexCubeBrief &mainInfo, const Info &description, J3DXDevice *parentDevice = J3DXDevice::common())
    :J3DXShaderTexCube(mainInfo, description.shaderCommon, description.viewsMipRange, description.resourceSpecs, parentDevice)  
        {
        }
    
    
private:
    J3DXShaderTexCubeView private__view;
    
    JS_DISABLE_COPY(J3DXShaderTexCube)
    
    
};



























//========================================================================================================================
//                    J3DXShaderTexCubeArray
//========================================================================================================================
class J3DXShaderTexCubeArray : public J3DXTextureCubeArray {

public:
    J3DXShaderTexCubeArray(const TexCubeArrayBrief &mainInfo, const TextureBrief &shaderCommon = Format::R8G8B8A8Unorm, const CubeViewArrayBrief &viewsCubeAndMipRange = CubeViewArrayBrief::AllCubesAndMips, const ResourceBriefEx &resourceSpecs = Usage::Basic, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTextureCubeArray(mainInfo, shaderCommon, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, viewsCubeAndMipRange)  
        {
        }
    
    
    J3DXShaderTexCubeArray(const QString &filename, const ResourceBriefEx &resourceSpecs = Usage::Fixed, const CubeViewArrayBrief &viewsCubeAndMipRange = CubeViewArrayBrief::AllCubesAndMips, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTextureCubeArray(filename, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, viewsCubeAndMipRange)  
        {
        }
    
    
    J3DXShaderTexCubeArray(const QStringList &filenames, const ResourceBriefEx &resourceSpecs = Usage::Basic, const CubeViewArrayBrief &viewsCubeAndMipRange = CubeViewArrayBrief::AllCubesAndMips, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTextureCubeArray(filenames, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, viewsCubeAndMipRange)  
        {
        }
    
    
    J3DXShaderTexCubeArrayView *view()
      { return &private__view; }
    
    const J3DXShaderTexCubeArrayView *view() const
      { return &private__view; }
    
    struct Info {
    
        Info():
            shaderCommon(Format::R8G8B8A8Unorm),
            viewsCubeAndMipRange(CubeViewArrayBrief::AllCubesAndMips),
            resourceSpecs(Usage::Basic)  
            {
            }
        
        
        
        TextureBrief shaderCommon;
        CubeViewArrayBrief viewsCubeAndMipRange;
        ResourceBriefEx resourceSpecs;
        
    };
    
    
    
    J3DXShaderTexCubeArray(const TexCubeArrayBrief &mainInfo, const Info &description, J3DXDevice *parentDevice = J3DXDevice::common())
    :J3DXShaderTexCubeArray(mainInfo, description.shaderCommon, description.viewsCubeAndMipRange, description.resourceSpecs, parentDevice)  
        {
        }
    
    
private:
    J3DXShaderTexCubeArrayView private__view;
    
    JS_DISABLE_COPY(J3DXShaderTexCubeArray)
    
    
};















//========================================================================================================================
//                    J3DXShaderTex3D
//========================================================================================================================
class J3DXShaderTex3D : public J3DXTexture3D {

public:
    J3DXShaderTex3D(const Tex3DBrief &mainInfo, const TextureBrief &shaderCommon = Format::R8G8B8A8Unorm, const MipRange &viewsMipRange = Range::All, const ResourceBriefEx &resourceSpecs = Usage::Basic, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTexture3D(mainInfo, shaderCommon, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, viewsMipRange)  
        {
        }
    
    
    J3DXShaderTex3D(const QString &filename, const ResourceBriefEx &resourceSpecs = Usage::Fixed, const MipRange &viewsMipRange = Range::All, J3DXDevice *parentDevice = J3DXDevice::common()):
        J3DXTexture3D(filename, resourceSpecs.withEnsuredBinds(Bind::ShaderResource), parentDevice),
        private__view(this, viewsMipRange)  
        {
        }
    
    
    J3DXShaderTex3DView *view()
      { return &private__view; }
    
    const J3DXShaderTex3DView *view() const
      { return &private__view; }
    
    struct Info {
    
        Info():
            shaderCommon(Format::R8G8B8A8Unorm),
            viewsMipRange(Range::All),
            resourceSpecs(Usage::Basic)  
            {
            }
        
        
        
        TextureBrief shaderCommon;
        MipRange viewsMipRange;
        ResourceBriefEx resourceSpecs;
        
    };
    
    
    
    J3DXShaderTex3D(const Tex3DBrief &mainInfo, const Info &description, J3DXDevice *parentDevice = J3DXDevice::common())
    :J3DXShaderTex3D(mainInfo, description.shaderCommon, description.viewsMipRange, description.resourceSpecs, parentDevice)  
        {
        }
    
    
private:
    J3DXShaderTex3DView private__view;
    
    JS_DISABLE_COPY(J3DXShaderTex3D)
    
    
};




No comments:

Post a Comment

Pages