小编Tho*_*mas的帖子

If语句会减慢我的着色器吗?

我想知道着色器内部的"If-statements"(顶点/片段/像素......)是否真的会降低着色器性能.例如:

使用它更好吗:

vec3 output;
output = input*enable + input2*(1-enable);
Run Code Online (Sandbox Code Playgroud)

而不是使用这个:

vec3 output;
if(enable == 1)
{
    output = input;
}
else
{
    output = input2;
}
Run Code Online (Sandbox Code Playgroud)

在另一个论坛上有一个关于那个的讨论(2013):http://answers.unity3d.com/questions/442688/shader-if-else-performance.html 这里有人说,If语句真的很糟糕用于着色器的性能.

此外,他们还在讨论if/else语句(2012)中的内容:https: //www.opengl.org/discussion_boards/showthread.php/177762-Performance-alternative-for-if-(-)

也许硬件或着色器编译器现在更好,他们修复了这个(可能不存在)性能问题.

编辑:

在这种情况下,这里假设enable是一个统一变量,它总是设置为0:

if(enable == 1) //never happens
{
    output = vec4(0,0,0,0);
}
else  //always happens
{
    output = calcPhong(normal, lightDir);
}
Run Code Online (Sandbox Code Playgroud)

我想在这里我们在着色器中有一个分支,它会降低着色器的速度.那是对的吗?

制作2个不同的阴影是否更有意义,比如一个用于其他阴影,另一个用于if部分?

opengl shader direct3d glsl hlsl

53
推荐指数
2
解决办法
3万
查看次数

将 GL_TEXTURE_2D 复制到 GL_TEXTURE_2D_ARRAY 纹理的一个切片中

我正在尝试将一个 GL_TEXTURE_2D 复制到 GL_TEXTURE_2D_ARRAY 纹理的选定切片中。

我尝试将通常的Texture_2D绑定到一个帧缓冲区,并将Texture_2D_Array的一部分绑定到另一个帧缓冲区(两者具有相同的大小(宽度、高度、GL_RGB、GL_UNSIGNED_BYTE))。后来我想glBlitFramebuffer将该纹理复制到这一个切片中......但我认为我误解了该glFramebufferTexture3D命令。

顺便说一句:GL_TEXTURE_2D 已正确加载,我也将其打印出来(有效)

这是我的代码:

//Create 2 FBOs for copying textures
glGenFramebuffers(1, &nFrameBufferRead); //FBO for texture2D
glGenFramebuffers(1, &nFrameBufferWrite); //FBO for one slice of the texture2d_array
CBasics::GetOpenGLError(); 

//generate the GL_TEXTURE_2D_ARRAY with given values (glgentextures is already called for this texture)
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGB, nWidth, nHeight, countSlices, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
CBasics::GetOpenGLError();  

//Bind the Texture2D to the readFramebuffer
glBindFramebuffer(GL_READ_FRAMEBUFFER, nFrameBufferRead);
glFramebufferTexture(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture2D_ID, 0);
CBasics::GetOpenGLError();

//try to bind the Texture2D_Array to the drawFramebuffer
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, …
Run Code Online (Sandbox Code Playgroud)

opengl

1
推荐指数
1
解决办法
861
查看次数

为什么这个反向函数不能在for循环中工作?

#include <stdio.h>
#include <math.h>
int prime (long n);
long reverse(long n);
int main(void)
{
    long n;
    long i, j;
    puts("Enter n dight number, and we will help you find symmetrical prime number");
    scanf("%ld", &n);
    for (i = 11; i < (pow(10, n) - 1); i+= 2)
    {
        if (prime(i))
        {
            j = reverse(i);
            if (i == j)
            {
                printf("%ld\n", i);
            }
        }        
    }
}

int prime (long n) //estimate whether the number n is primer number
{
    int status = …
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
83
查看次数

变换模型矩阵

使用 glm 设置 ViewMatrix 很容易:

glm::lookAt(Position, Direction, UpVector);
Run Code Online (Sandbox Code Playgroud)

但是如果我尝试将 funktion 与 modelMatrix 一起使用,我会得到令人困惑的值(模型的位置不正确,而且旋转看起来也不正确)。我只想以与设置相机相同的方式设置对象。我可以使用 lookAt 功能并在之后进行一些更改吗?还是我必须为此编写自己的功能?如果是这样,如何?

我用这个固定了位置:

m_Orientation = glm::lookAtLH(Position, Direction, UpVector);
m_Orientation[3][0] = -m_Orientation[3][0];
m_Orientation[3][1] = -m_Orientation[3][1];
m_Orientation[3][2] = -m_Orientation[3][2];
Run Code Online (Sandbox Code Playgroud)

也在 vertexshader 里面我用这个:

gl_Position = CameraMatrix * ModelMatrix * Pos;
Run Code Online (Sandbox Code Playgroud)

其中 CameraMatrix 是一个 viewProjectionMatrix,ModelMatrix(我的问题)和 Pos 是我的顶点在模型空间中的位置

c++ opengl coordinate-transformation glm-math

0
推荐指数
1
解决办法
1999
查看次数

通过 API 编辑现有的 Pastebin 文档

我正在尝试在 LUA 中编写一个函数来编辑我的 pastebin 代码。

我可以使用此代码制作 HTTP 帖子:

http.post(string url, string postData [, table headers])

我也可以使用此代码进行 HTTP 获取:

http.get(string url [, table headers])

Pastebin 网站https://pastebin.com/api上提供了有关使用 API 的信息。我不确定,如果这个网站可以帮助我解决我的问题。

有人知道如何填写标题表吗?

这是我试过的程序:

headers = {}
headers["api_dev_key"]= 'myDevKey...'; // your api_developer_key
headers["api_paste_code"]   = 'my edited text'; // your paste text
headers["api_paste_private"] = '0'; // 0=public 1=unlisted 2=private
headers["api_paste_name"] = 'justmyfilename.php'; // name or title of your paste
headers["api_paste_expire_date"] = '10M';
headers["api_paste_format"] = 'php';
headers["api_user_key"] = ''; // if an invalid or expired …
Run Code Online (Sandbox Code Playgroud)

lua http pastebin

0
推荐指数
1
解决办法
5241
查看次数

标签 统计

opengl ×3

c ×1

c++ ×1

coordinate-transformation ×1

direct3d ×1

glm-math ×1

glsl ×1

hlsl ×1

http ×1

lua ×1

pastebin ×1

shader ×1