BsScriptTexture2D.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsScriptTexture2D.h"
  4. #include "BsScriptResourceManager.h"
  5. #include "BsScriptMeta.h"
  6. #include "BsMonoField.h"
  7. #include "BsMonoClass.h"
  8. #include "BsMonoManager.h"
  9. #include "BsTexture.h"
  10. #include "BsPixelUtil.h"
  11. #include "BsException.h"
  12. #include "BsScriptPixelData.h"
  13. #include "BsScriptAsyncOp.h"
  14. #include "BsCoreThread.h"
  15. using namespace std::placeholders;
  16. namespace BansheeEngine
  17. {
  18. ScriptTexture2D::ScriptTexture2D(MonoObject* instance, const HTexture& texture)
  19. :TScriptResource(instance, texture)
  20. {
  21. }
  22. void ScriptTexture2D::initRuntimeData()
  23. {
  24. metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptTexture2D::internal_createInstance);
  25. metaData.scriptClass->addInternalCall("Internal_GetPixels", &ScriptTexture2D::internal_getPixels);
  26. metaData.scriptClass->addInternalCall("Internal_GetGPUPixels", &ScriptTexture2D::internal_getGPUPixels);
  27. metaData.scriptClass->addInternalCall("Internal_SetPixels", &ScriptTexture2D::internal_setPixels);
  28. metaData.scriptClass->addInternalCall("Internal_SetPixelsArray", &ScriptTexture2D::internal_setPixelsArray);
  29. }
  30. void ScriptTexture2D::internal_createInstance(MonoObject* instance, PixelFormat format, UINT32 width,
  31. UINT32 height, TextureUsage usage, UINT32 numSamples, bool hasMipmaps, bool gammaCorrection)
  32. {
  33. int numMips = 0;
  34. if(hasMipmaps)
  35. numMips = PixelUtil::getMaxMipmaps(width, height, 1, format);
  36. HTexture texture = Texture::create(TEX_TYPE_2D, width, height, numMips, format, usage, gammaCorrection, numSamples);
  37. ScriptTexture2D* scriptInstance;
  38. ScriptResourceManager::instance().createScriptResource(instance, texture, &scriptInstance);
  39. }
  40. MonoObject* ScriptTexture2D::internal_getPixels(ScriptTexture2D* thisPtr, UINT32 mipLevel)
  41. {
  42. HTexture texture = thisPtr->getHandle();
  43. UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(0, mipLevel);
  44. PixelDataPtr pixelData = texture->getProperties().allocateSubresourceBuffer(subresourceIdx);
  45. texture->readData(*pixelData, mipLevel);
  46. return ScriptPixelData::create(pixelData);
  47. }
  48. MonoObject* ScriptTexture2D::internal_getGPUPixels(ScriptTexture2D* thisPtr, UINT32 mipLevel)
  49. {
  50. HTexture texture = thisPtr->getHandle();
  51. UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(0, mipLevel);
  52. PixelDataPtr readData = texture->getProperties().allocateSubresourceBuffer(subresourceIdx);
  53. AsyncOp asyncOp = texture->readSubresource(gCoreAccessor(), subresourceIdx, readData);
  54. std::function<MonoObject*(const AsyncOp&, const PixelDataPtr&)> asyncOpToMono =
  55. [&](const AsyncOp& op, const PixelDataPtr& returnValue)
  56. {
  57. return ScriptPixelData::create(returnValue);
  58. };
  59. return ScriptAsyncOp::create(asyncOp, std::bind(asyncOpToMono, _1, readData));
  60. }
  61. void ScriptTexture2D::internal_setPixels(ScriptTexture2D* thisPtr, MonoObject* data, UINT32 mipLevel)
  62. {
  63. ScriptPixelData* scriptPixelData = ScriptPixelData::toNative(data);
  64. if (scriptPixelData != nullptr)
  65. {
  66. HTexture texture = thisPtr->getHandle();
  67. UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(0, mipLevel);
  68. texture->writeSubresource(gCoreAccessor(), subresourceIdx, scriptPixelData->getInternalValue(), false);
  69. }
  70. }
  71. void ScriptTexture2D::internal_setPixelsArray(ScriptTexture2D* thisPtr, MonoArray* colors, UINT32 mipLevel)
  72. {
  73. Color* colorsRaw = (Color*)mono_array_addr_with_size(colors, sizeof(Color), 0);
  74. UINT32 numElements = (UINT32)mono_array_length(colors);
  75. HTexture texture = thisPtr->getHandle();
  76. const TextureProperties& props = texture->getProperties();
  77. PixelDataPtr pixelData = bs_shared_ptr_new<PixelData>(props.getWidth(), props.getHeight(), props.getDepth(), props.getFormat());
  78. pixelData->allocateInternalBuffer();
  79. pixelData->setColors(colorsRaw, numElements);
  80. UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(0, mipLevel);
  81. texture->writeSubresource(gCoreAccessor(), subresourceIdx, pixelData, false);
  82. }
  83. MonoObject* ScriptTexture2D::createInstance()
  84. {
  85. return metaData.scriptClass->createInstance();
  86. }
  87. }