12 #ifndef hifi_GeometryCache_h
13 #define hifi_GeometryCache_h
15 #include "model-networking/ModelCache.h"
22 #include <DependencyManager.h>
24 #include <shared/Shapes.h>
26 #include <gpu/Batch.h>
27 #include <gpu/Stream.h>
29 #include <render/ShapePipeline.h>
31 #include <graphics/Material.h>
32 #include <graphics/Asset.h>
34 #include "FadeObjectParams.shared.slh"
36 class SimpleProgramKey;
38 typedef QPair<glm::vec2, float> Vec2FloatPair;
39 typedef QPair<Vec2FloatPair, Vec2FloatPair> Vec2FloatPairPair;
40 typedef QPair<glm::vec2, glm::vec2> Vec2Pair;
41 typedef QPair<Vec2Pair, Vec2Pair> Vec2PairPair;
42 typedef QPair<glm::vec3, glm::vec3> Vec3Pair;
43 typedef QPair<glm::vec4, glm::vec4> Vec4Pair;
44 typedef QPair<Vec3Pair, Vec2Pair> Vec3PairVec2Pair;
45 typedef QPair<Vec3Pair, glm::vec4> Vec3PairVec4;
46 typedef QPair<Vec3Pair, Vec4Pair> Vec3PairVec4Pair;
47 typedef QPair<Vec4Pair, glm::vec4> Vec4PairVec4;
48 typedef QPair<Vec4Pair, Vec4Pair> Vec4PairVec4Pair;
50 inline uint qHash(
const Vec2FloatPairPair& v, uint seed) {
52 return qHash(v.first.first.x + 5009 * v.first.first.y + 5011 * v.first.second +
53 5021 * v.second.first.x + 5023 * v.second.first.y + 5039 * v.second.second);
56 inline uint qHash(
const Vec2Pair& v, uint seed) {
58 return qHash(v.first.x + 5009 * v.first.y + 5011 * v.second.x + 5021 * v.second.y, seed);
61 inline uint qHash(
const glm::vec4& v, uint seed) {
63 return qHash(v.x + 5009 * v.y + 5011 * v.z + 5021 * v.w, seed);
66 inline uint qHash(
const Vec2PairPair& v, uint seed) {
68 return qHash(v.first.first.x + 5009 * v.first.first.y
69 + 5011 * v.first.second.x + 5021 * v.first.second.y
70 + 5023 * v.second.first.x + 5039 * v.second.first.y
71 + 5051 * v.second.second.x + 5059 * v.second.second.y, seed);
74 inline uint qHash(
const Vec3Pair& v, uint seed) {
76 return qHash(v.first.x + 5009 * v.first.y + 5011 * v.first.z
77 + 5021 * v.second.x + 5023 * v.second.y + 5039 * v.second.z, seed);
80 inline uint qHash(
const Vec4Pair& v, uint seed) {
82 return qHash(v.first.x + 5009 * v.first.y + 5011 * v.first.z + 5021 * v.first.w
83 + 5023 * v.second.x + 5039 * v.second.y + 5051 * v.second.z + 5059 * v.second.w , seed);
86 inline uint qHash(
const Vec3PairVec2Pair& v, uint seed) {
88 return qHash(v.first.first.x + 5009 * v.first.first.y + 5011 * v.first.first.z +
89 5021 * v.first.second.x + 5023 * v.first.second.y + 5039 * v.first.second.z +
90 5051 * v.second.first.x + 5059 * v.second.first.y +
91 5077 * v.second.second.x + 5081 * v.second.second.y, seed);
94 inline uint qHash(
const Vec3PairVec4& v, uint seed) {
96 return qHash(v.first.first.x + 5009 * v.first.first.y + 5011 * v.first.first.z +
97 5021 * v.first.second.x + 5023 * v.first.second.y + 5039 * v.first.second.z +
98 5051 * v.second.x + 5059 * v.second.y + 5077 * v.second.z + 5081 * v.second.w, seed);
102 inline uint qHash(
const Vec3PairVec4Pair& v, uint seed) {
104 return qHash(v.first.first.x + 5009 * v.first.first.y + 5011 * v.first.first.z
105 + 5023 * v.first.second.x + 5039 * v.first.second.y + 5051 * v.first.second.z
106 + 5077 * v.second.first.x + 5081 * v.second.first.y + 5087 * v.second.first.z + 5099 * v.second.first.w
107 + 5101 * v.second.second.x + 5107 * v.second.second.y + 5113 * v.second.second.z + 5119 * v.second.second.w,
111 inline uint qHash(
const Vec4PairVec4& v, uint seed) {
113 return qHash(v.first.first.x + 5009 * v.first.first.y + 5011 * v.first.first.z + 5021 * v.first.first.w
114 + 5023 * v.first.second.x + 5039 * v.first.second.y + 5051 * v.first.second.z + 5059 * v.first.second.w
115 + 5077 * v.second.x + 5081 * v.second.y + 5087 * v.second.z + 5099 * v.second.w,
119 inline uint qHash(
const Vec4PairVec4Pair& v, uint seed) {
121 return qHash(v.first.first.x + 5009 * v.first.first.y + 5011 * v.first.first.z + 5021 * v.first.first.w
122 + 5023 * v.first.second.x + 5039 * v.first.second.y + 5051 * v.first.second.z + 5059 * v.first.second.w
123 + 5077 * v.second.first.x + 5081 * v.second.first.y + 5087 * v.second.first.z + 5099 * v.second.first.w
124 + 5101 * v.second.second.x + 5107 * v.second.second.y + 5113 * v.second.second.z + 5119 * v.second.second.w,
129 void append(
const FadeObjectParams& fadeParams);
133 void bind(gpu::Batch& batch)
const;
135 gpu::BufferPointer _fade1Buffer { std::make_shared<gpu::Buffer>(gpu::Buffer::VertexBuffer) };
136 gpu::BufferPointer _fade2Buffer { std::make_shared<gpu::Buffer>(gpu::Buffer::VertexBuffer) };
137 gpu::BufferPointer _fade3Buffer { std::make_shared<gpu::Buffer>(gpu::Buffer::VertexBuffer) };
138 gpu::BufferPointer _fade4Buffer { std::make_shared<gpu::Buffer>(gpu::Buffer::VertexBuffer) };
139 gpu::BufferPointer _fade5Buffer { std::make_shared<gpu::Buffer>(gpu::Buffer::VertexBuffer) };
140 gpu::BufferPointer _fade6Buffer { std::make_shared<gpu::Buffer>(gpu::Buffer::VertexBuffer) };
141 gpu::BufferPointer _fade7Buffer { std::make_shared<gpu::Buffer>(gpu::Buffer::VertexBuffer) };
174 static QString stringFromShape(GeometryCache::Shape geoShape);
176 static void computeSimpleHullPointListForShape(
int entityShape,
const glm::vec3 &entityExtents, QVector<glm::vec3> &outPointList);
178 int allocateID() {
return _nextID++; }
179 void releaseID(
int id);
180 static const int UNKNOWN_ID;
183 void bindSimpleProgram(gpu::Batch& batch,
bool textured =
false,
bool transparent =
false,
bool unlit =
false,
bool depthBias =
false,
184 bool isAntiAliased =
true,
bool forward =
false, graphics::MaterialKey::CullFaceMode cullFaceMode = graphics::MaterialKey::CullFaceMode::CULL_BACK);
186 static gpu::PipelinePointer getSimplePipeline(
bool textured =
false,
bool transparent =
false,
bool unlit =
false,
bool depthBias =
false,
187 bool fading =
false,
bool isAntiAliased =
true,
bool forward =
false, graphics::MaterialKey::CullFaceMode cullFaceMode = graphics::MaterialKey::CullFaceMode::CULL_BACK);
189 static void initializeShapePipelines();
190 render::ShapePipelinePointer getShapePipelinePointer(
bool transparent,
bool unlit,
bool forward,
bool fading =
false,
191 graphics::MaterialKey::CullFaceMode cullFaceMode = graphics::MaterialKey::CULL_BACK) {
return _shapePipelines[std::make_tuple(transparent, unlit, forward, fading, cullFaceMode)]; }
194 void renderShapeInstances(gpu::Batch& batch, Shape shape,
size_t count, gpu::BufferPointer& colorBuffer);
195 void renderWireShapeInstances(gpu::Batch& batch, Shape shape,
size_t count, gpu::BufferPointer& colorBuffer);
196 void renderShapeFadeInstances(gpu::Batch& batch, Shape shape,
size_t count, gpu::BufferPointer& colorBuffer,
const FadeBuffers& fadeBuffers);
197 void renderWireShapeFadeInstances(gpu::Batch& batch, Shape shape,
size_t count, gpu::BufferPointer& colorBuffer,
const FadeBuffers& fadeBuffers);
199 void renderShapeInstance(RenderArgs* args, gpu::Batch& batch, Shape shape,
bool wire,
const glm::vec4& color,
200 const render::ShapePipelinePointer& pipeline);
202 void renderSolidSphereInstance(RenderArgs* args, gpu::Batch& batch,
const glm::vec4& color,
203 const render::ShapePipelinePointer& pipeline);
204 void renderSolidSphereInstance(RenderArgs* args, gpu::Batch& batch,
const glm::vec3& color,
205 const render::ShapePipelinePointer& pipeline) {
206 renderSolidSphereInstance(args, batch, glm::vec4(color, 1.0f), pipeline);
209 void renderWireCubeInstance(RenderArgs* args, gpu::Batch& batch,
const glm::vec4& color,
210 const render::ShapePipelinePointer& pipeline);
211 void renderWireCubeInstance(RenderArgs* args, gpu::Batch& batch,
const glm::vec3& color,
212 const render::ShapePipelinePointer& pipeline) {
213 renderWireCubeInstance(args, batch, glm::vec4(color, 1.0f), pipeline);
216 void renderShapeFadeInstance(RenderArgs* args, gpu::Batch& batch, Shape shape,
bool wire,
const glm::vec4& color,
217 const FadeObjectParams& fadeParams,
const render::ShapePipelinePointer& pipeline);
220 void renderShape(gpu::Batch& batch, Shape shape, gpu::BufferPointer& colorBuffer);
221 void renderWireShape(gpu::Batch& batch, Shape shape, gpu::BufferPointer& colorBuffer);
222 void renderShapeFade(gpu::Batch& batch, Shape shape, gpu::BufferPointer& colorBuffer,
const FadeBuffers& fadeBuffers);
223 void renderWireShapeFade(gpu::Batch& batch, Shape shape, gpu::BufferPointer& colorBuffer,
const FadeBuffers& fadeBuffers);
224 size_t getShapeTriangleCount(Shape shape);
226 void renderGrid(gpu::Batch& batch,
const glm::vec2& minCorner,
const glm::vec2& maxCorner,
227 int majorRows,
int majorCols,
float majorEdge,
228 int minorRows,
int minorCols,
float minorEdge,
229 const glm::vec4& color,
bool forward,
int id);
231 void renderBevelCornersRect(gpu::Batch& batch,
int x,
int y,
int width,
int height,
int bevelDistance,
const glm::vec4& color,
int id);
233 void renderUnitQuad(gpu::Batch& batch,
const glm::vec4& color,
int id);
235 void renderQuad(gpu::Batch& batch,
int x,
int y,
int width,
int height,
const glm::vec4& color,
int id)
236 { renderQuad(batch, glm::vec2(x,y), glm::vec2(x + width, y + height), color,
id); }
238 void renderQuad(gpu::Batch& batch,
const glm::vec2& minCorner,
const glm::vec2& maxCorner,
const glm::vec4& color,
int id);
239 void renderQuadFade(gpu::Batch& batch,
const glm::vec2& minCorner,
const glm::vec2& maxCorner,
const glm::vec4& color,
const FadeBuffers& fadeBuffers,
int id);
241 void renderQuad(gpu::Batch& batch,
const glm::vec2& minCorner,
const glm::vec2& maxCorner,
242 const glm::vec2& texCoordMinCorner,
const glm::vec2& texCoordMaxCorner,
243 const glm::vec4& color,
int id);
244 void renderQuadFade(gpu::Batch& batch,
const glm::vec2& minCorner,
const glm::vec2& maxCorner,
245 const glm::vec2& texCoordMinCorner,
const glm::vec2& texCoordMaxCorner,
246 const glm::vec4& color,
const FadeBuffers& fadeBuffers,
int id);
248 void renderQuad(gpu::Batch& batch,
const glm::vec3& minCorner,
const glm::vec3& maxCorner,
const glm::vec4& color,
int id);
250 void renderQuad(gpu::Batch& batch,
const glm::vec3& topLeft,
const glm::vec3& bottomLeft,
251 const glm::vec3& bottomRight,
const glm::vec3& topRight,
252 const glm::vec2& texCoordTopLeft,
const glm::vec2& texCoordBottomLeft,
253 const glm::vec2& texCoordBottomRight,
const glm::vec2& texCoordTopRight,
254 const glm::vec4& color,
int id);
257 void renderLine(gpu::Batch& batch,
const glm::vec3& p1,
const glm::vec3& p2,
const glm::vec3& color,
int id)
258 { renderLine(batch, p1, p2, color, color,
id); }
260 void renderLine(gpu::Batch& batch,
const glm::vec3& p1,
const glm::vec3& p2,
261 const glm::vec3& color1,
const glm::vec3& color2,
int id)
262 { renderLine(batch, p1, p2, glm::vec4(color1, 1.0f), glm::vec4(color2, 1.0f),
id); }
264 void renderLine(gpu::Batch& batch,
const glm::vec3& p1,
const glm::vec3& p2,
265 const glm::vec4& color,
int id)
266 { renderLine(batch, p1, p2, color, color,
id); }
268 void renderLine(gpu::Batch& batch,
const glm::vec3& p1,
const glm::vec3& p2,
269 const glm::vec4& color1,
const glm::vec4& color2,
int id);
271 void renderDashedLine(gpu::Batch& batch,
const glm::vec3& start,
const glm::vec3& end,
const glm::vec4& color,
int id)
272 { renderDashedLine(batch, start, end, color, 0.05f, 0.025f,
id); }
274 void renderDashedLine(gpu::Batch& batch,
const glm::vec3& start,
const glm::vec3& end,
const glm::vec4& color,
275 const float dash_length,
const float gap_length,
int id);
277 void updateVertices(
int id,
const QVector<glm::vec2>& points,
const glm::vec4& color);
278 void updateVertices(
int id,
const QVector<glm::vec2>& points,
const QVector<glm::vec4>& colors);
279 void updateVertices(
int id,
const QVector<glm::vec3>& points,
const glm::vec4& color);
280 void updateVertices(
int id,
const QVector<glm::vec3>& points,
const QVector<glm::vec4>& colors);
281 void updateVertices(
int id,
const QVector<glm::vec3>& points,
const QVector<glm::vec2>& texCoords,
const glm::vec4& color);
282 void renderVertices(gpu::Batch& batch, gpu::Primitive primitiveType,
int id);
284 void renderTorus(gpu::Batch& batch,
float innerRadius, gpu::BufferPointer& colorBuffer,
int id);
285 void renderTorusFade(gpu::Batch& batch,
float innerRadius, gpu::BufferPointer& colorBuffer,
const FadeBuffers& fadeBuffers,
int id);
291 ShapeVertex(
const vec3& pos,
const vec3& normal,
const vec2& uv,
const vec3& tangent) : pos(pos), normal(normal), uv(uv), tangent(tangent) {}
300 gpu::BufferView _positionView;
301 gpu::BufferView _normalView;
302 gpu::BufferView _texCoordView;
303 gpu::BufferView _tangentView;
304 gpu::BufferView _indicesView;
305 gpu::BufferView _wireIndicesView;
307 void setupVertices(gpu::BufferPointer& vertexBuffer,
const std::vector<ShapeVertex>& vertices);
308 void setupIndices(gpu::BufferPointer& indexBuffer,
const geometry::IndexVector& indices,
const geometry::IndexVector& wireIndices);
309 void setupBatch(gpu::Batch& batch)
const;
310 void draw(gpu::Batch& batch)
const;
311 void drawWire(gpu::Batch& batch)
const;
312 void drawInstances(gpu::Batch& batch,
size_t count)
const;
313 void drawWireInstances(gpu::Batch& batch,
size_t count)
const;
316 using VShape = std::array<ShapeData, NUM_SHAPES>;
322 graphics::MeshPointer meshFromShape(Shape geometryShape, glm::vec3 color);
324 static uint32_t toCompactColor(
const glm::vec4& color);
332 typedef QPair<int, int> IntPair;
333 typedef QPair<unsigned int, unsigned int> VerticesIndices;
338 gpu::PipelinePointer _standardDrawPipeline;
339 gpu::PipelinePointer _standardDrawPipelineNoBlend;
341 gpu::BufferPointer _shapeVertices { std::make_shared<gpu::Buffer>(gpu::Buffer::VertexBuffer) };
342 gpu::BufferPointer _shapeIndices { std::make_shared<gpu::Buffer>(gpu::Buffer::IndexBuffer) };
351 using GridBuffer = gpu::BufferView;
352 void useGridPipeline(gpu::Batch& batch, GridBuffer gridBuffer,
bool transparent,
bool forward);
353 static std::map<std::pair<bool, bool>, gpu::PipelinePointer> _gridPipelines;
355 class BatchItemDetails {
357 static int population;
358 gpu::BufferPointer verticesBuffer;
359 gpu::BufferPointer normalBuffer;
360 gpu::BufferPointer colorBuffer;
361 gpu::BufferPointer uniformBuffer;
362 gpu::Stream::FormatPointer streamFormat;
363 gpu::BufferStreamPointer stream;
370 BatchItemDetails(
const GeometryCache::BatchItemDetails& other);
375 QHash<IntPair, VerticesIndices> _coneVBOs;
379 QHash<int, Vec3PairVec4Pair> _lastRegisteredQuad3DTexture;
380 QHash<int, BatchItemDetails> _registeredQuad3DTextures;
382 QHash<int, Vec4PairVec4> _lastRegisteredQuad2DTexture;
383 QHash<int, BatchItemDetails> _registeredQuad2DTextures;
385 QHash<int, Vec4PairVec4> _lastRegisteredQuad2DTextureFade;
386 QHash<int, BatchItemDetails> _registeredQuad2DTexturesFade;
388 QHash<int, Vec3PairVec4> _lastRegisteredQuad3D;
389 QHash<int, BatchItemDetails> _registeredQuad3D;
391 QHash<int, Vec4Pair> _lastRegisteredQuad2D;
392 QHash<int, BatchItemDetails> _registeredQuad2D;
394 QHash<int, Vec4Pair> _lastRegisteredQuad2DFade;
395 QHash<int, BatchItemDetails> _registeredQuad2DFade;
397 QHash<int, Vec3Pair> _lastRegisteredBevelRects;
398 QHash<int, BatchItemDetails> _registeredBevelRects;
400 QHash<int, Vec3Pair> _lastRegisteredLine3D;
401 QHash<int, BatchItemDetails> _registeredLine3DVBOs;
403 QHash<int, Vec2Pair> _lastRegisteredLine2D;
404 QHash<int, BatchItemDetails> _registeredLine2DVBOs;
406 QHash<int, BatchItemDetails> _registeredVertices;
408 QHash<int, Vec3PairVec2Pair> _lastRegisteredDashedLines;
409 QHash<int, BatchItemDetails> _registeredDashedLines;
411 QHash<int, Vec2FloatPairPair> _lastRegisteredGridBuffer;
412 QHash<int, GridBuffer> _registeredGridBuffers;
414 QHash<int, float> _lastRegisteredTorusBuffer;
415 QHash<int, BatchItemDetails> _registeredTorusBuffers;
417 QHash<int, float> _lastRegisteredTorusBufferFade;
418 QHash<int, BatchItemDetails> _registeredTorusBuffersFade;
421 static std::map<std::tuple<bool, bool, bool, bool>, gpu::ShaderPointer> _shapeShaders;
423 static std::map<std::tuple<bool, bool, bool, bool, graphics::MaterialKey::CullFaceMode>, render::ShapePipelinePointer> _shapePipelines;
424 static QHash<SimpleProgramKey, gpu::PipelinePointer> _simplePrograms;
426 static render::ShapePipelinePointer getShapePipeline(
bool textured =
false,
bool transparent =
false,
bool unlit =
false,
427 bool depthBias =
false,
bool forward =
false, graphics::MaterialKey::CullFaceMode cullFaceMode = graphics::MaterialKey::CullFaceMode::CULL_BACK);
428 static render::ShapePipelinePointer getFadingShapePipeline(
bool textured =
false,
bool transparent =
false,
bool unlit =
false,
429 bool depthBias =
false,
bool forward =
false, graphics::MaterialKey::CullFaceMode cullFaceMode = graphics::MaterialKey::CullFaceMode::CULL_BACK);
Stores cached geometry.
Definition: GeometryCache.h:145
void useSimpleDrawPipeline(gpu::Batch &batch, bool noBlend=false)
Set a batch to the simple pipeline, returning the previous pipeline.
Definition: GeometryCache.cpp:2166
static GeometryCache::Shape getShapeForEntityShape(int entityShapeEnum)
Definition: GeometryCache.cpp:612
const ShapeData * getShapeData(Shape shape) const
Definition: GeometryCache.cpp:601