TextureView.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Common;
  3. using Ryujinx.Common.Memory;
  4. using Ryujinx.Graphics.GAL;
  5. using System;
  6. namespace Ryujinx.Graphics.OpenGL.Image
  7. {
  8. class TextureView : TextureBase, ITexture, ITextureInfo
  9. {
  10. private readonly OpenGLRenderer _renderer;
  11. private readonly TextureStorage _parent;
  12. public ITextureInfo Storage => _parent;
  13. public int FirstLayer { get; private set; }
  14. public int FirstLevel { get; private set; }
  15. public TextureView(
  16. OpenGLRenderer renderer,
  17. TextureStorage parent,
  18. TextureCreateInfo info,
  19. int firstLayer,
  20. int firstLevel) : base(info, parent.ScaleFactor)
  21. {
  22. _renderer = renderer;
  23. _parent = parent;
  24. FirstLayer = firstLayer;
  25. FirstLevel = firstLevel;
  26. CreateView();
  27. }
  28. private void CreateView()
  29. {
  30. TextureTarget target = Target.Convert();
  31. FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
  32. PixelInternalFormat pixelInternalFormat;
  33. if (format.IsCompressed)
  34. {
  35. pixelInternalFormat = (PixelInternalFormat)format.PixelFormat;
  36. }
  37. else
  38. {
  39. pixelInternalFormat = format.PixelInternalFormat;
  40. }
  41. int levels = Info.GetLevelsClamped();
  42. GL.TextureView(
  43. Handle,
  44. target,
  45. _parent.Handle,
  46. pixelInternalFormat,
  47. FirstLevel,
  48. levels,
  49. FirstLayer,
  50. Info.GetLayers());
  51. GL.ActiveTexture(TextureUnit.Texture0);
  52. GL.BindTexture(target, Handle);
  53. int[] swizzleRgba = new int[]
  54. {
  55. (int)Info.SwizzleR.Convert(),
  56. (int)Info.SwizzleG.Convert(),
  57. (int)Info.SwizzleB.Convert(),
  58. (int)Info.SwizzleA.Convert()
  59. };
  60. if (Info.Format == Format.A1B5G5R5Unorm)
  61. {
  62. int temp = swizzleRgba[0];
  63. int temp2 = swizzleRgba[1];
  64. swizzleRgba[0] = swizzleRgba[3];
  65. swizzleRgba[1] = swizzleRgba[2];
  66. swizzleRgba[2] = temp2;
  67. swizzleRgba[3] = temp;
  68. }
  69. else if (Info.Format.IsBgr())
  70. {
  71. // Swap B <-> R for BGRA formats, as OpenGL has no support for them
  72. // and we need to manually swap the components on read/write on the GPU.
  73. int temp = swizzleRgba[0];
  74. swizzleRgba[0] = swizzleRgba[2];
  75. swizzleRgba[2] = temp;
  76. }
  77. GL.TexParameter(target, TextureParameterName.TextureSwizzleRgba, swizzleRgba);
  78. int maxLevel = levels - 1;
  79. if (maxLevel < 0)
  80. {
  81. maxLevel = 0;
  82. }
  83. GL.TexParameter(target, TextureParameterName.TextureMaxLevel, maxLevel);
  84. GL.TexParameter(target, TextureParameterName.DepthStencilTextureMode, (int)Info.DepthStencilMode.Convert());
  85. }
  86. public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
  87. {
  88. firstLayer += FirstLayer;
  89. firstLevel += FirstLevel;
  90. return _parent.CreateView(info, firstLayer, firstLevel);
  91. }
  92. public void CopyTo(ITexture destination, int firstLayer, int firstLevel)
  93. {
  94. TextureView destinationView = (TextureView)destination;
  95. bool srcIsMultisample = Target.IsMultisample();
  96. bool dstIsMultisample = destinationView.Target.IsMultisample();
  97. if (dstIsMultisample != srcIsMultisample && Info.Format.IsDepthOrStencil())
  98. {
  99. int layers = Math.Min(Info.GetLayers(), destinationView.Info.GetLayers() - firstLayer);
  100. CopyWithBlitForDepthMS(destinationView, 0, firstLayer, layers);
  101. }
  102. else if (!dstIsMultisample && srcIsMultisample)
  103. {
  104. int layers = Math.Min(Info.GetLayers(), destinationView.Info.GetLayers() - firstLayer);
  105. _renderer.TextureCopyMS.CopyMSToNonMS(this, destinationView, 0, firstLayer, layers);
  106. }
  107. else if (dstIsMultisample && !srcIsMultisample)
  108. {
  109. int layers = Math.Min(Info.GetLayers(), destinationView.Info.GetLayers() - firstLayer);
  110. _renderer.TextureCopyMS.CopyNonMSToMS(this, destinationView, 0, firstLayer, layers);
  111. }
  112. else if (destinationView.Info.BytesPerPixel != Info.BytesPerPixel)
  113. {
  114. int layers = Math.Min(Info.GetLayers(), destinationView.Info.GetLayers() - firstLayer);
  115. int levels = Math.Min(Info.Levels, destinationView.Info.Levels - firstLevel);
  116. _renderer.TextureCopyIncompatible.CopyIncompatibleFormats(this, destinationView, 0, firstLayer, 0, firstLevel, layers, levels);
  117. }
  118. else
  119. {
  120. _renderer.TextureCopy.CopyUnscaled(this, destinationView, 0, firstLayer, 0, firstLevel);
  121. }
  122. }
  123. public void CopyTo(ITexture destination, int srcLayer, int dstLayer, int srcLevel, int dstLevel)
  124. {
  125. TextureView destinationView = (TextureView)destination;
  126. bool srcIsMultisample = Target.IsMultisample();
  127. bool dstIsMultisample = destinationView.Target.IsMultisample();
  128. if (dstIsMultisample != srcIsMultisample && Info.Format.IsDepthOrStencil())
  129. {
  130. CopyWithBlitForDepthMS(destinationView, srcLayer, dstLayer, 1);
  131. }
  132. else if (!dstIsMultisample && srcIsMultisample)
  133. {
  134. _renderer.TextureCopyMS.CopyMSToNonMS(this, destinationView, srcLayer, dstLayer, 1);
  135. }
  136. else if (dstIsMultisample && !srcIsMultisample)
  137. {
  138. _renderer.TextureCopyMS.CopyNonMSToMS(this, destinationView, srcLayer, dstLayer, 1);
  139. }
  140. else if (destinationView.Info.BytesPerPixel != Info.BytesPerPixel)
  141. {
  142. _renderer.TextureCopyIncompatible.CopyIncompatibleFormats(this, destinationView, srcLayer, dstLayer, srcLevel, dstLevel, 1, 1);
  143. }
  144. else
  145. {
  146. _renderer.TextureCopy.CopyUnscaled(this, destinationView, srcLayer, dstLayer, srcLevel, dstLevel, 1, 1);
  147. }
  148. }
  149. private void CopyWithBlitForDepthMS(TextureView destinationView, int srcLayer, int dstLayer, int layers)
  150. {
  151. // This is currently used for multisample <-> non-multisample copies.
  152. // We can't do that with compute because it's not possible to write depth textures on compute.
  153. // It can be done with draws, but we don't have support for saving and restoring the OpenGL state
  154. // for a draw with different state right now.
  155. // This approach uses blit, which causes a resolution loss since some samples will be lost
  156. // in the process.
  157. Extents2D srcRegion = new Extents2D(0, 0, Width, Height);
  158. Extents2D dstRegion = new Extents2D(0, 0, destinationView.Width, destinationView.Height);
  159. if (destinationView.Target.IsMultisample())
  160. {
  161. TextureView intermmediate = _renderer.TextureCopy.IntermediatePool.GetOrCreateWithAtLeast(
  162. Info.Target,
  163. Info.BlockWidth,
  164. Info.BlockHeight,
  165. Info.BytesPerPixel,
  166. Format,
  167. destinationView.Width,
  168. destinationView.Height,
  169. Info.Depth,
  170. 1,
  171. 1);
  172. _renderer.TextureCopy.Copy(this, intermmediate, srcRegion, dstRegion, false);
  173. _renderer.TextureCopy.Copy(intermmediate, destinationView, dstRegion, dstRegion, false, srcLayer, dstLayer, 0, 0, layers, 1);
  174. }
  175. else
  176. {
  177. Target target = Target switch
  178. {
  179. Target.Texture2DMultisample => Target.Texture2D,
  180. Target.Texture2DMultisampleArray => Target.Texture2DArray,
  181. _ => Target
  182. };
  183. TextureView intermmediate = _renderer.TextureCopy.IntermediatePool.GetOrCreateWithAtLeast(
  184. target,
  185. Info.BlockWidth,
  186. Info.BlockHeight,
  187. Info.BytesPerPixel,
  188. Format,
  189. Width,
  190. Height,
  191. Info.Depth,
  192. 1,
  193. 1);
  194. _renderer.TextureCopy.Copy(this, intermmediate, srcRegion, srcRegion, false);
  195. _renderer.TextureCopy.Copy(intermmediate, destinationView, srcRegion, dstRegion, false, srcLayer, dstLayer, 0, 0, layers, 1);
  196. }
  197. }
  198. public void CopyTo(ITexture destination, Extents2D srcRegion, Extents2D dstRegion, bool linearFilter)
  199. {
  200. _renderer.TextureCopy.Copy(this, (TextureView)destination, srcRegion, dstRegion, linearFilter);
  201. }
  202. public unsafe PinnedSpan<byte> GetData()
  203. {
  204. int size = 0;
  205. int levels = Info.GetLevelsClamped();
  206. for (int level = 0; level < levels; level++)
  207. {
  208. size += Info.GetMipSize(level);
  209. }
  210. ReadOnlySpan<byte> data;
  211. if (HwCapabilities.UsePersistentBufferForFlush)
  212. {
  213. data = _renderer.PersistentBuffers.Default.GetTextureData(this, size);
  214. }
  215. else
  216. {
  217. IntPtr target = _renderer.PersistentBuffers.Default.GetHostArray(size);
  218. WriteTo(target);
  219. data = new ReadOnlySpan<byte>(target.ToPointer(), size);
  220. }
  221. if (Format == Format.S8UintD24Unorm)
  222. {
  223. data = FormatConverter.ConvertD24S8ToS8D24(data);
  224. }
  225. return PinnedSpan<byte>.UnsafeFromSpan(data);
  226. }
  227. public unsafe PinnedSpan<byte> GetData(int layer, int level)
  228. {
  229. int size = Info.GetMipSize(level);
  230. if (HwCapabilities.UsePersistentBufferForFlush)
  231. {
  232. return PinnedSpan<byte>.UnsafeFromSpan(_renderer.PersistentBuffers.Default.GetTextureData(this, size, layer, level));
  233. }
  234. else
  235. {
  236. IntPtr target = _renderer.PersistentBuffers.Default.GetHostArray(size);
  237. int offset = WriteTo2D(target, layer, level);
  238. return new PinnedSpan<byte>((byte*)target.ToPointer() + offset, size);
  239. }
  240. }
  241. public void WriteToPbo(int offset, bool forceBgra)
  242. {
  243. WriteTo(IntPtr.Zero + offset, forceBgra);
  244. }
  245. public int WriteToPbo2D(int offset, int layer, int level)
  246. {
  247. return WriteTo2D(IntPtr.Zero + offset, layer, level);
  248. }
  249. private int WriteTo2D(IntPtr data, int layer, int level)
  250. {
  251. TextureTarget target = Target.Convert();
  252. Bind(target, 0);
  253. FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
  254. PixelFormat pixelFormat = format.PixelFormat;
  255. PixelType pixelType = format.PixelType;
  256. if (target == TextureTarget.TextureCubeMap || target == TextureTarget.TextureCubeMapArray)
  257. {
  258. target = TextureTarget.TextureCubeMapPositiveX + (layer % 6);
  259. }
  260. int mipSize = Info.GetMipSize2D(level);
  261. if (format.IsCompressed)
  262. {
  263. GL.GetCompressedTextureSubImage(Handle, level, 0, 0, layer, Math.Max(1, Info.Width >> level), Math.Max(1, Info.Height >> level), 1, mipSize, data);
  264. }
  265. else if (format.PixelFormat != PixelFormat.DepthStencil)
  266. {
  267. GL.GetTextureSubImage(Handle, level, 0, 0, layer, Math.Max(1, Info.Width >> level), Math.Max(1, Info.Height >> level), 1, pixelFormat, pixelType, mipSize, data);
  268. }
  269. else
  270. {
  271. GL.GetTexImage(target, level, pixelFormat, pixelType, data);
  272. // The GL function returns all layers. Must return the offset of the layer we're interested in.
  273. return target switch
  274. {
  275. TextureTarget.TextureCubeMapArray => (layer / 6) * mipSize,
  276. TextureTarget.Texture1DArray => layer * mipSize,
  277. TextureTarget.Texture2DArray => layer * mipSize,
  278. _ => 0
  279. };
  280. }
  281. return 0;
  282. }
  283. private void WriteTo(IntPtr data, bool forceBgra = false)
  284. {
  285. TextureTarget target = Target.Convert();
  286. Bind(target, 0);
  287. FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
  288. PixelFormat pixelFormat = format.PixelFormat;
  289. PixelType pixelType = format.PixelType;
  290. if (forceBgra)
  291. {
  292. if (pixelType == PixelType.UnsignedShort565)
  293. {
  294. pixelType = PixelType.UnsignedShort565Reversed;
  295. }
  296. else if (pixelType == PixelType.UnsignedShort565Reversed)
  297. {
  298. pixelType = PixelType.UnsignedShort565;
  299. }
  300. else
  301. {
  302. pixelFormat = PixelFormat.Bgra;
  303. }
  304. }
  305. int faces = 1;
  306. if (target == TextureTarget.TextureCubeMap)
  307. {
  308. target = TextureTarget.TextureCubeMapPositiveX;
  309. faces = 6;
  310. }
  311. int levels = Info.GetLevelsClamped();
  312. for (int level = 0; level < levels; level++)
  313. {
  314. for (int face = 0; face < faces; face++)
  315. {
  316. int faceOffset = face * Info.GetMipSize2D(level);
  317. if (format.IsCompressed)
  318. {
  319. GL.GetCompressedTexImage(target + face, level, data + faceOffset);
  320. }
  321. else
  322. {
  323. GL.GetTexImage(target + face, level, pixelFormat, pixelType, data + faceOffset);
  324. }
  325. }
  326. data += Info.GetMipSize(level);
  327. }
  328. }
  329. public void SetData(SpanOrArray<byte> data)
  330. {
  331. var dataSpan = data.AsSpan();
  332. if (Format == Format.S8UintD24Unorm)
  333. {
  334. dataSpan = FormatConverter.ConvertS8D24ToD24S8(dataSpan);
  335. }
  336. unsafe
  337. {
  338. fixed (byte* ptr = dataSpan)
  339. {
  340. ReadFrom((IntPtr)ptr, dataSpan.Length);
  341. }
  342. }
  343. }
  344. public void SetData(SpanOrArray<byte> data, int layer, int level)
  345. {
  346. var dataSpan = data.AsSpan();
  347. if (Format == Format.S8UintD24Unorm)
  348. {
  349. dataSpan = FormatConverter.ConvertS8D24ToD24S8(dataSpan);
  350. }
  351. unsafe
  352. {
  353. fixed (byte* ptr = dataSpan)
  354. {
  355. int width = Math.Max(Info.Width >> level, 1);
  356. int height = Math.Max(Info.Height >> level, 1);
  357. ReadFrom2D((IntPtr)ptr, layer, level, 0, 0, width, height);
  358. }
  359. }
  360. }
  361. public void SetData(SpanOrArray<byte> data, int layer, int level, Rectangle<int> region)
  362. {
  363. var dataSpan = data.AsSpan();
  364. if (Format == Format.S8UintD24Unorm)
  365. {
  366. dataSpan = FormatConverter.ConvertS8D24ToD24S8(dataSpan);
  367. }
  368. int wInBlocks = BitUtils.DivRoundUp(region.Width, Info.BlockWidth);
  369. int hInBlocks = BitUtils.DivRoundUp(region.Height, Info.BlockHeight);
  370. unsafe
  371. {
  372. fixed (byte* ptr = dataSpan)
  373. {
  374. ReadFrom2D(
  375. (IntPtr)ptr,
  376. layer,
  377. level,
  378. region.X,
  379. region.Y,
  380. region.Width,
  381. region.Height,
  382. BitUtils.AlignUp(wInBlocks * Info.BytesPerPixel, 4) * hInBlocks);
  383. }
  384. }
  385. }
  386. public void ReadFromPbo(int offset, int size)
  387. {
  388. ReadFrom(IntPtr.Zero + offset, size);
  389. }
  390. public void ReadFromPbo2D(int offset, int layer, int level, int width, int height)
  391. {
  392. ReadFrom2D(IntPtr.Zero + offset, layer, level, 0, 0, width, height);
  393. }
  394. private void ReadFrom2D(IntPtr data, int layer, int level, int x, int y, int width, int height)
  395. {
  396. int mipSize = Info.GetMipSize2D(level);
  397. ReadFrom2D(data, layer, level, x, y, width, height, mipSize);
  398. }
  399. private void ReadFrom2D(IntPtr data, int layer, int level, int x, int y, int width, int height, int mipSize)
  400. {
  401. TextureTarget target = Target.Convert();
  402. Bind(target, 0);
  403. FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
  404. switch (Target)
  405. {
  406. case Target.Texture1D:
  407. if (format.IsCompressed)
  408. {
  409. GL.CompressedTexSubImage1D(
  410. target,
  411. level,
  412. x,
  413. width,
  414. format.PixelFormat,
  415. mipSize,
  416. data);
  417. }
  418. else
  419. {
  420. GL.TexSubImage1D(
  421. target,
  422. level,
  423. x,
  424. width,
  425. format.PixelFormat,
  426. format.PixelType,
  427. data);
  428. }
  429. break;
  430. case Target.Texture1DArray:
  431. if (format.IsCompressed)
  432. {
  433. GL.CompressedTexSubImage2D(
  434. target,
  435. level,
  436. x,
  437. layer,
  438. width,
  439. 1,
  440. format.PixelFormat,
  441. mipSize,
  442. data);
  443. }
  444. else
  445. {
  446. GL.TexSubImage2D(
  447. target,
  448. level,
  449. x,
  450. layer,
  451. width,
  452. 1,
  453. format.PixelFormat,
  454. format.PixelType,
  455. data);
  456. }
  457. break;
  458. case Target.Texture2D:
  459. if (format.IsCompressed)
  460. {
  461. GL.CompressedTexSubImage2D(
  462. target,
  463. level,
  464. x,
  465. y,
  466. width,
  467. height,
  468. format.PixelFormat,
  469. mipSize,
  470. data);
  471. }
  472. else
  473. {
  474. GL.TexSubImage2D(
  475. target,
  476. level,
  477. x,
  478. y,
  479. width,
  480. height,
  481. format.PixelFormat,
  482. format.PixelType,
  483. data);
  484. }
  485. break;
  486. case Target.Texture2DArray:
  487. case Target.Texture3D:
  488. case Target.CubemapArray:
  489. if (format.IsCompressed)
  490. {
  491. GL.CompressedTexSubImage3D(
  492. target,
  493. level,
  494. x,
  495. y,
  496. layer,
  497. width,
  498. height,
  499. 1,
  500. format.PixelFormat,
  501. mipSize,
  502. data);
  503. }
  504. else
  505. {
  506. GL.TexSubImage3D(
  507. target,
  508. level,
  509. x,
  510. y,
  511. layer,
  512. width,
  513. height,
  514. 1,
  515. format.PixelFormat,
  516. format.PixelType,
  517. data);
  518. }
  519. break;
  520. case Target.Cubemap:
  521. if (format.IsCompressed)
  522. {
  523. GL.CompressedTexSubImage2D(
  524. TextureTarget.TextureCubeMapPositiveX + layer,
  525. level,
  526. x,
  527. y,
  528. width,
  529. height,
  530. format.PixelFormat,
  531. mipSize,
  532. data);
  533. }
  534. else
  535. {
  536. GL.TexSubImage2D(
  537. TextureTarget.TextureCubeMapPositiveX + layer,
  538. level,
  539. x,
  540. y,
  541. width,
  542. height,
  543. format.PixelFormat,
  544. format.PixelType,
  545. data);
  546. }
  547. break;
  548. }
  549. }
  550. private void ReadFrom(IntPtr data, int size)
  551. {
  552. TextureTarget target = Target.Convert();
  553. int baseLevel = 0;
  554. // glTexSubImage on cubemap views is broken on Intel, we have to use the storage instead.
  555. if (Target == Target.Cubemap && HwCapabilities.Vendor == HwCapabilities.GpuVendor.IntelWindows)
  556. {
  557. GL.ActiveTexture(TextureUnit.Texture0);
  558. GL.BindTexture(target, Storage.Handle);
  559. baseLevel = FirstLevel;
  560. }
  561. else
  562. {
  563. Bind(target, 0);
  564. }
  565. FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
  566. int width = Info.Width;
  567. int height = Info.Height;
  568. int depth = Info.Depth;
  569. int levels = Info.GetLevelsClamped();
  570. int offset = 0;
  571. for (int level = 0; level < levels; level++)
  572. {
  573. int mipSize = Info.GetMipSize(level);
  574. int endOffset = offset + mipSize;
  575. if ((uint)endOffset > (uint)size)
  576. {
  577. return;
  578. }
  579. switch (Target)
  580. {
  581. case Target.Texture1D:
  582. if (format.IsCompressed)
  583. {
  584. GL.CompressedTexSubImage1D(
  585. target,
  586. level,
  587. 0,
  588. width,
  589. format.PixelFormat,
  590. mipSize,
  591. data);
  592. }
  593. else
  594. {
  595. GL.TexSubImage1D(
  596. target,
  597. level,
  598. 0,
  599. width,
  600. format.PixelFormat,
  601. format.PixelType,
  602. data);
  603. }
  604. break;
  605. case Target.Texture1DArray:
  606. case Target.Texture2D:
  607. if (format.IsCompressed)
  608. {
  609. GL.CompressedTexSubImage2D(
  610. target,
  611. level,
  612. 0,
  613. 0,
  614. width,
  615. height,
  616. format.PixelFormat,
  617. mipSize,
  618. data);
  619. }
  620. else
  621. {
  622. GL.TexSubImage2D(
  623. target,
  624. level,
  625. 0,
  626. 0,
  627. width,
  628. height,
  629. format.PixelFormat,
  630. format.PixelType,
  631. data);
  632. }
  633. break;
  634. case Target.Texture2DArray:
  635. case Target.Texture3D:
  636. case Target.CubemapArray:
  637. if (format.IsCompressed)
  638. {
  639. GL.CompressedTexSubImage3D(
  640. target,
  641. level,
  642. 0,
  643. 0,
  644. 0,
  645. width,
  646. height,
  647. depth,
  648. format.PixelFormat,
  649. mipSize,
  650. data);
  651. }
  652. else
  653. {
  654. GL.TexSubImage3D(
  655. target,
  656. level,
  657. 0,
  658. 0,
  659. 0,
  660. width,
  661. height,
  662. depth,
  663. format.PixelFormat,
  664. format.PixelType,
  665. data);
  666. }
  667. break;
  668. case Target.Cubemap:
  669. int faceOffset = 0;
  670. for (int face = 0; face < 6; face++, faceOffset += mipSize / 6)
  671. {
  672. if (format.IsCompressed)
  673. {
  674. GL.CompressedTexSubImage2D(
  675. TextureTarget.TextureCubeMapPositiveX + face,
  676. baseLevel + level,
  677. 0,
  678. 0,
  679. width,
  680. height,
  681. format.PixelFormat,
  682. mipSize / 6,
  683. data + faceOffset);
  684. }
  685. else
  686. {
  687. GL.TexSubImage2D(
  688. TextureTarget.TextureCubeMapPositiveX + face,
  689. baseLevel + level,
  690. 0,
  691. 0,
  692. width,
  693. height,
  694. format.PixelFormat,
  695. format.PixelType,
  696. data + faceOffset);
  697. }
  698. }
  699. break;
  700. }
  701. data += mipSize;
  702. offset += mipSize;
  703. width = Math.Max(1, width >> 1);
  704. height = Math.Max(1, height >> 1);
  705. if (Target == Target.Texture3D)
  706. {
  707. depth = Math.Max(1, depth >> 1);
  708. }
  709. }
  710. }
  711. public void SetStorage(BufferRange buffer)
  712. {
  713. throw new NotSupportedException();
  714. }
  715. private void DisposeHandles()
  716. {
  717. if (Handle != 0)
  718. {
  719. GL.DeleteTexture(Handle);
  720. Handle = 0;
  721. }
  722. }
  723. /// <summary>
  724. /// Release the view without necessarily disposing the parent if we are the default view.
  725. /// This allows it to be added to the resource pool and reused later.
  726. /// </summary>
  727. public void Release()
  728. {
  729. bool hadHandle = Handle != 0;
  730. if (_parent.DefaultView != this)
  731. {
  732. DisposeHandles();
  733. }
  734. if (hadHandle)
  735. {
  736. _parent.DecrementViewsCount();
  737. }
  738. }
  739. public void Dispose()
  740. {
  741. if (_parent.DefaultView == this)
  742. {
  743. // Remove the default view (us), so that the texture cannot be released to the cache.
  744. _parent.DeleteDefault();
  745. }
  746. Release();
  747. }
  748. }
  749. }