nvenc-opengl.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "nvenc-internal.h"
  2. #include "nvenc-helpers.h"
  3. /*
  4. * NVENC implementation using CUDA context and OpenGL textures
  5. */
  6. void cuda_opengl_free(struct nvenc_data *enc)
  7. {
  8. if (!enc->cu_ctx)
  9. return;
  10. cu->cuCtxPushCurrent(enc->cu_ctx);
  11. for (size_t i = 0; i < enc->input_textures.num; i++) {
  12. CUgraphicsResource res_y = enc->input_textures.array[i].res_y;
  13. CUgraphicsResource res_uv = enc->input_textures.array[i].res_uv;
  14. cu->cuGraphicsUnregisterResource(res_y);
  15. cu->cuGraphicsUnregisterResource(res_uv);
  16. }
  17. cu->cuCtxPopCurrent(NULL);
  18. }
  19. /* ------------------------------------------------------------------------- */
  20. /* Actual encoding stuff */
  21. static inline bool get_res_for_tex_ids(struct nvenc_data *enc, GLuint tex_id_y, GLuint tex_id_uv,
  22. CUgraphicsResource *tex_y, CUgraphicsResource *tex_uv)
  23. {
  24. bool success = true;
  25. for (size_t idx = 0; idx < enc->input_textures.num; idx++) {
  26. struct handle_tex *ht = &enc->input_textures.array[idx];
  27. if (ht->tex_id != tex_id_y)
  28. continue;
  29. *tex_y = ht->res_y;
  30. *tex_uv = ht->res_uv;
  31. return success;
  32. }
  33. CU_CHECK(cu->cuGraphicsGLRegisterImage(tex_y, tex_id_y, GL_TEXTURE_2D, CU_GRAPHICS_REGISTER_FLAGS_READ_ONLY))
  34. CU_CHECK(cu->cuGraphicsGLRegisterImage(tex_uv, tex_id_uv, GL_TEXTURE_2D, CU_GRAPHICS_REGISTER_FLAGS_READ_ONLY))
  35. struct handle_tex ht = {tex_id_y, *tex_y, *tex_uv};
  36. da_push_back(enc->input_textures, &ht);
  37. unmap:
  38. if (!success) {
  39. cu->cuGraphicsUnregisterResource(*tex_y);
  40. cu->cuGraphicsUnregisterResource(*tex_uv);
  41. }
  42. return success;
  43. }
  44. static inline bool copy_tex_data(struct nvenc_data *enc, const bool p010, GLuint tex[2], struct nv_cuda_surface *surf)
  45. {
  46. bool success = true;
  47. CUgraphicsResource mapped_tex[2] = {0};
  48. CUarray mapped_cuda;
  49. if (!get_res_for_tex_ids(enc, tex[0], tex[1], &mapped_tex[0], &mapped_tex[1]))
  50. return false;
  51. CU_CHECK(cu->cuGraphicsMapResources(2, mapped_tex, 0))
  52. CUDA_MEMCPY2D m = {0};
  53. m.dstMemoryType = CU_MEMORYTYPE_ARRAY;
  54. m.srcMemoryType = CU_MEMORYTYPE_ARRAY;
  55. m.dstArray = surf->tex;
  56. m.WidthInBytes = p010 ? enc->cx * 2 : enc->cx;
  57. m.Height = enc->cy;
  58. // Map and copy Y texture
  59. CU_CHECK(cu->cuGraphicsSubResourceGetMappedArray(&mapped_cuda, mapped_tex[0], 0, 0));
  60. m.srcArray = mapped_cuda;
  61. CU_CHECK(cu->cuMemcpy2D(&m))
  62. // Map and copy UV texture
  63. CU_CHECK(cu->cuGraphicsSubResourceGetMappedArray(&mapped_cuda, mapped_tex[1], 0, 0))
  64. m.srcArray = mapped_cuda;
  65. m.dstY += enc->cy;
  66. m.Height = enc->cy / 2;
  67. CU_CHECK(cu->cuMemcpy2D(&m))
  68. unmap:
  69. cu->cuGraphicsUnmapResources(2, mapped_tex, 0);
  70. return success;
  71. }
  72. bool cuda_opengl_encode(void *data, struct encoder_texture *tex, int64_t pts, uint64_t lock_key, uint64_t *next_key,
  73. struct encoder_packet *packet, bool *received_packet)
  74. {
  75. struct nvenc_data *enc = data;
  76. struct nv_cuda_surface *surf;
  77. struct nv_bitstream *bs;
  78. const bool p010 = obs_encoder_video_tex_active(enc->encoder, VIDEO_FORMAT_P010);
  79. GLuint input_tex[2];
  80. if (tex == NULL || tex->tex[0] == NULL) {
  81. error("Encode failed: bad texture handle");
  82. *next_key = lock_key;
  83. return false;
  84. }
  85. bs = &enc->bitstreams.array[enc->next_bitstream];
  86. surf = &enc->surfaces.array[enc->next_bitstream];
  87. deque_push_back(&enc->dts_list, &pts, sizeof(pts));
  88. /* ------------------------------------ */
  89. /* copy to CUDA data */
  90. CU_FAILED(cu->cuCtxPushCurrent(enc->cu_ctx))
  91. obs_enter_graphics();
  92. input_tex[0] = *(GLuint *)gs_texture_get_obj(tex->tex[0]);
  93. input_tex[1] = *(GLuint *)gs_texture_get_obj(tex->tex[1]);
  94. bool success = copy_tex_data(enc, p010, input_tex, surf);
  95. obs_leave_graphics();
  96. CU_FAILED(cu->cuCtxPopCurrent(NULL))
  97. if (!success)
  98. return false;
  99. /* ------------------------------------ */
  100. /* map output tex so nvenc can use it */
  101. NV_ENC_MAP_INPUT_RESOURCE map = {NV_ENC_MAP_INPUT_RESOURCE_VER};
  102. map.registeredResource = surf->res;
  103. map.mappedBufferFmt = p010 ? NV_ENC_BUFFER_FORMAT_YUV420_10BIT : NV_ENC_BUFFER_FORMAT_NV12;
  104. if (NV_FAILED(nv.nvEncMapInputResource(enc->session, &map)))
  105. return false;
  106. surf->mapped_res = map.mappedResource;
  107. /* ------------------------------------ */
  108. /* do actual encode call */
  109. return nvenc_encode_base(enc, bs, surf->mapped_res, pts, packet, received_packet);
  110. }