texture_resource.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #if CROWN_DEBUG
  13. #define TEXTUREC_NAME "texturec-debug-"
  14. #elif CROWN_DEVELOPMENT
  15. #define TEXTUREC_NAME "texturec-development-"
  16. #else
  17. #define TEXTUREC_NAME "texturec-release-"
  18. #endif // CROWN_DEBUG
  19. #if CROWN_ARCH_32BIT
  20. #define TEXTUREC_BITS "32"
  21. #elif CROWN_ARCH_64BIT
  22. #define TEXTUREC_BITS "64"
  23. #endif // CROWN_ARCH_32BIT
  24. #if CROWN_PLATFORM_LINUX
  25. #define TEXTUREC_PATH "./" TEXTUREC_NAME "" TEXTUREC_BITS
  26. #elif CROWN_PLATFORM_WINDOWS
  27. #define TEXTUREC_PATH TEXTUREC_NAME "" TEXTUREC_BITS ".exe"
  28. #else
  29. #define TEXTUREC_PATH ""
  30. #endif // CROWN_PLATFORM_LINUX
  31. namespace crown
  32. {
  33. namespace texture_resource
  34. {
  35. void compile(const char* path, CompileOptions& opts)
  36. {
  37. Buffer buf = opts.read(path);
  38. TempAllocator4096 ta;
  39. JsonObject object(ta);
  40. sjson::parse(buf, object);
  41. DynamicString name(ta);
  42. sjson::parse_string(object["source"], name);
  43. const bool generate_mips = sjson::parse_bool(object["generate_mips"]);
  44. const bool is_normalmap = sjson::parse_bool(object["is_normalmap"]);
  45. DynamicString texsrc(ta);
  46. DynamicString texout(ta);
  47. opts.get_absolute_path(name.c_str(), texsrc);
  48. opts.get_absolute_path("texture.ktx", texout);
  49. using namespace string_stream;
  50. StringStream args(ta);
  51. args << " -f " << texsrc.c_str();
  52. args << " -o " << texout.c_str();
  53. args << (generate_mips ? " -m " : "");
  54. args << (is_normalmap ? " -n " : "");
  55. StringStream output(ta);
  56. int exitcode = os::execute_process(TEXTUREC_PATH, c_str(args), output);
  57. RESOURCE_COMPILER_ASSERT(exitcode == 0
  58. , opts
  59. , "Failed to compile texture:\n%s"
  60. , c_str(output)
  61. );
  62. Buffer blob = opts.read(texout.c_str());
  63. opts.delete_file(texout.c_str());
  64. // Write DDS
  65. opts.write(TEXTURE_VERSION);
  66. opts.write(array::size(blob));
  67. opts.write(blob);
  68. }
  69. void* load(File& file, Allocator& a)
  70. {
  71. const u32 file_size = file.size();
  72. file.skip(sizeof(TextureHeader));
  73. const bgfx::Memory* mem = bgfx::alloc(file_size);
  74. file.read(mem->data, file_size - sizeof(TextureHeader));
  75. TextureResource* teximg = (TextureResource*) a.allocate(sizeof(TextureResource));
  76. teximg->mem = mem;
  77. teximg->handle.idx = bgfx::invalidHandle;
  78. return teximg;
  79. }
  80. void online(StringId64 id, ResourceManager& rm)
  81. {
  82. TextureResource* teximg = (TextureResource*) rm.get(TEXTURE_TYPE, id);
  83. teximg->handle = bgfx::createTexture(teximg->mem);
  84. }
  85. void offline(StringId64 id, ResourceManager& rm)
  86. {
  87. TextureResource* teximg = (TextureResource*) rm.get(TEXTURE_TYPE, id);
  88. bgfx::destroyTexture(teximg->handle);
  89. }
  90. void unload(Allocator& a, void* resource)
  91. {
  92. a.deallocate(resource);
  93. }
  94. } // namespace texture_resource
  95. } // namespace crown