texture_resource.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "compile_options.h"
  6. #include "map.h"
  7. #include "reader_writer.h"
  8. #include "resource_manager.h"
  9. #include "sjson.h"
  10. #include "texture_resource.h"
  11. #include "os.h"
  12. #include "string_stream.h"
  13. #if CROWN_DEVELOPMENT
  14. #define TEXTUREC_NAME "texturec-development-"
  15. #elif CROWN_DEBUG
  16. #define TEXTUREC_NAME "texturec-debug-"
  17. #else
  18. #define TEXTUREC_NAME "texturec-release-"
  19. #endif // CROWN_DEBUG
  20. #if CROWN_ARCH_32BIT
  21. #define TEXTUREC_BITS "32"
  22. #elif CROWN_ARCH_64BIT
  23. #define TEXTUREC_BITS "64"
  24. #endif // CROWN_ARCH_32BIT
  25. #if CROWN_PLATFORM_LINUX
  26. #define TEXTUREC_PATH "./" TEXTUREC_NAME "" TEXTUREC_BITS
  27. #elif CROWN_PLATFORM_WINDOWS
  28. #define TEXTUREC_PATH TEXTUREC_NAME "" TEXTUREC_BITS ".exe"
  29. #else
  30. #define TEXTUREC_PATH ""
  31. #endif // CROWN_PLATFORM_LINUX
  32. namespace crown
  33. {
  34. namespace texture_resource
  35. {
  36. void compile(const char* path, CompileOptions& opts)
  37. {
  38. Buffer buf = opts.read(path);
  39. TempAllocator4096 ta;
  40. JsonObject object(ta);
  41. sjson::parse(buf, object);
  42. DynamicString name(ta);
  43. sjson::parse_string(object["source"], name);
  44. RESOURCE_COMPILER_ASSERT_FILE_EXISTS(name.c_str(), opts);
  45. const bool generate_mips = sjson::parse_bool(object["generate_mips"]);
  46. const bool is_normalmap = sjson::parse_bool(object["is_normalmap"]);
  47. DynamicString texsrc(ta);
  48. DynamicString texout(ta);
  49. opts.get_absolute_path(name.c_str(), texsrc);
  50. opts.get_absolute_path("texture.ktx", texout);
  51. StringStream args(ta);
  52. args << " -f " << texsrc.c_str();
  53. args << " -o " << texout.c_str();
  54. args << (generate_mips ? " -m " : "");
  55. args << (is_normalmap ? " -n " : "");
  56. StringStream output(ta);
  57. int exitcode = os::execute_process(TEXTUREC_PATH, string_stream::c_str(args), output);
  58. RESOURCE_COMPILER_ASSERT(exitcode == 0
  59. , opts
  60. , "Failed to compile texture:\n%s"
  61. , string_stream::c_str(output)
  62. );
  63. Buffer blob = opts.read_temporary(texout.c_str());
  64. opts.delete_file(texout.c_str());
  65. // Write DDS
  66. opts.write(RESOURCE_VERSION_TEXTURE);
  67. opts.write(array::size(blob));
  68. opts.write(blob);
  69. }
  70. void* load(File& file, Allocator& a)
  71. {
  72. BinaryReader br(file);
  73. u32 version;
  74. br.read(version);
  75. CE_ASSERT(version == RESOURCE_VERSION_TEXTURE, "Wrong version");
  76. u32 size;
  77. br.read(size);
  78. TextureResource* tr = (TextureResource*)a.allocate(sizeof(TextureResource) + size);
  79. void* data = &tr[1];
  80. br.read(data, size);
  81. tr->mem = bgfx::makeRef(data, size);
  82. tr->handle.idx = bgfx::invalidHandle;
  83. return tr;
  84. }
  85. void online(StringId64 id, ResourceManager& rm)
  86. {
  87. TextureResource* tr = (TextureResource*)rm.get(RESOURCE_TYPE_TEXTURE, id);
  88. tr->handle = bgfx::createTexture(tr->mem);
  89. }
  90. void offline(StringId64 id, ResourceManager& rm)
  91. {
  92. TextureResource* tr = (TextureResource*)rm.get(RESOURCE_TYPE_TEXTURE, id);
  93. bgfx::destroyTexture(tr->handle);
  94. }
  95. void unload(Allocator& a, void* resource)
  96. {
  97. a.deallocate(resource);
  98. }
  99. } // namespace texture_resource
  100. } // namespace crown