BsScriptTexture2D.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. :TScriptResource(instance, 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. ScriptTexture2D* scriptInstance;
  36. ScriptResourceManager::instance().createScriptResource(instance, texture, &scriptInstance);
  37. }
  38. MonoObject* ScriptTexture2D::internal_getPixels(ScriptTexture2D* thisPtr, UINT32 mipLevel)
  39. {
  40. HTexture texture = thisPtr->getHandle();
  41. UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(0, mipLevel);
  42. PixelDataPtr pixelData = texture->getProperties().allocateSubresourceBuffer(subresourceIdx);
  43. texture->readData(*pixelData, mipLevel);
  44. return ScriptPixelData::create(pixelData);
  45. }
  46. MonoObject* ScriptTexture2D::internal_getGPUPixels(ScriptTexture2D* thisPtr, UINT32 mipLevel)
  47. {
  48. HTexture texture = thisPtr->getHandle();
  49. UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(0, mipLevel);
  50. PixelDataPtr readData = texture->getProperties().allocateSubresourceBuffer(subresourceIdx);
  51. AsyncOp asyncOp = texture->readSubresource(gCoreAccessor(), subresourceIdx, readData);
  52. std::function<MonoObject*(const AsyncOp&, const PixelDataPtr&)> asyncOpToMono =
  53. [&](const AsyncOp& op, const PixelDataPtr& returnValue)
  54. {
  55. return ScriptPixelData::create(returnValue);
  56. };
  57. return ScriptAsyncOp::create(asyncOp, std::bind(asyncOpToMono, _1, readData));
  58. }
  59. void ScriptTexture2D::internal_setPixels(ScriptTexture2D* thisPtr, MonoObject* data, UINT32 mipLevel)
  60. {
  61. ScriptPixelData* scriptPixelData = ScriptPixelData::toNative(data);
  62. if (scriptPixelData != nullptr)
  63. {
  64. HTexture texture = thisPtr->getHandle();
  65. UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(0, mipLevel);
  66. texture->writeSubresource(gCoreAccessor(), subresourceIdx, scriptPixelData->getInternalValue(), false);
  67. }
  68. }
  69. void ScriptTexture2D::internal_setPixelsArray(ScriptTexture2D* thisPtr, MonoArray* colors, UINT32 mipLevel)
  70. {
  71. Color* colorsRaw = (Color*)mono_array_addr_with_size(colors, sizeof(Color), 0);
  72. UINT32 numElements = (UINT32)mono_array_length(colors);
  73. HTexture texture = thisPtr->getHandle();
  74. const TextureProperties& props = texture->getProperties();
  75. PixelDataPtr pixelData = bs_shared_ptr_new<PixelData>(props.getWidth(), props.getHeight(), props.getDepth(), props.getFormat());
  76. pixelData->allocateInternalBuffer();
  77. pixelData->setColors(colorsRaw, numElements);
  78. UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(0, mipLevel);
  79. texture->writeSubresource(gCoreAccessor(), subresourceIdx, pixelData, false);
  80. }
  81. }