| 1 | /* |
| 2 | * Copyright (c) 2019, Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included |
| 12 | * in all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | */ |
| 22 | //! |
| 23 | //! \file vphal_render_hdr_g11.cpp |
| 24 | //! \brief Common interface and structure used in HDR for GEN11 |
| 25 | //! \details Common interface and structure used in HDR for GEN11 Fixed Function Vebox 3DLut |
| 26 | //! |
| 27 | #if !EMUL |
| 28 | |
| 29 | #include "vphal_render_hdr_g11.h" |
| 30 | #include "renderhal_legacy.h" |
| 31 | #include "mos_interface.h" |
| 32 | |
| 33 | static const std::string DumpRoot("C:\\temp\\"); |
| 34 | static const std::string OutputDumpDirectory(DumpRoot + "Output\\"); |
| 35 | static const std::string Hdr3DLutKernelName("hdr_3dlut"); |
| 36 | |
| 37 | static const bool enableDump = false; |
| 38 | static const int32_t lutWidth = 65*2; |
| 39 | static const int32_t lutHeight = 65 * 128; |
| 40 | |
| 41 | static const float ccm_identity[12] = { 1.0f, 0.0f, 0.0f, 0.0f, |
| 42 | 0.0f, 1.0f, 0.0f, 0.0f, |
| 43 | 0.0f, 0.0f, 1.0f, 0.0f }; |
| 44 | |
| 45 | static float color_matrix_calculation[3][4] = {0.0f}; |
| 46 | |
| 47 | template <typename T>void mat_3by3_inv(T *m1, T *m2) |
| 48 | { |
| 49 | double a11, a12, a13, a21, a22, a23, a31, a32, a33; |
| 50 | double det, det_inv; |
| 51 | |
| 52 | a11 = *m1; |
| 53 | a12 = *(m1 + 1); |
| 54 | a13 = *(m1 + 2); |
| 55 | a21 = *(m1 + 3); |
| 56 | a22 = *(m1 + 4); |
| 57 | a23 = *(m1 + 5); |
| 58 | a31 = *(m1 + 6); |
| 59 | a32 = *(m1 + 7); |
| 60 | a33 = *(m1 + 8); |
| 61 | |
| 62 | det = a11 * (a33*a22 - a32 * a23) - a21 * (a33*a12 - a32 * a13) + a31 * (a12*a23 - a13 * a22); |
| 63 | |
| 64 | det_inv = 1 / det; |
| 65 | |
| 66 | *m2 = (float)(det_inv*(a33*a22 - a32 * a23)); |
| 67 | *(m2 + 1) = (float)(-det_inv * (a33*a12 - a32 * a13)); |
| 68 | *(m2 + 2) = (float)(det_inv*(a12*a23 - a13 * a22)); |
| 69 | *(m2 + 3) = (float)(-det_inv * (a33*a21 - a31 * a23)); |
| 70 | *(m2 + 4) = (float)(det_inv*(a33*a11 - a31 * a13)); |
| 71 | *(m2 + 5) = (float)(-det_inv * (a23*a11 - a21 * a13)); |
| 72 | *(m2 + 6) = (float)(det_inv*(a32*a21 - a31 * a22)); |
| 73 | *(m2 + 7) = (float)(-det_inv * (a32*a11 - a31 * a12)); |
| 74 | *(m2 + 8) = (float)(det_inv*(a22*a11 - a21 * a12)); |
| 75 | } |
| 76 | |
| 77 | template <typename T>void mat_mul_float(T *m1, T *m2, T *m3, short m_size, short n_size, short l_size) |
| 78 | { |
| 79 | int i, j, k, Tjn, Tjl, off3; |
| 80 | float *m1_offs, *m2_offs, *m2_k_offs, *m3_offs; |
| 81 | |
| 82 | for (j = 0; j < m_size; j++) |
| 83 | { |
| 84 | Tjn = j * n_size; |
| 85 | Tjl = j * l_size; |
| 86 | for (k = 0; k < l_size; k++) |
| 87 | { |
| 88 | off3 = Tjl + k; |
| 89 | m3_offs = m3 + off3; |
| 90 | *m3_offs = 0; |
| 91 | m2_k_offs = m2 + k; |
| 92 | m1_offs = m1 + Tjn; |
| 93 | for (i = 0; i < n_size; i++) |
| 94 | { |
| 95 | m2_offs = m2_k_offs + i * l_size; |
| 96 | *m3_offs += *m1_offs * *m2_offs; |
| 97 | m1_offs++; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static void CalcCCMMatrix() |
| 104 | { |
| 105 | float mat_rgb2xyz_src[3][3] = {}, mat_rgb2xyz_dst[3][3] = {}; |
| 106 | float mat_xyz[3][3] = {}, mat_invxyz[3][3] = {}; |
| 107 | float matW[3] = {}, matE[3] = {}; |
| 108 | |
| 109 | float fccmSrcZR, fccmSrcZG, fccmSrcZB, fccmSrcZW; |
| 110 | float fccmDstZR, fccmDstZG, fccmDstZB, fccmDstZW; |
| 111 | |
| 112 | const float fccmSrcXR = 0.708000000000000f; |
| 113 | const float fccmSrcYR = 0.292000000000000f; |
| 114 | const float fccmSrcXG = 0.170000000000000f; |
| 115 | const float fccmSrcYG = 0.797000000000000f; |
| 116 | const float fccmSrcXB = 0.131000000000000f; |
| 117 | const float fccmSrcYB = 0.046000000000000f; |
| 118 | const float fccmSrcXW = 0.312700000000000f; |
| 119 | const float fccmSrcYW = 0.329000000000000f; |
| 120 | const float fccmDstXR = 0.640000000000000f; |
| 121 | const float fccmDstYR = 0.330000000000000f; |
| 122 | const float fccmDstXG = 0.300000000000000f; |
| 123 | const float fccmDstYG = 0.600000000000000f; |
| 124 | const float fccmDstXB = 0.150000000000000f; |
| 125 | const float fccmDstYB = 0.060000000000000f; |
| 126 | const float fccmDstXW = 0.312700000000000f; |
| 127 | const float FccmDstYW = 0.329000000000000f; |
| 128 | |
| 129 | fccmSrcZR = 1 - fccmSrcXR - fccmSrcYR; |
| 130 | fccmSrcZG = 1 - fccmSrcXG - fccmSrcYG; |
| 131 | fccmSrcZB = 1 - fccmSrcXB - fccmSrcYB; |
| 132 | fccmSrcZW = 1 - fccmSrcXW - fccmSrcYW; |
| 133 | |
| 134 | mat_xyz[0][0] = fccmSrcXR; |
| 135 | mat_xyz[0][1] = fccmSrcXG; |
| 136 | mat_xyz[0][2] = fccmSrcXB; |
| 137 | mat_xyz[1][0] = fccmSrcYR; |
| 138 | mat_xyz[1][1] = fccmSrcYG; |
| 139 | mat_xyz[1][2] = fccmSrcYB; |
| 140 | mat_xyz[2][0] = fccmSrcZR; |
| 141 | mat_xyz[2][1] = fccmSrcZG; |
| 142 | mat_xyz[2][2] = fccmSrcZB; |
| 143 | |
| 144 | mat_3by3_inv(mat_xyz[0], mat_invxyz[0]); |
| 145 | |
| 146 | matW[0] = fccmSrcXW / fccmSrcYW; |
| 147 | matW[1] = 1; |
| 148 | matW[2] = fccmSrcZW / fccmSrcYW; |
| 149 | |
| 150 | mat_mul_float(mat_invxyz[0], matW, matE, 3, 3, 1); |
| 151 | |
| 152 | mat_rgb2xyz_src[0][0] = matE[0] * fccmSrcXR; |
| 153 | mat_rgb2xyz_src[1][0] = matE[0] * fccmSrcYR; |
| 154 | mat_rgb2xyz_src[2][0] = matE[0] * fccmSrcZR; |
| 155 | mat_rgb2xyz_src[0][1] = matE[1] * fccmSrcXG; |
| 156 | mat_rgb2xyz_src[1][1] = matE[1] * fccmSrcYG; |
| 157 | mat_rgb2xyz_src[2][1] = matE[1] * fccmSrcZG; |
| 158 | mat_rgb2xyz_src[0][2] = matE[2] * fccmSrcXB; |
| 159 | mat_rgb2xyz_src[1][2] = matE[2] * fccmSrcYB; |
| 160 | mat_rgb2xyz_src[2][2] = matE[2] * fccmSrcZB; |
| 161 | |
| 162 | fccmDstZR = 1 - fccmDstXR - fccmDstYR; |
| 163 | fccmDstZG = 1 - fccmDstXG - fccmDstYG; |
| 164 | fccmDstZB = 1 - fccmDstXB - fccmDstYB; |
| 165 | fccmDstZW = 1 - fccmDstXW - FccmDstYW; |
| 166 | |
| 167 | mat_xyz[0][0] = fccmDstXR; |
| 168 | mat_xyz[0][1] = fccmDstXG; |
| 169 | mat_xyz[0][2] = fccmDstXB; |
| 170 | mat_xyz[1][0] = fccmDstYR; |
| 171 | mat_xyz[1][1] = fccmDstYG; |
| 172 | mat_xyz[1][2] = fccmDstYB; |
| 173 | mat_xyz[2][0] = fccmDstZR; |
| 174 | mat_xyz[2][1] = fccmDstZG; |
| 175 | mat_xyz[2][2] = fccmDstZB; |
| 176 | |
| 177 | mat_3by3_inv(mat_xyz[0], mat_invxyz[0]); |
| 178 | |
| 179 | matW[0] = fccmDstXW / FccmDstYW; |
| 180 | matW[1] = 1; |
| 181 | matW[2] = fccmDstZW / FccmDstYW; |
| 182 | |
| 183 | mat_mul_float(mat_invxyz[0], matW, matE, 3, 3, 1); |
| 184 | |
| 185 | mat_rgb2xyz_dst[0][0] = matE[0] * fccmDstXR; |
| 186 | mat_rgb2xyz_dst[1][0] = matE[0] * fccmDstYR; |
| 187 | mat_rgb2xyz_dst[2][0] = matE[0] * fccmDstZR; |
| 188 | mat_rgb2xyz_dst[0][1] = matE[1] * fccmDstXG; |
| 189 | mat_rgb2xyz_dst[1][1] = matE[1] * fccmDstYG; |
| 190 | mat_rgb2xyz_dst[2][1] = matE[1] * fccmDstZG; |
| 191 | mat_rgb2xyz_dst[0][2] = matE[2] * fccmDstXB; |
| 192 | mat_rgb2xyz_dst[1][2] = matE[2] * fccmDstYB; |
| 193 | mat_rgb2xyz_dst[2][2] = matE[2] * fccmDstZB; |
| 194 | |
| 195 | float mat_invrgb2xyz_dst[3][3]; |
| 196 | mat_3by3_inv(mat_rgb2xyz_dst[0], mat_invrgb2xyz_dst[0]); |
| 197 | |
| 198 | float CCMmat[3][3]; |
| 199 | mat_mul_float(mat_invrgb2xyz_dst[0], mat_rgb2xyz_src[0], CCMmat[0], 3, 3, 3); |
| 200 | |
| 201 | color_matrix_calculation[0][0] = CCMmat[0][0]; |
| 202 | color_matrix_calculation[0][1] = CCMmat[0][1]; |
| 203 | color_matrix_calculation[0][2] = CCMmat[0][2]; |
| 204 | color_matrix_calculation[0][3] = 0.0f; |
| 205 | color_matrix_calculation[1][0] = CCMmat[1][0]; |
| 206 | color_matrix_calculation[1][1] = CCMmat[1][1]; |
| 207 | color_matrix_calculation[1][2] = CCMmat[1][2]; |
| 208 | color_matrix_calculation[1][3] = 0.0f; |
| 209 | color_matrix_calculation[2][0] = CCMmat[2][0]; |
| 210 | color_matrix_calculation[2][1] = CCMmat[2][1]; |
| 211 | color_matrix_calculation[2][2] = CCMmat[2][2]; |
| 212 | color_matrix_calculation[2][3] = 0.0f; |
| 213 | } |
| 214 | |
| 215 | Hdr3DLutCmRender::Hdr3DLutCmRender(uint32_t *kernelBinary, uint32_t kernelSize, CmContext *cmContext) : |
| 216 | VPCmRenderer("Hdr3DLutCmRender", cmContext), |
| 217 | m_cmProgram(nullptr), |
| 218 | m_cmKernel(nullptr), |
| 219 | m_cmPayload(nullptr) |
| 220 | { |
| 221 | m_cmProgram = LoadProgram(kernelBinary, kernelSize); |
| 222 | |
| 223 | VPHAL_RENDER_CHK_NULL_NO_STATUS_RETURN(cmContext); |
| 224 | if (!m_cmProgram) |
| 225 | { |
| 226 | VPHAL_RENDER_ASSERTMESSAGE("Hdr3DLutCmRender [%s]: CM LoadProgram error %d\n"); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | CmDevice *dev = cmContext->GetCmDevice(); |
| 231 | int result = dev->CreateKernel(m_cmProgram, _NAME(hdr_3dlut), m_cmKernel); |
| 232 | if (result != CM_SUCCESS) |
| 233 | { |
| 234 | VPHAL_RENDER_ASSERTMESSAGE("Hdr3DLutCmRender [%s]: CM CreateKernel error %d\n", mName.c_str(), result); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | Hdr3DLutCmRender::~Hdr3DLutCmRender() |
| 239 | { |
| 240 | VPHAL_RENDER_CHK_NULL_NO_STATUS_RETURN(m_cmContext); |
| 241 | CmDevice *dev = m_cmContext->GetCmDevice(); |
| 242 | if (m_cmKernel) |
| 243 | { |
| 244 | dev->DestroyKernel(m_cmKernel); |
| 245 | } |
| 246 | |
| 247 | if (m_cmProgram) |
| 248 | { |
| 249 | dev->DestroyProgram(m_cmProgram); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void Hdr3DLutCmRender::AttachPayload(void *payload) |
| 254 | { |
| 255 | m_cmPayload = static_cast<Hdr3DLutPayload *>(payload); |
| 256 | } |
| 257 | |
| 258 | CmKernel* Hdr3DLutCmRender::GetKernelToRun(std::string &name) |
| 259 | { |
| 260 | name = Hdr3DLutKernelName; |
| 261 | return m_cmKernel; |
| 262 | } |
| 263 | |
| 264 | void Hdr3DLutCmRender::GetThreadSpaceDimension(int &tsWidth, int &tsHeight, int &tsColor) |
| 265 | { |
| 266 | #define BLOCK_WIDTH 16 |
| 267 | #define BLOCK_HEIGHT 8 |
| 268 | |
| 269 | tsWidth = (lutWidth + BLOCK_WIDTH - 1) / BLOCK_WIDTH; |
| 270 | tsHeight = (lutHeight + BLOCK_HEIGHT - 1) / BLOCK_HEIGHT; |
| 271 | tsColor = 1; |
| 272 | } |
| 273 | |
| 274 | void Hdr3DLutCmRender::PrepareKernel(CmKernel *kernel) |
| 275 | { |
| 276 | kernel->SetKernelArg(0, sizeof(SurfaceIndex), m_cmPayload->hdr3DLutSurface->GetCmSurfaceIndex()); |
| 277 | kernel->SetKernelArg(1, sizeof(SurfaceIndex), m_cmPayload->hdrCoefSurface->GetCmSurfaceIndex()); |
| 278 | kernel->SetKernelArg(2, sizeof(uint16_t), &m_cmPayload->hdr3DLutSurfaceWidth); |
| 279 | kernel->SetKernelArg(3, sizeof(uint16_t), &m_cmPayload->hdr3DLutSurfaceHeight); |
| 280 | } |
| 281 | |
| 282 | Hdr3DLutGenerator::Hdr3DLutGenerator(PRENDERHAL_INTERFACE renderHal, uint32_t *kernelBinary, uint32_t kernelSize) : |
| 283 | m_renderHal(renderHal), |
| 284 | m_hdr3DLutSurface(nullptr), |
| 285 | m_hdrCoefSurface(nullptr), |
| 286 | m_hdr3DLutCmRender(nullptr), |
| 287 | m_hdrcoefBuffer(nullptr), |
| 288 | m_bHdr3DLutInit(false), |
| 289 | m_savedMaxDLL(1000), |
| 290 | m_savedMaxCLL(4000), |
| 291 | m_savedHdrMode(VPHAL_HDR_MODE_NONE) |
| 292 | { |
| 293 | VPHAL_RENDER_CHK_NULL_NO_STATUS_RETURN(m_renderHal); |
| 294 | VPHAL_RENDER_CHK_NULL_NO_STATUS_RETURN(m_renderHal->pOsInterface); |
| 295 | m_cmContext = MOS_New(CmContext, m_renderHal->pOsInterface); |
| 296 | |
| 297 | VPHAL_RENDER_NORMALMESSAGE("Hdr3DLutGenerator Constructor!"); |
| 298 | |
| 299 | m_kernelBinary = kernelBinary; |
| 300 | m_kernelSize = kernelSize; |
| 301 | } |
| 302 | |
| 303 | Hdr3DLutGenerator::~Hdr3DLutGenerator() |
| 304 | { |
| 305 | FreeResources(); |
| 306 | |
| 307 | MOS_Delete(m_hdr3DLutCmRender); |
| 308 | |
| 309 | MOS_Delete(m_eventManager); |
| 310 | |
| 311 | MOS_Delete(m_cmContext); |
| 312 | |
| 313 | VPHAL_RENDER_NORMALMESSAGE("Hdr3DLutGenerator Destructor!"); |
| 314 | } |
| 315 | |
| 316 | void Hdr3DLutGenerator::AllocateResources() |
| 317 | { |
| 318 | const int32_t coefWidth = 8; |
| 319 | const int32_t coefHeight = 8; |
| 320 | const int32_t lutWidth = 65*2; |
| 321 | const int32_t lutHeight = 65 * 128; |
| 322 | |
| 323 | // Allocate 3DLut buffer in CPU memory to init 3DLut Surface |
| 324 | m_hdr3DLutSysBuffer = MOS_NewArray(uint8_t, m_lutSizeInBytes); |
| 325 | Init3DLutSurface(); |
| 326 | // Allocate 3DLut Surface |
| 327 | VPHAL_RENDER_CHK_NULL_NO_STATUS_RETURN(m_renderHal); |
| 328 | VPHAL_RENDER_CHK_NULL_NO_STATUS_RETURN(m_renderHal->pOsInterface); |
| 329 | m_hdr3DLutSurface = MOS_New(VpCmSurfaceHolder<CmSurface2D>, lutWidth, lutHeight, 1, m_renderHal->pOsInterface->pfnMosFmtToGmmFmt(Format_A8R8G8B8), m_cmContext); |
| 330 | m_hdr3DLutSurface->GetCmSurface()->WriteSurface(m_hdr3DLutSysBuffer, nullptr); |
| 331 | |
| 332 | // Allocate Coefficient Surface in GPU memory |
| 333 | m_hdrCoefSurface = MOS_New(VpCmSurfaceHolder<CmSurface2D>, coefWidth, coefHeight, 1, GMM_FORMAT_B8G8R8A8_UNORM_TYPE, m_cmContext); |
| 334 | // Allocate Coefficient Surface in CPU memory |
| 335 | m_hdrcoefBuffer = MOS_NewArray(float, coefWidth * coefHeight); |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | void Hdr3DLutGenerator::FreeResources() |
| 340 | { |
| 341 | MOS_Delete(m_hdr3DLutSurface); |
| 342 | MOS_Delete(m_hdrCoefSurface); |
| 343 | MOS_DeleteArray(m_hdrcoefBuffer); |
| 344 | MOS_DeleteArray(m_hdr3DLutSysBuffer); |
| 345 | } |
| 346 | |
| 347 | void Hdr3DLutGenerator::Init3DLutSurface() |
| 348 | { |
| 349 | int in_prec = 16; |
| 350 | int max_input_level = ((1 << in_prec) - 1); |
| 351 | int R = 0, G = 0, B = 0; |
| 352 | int lutIndex = 0; |
| 353 | |
| 354 | uint16_t *pLut = (uint16_t *)m_hdr3DLutSysBuffer; |
| 355 | |
| 356 | int lutMul = m_mulSize; |
| 357 | int lutSeg = m_segSize; |
| 358 | for (int rr = 0; rr<lutSeg; rr++) { |
| 359 | for (int gg = 0; gg<lutSeg; gg++) { |
| 360 | for (int bb = 0; bb<lutMul; bb++) { |
| 361 | //--- convert fixed point to floating point |
| 362 | |
| 363 | if (bb >= lutSeg) { |
| 364 | pLut[lutIndex + 0] = 0; |
| 365 | pLut[lutIndex + 1] = 0; |
| 366 | pLut[lutIndex + 2] = 0; |
| 367 | pLut[lutIndex + 3] = 0; |
| 368 | lutIndex += 4; |
| 369 | continue; |
| 370 | } |
| 371 | |
| 372 | if (rr == (lutSeg - 1)) |
| 373 | R = max_input_level; |
| 374 | else |
| 375 | R = rr * ((max_input_level + 1) / (lutSeg - 1)); |
| 376 | |
| 377 | if (gg == (lutSeg - 1)) |
| 378 | G = max_input_level; |
| 379 | else |
| 380 | G = gg * ((max_input_level + 1) / (lutSeg - 1)); |
| 381 | |
| 382 | if (bb == (lutSeg - 1)) |
| 383 | B = max_input_level; |
| 384 | else |
| 385 | B = bb * ((max_input_level + 1) / (lutSeg - 1)); |
| 386 | |
| 387 | pLut[lutIndex + 0] = (unsigned short)R; |
| 388 | pLut[lutIndex + 1] = (unsigned short)G; |
| 389 | pLut[lutIndex + 2] = (unsigned short)B; |
| 390 | pLut[lutIndex + 3] = 0; |
| 391 | lutIndex += 4; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | void Hdr3DLutGenerator::InitCoefSurface(const uint32_t maxDLL, const uint32_t maxCLL, const VPHAL_HDR_MODE hdrMode) |
| 398 | { |
| 399 | int32_t oetfCurve = 0, tmMode = 0, tmSrcType = 0; |
| 400 | float ccmMatrix[12] = { 0.0 }; |
| 401 | float tmMaxCLL = 0.0f, tmMaxDLL = 0.0f; |
| 402 | |
| 403 | tmMaxCLL = (float)maxCLL; |
| 404 | tmMaxDLL = (float)maxDLL; |
| 405 | |
| 406 | if (hdrMode == VPHAL_HDR_MODE_TONE_MAPPING) // H2S |
| 407 | { |
| 408 | CalcCCMMatrix(); |
| 409 | MOS_SecureMemcpy(ccmMatrix, sizeof(float) * 12, color_matrix_calculation, sizeof(float) * 12); |
| 410 | |
| 411 | tmMode = (TONE_MAPPING_MODE)TONE_MAPPING_MODE_H2S; |
| 412 | oetfCurve = (OETF_CURVE_TYPE)OETF_SRGB; |
| 413 | tmSrcType = (TONE_MAPPING_SOURCE_TYPE)TONE_MAPPING_SOURCE_PSEUDO_Y_BT709; |
| 414 | } |
| 415 | else // H2H |
| 416 | { |
| 417 | MOS_SecureMemcpy(ccmMatrix, sizeof(float) * 12, ccm_identity, sizeof(float) * 12); |
| 418 | |
| 419 | tmMode = (TONE_MAPPING_MODE)TONE_MAPPING_MODE_H2H; |
| 420 | oetfCurve = (OETF_CURVE_TYPE)OETF_CURVE_HDR_2084; |
| 421 | tmSrcType = (TONE_MAPPING_SOURCE_TYPE)TONE_MAPPING_SOURCE_PSEUDO_Y_BT709; |
| 422 | } |
| 423 | |
| 424 | // Fill Coefficient Surface: Media kernel define the layout of coefficients. Please don't change it. |
| 425 | const uint32_t pos_coef[17] = { 7, 16, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 29, 54, 55, 62, 63 }; |
| 426 | |
| 427 | // OETF curve |
| 428 | ((int*)m_hdrcoefBuffer)[pos_coef[0]] = oetfCurve; |
| 429 | // CCM |
| 430 | m_hdrcoefBuffer[pos_coef[1]] = ccmMatrix[0]; |
| 431 | m_hdrcoefBuffer[pos_coef[2]] = ccmMatrix[1]; |
| 432 | m_hdrcoefBuffer[pos_coef[3]] = ccmMatrix[2]; |
| 433 | m_hdrcoefBuffer[pos_coef[4]] = ccmMatrix[3]; |
| 434 | m_hdrcoefBuffer[pos_coef[5]] = ccmMatrix[4]; |
| 435 | m_hdrcoefBuffer[pos_coef[6]] = ccmMatrix[5]; |
| 436 | m_hdrcoefBuffer[pos_coef[7]] = ccmMatrix[6]; |
| 437 | m_hdrcoefBuffer[pos_coef[8]] = ccmMatrix[7]; |
| 438 | m_hdrcoefBuffer[pos_coef[9]] = ccmMatrix[8]; |
| 439 | m_hdrcoefBuffer[pos_coef[10]] = ccmMatrix[9]; |
| 440 | m_hdrcoefBuffer[pos_coef[11]] = ccmMatrix[10]; |
| 441 | m_hdrcoefBuffer[pos_coef[12]] = ccmMatrix[11]; |
| 442 | // TM Source Type |
| 443 | ((int*)m_hdrcoefBuffer)[pos_coef[13]] = tmSrcType; |
| 444 | // TM Mode |
| 445 | ((int*)m_hdrcoefBuffer)[pos_coef[14]] = tmMode; |
| 446 | // Max CLL and DLL |
| 447 | m_hdrcoefBuffer[pos_coef[15]] = tmMaxCLL; |
| 448 | m_hdrcoefBuffer[pos_coef[16]] = tmMaxDLL; |
| 449 | } |
| 450 | |
| 451 | void Hdr3DLutGenerator::Render(const uint32_t maxDLL, const uint32_t maxCLL, const VPHAL_HDR_MODE hdrMode, const PVPHAL_SURFACE p3DLutSurface) |
| 452 | { |
| 453 | MOS_STATUS eStatus = MOS_STATUS_SUCCESS; |
| 454 | PMOS_INTERFACE pOsInterface = nullptr; |
| 455 | |
| 456 | VPHAL_RENDER_CHK_NULL_NO_STATUS(m_renderHal); |
| 457 | VPHAL_RENDER_CHK_NULL_NO_STATUS(p3DLutSurface); |
| 458 | |
| 459 | pOsInterface = m_renderHal->pOsInterface; |
| 460 | VPHAL_RENDER_CHK_NULL_NO_STATUS(pOsInterface); |
| 461 | |
| 462 | if (nullptr == m_hdr3DLutCmRender) |
| 463 | { |
| 464 | m_eventManager = MOS_New(EventManager, "EventManager", m_cmContext); |
| 465 | VPHAL_RENDER_CHK_NULL_NO_STATUS(m_cmContext); |
| 466 | |
| 467 | m_hdr3DLutCmRender = MOS_New(Hdr3DLutCmRender, m_kernelBinary, m_kernelSize, m_cmContext); |
| 468 | AllocateResources(); |
| 469 | |
| 470 | VPHAL_RENDER_NORMALMESSAGE("Hdr3DLutGenerator Init Hdr3DLutCmRender and Allocate Necessary Resources!"); |
| 471 | } |
| 472 | |
| 473 | if (maxCLL != m_savedMaxCLL || maxDLL != m_savedMaxDLL || hdrMode != m_savedHdrMode) |
| 474 | { |
| 475 | m_savedMaxCLL = maxCLL; |
| 476 | m_savedMaxDLL = maxDLL; |
| 477 | m_savedHdrMode = hdrMode; |
| 478 | |
| 479 | InitCoefSurface(maxDLL, maxCLL, hdrMode); |
| 480 | m_hdrCoefSurface->GetCmSurface()->WriteSurface((uint8_t*)m_hdrcoefBuffer, nullptr); |
| 481 | |
| 482 | Hdr3DLutCmRender::Hdr3DLutPayload hdr3DLutPayload = { 0 }; |
| 483 | hdr3DLutPayload.hdr3DLutSurface = m_hdr3DLutSurface; |
| 484 | hdr3DLutPayload.hdrCoefSurface = m_hdrCoefSurface; |
| 485 | hdr3DLutPayload.hdr3DLutSurfaceWidth = lutWidth; |
| 486 | hdr3DLutPayload.hdr3DLutSurfaceHeight = lutHeight; |
| 487 | |
| 488 | VPHAL_RENDER_CHK_NULL_NO_STATUS(m_cmContext); |
| 489 | m_cmContext->ConnectEventListener(m_eventManager); |
| 490 | m_hdr3DLutCmRender->Render(&hdr3DLutPayload); |
| 491 | m_cmContext->FlushBatchTask(false); |
| 492 | m_cmContext->ConnectEventListener(nullptr); |
| 493 | |
| 494 | { |
| 495 | MOS_LOCK_PARAMS LockFlags; |
| 496 | LockFlags.NoOverWrite = 1; |
| 497 | uint8_t *pVebox3DLutBuffer = (uint8_t*)m_renderHal->pOsInterface->pfnLockResource( |
| 498 | pOsInterface, |
| 499 | &p3DLutSurface->OsResource, |
| 500 | &LockFlags); |
| 501 | if (pVebox3DLutBuffer) |
| 502 | { |
| 503 | m_hdr3DLutSurface->GetCmSurface()->ReadSurface(pVebox3DLutBuffer, nullptr); |
| 504 | } |
| 505 | else |
| 506 | { |
| 507 | VPHAL_RENDER_NORMALMESSAGE("Hdr3DLutGenerator::Render 3DLut Surface Lock Failed!"); |
| 508 | } |
| 509 | pOsInterface->pfnUnlockResource( |
| 510 | pOsInterface, |
| 511 | &p3DLutSurface->OsResource); |
| 512 | } |
| 513 | |
| 514 | if (enableDump) |
| 515 | { |
| 516 | // Dump 3DLut Surface |
| 517 | int32_t width = 0, height = 0, depth = 0; |
| 518 | m_hdr3DLutSurface->GetSurfaceDimentions(width, height, depth); |
| 519 | m_hdr3DLutSurface->DumpSurfaceToFile(OutputDumpDirectory + "3DLutSurface" + std::to_string(width) + "x" + std::to_string(height) + ".dat"); |
| 520 | // Dump Coefficient Surface(including CCM, Tone Mapping Type etc.) |
| 521 | m_hdrCoefSurface->GetSurfaceDimentions(width, height, depth); |
| 522 | m_hdrCoefSurface->DumpSurfaceToFile(OutputDumpDirectory + "CoffSurface" + std::to_string(width) + "x" + std::to_string(height) + ".dat"); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | VPHAL_RENDER_NORMALMESSAGE("Hdr3DLutGenerator Render maxCLL %d, maxDLL %d, hdrMode: %d!", maxCLL, maxDLL, hdrMode); |
| 527 | |
| 528 | finish: |
| 529 | return; |
| 530 | } |
| 531 | #endif |