GXS.MultisampleImage.pas 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // The graphics engine GLXEngine. The unit of GXScene for Delphi
  3. //
  4. unit GXS.MultisampleImage;
  5. (*
  6. This unit provides support for two new types of "multisample
  7. textures" - two-dimensional and two-dimensional array - as well as
  8. mechanisms to fetch a specific sample from such a texture in a shader,
  9. and to attach such textures to FBOs for rendering.
  10. *)
  11. interface
  12. {$I Stage.Defines.inc}
  13. uses
  14. Winapi.OpenGL,
  15. Winapi.OpenGLext,
  16. System.Classes,
  17. GXS.Context,
  18. GXS.Texture,
  19. GXS.Graphics,
  20. Stage.TextureFormat;
  21. type
  22. TgxMultisampleImage = class(TgxTextureImage)
  23. private
  24. FBitmap: TgxBitmap32;
  25. FSamplesCount: Integer;
  26. FWidth, FHeight, FDepth: Integer;
  27. FFixedSamplesLocation: GLboolean;
  28. procedure SetWidth(val: Integer);
  29. procedure SetHeight(val: Integer);
  30. procedure SetDepth(val: Integer);
  31. procedure SetSamplesCount(val: Integer);
  32. procedure SetFixedSamplesLocation(val: GLboolean);
  33. protected
  34. function GetWidth: Integer; override;
  35. function GetHeight: Integer; override;
  36. function GetDepth: Integer; override;
  37. function GetTextureTarget: TglTextureTarget; override;
  38. public
  39. constructor Create(AOwner: TPersistent); override;
  40. destructor Destroy; override;
  41. procedure Assign(Source: TPersistent); override;
  42. class function IsSelfLoading: Boolean; override;
  43. procedure LoadTexture(AInternalFormat: TglInternalFormat); override;
  44. function GetBitmap32: TgxBitmap32; override;
  45. procedure ReleaseBitmap32; override;
  46. procedure SaveToFile(const fileName: string); override;
  47. procedure LoadFromFile(const fileName: string); override;
  48. class function FriendlyName: string; override;
  49. class function FriendlyDescription: string; override;
  50. property NativeTextureTarget;
  51. published
  52. // Width of the blank image (for memory allocation).
  53. property Width: Integer read GetWidth write SetWidth default 256;
  54. // Width of the blank image (for memory allocation).
  55. property Height: Integer read GetHeight write SetHeight default 256;
  56. property Depth: Integer read GetDepth write SetDepth default 0;
  57. property SamplesCount: Integer read FSamplesCount write SetSamplesCount
  58. default 0;
  59. property FixedSamplesLocation: GLboolean read FFixedSamplesLocation write
  60. SetFixedSamplesLocation;
  61. end;
  62. //------------------------------------------
  63. implementation
  64. //------------------------------------------
  65. // ------------------
  66. // ------------------ TgxMultisampleImage ------------------
  67. // ------------------
  68. constructor TgxMultisampleImage.Create(AOwner: TPersistent);
  69. begin
  70. inherited;
  71. FWidth := 256;
  72. FHeight := 256;
  73. FDepth := 0;
  74. FSamplesCount := 0;
  75. end;
  76. destructor TgxMultisampleImage.Destroy;
  77. begin
  78. ReleaseBitmap32;
  79. inherited Destroy;
  80. end;
  81. procedure TgxMultisampleImage.Assign(Source: TPersistent);
  82. begin
  83. if Assigned(Source) then
  84. begin
  85. if (Source is TgxMultisampleImage) then
  86. begin
  87. FWidth := TgxMultisampleImage(Source).FWidth;
  88. FHeight := TgxMultisampleImage(Source).FHeight;
  89. FDepth := TgxMultisampleImage(Source).FDepth;
  90. FSamplesCount := TgxMultisampleImage(Source).FSamplesCount;
  91. Invalidate;
  92. end
  93. else
  94. inherited;
  95. end
  96. else
  97. inherited;
  98. end;
  99. procedure TgxMultisampleImage.SetWidth(val: Integer);
  100. begin
  101. if val <> FWidth then
  102. begin
  103. FWidth := val;
  104. if FWidth < 1 then
  105. FWidth := 1;
  106. Invalidate;
  107. end;
  108. end;
  109. function TgxMultisampleImage.GetWidth: Integer;
  110. begin
  111. Result := FWidth;
  112. end;
  113. procedure TgxMultisampleImage.SetHeight(val: Integer);
  114. begin
  115. if val <> FHeight then
  116. begin
  117. FHeight := val;
  118. if FHeight < 1 then
  119. FHeight := 1;
  120. Invalidate;
  121. end;
  122. end;
  123. function TgxMultisampleImage.GetHeight: Integer;
  124. begin
  125. Result := FHeight;
  126. end;
  127. function TgxMultisampleImage.GetDepth: Integer;
  128. begin
  129. Result := FDepth;
  130. end;
  131. procedure TgxMultisampleImage.SetDepth(val: Integer);
  132. begin
  133. if val <> FDepth then
  134. begin
  135. FDepth := val;
  136. if FDepth < 0 then
  137. FDepth := 0;
  138. Invalidate;
  139. end;
  140. end;
  141. procedure TgxMultisampleImage.SetSamplesCount(val: Integer);
  142. begin
  143. if val < 0 then
  144. val := 0;
  145. if val <> FSamplesCount then
  146. begin
  147. FSamplesCount := val;
  148. Invalidate;
  149. end;
  150. end;
  151. procedure TgxMultisampleImage.SetFixedSamplesLocation(val: GLboolean);
  152. begin
  153. if val <> FFixedSamplesLocation then
  154. begin
  155. FFixedSamplesLocation := val;
  156. Invalidate;
  157. end;
  158. end;
  159. function TgxMultisampleImage.GetBitmap32: TgxBitmap32;
  160. begin
  161. if not Assigned(FBitmap) then
  162. begin
  163. FBitmap := TgxBitmap32.Create;
  164. FBitmap.Blank := true;
  165. FBitmap.Width := FWidth;
  166. FBitmap.Height := FHeight;
  167. end;
  168. Result := FBitmap;
  169. end;
  170. procedure TgxMultisampleImage.ReleaseBitmap32;
  171. begin
  172. FBitmap.Free;
  173. FBitmap := nil;
  174. end;
  175. procedure TgxMultisampleImage.SaveToFile(const fileName: string);
  176. begin
  177. end;
  178. procedure TgxMultisampleImage.LoadFromFile(const fileName: string);
  179. begin
  180. end;
  181. class function TgxMultisampleImage.FriendlyName: string;
  182. begin
  183. Result := 'Multisample Image';
  184. end;
  185. class function TgxMultisampleImage.FriendlyDescription: string;
  186. begin
  187. Result := 'Image for rendering to texture with antialiasing';
  188. end;
  189. function TgxMultisampleImage.GetTextureTarget: TglTextureTarget;
  190. begin
  191. if fDepth > 0 then
  192. Result := ttTexture2DMultisampleArray
  193. else
  194. Result := ttTexture2DMultisample;
  195. end;
  196. class function TgxMultisampleImage.IsSelfLoading: Boolean;
  197. begin
  198. Result := True;
  199. end;
  200. procedure TgxMultisampleImage.LoadTexture(AInternalFormat: TglInternalFormat);
  201. var
  202. target: TglTextureTarget;
  203. maxSamples, maxSize: GLint;
  204. begin
  205. // Check smaples count range
  206. glGetIntegerv(GL_MAX_SAMPLES, @maxSamples);
  207. if FSamplesCount > maxSamples then
  208. FSamplesCount := maxSamples;
  209. if IsDepthFormat(AInternalFormat) then
  210. begin
  211. glGetIntegerv(GL_MAX_DEPTH_TEXTURE_SAMPLES, @maxSamples);
  212. if FSamplesCount > maxSamples then
  213. FSamplesCount := maxSamples;
  214. end
  215. else
  216. begin
  217. glGetIntegerv(GL_MAX_COLOR_TEXTURE_SAMPLES, @maxSamples);
  218. if FSamplesCount > maxSamples then
  219. FSamplesCount := maxSamples;
  220. end;
  221. // Check texture size
  222. glGetIntegerv(GL_MAX_TEXTURE_SIZE, @maxSize);
  223. if FWidth > maxSize then
  224. FWidth := maxSize;
  225. if FHeight > maxSize then
  226. FHeight := maxSize;
  227. target := NativeTextureTarget;
  228. case target of
  229. ttTexture2DMultisample:
  230. glTexImage2DMultisample(
  231. DecodeTextureTarget(target),
  232. SamplesCount,
  233. InternalFormatToOpenGLFormat(AInternalFormat),
  234. Width,
  235. Height,
  236. FFixedSamplesLocation);
  237. ttTexture2DMultisampleArray:
  238. glTexImage3DMultisample(
  239. DecodeTextureTarget(target),
  240. SamplesCount,
  241. InternalFormatToOpenGLFormat(AInternalFormat),
  242. Width,
  243. Height,
  244. Depth,
  245. FFixedSamplesLocation);
  246. end;
  247. end;
  248. initialization
  249. RegisterTextureImageClass(TgxMultisampleImage);
  250. end.