Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

142 wiersze
3.6 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #pragma once
  11. //
  12. // The Shader class
  13. // ----------------
  14. //
  15. #include <stdint.h>
  16. #include "entity.h"
  17. /* External declaration for LolFx files. */
  18. #define LOLFX_RESOURCE_DECLARE(name) \
  19. extern "C" char const *lolfx_resource_##name
  20. #define LOLFX_RESOURCE_HELPER(name) #name ".lolfx"
  21. #define LOLFX_RESOURCE_NAME(name) \
  22. LOLFX_RESOURCE_HELPER(name), lolfx_resource_##name
  23. namespace lol
  24. {
  25. /* A safe enum to indicate how a vertex stream is going to be used. For
  26. * now there is only TexCoord and not TexCoord0 TexCoord1 etc. because
  27. * we can always reorganise the vertex declaration for the indices to
  28. * match. If the need arises these enums will be added. */
  29. LOL_SAFE_ENUM(VertexUsage,
  30. Position,
  31. BlendWeight,
  32. BlendIndices,
  33. Normal,
  34. PointSize,
  35. TexCoord,
  36. TexCoordExt,
  37. Tangent,
  38. Binormal,
  39. TessFactor,
  40. PositionT,
  41. Color,
  42. Fog,
  43. Depth,
  44. Sample,
  45. MAX,
  46. );
  47. struct ShaderUniform
  48. {
  49. friend class Shader;
  50. public:
  51. inline ShaderUniform() : flags(0) {}
  52. private:
  53. uintptr_t frag, vert;
  54. /* FIXME: do we really need this to indicate which locations are valid? */
  55. uint32_t flags;
  56. };
  57. struct ShaderAttrib
  58. {
  59. friend class Shader;
  60. friend class VertexDeclaration;
  61. public:
  62. inline ShaderAttrib() : m_flags((uint64_t)0 - 1) {}
  63. inline bool IsValid() { return m_flags != (uint64_t)0 - 1; }
  64. inline VertexUsage GetUsage() { return VertexUsage((int)(m_flags >> 16) & 0xffff); }
  65. inline int GetIndex() { return (int)(m_flags & 0xffff); }
  66. private:
  67. uint64_t m_flags;
  68. };
  69. struct TextureUniform
  70. {
  71. friend class Shader;
  72. friend class Framebuffer;
  73. friend class Texture;
  74. public:
  75. inline TextureUniform() : m_flags(0) {}
  76. private:
  77. uint64_t m_flags;
  78. #if defined USE_D3D9 || defined _XBOX
  79. uint32_t m_attrib;
  80. #endif
  81. };
  82. class ShaderData;
  83. class Shader : public Entity
  84. {
  85. public:
  86. static Shader *Create(String const &name, String const &code);
  87. static void Destroy(Shader *shader);
  88. int GetAttribCount() const;
  89. ShaderAttrib GetAttribLocation(VertexUsage usage, int index) const;
  90. ShaderUniform GetUniformLocation(char const *uni) const;
  91. void SetUniform(ShaderUniform const &uni, int i);
  92. void SetUniform(ShaderUniform const &uni, ivec2 const &v);
  93. void SetUniform(ShaderUniform const &uni, ivec3 const &v);
  94. void SetUniform(ShaderUniform const &uni, ivec4 const &v);
  95. void SetUniform(ShaderUniform const &uni, float f);
  96. void SetUniform(ShaderUniform const &uni, vec2 const &v);
  97. void SetUniform(ShaderUniform const &uni, vec3 const &v);
  98. void SetUniform(ShaderUniform const &uni, vec4 const &v);
  99. void SetUniform(ShaderUniform const &uni, mat2 const &m);
  100. void SetUniform(ShaderUniform const &uni, mat3 const &m);
  101. void SetUniform(ShaderUniform const &uni, mat4 const &m);
  102. void SetUniform(ShaderUniform const &uni, TextureUniform tex, int index);
  103. void SetUniform(ShaderUniform const &uni, array<float> const &v);
  104. void SetUniform(ShaderUniform const &uni, array<vec2> const &v);
  105. void SetUniform(ShaderUniform const &uni, array<vec3> const &v);
  106. void SetUniform(ShaderUniform const &uni, array<vec4> const &v);
  107. void Bind() const;
  108. void Unbind() const;
  109. protected:
  110. Shader(String const &name, char const *vert, char const *frag);
  111. ~Shader();
  112. private:
  113. ShaderData *data;
  114. };
  115. } /* namespace lol */