BsGpuProgram.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "CoreThread/BsCoreObject.h"
  6. #include "Reflection/BsIReflectable.h"
  7. namespace bs
  8. {
  9. /** @addtogroup RenderAPI
  10. * @{
  11. */
  12. /** Descriptor structure used for initialization of a GpuProgram. */
  13. struct GPU_PROGRAM_DESC
  14. {
  15. String source; /**< Source code to compile the program from. */
  16. String entryPoint; /**< Name of the entry point function, for example "main". */
  17. String language; /**< Language the source is written in, for example "hlsl" or "glsl". */
  18. GpuProgramType type = GPT_VERTEX_PROGRAM; /**< Type of the program, for example vertex or fragment. */
  19. bool requiresAdjacency = false; /**< If true then adjacency information will be provided when rendering. */
  20. };
  21. /** Data describing a GpuProgram. */
  22. class BS_CORE_EXPORT GpuProgramProperties
  23. {
  24. public:
  25. GpuProgramProperties(const String& source, const String& entryPoint, GpuProgramType gptype);
  26. virtual ~GpuProgramProperties() { }
  27. /** Source used for creating this program. */
  28. const String& getSource() const { return mSource; }
  29. /** Type of GPU program (for example fragment, vertex). */
  30. GpuProgramType getType() const { return mType; }
  31. /** Name of the program entry method (for example "main"). */
  32. const String& getEntryPoint() const { return mEntryPoint; }
  33. protected:
  34. friend class GpuProgramRTTI;
  35. GpuProgramType mType;
  36. String mEntryPoint;
  37. String mSource;
  38. };
  39. /**
  40. * Contains a GPU program such as vertex or fragment program which gets compiled from the provided source code.
  41. *
  42. * @note Sim thread only.
  43. */
  44. class BS_CORE_EXPORT GpuProgram : public IReflectable, public CoreObject
  45. {
  46. public:
  47. virtual ~GpuProgram() { }
  48. /**
  49. * Returns true if the program was successfully compiled.
  50. *
  51. * @note Only valid after core thread has initialized the program.
  52. */
  53. bool isCompiled() const;
  54. /**
  55. * Returns an error message returned by the compiler, if the compilation failed.
  56. *
  57. * @note Only valid after core thread has initialized the program.
  58. */
  59. String getCompileErrorMessage() const;
  60. /**
  61. * Returns description of all parameters in this GPU program.
  62. *
  63. * @note Only valid after core thread has initialized the program.
  64. */
  65. SPtr<GpuParamDesc> getParamDesc() const;
  66. /** Retrieves a core implementation of a gpu program usable only from the core thread. */
  67. SPtr<ct::GpuProgram> getCore() const;
  68. /** Returns properties that contain information about the GPU program. */
  69. const GpuProgramProperties& getProperties() const { return mProperties; }
  70. /**
  71. * Creates a new GPU program using the provided source code. If compilation fails or program is not supported
  72. * isCompiled() with return false, and you will be able to retrieve the error message via getCompileErrorMessage().
  73. *
  74. * @param[in] desc Description of the program to create.
  75. */
  76. static SPtr<GpuProgram> create(const GPU_PROGRAM_DESC& desc);
  77. protected:
  78. friend class GpuProgramManager;
  79. GpuProgram(const GPU_PROGRAM_DESC& desc);
  80. /** @copydoc CoreObject::createCore */
  81. SPtr<ct::CoreObject> createCore() const override;
  82. protected:
  83. bool mNeedsAdjacencyInfo;
  84. String mLanguage;
  85. GpuProgramProperties mProperties;
  86. /************************************************************************/
  87. /* SERIALIZATION */
  88. /************************************************************************/
  89. public:
  90. friend class GpuProgramRTTI;
  91. static RTTITypeBase* getRTTIStatic();
  92. RTTITypeBase* getRTTI() const override;
  93. };
  94. /** @} */
  95. namespace ct
  96. {
  97. /** @addtogroup RenderAPI-Internal
  98. * @{
  99. */
  100. /**
  101. * Core thread version of a bs::GpuProgram.
  102. *
  103. * @note Core thread only.
  104. */
  105. class BS_CORE_EXPORT GpuProgram : public CoreObject
  106. {
  107. public:
  108. virtual ~GpuProgram() { }
  109. /** Returns whether this program can be supported on the current renderer and hardware. */
  110. virtual bool isSupported() const;
  111. /** Returns true if program was successfully compiled. */
  112. virtual bool isCompiled() const { return mIsCompiled; }
  113. /** Returns an error message returned by the compiler, if the compilation failed. */
  114. virtual String getCompileErrorMessage() const { return mCompileError; }
  115. /**
  116. * Sets whether this geometry program requires adjacency information from the input primitives.
  117. *
  118. * @note Only relevant for geometry programs.
  119. */
  120. virtual void setAdjacencyInfoRequired(bool required) { mNeedsAdjacencyInfo = required; }
  121. /**
  122. * Returns whether this geometry program requires adjacency information from the input primitives.
  123. *
  124. * @note Only relevant for geometry programs.
  125. */
  126. virtual bool isAdjacencyInfoRequired() const { return mNeedsAdjacencyInfo; }
  127. /** @copydoc bs::GpuProgram::getParamDesc */
  128. SPtr<GpuParamDesc> getParamDesc() const { return mParametersDesc; }
  129. /** Returns GPU program input declaration. Only relevant for vertex programs. */
  130. SPtr<VertexDeclaration> getInputDeclaration() const { return mInputDeclaration; }
  131. /** Returns properties that contain information about the GPU program. */
  132. const GpuProgramProperties& getProperties() const { return mProperties; }
  133. /**
  134. * @copydoc bs::GpuProgram::create
  135. * @param[in] deviceMask Mask that determines on which GPU devices should the object be created on.
  136. */
  137. static SPtr<GpuProgram> create(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask = GDF_DEFAULT);
  138. protected:
  139. GpuProgram(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask);
  140. bool mNeedsAdjacencyInfo;
  141. bool mIsCompiled;
  142. String mCompileError;
  143. SPtr<GpuParamDesc> mParametersDesc;
  144. SPtr<VertexDeclaration> mInputDeclaration;
  145. GpuProgramProperties mProperties;
  146. };
  147. /** @} */
  148. }
  149. }