OGLShaderProgram.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../../Container/HashMap.h"
  5. #include "../../Container/RefCounted.h"
  6. #include "../../GraphicsAPI/GPUObject.h"
  7. #include "../../GraphicsAPI/GraphicsDefs.h"
  8. #include "../../GraphicsAPI/ShaderVariation.h"
  9. namespace Urho3D
  10. {
  11. class ConstantBuffer;
  12. class Graphics;
  13. /// Linked shader program on the GPU.
  14. class URHO3D_API ShaderProgram_OGL : public RefCounted, public GPUObject
  15. {
  16. public:
  17. /// Construct.
  18. ShaderProgram_OGL(Graphics* graphics, ShaderVariation* vertexShader, ShaderVariation* pixelShader);
  19. /// Destruct.
  20. ~ShaderProgram_OGL() override;
  21. /// Mark the GPU resource destroyed on context destruction.
  22. void OnDeviceLost() override;
  23. /// Release shader program.
  24. void Release() override;
  25. /// Link the shaders and examine the uniforms and samplers used. Return true if successful.
  26. bool Link();
  27. /// Return the vertex shader.
  28. ShaderVariation* GetVertexShader() const;
  29. /// Return the pixel shader.
  30. ShaderVariation* GetPixelShader() const;
  31. /// Return whether uses a shader parameter.
  32. bool HasParameter(StringHash param) const;
  33. /// Return whether uses a texture unit.
  34. bool HasTextureUnit(TextureUnit unit) const { return useTextureUnits_[unit]; }
  35. /// Return the info for a shader parameter, or null if does not exist.
  36. const ShaderParameter* GetParameter(StringHash param) const;
  37. /// Return linker output.
  38. const String& GetLinkerOutput() const { return linkerOutput_; }
  39. /// Return semantic to vertex attributes location mappings used by the shader.
  40. const HashMap<Pair<i8, i8>, unsigned>& GetVertexAttributes() const { return vertexAttributes_; }
  41. /// Return attribute location use bitmask.
  42. unsigned GetUsedVertexAttributes() const { return usedVertexAttributes_; }
  43. /// Return all constant buffers.
  44. const SharedPtr<ConstantBuffer>* GetConstantBuffers() const { return &constantBuffers_[0]; }
  45. /// Check whether a shader parameter group needs update. Does not actually check whether parameters exist in the shaders.
  46. bool NeedParameterUpdate(ShaderParameterGroup group, const void* source);
  47. /// Clear a parameter source. Affects only the current shader program if appropriate.
  48. void ClearParameterSource(ShaderParameterGroup group);
  49. /// Clear all parameter sources from all shader programs by incrementing the global parameter source framenumber.
  50. static void ClearParameterSources();
  51. /// Clear a global parameter source when constant buffers change.
  52. static void ClearGlobalParameterSource(ShaderParameterGroup group);
  53. private:
  54. /// Vertex shader.
  55. WeakPtr<ShaderVariation> vertexShader_;
  56. /// Pixel shader.
  57. WeakPtr<ShaderVariation> pixelShader_;
  58. /// Shader parameters.
  59. HashMap<StringHash, ShaderParameter> shaderParameters_;
  60. /// Texture unit use.
  61. bool useTextureUnits_[MAX_TEXTURE_UNITS]{};
  62. /// Vertex attributes.
  63. HashMap<Pair<i8, i8>, unsigned> vertexAttributes_;
  64. /// Used vertex attribute location bitmask.
  65. unsigned usedVertexAttributes_{};
  66. /// Constant buffers by binding index.
  67. SharedPtr<ConstantBuffer> constantBuffers_[MAX_SHADER_PARAMETER_GROUPS * 2];
  68. /// Remembered shader parameter sources for individual uniform mode.
  69. const void* parameterSources_[MAX_SHADER_PARAMETER_GROUPS]{};
  70. /// Shader link error string.
  71. String linkerOutput_;
  72. /// Shader parameter source framenumber.
  73. i32 frameNumber_{};
  74. /// Global shader parameter source framenumber.
  75. static i32 globalFrameNumber;
  76. /// Remembered global shader parameter sources for constant buffer mode.
  77. static const void* globalParameterSources[MAX_SHADER_PARAMETER_GROUPS];
  78. };
  79. }