BsScriptTexture2D.cpp 4.3 KB

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