DynamicIndexBufferWriter.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #region License
  2. // Copyright 2016 Kastellanos Nikolaos
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using Microsoft.Xna.Framework.Content.Pipeline;
  18. using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
  19. using nkast.Aether.Content.Pipeline.Graphics;
  20. namespace nkast.Aether.Content.Pipeline.Serialization
  21. {
  22. [ContentTypeWriter]
  23. class DynamicIndexBufferWriter : ContentTypeWriter<DynamicIndexBufferContent>
  24. {
  25. protected override void Write(ContentWriter output, DynamicIndexBufferContent buffer)
  26. {
  27. WriteIndexBuffer(output, buffer);
  28. output.Write(buffer.IsWriteOnly);
  29. return;
  30. }
  31. private static void WriteIndexBuffer(ContentWriter output, DynamicIndexBufferContent buffer)
  32. {
  33. // check if the buffer contains values greater than UInt16.MaxValue
  34. bool is16Bit = true;
  35. foreach(int index in buffer)
  36. {
  37. if(index > UInt16.MaxValue)
  38. {
  39. is16Bit = false;
  40. break;
  41. }
  42. }
  43. int stride = (is16Bit) ? 2 : 4;
  44. output.Write(is16Bit); // Is 16 bit
  45. output.Write((UInt32)(buffer.Count*stride)); // Data size
  46. if (is16Bit)
  47. {
  48. foreach (int item in buffer)
  49. output.Write((UInt16)item);
  50. }
  51. else
  52. {
  53. foreach (int item in buffer)
  54. output.Write(item);
  55. }
  56. }
  57. public override string GetRuntimeReader(TargetPlatform targetPlatform)
  58. {
  59. return "nkast.Aether.Graphics.Content.DynamicIndexBufferReader, Aether.Graphics";
  60. }
  61. }
  62. }