Skip to content

Commit

Permalink
shaders/colorspace: refactor icc/3dlut API
Browse files Browse the repository at this point in the history
Renamed from 3DLUT to ICC across the board, and moved to its own
conditionally installed header Also, merged with the internal lcms code,
since it was a bit redundant now.

Mostly to make room for more LUT formats I will be adding, and since
colorspace.h was a bit cluttered.
  • Loading branch information
haasn committed Feb 19, 2021
1 parent 1dda6b1 commit 558cfcb
Show file tree
Hide file tree
Showing 13 changed files with 290 additions and 276 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ routines which libplacebo exports:
- `shaders/custom.h`: Allows directly ingesting custom GLSL logic into the
`pl_shader` abstraction, either as bare GLSL or in [mpv .hook
format](https://mpv.io/manual/master/#options-glsl-shaders).
- `shaders/icc.h`: Shader for ICC profile based color management.
- `shaders/sampling.h`: Shader routines for various algorithms that sample
from images, such as debanding and scaling.

Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ project('libplacebo', ['c', 'cpp'],
license: 'LGPL2.1+',
default_options: ['c_std=c99', 'cpp_std=c++11', 'warning_level=2'],
meson_version: '>=0.51',
version: '3.111.0',
version: '3.112.0',
)

# Version number
Expand Down
4 changes: 4 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
#include "include/libplacebo/swapchain.h"
#include "include/libplacebo/utils/upload.h"

#ifdef PL_HAVE_LCMS
#include "include/libplacebo/shaders/icc.h"
#endif

#ifdef PL_HAVE_VULKAN
#include "include/libplacebo/vulkan.h"
#endif
Expand Down
14 changes: 9 additions & 5 deletions src/include/libplacebo/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ struct pl_render_params {
// as NULL disables dithering.
const struct pl_dither_params *dither_params;

// Configures the settings used to generate a 3DLUT, if required. If NULL,
// defaults to `&pl_3dlut_default_params`.
const struct pl_3dlut_params *lut3d_params;
// Configures the settings used to handle ICC profiles, if required. If
// NULL, defaults to `&pl_icc_default_params`.
const struct pl_icc_params *icc_params;

// Configures the settings used to simulate color blindness, if desired.
// If NULL, this feature is disabled.
Expand Down Expand Up @@ -183,10 +183,10 @@ struct pl_render_params {
// general-purpose ones.
bool disable_builtin_scalers;

// Forces the use of a 3DLUT, even in cases where the use of one is
// Forces the use of an ICC 3DLUT, even in cases where the use of one is
// unnecessary. This is slower, but may improve the quality of the gamut
// reduction step, if one is performed.
bool force_3dlut;
bool force_icc_lut;

// Forces the use of dithering, even when rendering to 16-bit FBOs. This is
// generally pretty pointless because most 16-bit FBOs have high enough
Expand All @@ -197,6 +197,10 @@ struct pl_render_params {
// Completely overrides the use of FBOs, as if there were no renderable
// texture format available. This disables most features.
bool disable_fbos;

// --- Deprecated aliases
const struct pl_icc_params *lut3d_params PL_DEPRECATED; // fallback for `icc_params`
bool force_3dlut PL_DEPRECATED; // fallback for `force_icc_lut`
};

// This contains the default/recommended options for reasonable image quality,
Expand Down
73 changes: 0 additions & 73 deletions src/include/libplacebo/shaders/colorspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
// Color space transformation shaders. These all input and output a color
// value (PL_SHADER_SIG_COLOR).

#include <stdint.h>

#include <libplacebo/colorspace.h>
#include <libplacebo/shaders.h>

Expand Down Expand Up @@ -358,75 +356,4 @@ void pl_shader_dither(struct pl_shader *sh, int new_depth,
struct pl_shader_obj **dither_state,
const struct pl_dither_params *params);

struct pl_3dlut_params {
// The rendering intent to use when computing the color transformation. A
// recommended value is PL_INTENT_RELATIVE_COLORIMETRIC for color-accurate
// video reproduction, or PL_INTENT_PERCEPTUAL for profiles containing
// meaningful perceptual mapping tables.
enum pl_rendering_intent intent;

// The size of the 3DLUT to generate. If left as NULL, these individually
// default to 64, which is the recommended default for all three.
size_t size_r, size_g, size_b;
};

extern const struct pl_3dlut_params pl_3dlut_default_params;

struct pl_3dlut_profile {
// The nominal, closest approximation representation of the color profile,
// as permitted by `pl_color_space` enums. This will be used as a fallback
// in the event that an ICC profile is absent, or that parsing the ICC
// profile fails. This is also that will be returned for the corresponding
// field in `pl_3dlut_result` when the ICC profile is in use.
struct pl_color_space color;

// The ICC profile itself. (Optional)
struct pl_icc_profile profile;
};

struct pl_3dlut_result {
// The source color space. This is the color space that the colors should
// actually be in at the point in time that they're ingested by the 3DLUT.
// This may differ from the `pl_color_space color` specified in the
// `pl_color_profile`. Users should make sure to apply
// `pl_shader_color_map` in order to get the colors into this format before
// applying `pl_shader_3dlut`.
//
// Note: `pl_shader_color_map` is a no-op when the source and destination
// color spaces are the same, so this can safely be used without disturbing
// the colors in the event that an ICC profile is actually in use.
struct pl_color_space src_color;

// The destination color space. This is the color space that the colors
// will (nominally) be in at the time they exit the 3DLUT.
struct pl_color_space dst_color;
};

#ifdef PL_HAVE_LCMS

// Updates/generates a 3DLUT. Returns success. If true, `out` will be updated
// to a struct describing the color space chosen for the input and output of
// the 3DLUT. (See `pl_color_profile`)
// If `params` is NULL, it defaults to &pl_3dlut_default_params.
//
// Note: This function must always be called before `pl_shader_3dlut`, on the
// same `pl_shader` object, The only reason it's separate from `pl_shader_3dlut`
// is to give users a chance to adapt the input colors to the color space
// chosen by the 3DLUT before applying it.
bool pl_3dlut_update(struct pl_shader *sh,
const struct pl_3dlut_profile *src,
const struct pl_3dlut_profile *dst,
struct pl_shader_obj **lut3d,
struct pl_3dlut_result *out,
const struct pl_3dlut_params *params);

// Actually applies a 3DLUT as generated by `pl_3dlut_update`. The reason this
// is separated from `pl_3dlut_update` is so that the user has the chance to
// correctly map the colors into the specified `src_color` space. This should
// be called only on the `pl_shader_obj` previously updated by
// `pl_3dlut_update`, and only when that function returned true.
void pl_3dlut_apply(struct pl_shader *sh, struct pl_shader_obj **lut3d);

#endif // PL_HAVE_LCMS

#endif // LIBPLACEBO_SHADERS_COLORSPACE_H_
117 changes: 117 additions & 0 deletions src/include/libplacebo/shaders/icc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* This file is part of libplacebo.
*
* libplacebo is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* libplacebo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libplacebo. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef LIBPLACEBO_SHADERS_ICC_H_
#define LIBPLACEBO_SHADERS_ICC_H_

// Functions for generating and applying ICC-derived 3DLUTs

#include <libplacebo/colorspace.h>
#include <libplacebo/shaders.h>

// ICC profiles

struct pl_icc_params {
// The rendering intent to use when computing the color transformation. A
// recommended value is PL_INTENT_RELATIVE_COLORIMETRIC for color-accurate
// video reproduction, or PL_INTENT_PERCEPTUAL for profiles containing
// meaningful perceptual mapping tables.
enum pl_rendering_intent intent;

// The size of the 3DLUT to generate. If left as NULL, these individually
// default to 64, which is the recommended default for all three.
size_t size_r, size_g, size_b;
};

extern const struct pl_icc_params pl_icc_default_params;

struct pl_icc_color_space {
// The nominal, closest approximation representation of the color profile,
// as permitted by `pl_color_space` enums. This will be used as a fallback
// in the event that an ICC profile is absent, or that parsing the ICC
// profile fails. This is also that will be returned for the corresponding
// field in `pl_icc_result` when the ICC profile is in use.
struct pl_color_space color;

// The ICC profile itself. (Optional)
struct pl_icc_profile profile;
};

struct pl_icc_result {
// The source color space. This is the color space that the colors should
// actually be in at the point in time that they're ingested by the 3DLUT.
// This may differ from the `pl_color_space color` specified in the
// `pl_icc_color_space`. Users should make sure to apply
// `pl_shader_color_map` in order to get the colors into this format before
// applying `pl_icc_apply`.
//
// Note: `pl_shader_color_map` is a no-op when the source and destination
// color spaces are the same, so this can safely be used without disturbing
// the colors in the event that an ICC profile is actually in use.
struct pl_color_space src_color;

// The destination color space. This is the color space that the colors
// will (nominally) be in at the time they exit the 3DLUT.
struct pl_color_space dst_color;
};

// Updates/generates a 3DLUT based on ICC profiles. Returns success. If true,
// `out` will be updated to a struct describing the color space chosen for the
// input and output of the 3DLUT. (See `pl_icc_color_space`) If `params` is
// NULL, it defaults to &pl_icc_default_params.
//
// Note: This function must always be called before `pl_icc_apply`, on the
// same `pl_shader` object, The only reason it's separate from `pl_icc_apply`
// is to give users a chance to adapt the input colors to the color space
// chosen by the ICC profile before applying it.
bool pl_icc_update(struct pl_shader *sh,
const struct pl_icc_color_space *src,
const struct pl_icc_color_space *dst,
struct pl_shader_obj **icc,
struct pl_icc_result *out,
const struct pl_icc_params *params);

// Actually applies a 3DLUT as generated by `pl_icc_update`. The reason this is
// separated from `pl_icc_update` is so that the user has the chance to
// correctly map the colors into the specified `src_color` space. This should
// be called only on the `pl_shader_obj` previously updated by `pl_icc_update`,
// and only when that function returned true.
void pl_icc_apply(struct pl_shader *sh, struct pl_shader_obj **icc);

// Backwards compatibility aliases
#define pl_3dlut_params pl_icc_params
#define pl_3dlut_default_params pl_icc_default_params
#define pl_3dlut_profile pl_icc_color_space
#define pl_3dlut_result pl_icc_result

static PL_DEPRECATED inline bool pl_3dlut_update(struct pl_shader *sh,
const struct pl_icc_color_space *src,
const struct pl_icc_color_space *dst,
struct pl_shader_obj **lut3d,
struct pl_icc_result *out,
const struct pl_icc_params *params)
{
return pl_icc_update(sh, src, dst, lut3d, out, params);
}

static PL_DEPRECATED inline void pl_3dlut_apply(struct pl_shader *sh,
struct pl_shader_obj **lut3d)
{
return pl_icc_apply(sh, lut3d);
}

#endif // LIBPLACEBO_SHADERS_ICC_H_
28 changes: 0 additions & 28 deletions src/lcms.h

This file was deleted.

3 changes: 2 additions & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ components = [
{
'name': 'lcms',
'deps': dependency('lcms2', version: '>=2.6', required: get_option('lcms')),
'srcs': 'lcms.c',
'srcs': 'shaders/icc.c',
'headers': 'shaders/icc.h',
}, {
'name': 'glslang',
'deps': glslang_combined,
Expand Down
Loading

0 comments on commit 558cfcb

Please sign in to comment.