<commit_before>/*
 * Copyright 2019 GoPro Inc.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <string.h>

#include "format.h"
#include "glcontext.h"
#include "log.h"
#include "nodes.h"
#include "pipeline.h"
#include "topology.h"
#include "type.h"

typedef void (*set_uniform_func)(struct glcontext *gl, GLint location, int count, const void *data);

struct uniform_desc {
    GLuint location;
    struct pipeline_uniform uniform;
    set_uniform_func set;
};

struct texture_desc {
    struct pipeline_texture texture;
};

struct buffer_desc {
    GLuint type;
    struct pipeline_buffer buffer;
};

struct attribute_desc {
    struct pipeline_attribute attribute;
};

static void set_uniform_1i(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform1iv(gl, location, count, data);
}

static void set_uniform_2iv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform2iv(gl, location, count, data);
}

static void set_uniform_3iv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform3iv(gl, location, count, data);
}

static void set_uniform_4iv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform4iv(gl, location, count, data);
}

static void set_uniform_1f(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform1fv(gl, location, count, data);
}

static void set_uniform_2fv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform2fv(gl, location, count, data);
}

static void set_uniform_3fv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform3fv(gl, location, count, data);
}

static void set_uniform_4fv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform4fv(gl, location, count, data);
}

static void set_uniform_mat3fv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniformMatrix3fv(gl, location, count, GL_FALSE, data);
}

static void set_uniform_mat4fv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniformMatrix4fv(gl, location, count, GL_FALSE, data);
}

static const set_uniform_func set_uniform_func_map[NGLI_TYPE_NB] = {
    [NGLI_TYPE_BOOL]  = set_uniform_1i,
    [NGLI_TYPE_INT]   = set_uniform_1i,
    [NGLI_TYPE_IVEC2] = set_uniform_2iv,
    [NGLI_TYPE_IVEC3] = set_uniform_3iv,
    [NGLI_TYPE_IVEC4] = set_uniform_4iv,
    [NGLI_TYPE_FLOAT] = set_uniform_1f,
    [NGLI_TYPE_VEC2]  = set_uniform_2fv,
    [NGLI_TYPE_VEC3]  = set_uniform_3fv,
    [NGLI_TYPE_VEC4]  = set_uniform_4fv,
    [NGLI_TYPE_MAT3]  = set_uniform_mat3fv,
    [NGLI_TYPE_MAT4]  = set_uniform_mat4fv,
};

static int build_uniform_descs(struct pipeline *s, const struct pipeline_params *params)
{
    const struct program *program = params->program;

    if (!program->uniforms)
        return 0;

    for (int i = 0; i < params->nb_uniforms; i++) {
        const struct pipeline_uniform *uniform = &params->uniforms[i];
        const struct program_variable_info *info = ngli_hmap_get(program->uniforms, uniform->name);
        if (!info)
            continue;

        if (uniform->type != info->type && (uniform->type != NGLI_TYPE_INT ||
            (info->type != NGLI_TYPE_BOOL && info->type != NGLI_TYPE_INT))) {
            LOG(ERROR, "uniform '%s' type does not match the type declared in the shader", uniform->name);
            return NGL_ERROR_INVALID_ARG;
        }

        const set_uniform_func set_func = set_uniform_func_map[uniform->type];
        ngli_assert(set_func);
        struct uniform_desc desc = {
            .location = info->location,
            .uniform = *uniform,
            .set = set_func,
        };
        if (!ngli_darray_push(&s->uniform_descs, &desc))
            return NGL_ERROR_MEMORY;
    }

    return 0;
}

static void set_uniforms(struct pipeline *s, struct glcontext *gl)
{
    const struct uniform_desc *descs = ngli_darray_data(&s->uniform_descs);
    for (int i = 0; i < ngli_darray_count(&s->uniform_descs); i++) {
        const struct uniform_desc *desc = &descs[i];
        const struct pipeline_uniform *uniform = &desc->uniform;
        if (uniform->data)
            desc->set(gl, desc->location, uniform->count, uniform->data);
    }
}<commit_msg>pipeline: support uint and uivec[234] uniform upload<commit_after>/*
 * Copyright 2019 GoPro Inc.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <string.h>

#include "format.h"
#include "glcontext.h"
#include "log.h"
#include "nodes.h"
#include "pipeline.h"
#include "topology.h"
#include "type.h"

typedef void (*set_uniform_func)(struct glcontext *gl, GLint location, int count, const void *data);

struct uniform_desc {
    GLuint location;
    struct pipeline_uniform uniform;
    set_uniform_func set;
};

struct texture_desc {
    struct pipeline_texture texture;
};

struct buffer_desc {
    GLuint type;
    struct pipeline_buffer buffer;
};

struct attribute_desc {
    struct pipeline_attribute attribute;
};

static void set_uniform_1i(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform1iv(gl, location, count, data);
}

static void set_uniform_2iv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform2iv(gl, location, count, data);
}

static void set_uniform_3iv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform3iv(gl, location, count, data);
}

static void set_uniform_4iv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform4iv(gl, location, count, data);
}

static void set_uniform_1ui(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform1uiv(gl, location, count, data);
}

static void set_uniform_2uiv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform2uiv(gl, location, count, data);
}

static void set_uniform_3uiv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform3uiv(gl, location, count, data);
}

static void set_uniform_4uiv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform4uiv(gl, location, count, data);
}

static void set_uniform_1f(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform1fv(gl, location, count, data);
}

static void set_uniform_2fv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform2fv(gl, location, count, data);
}

static void set_uniform_3fv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform3fv(gl, location, count, data);
}

static void set_uniform_4fv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniform4fv(gl, location, count, data);
}

static void set_uniform_mat3fv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniformMatrix3fv(gl, location, count, GL_FALSE, data);
}

static void set_uniform_mat4fv(struct glcontext *gl, GLint location, int count, const void *data)
{
    ngli_glUniformMatrix4fv(gl, location, count, GL_FALSE, data);
}

static const set_uniform_func set_uniform_func_map[NGLI_TYPE_NB] = {
    [NGLI_TYPE_BOOL]  = set_uniform_1i,
    [NGLI_TYPE_INT]   = set_uniform_1i,
    [NGLI_TYPE_IVEC2] = set_uniform_2iv,
    [NGLI_TYPE_IVEC3] = set_uniform_3iv,
    [NGLI_TYPE_IVEC4] = set_uniform_4iv,
    [NGLI_TYPE_UINT]   = set_uniform_1ui,
    [NGLI_TYPE_UIVEC2] = set_uniform_2uiv,
    [NGLI_TYPE_UIVEC3] = set_uniform_3uiv,
    [NGLI_TYPE_UIVEC4] = set_uniform_4uiv,
    [NGLI_TYPE_FLOAT] = set_uniform_1f,
    [NGLI_TYPE_VEC2]  = set_uniform_2fv,
    [NGLI_TYPE_VEC3]  = set_uniform_3fv,
    [NGLI_TYPE_VEC4]  = set_uniform_4fv,
    [NGLI_TYPE_MAT3]  = set_uniform_mat3fv,
    [NGLI_TYPE_MAT4]  = set_uniform_mat4fv,
};

static int build_uniform_descs(struct pipeline *s, const struct pipeline_params *params)
{
    const struct program *program = params->program;

    if (!program->uniforms)
        return 0;

    struct ngl_ctx *ctx = s->ctx;
    struct glcontext *gl = ctx->glcontext;

    for (int i = 0; i < params->nb_uniforms; i++) {
        const struct pipeline_uniform *uniform = &params->uniforms[i];
        const struct program_variable_info *info = ngli_hmap_get(program->uniforms, uniform->name);
        if (!info)
            continue;

        if (uniform->type != info->type && (uniform->type != NGLI_TYPE_INT ||
            (info->type != NGLI_TYPE_BOOL && info->type != NGLI_TYPE_INT))) {
            LOG(ERROR, "uniform '%s' type does not match the type declared in the shader", uniform->name);
            return NGL_ERROR_INVALID_ARG;
        }

        if (!(gl->features & NGLI_FEATURE_UINT_UNIFORMS) &&
            (uniform->type == NGLI_TYPE_UINT ||
             uniform->type == NGLI_TYPE_UIVEC2 ||
             uniform->type == NGLI_TYPE_UIVEC3 ||
             uniform->type == NGLI_TYPE_UIVEC4)) {
            LOG(ERROR, "context does not support unsigned int uniform flavours");
            return NGL_ERROR_UNSUPPORTED;
        }

        const set_uniform_func set_func = set_uniform_func_map[uniform->type];
        ngli_assert(set_func);
        struct uniform_desc desc = {
            .location = info->location,
            .uniform = *uniform,
            .set = set_func,
        };
        if (!ngli_darray_push(&s->uniform_descs, &desc))
            return NGL_ERROR_MEMORY;
    }

    return 0;
}

static void set_uniforms(struct pipeline *s, struct glcontext *gl)
{
    const struct uniform_desc *descs = ngli_darray_data(&s->uniform_descs);
    for (int i = 0; i < ngli_darray_count(&s->uniform_descs); i++) {
        const struct uniform_desc *desc = &descs[i];
        const struct pipeline_uniform *uniform = &desc->uniform;
        if (uniform->data)
            desc->set(gl, desc->location, uniform->count, uniform->data);
    }
}
{'fraction_non_alphanumeric': 0.10051299886966351, 'fraction_numerical': 0.010694722198069733, 'mean_word_length': 3.8449873631002527, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3701344', 'n_tokens_mistral': 3841, 'n_tokens_neox': 3771, 'n_words': 1068}