#include <functional>
int func(int x, int y)
{
return x+y;
}
int main()
{
typedef std::function<int(int, int)> Funcp;
Funcp funcp = func;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是有可能指向模板功能吗?
#include <functional>
template<class T>
T func(T x, T y)
{
return x+y;
}
int main()
{
typedef std::function<?(?, ?)> Funcp;
Funcp funcp = func;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想要做的是使用相同的vbo多次渲染网格但具有不同的偏移.例:
//Load VBO
glGenBuffers(2, &bufferObjects[0]);
glBindBuffer(GL_ARRAY_BUFFER, bufferObjects[VERTEX_DATA]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*size(vertices)*3, &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects[INDEX_DATA]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*size(indices), &indices[0], GL_STATIC_DRAW);
//Render VBO
glBindBuffer(GL_ARRAY_BUFFER, bufferObjects[VERTEX_DATA]);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects[INDEX_DATA]);
glDrawElements(renderFlag, nrIndices, GL_UNSIGNED_INT, 0);
Run Code Online (Sandbox Code Playgroud)
如果我同时绘制孔网格没有问题,但是可以使用不同的起始索引绘制相同的网格,如下所示:
glDrawElements(renderFlag, 20, GL_UNSIGNED_INT, "WHAT TO WRITE HERE"?);
Run Code Online (Sandbox Code Playgroud) __m256 dst = _mm256_cmp_ps(value1, value2, _CMP_LE_OQ);
Run Code Online (Sandbox Code Playgroud)
如果dst是,[0,0,0,-nan, 0,0,0,-nan];
我希望能够知道第一个-nan索引,在这种情况下,3无需进行for循环8迭代。这可能吗?
可能重复:
使用"删除此项"删除当前对象是否可以?
我刚刚看到他们delete this;在类函数中完成的一些代码,我知道这不是一个好的设计,但它定义了会发生什么,让我们说这个类总是来自某个地方的指针.是否会以正确的方式删除它?
class A
{
public:
void abort() { delete this; }
};
class B
{
void func() { A* a = new A; a->abort(); }
};
Run Code Online (Sandbox Code Playgroud) 从规格:
VK_KHR_maintenance1
允许在slink :: VkViewport :: height字段中指定负高度,以执行剪辑空间到帧缓冲区空间变换的y反转.这样,应用就可以避免在着色器中使用gl_Position.y = -gl_Position.y,同时针对其他API.
我的物理设备支持版本1.0.42,我启用了VK_KHR_maintenance1.当我在VkViewport结构中执行负高度时,我没有得到任何验证错误/警告.
vkCmdSetViewport(vkContext.commandBuffer, 0, 1, &viewport);
Run Code Online (Sandbox Code Playgroud)
但是我在屏幕上看不到任何东西,它的黑色,如果我删除viewport所有内容中的负值都按预期呈现.我是否需要做任何其他事情来翻转视口VK_KHR_maintenance1 extension?我只是在屏幕上渲染一个红色四边形,带有全屏视口.
VkViewport viewport = {};
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = 1024.0f;
viewport.height = -1024.0f;
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
vkCmdSetViewport(vkContext.commandBuffer, 0, 1, &viewport);
Run Code Online (Sandbox Code Playgroud)
顶点着色器:
void main() {
vec4 positions[3] = {
vec4(-1.0, -1.0, 0.0, 1.0),
vec4(3.0, -1.0, 0.0, 1.0),
vec4(-1.0, 3.0, 0.0, 1.0)
};
gl_Position = positions[gl_VertexIndex % 3];
}
Run Code Online (Sandbox Code Playgroud)
片段着色器: …
我正在努力加快GLM(OpenGL数学)的编译时间.GLM大量使用C++模板.
这是我到目前为止所尝试的.
math.h
#pragma once
#include <glm\glm.hpp>
extern template struct glm::tvec3<float, glm::highp>;
Run Code Online (Sandbox Code Playgroud)
math.cpp
#include "math.h"
template struct glm::tvec3<float, glm::highp>;
Run Code Online (Sandbox Code Playgroud)
然后我有三个使用glm::vec3模板的文件,glm::vec3是一个typedef glm::tvec3<float, glm::highp>.这三个文件a,b,c看起来几乎相同:
a.cpp, b.cpp, c.cpp
#include "math.h"
glm::vec3 func() {
glm::vec3 a = glm::vec3{1,1,1};
glm::vec3 b = glm::vec3{1,1,1};
return a + b;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用显式实例化定义和显式实例化声明.所以文件a,b,c不应该导致隐式实例化.但编译时间和我不这样做的时间相同.
我有一个模板,它采用具有不同值的结构,例如:
struct Something
{
char str[10];
int value;
...
...
};
Run Code Online (Sandbox Code Playgroud)
在函数内部我使用sizeof运算符:跳入内存 sizeof(Something);
有时我想不跳任何东西; 我希望sizeof返回零.如果我输入一个空结构,它将返回1; 我可以在模板中放置什么来使sizeof返回零?
我的stackAlloc功能看起来像这样:
void* stackAlloc(size_t size) {
if (size > maxStackAllocation)
return malloc(size);
else
return _alloca(size);
}
void stackAllocFree(void *ptr, size_t size) {
if (size > maxStackAllocation) {
free(ptr);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我改变那么stackAlloc功能总是使用malloc而不是alloca一切正常.
我将函数更改为宏,现在它按预期工作:
#define maxStackAllocation 1024
#define stackAlloc(size) \
( \
(size > maxStackAllocation)? \
malloc(size): \
_alloca(size) \
)
#define stackAllocFree(ptr, size) \
( \
(size > maxStackAllocation)? \
free(ptr): \
void() \
)
Run Code Online (Sandbox Code Playgroud) 假设我想用不同的材料渲染多个网格。我知道我可以push constants在这个例子中使用,但这个问题更多地是为了理解 vkDescriptorset 是如何工作的。
struct Material {
vec4 color;
vkDescriptorset descriptorSet;
VkDescriptorBufferInfo descriptorBufferInfo;
};
Run Code Online (Sandbox Code Playgroud)
我只叫vkUpdateDescriptorSets了_descriptorBufferInfo对数据的缓冲区创建后。一切正常。
我测试了另一个解决方案。我不是vkDescriptorset每种材料都有一个,而是所有材料都只有一个。而里面的rendepassI call vkUpdateDescriptorSetsfor each of the material VkDescriptorBufferInfo。
vkDescriptorset globalDescriptorSet;
struct Material {
vec4 color;
VkDescriptorBufferInfo descriptorBufferInfo;
};
beginCommandBuffer
beginRenderPass
for (auto &mesh : meshes) {
...
writeDescriptorSets.dstSet = globalDescriptorSet;
writeDescriptorSets.pBufferInfo = &mesh.material.descriptorBufferInfo;
....
vkUpdateDescriptorSets(device, 1, &writeDescriptorSets, 0, nullptr);
renderMesh(mesh);
}
endRenderPass
endCommandBuffer
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,它不起作用。验证层表示您必须beginCommandBuffer在为我渲染的第二个网格调用任何命令vkCmdBindDescriptorSets、vkCmdBindPipeline等之前调用。
那么这里的问题是什么,我不能在多个 VkDescriptorBufferInfo 之间共享一个 …
#include <iostream>
using namespace std;
struct A {
A() { cout << "default" << endl; }
A(const A&) { cout << "copy" << endl; }
A(A&&) { cout << "move" << endl; }
};
int main() {
A a{};
auto aa = A{};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序将打印default,default在MSVC2013.标准是否表示在左侧创建带有auto的对象,或者第二个版本,auto aa = A{};首先调用默认构造函数然后将tmp变量移动/复制到左侧?