Welcome to mirror list, hosted at ThFree Co, Russian Federation.

draw_attributes.cc « intern « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8fb4210901f1b6845092933ea4d35612bd638d9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2022 Blender Foundation. All rights reserved. */

#include "draw_attributes.h"

/* Return true if the given DRW_AttributeRequest is already in the requests. */
static bool drw_attributes_has_request(const DRW_Attributes *requests, DRW_AttributeRequest req)
{
  for (int i = 0; i < requests->num_requests; i++) {
    const DRW_AttributeRequest src_req = requests->requests[i];
    if (src_req.domain != req.domain) {
      continue;
    }
    if (src_req.layer_index != req.layer_index) {
      continue;
    }
    if (src_req.cd_type != req.cd_type) {
      continue;
    }
    return true;
  }
  return false;
}

static void drw_attributes_merge_requests(const DRW_Attributes *src_requests,
                                          DRW_Attributes *dst_requests)
{
  for (int i = 0; i < src_requests->num_requests; i++) {
    if (dst_requests->num_requests == GPU_MAX_ATTR) {
      return;
    }

    if (drw_attributes_has_request(dst_requests, src_requests->requests[i])) {
      continue;
    }

    dst_requests->requests[dst_requests->num_requests] = src_requests->requests[i];
    dst_requests->num_requests += 1;
  }
}

void drw_attributes_clear(DRW_Attributes *attributes)
{
  memset(attributes, 0, sizeof(DRW_Attributes));
}

void drw_attributes_merge(DRW_Attributes *dst,
                          const DRW_Attributes *src,
                          ThreadMutex *render_mutex)
{
  BLI_mutex_lock(render_mutex);
  drw_attributes_merge_requests(src, dst);
  BLI_mutex_unlock(render_mutex);
}

bool drw_attributes_overlap(const DRW_Attributes *a, const DRW_Attributes *b)
{
  for (int i = 0; i < b->num_requests; i++) {
    if (!drw_attributes_has_request(a, b->requests[i])) {
      return false;
    }
  }

  return true;
}

DRW_AttributeRequest *drw_attributes_add_request(DRW_Attributes *attrs,
                                                 eCustomDataType type,
                                                 int layer,
                                                 eAttrDomain domain)
{
  if (attrs->num_requests >= GPU_MAX_ATTR) {
    return nullptr;
  }

  DRW_AttributeRequest *req = &attrs->requests[attrs->num_requests];
  req->cd_type = type;
  req->layer_index = layer;
  req->domain = domain;
  attrs->num_requests += 1;
  return req;
}

bool drw_custom_data_match_attribute(const CustomData *custom_data,
                                     const char *name,
                                     int *r_layer_index,
                                     eCustomDataType *r_type)
{
  const eCustomDataType possible_attribute_types[7] = {
      CD_PROP_BOOL,
      CD_PROP_INT8,
      CD_PROP_INT32,
      CD_PROP_FLOAT,
      CD_PROP_FLOAT2,
      CD_PROP_FLOAT3,
      CD_PROP_COLOR,
  };

  for (int i = 0; i < ARRAY_SIZE(possible_attribute_types); i++) {
    const eCustomDataType attr_type = possible_attribute_types[i];
    int layer_index = CustomData_get_named_layer(custom_data, attr_type, name);
    if (layer_index == -1) {
      continue;
    }

    *r_layer_index = layer_index;
    *r_type = attr_type;
    return true;
  }

  return false;
}