Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

OpenGL glGenTextures(n, texture) - czas tworzenia tekstur w zaleznosci od parametru n

Ostatnio zmodyfikowano 2017-08-25 15:53
Autor Wiadomość
Nazgul
Temat założony przez niniejszego użytkownika
» 2017-08-25 15:53:08
Przeprowadziłem test:

Tu jest kod programu testującego
C/C++
#include "Library.h" // openGL, GLEW, GLFW, lodepng
#include <Windows.h>

class Stopwatch
{
public:
    Stopwatch()
    {
        QueryPerformanceFrequency( reinterpret_cast < LARGE_INTEGER *>( & Frequency ) );
        QueryPerformanceCounter( reinterpret_cast < LARGE_INTEGER *>( & KnockCountStart ) );
        QueryPerformanceCounter( reinterpret_cast < LARGE_INTEGER *>( & KnockCountStop ) );
        ResultNanoseconds = 0;
    }
    ~Stopwatch() { }
   
    void Start() {
        QueryPerformanceCounter( reinterpret_cast < LARGE_INTEGER *>( & KnockCountStart ) );
    }
    void Stop() {
        QueryPerformanceCounter( reinterpret_cast < LARGE_INTEGER *>( & KnockCountStop ) );
        ResultNanoseconds =( 1000000000 *( KnockCountStop - KnockCountStart ) ) / Frequency;
    }
    unsigned long long & ReadPassedNanoseconds() {
        return ResultNanoseconds;
    }
private:
    unsigned long long Frequency;
    unsigned long long KnockCountStop;
    unsigned long long KnockCountStart;
    unsigned long long ResultNanoseconds;
};

GLboolean loadtexturedata( std::wstring fileName_, GLuint & width_, GLuint & height_, std::vector < GLubyte >& image_ )
{
    std::fstream file_( fileName_.c_str(), std::ios::in | std::ios::out | std::ios::binary );
    if( !file_.is_open() ) return false;
   
    GLuint fileSize;
    {
        file_.seekg( 0, std::ios::end );
        fileSize = file_.tellg();
        file_.seekg( 0, std::ios::beg );
    }
    std::vector < GLubyte > data;
   
    data.resize( fileSize );
    file_.read(( char * )( & data[ 0 ] ), sizeof( GLubyte ) * fileSize );
   
    std::vector < GLubyte > imageBYTE;
    GLuint width, height;
    if( lodepng::decode( imageBYTE, width, height, data ) ) return false;
   
    width_ = width;
    height_ = height;
    image_ = imageBYTE;
    return true;
    //glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &imageBYTE[0]);
}

std::wstring getPNGfile()
{
    wchar_t fileName[ 300 ];
    /*GetSaveFileName*/ {
        OPENFILENAMEW ofn;
        ZeroMemory( & ofn, sizeof( ofn ) );
        ofn.lStructSize = sizeof( ofn );
        ofn.hwndOwner = NULL;
        ofn.lpstrFile = fileName;
        ofn.lpstrFile[ 0 ] = '\0';
        ofn.nMaxFile = sizeof( fileName );
        ofn.lpstrFilter = L"PNG image file (*.png)\0*.png\0All files (*)\0*.*\0";
        ofn.nFilterIndex = 1;
        ofn.lpstrFileTitle = NULL;
        ofn.nMaxFileTitle = 0;
        ofn.lpstrTitle = L"Open Project";
        ofn.lpstrInitialDir = NULL;
        ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
        GetOpenFileNameW( & ofn );
    }
    return std::wstring( fileName );
   
}
int main() {
    GLFWwindow * window;
    //GLFWinit, create window, etc.
    {
        if( !glfwInit() )
        {
            std::cout << "ERROR: glfwInit fail." << std::endl;
            system( "pause" );
            exit( - 1 );
        }
       
        //glfwWindowHint(GLFW_DECORATED, false);
        window = glfwCreateWindow( 800, 600, "Creator", NULL, NULL );
        {
            if( !window )
            {
                glfwTerminate();
                std::cout << "ERROR: glfwCreateWindow fail." << std::endl;
                system( "pause" );
                exit( - 1 );
            }
           
            glfwMakeContextCurrent( window );
            glClearColor( 0, 0, 0, 1 );
            glEnable( GL_DEPTH_TEST );
            glDepthFunc( GL_LESS );
            glEnable( GL_BLEND );
            glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
            glEnable( GL_TEXTURE_2D );
           
            //glfwSetInputMode(window_game, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
           
            //glfwSetWindowPosCallback(window, windowWindowposfun);
            //glfwSetWindowSizeCallback(window, windowWindowsizefun);
            //glfwSetWindowCloseCallback(window, windowWindowclosefun);
            //glfwSetWindowRefreshCallback(window, windowWindowrefreshfun);
            //glfwSetWindowFocusCallback(window, windowWindowfocusfun);
            //glfwSetWindowIconifyCallback(window, windowWindowiconifyfun);
            //glfwSetFramebufferSizeCallback(window, windowFramebuffersizefun);
            //glfwSetKeyCallback(window, windowKeyfun);
            //glfwSetCharCallback(window, windowCharfun);
            //glfwSetCharModsCallback(window, windowCharmodsfun);
            //glfwSetMouseButtonCallback(window, windowMousebuttonfun);
            //glfwSetCursorPosCallback(window, windowCursorposfun);
            //glfwSetCursorEnterCallback(window, windowCursorenterfun);
            //glfwSetScrollCallback(window, windowScrollfun);
            //glfwSetDropCallback(window, windowDropfun);
           
            glViewport( 0, 0, 800, 600 );
            glMatrixMode( GL_PROJECTION );
            //glm::perspectiveFov(glm::degrees(90.0), 800.0, 600.0, 0.01, 10000.0);
            glLoadMatrixd( & glm::perspectiveFov( 45.0, 800.0, 600.0, 0.01, 10000.0 )[ 0 ][ 0 ] );
            glMatrixMode( GL_MODELVIEW );
        }
    }
   
    GLuint width, height;
    std::vector < GLubyte > image;
   
    if( !loadtexturedata( getPNGfile(), width, height, image ) )
    {
        glfwTerminate();
        std::cout << "ERROR: texture data fail" << std::endl;
        system( "pause" );
        exit( - 1 );
    }
   
    Stopwatch stopwatch;
   
    GLuint n;
    while( true )
    {
        std::cout << "n = ";
        std::cin >> n;
        if( n == 0 ) break;
       
        {
            GLuint * textures = new GLuint[ n ];
           
            stopwatch.Start();
            for( GLuint i = 0; i < n; i++ )
            {
                glGenTextures( 1, & textures[ i ] );
                glBindTexture( GL_TEXTURE_2D, textures[ i ] );
                glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
                glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
                glTexImage2D( GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, & image[ 0 ] );
            }
            stopwatch.Stop();
           
            std::cout << "glGenTextures(1, ...) - \t" << stopwatch.ReadPassedNanoseconds() << " nanoseconds" << std::endl;
           
            stopwatch.Start();
            for( GLuint i = 0; i < n; i++ )
            {
                glDeleteTextures( 1, & textures[ i ] );
            }
            stopwatch.Stop();
           
            std::cout << "glDeleteTextures(1, ...) - \t" << stopwatch.ReadPassedNanoseconds() << " nanoseconds" << std::endl;
            delete[] textures;
        }
        {
            GLuint * textures = new GLuint[ n ];
           
            stopwatch.Start();
            glGenTextures( n, textures );
            for( GLuint i = 0; i < n; i++ )
            {
                glBindTexture( GL_TEXTURE_2D, textures[ i ] );
                glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
                glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
                glTexImage2D( GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, & image[ 0 ] );
            }
            stopwatch.Stop();
           
            std::cout << "glGenTextures(n, ...) - \t" << stopwatch.ReadPassedNanoseconds() << " nanoseconds" << std::endl;
           
            stopwatch.Start();
            glDeleteTextures( n, textures );
            stopwatch.Stop();
           
            std::cout << "glDeleteTextures(n, ...) - \t" << stopwatch.ReadPassedNanoseconds() << " nanoseconds" << std::endl;
            delete[] textures;
        }
        std::cout << std::endl;
    }
   
    return 0;
   
   
}

a tu przykładowe wyniki:

n = 1
glGenTextures(1, ...) -          9688900 nanoseconds
glDeleteTextures(1, ...) -         22169 nanoseconds
glGenTextures(n, ...) -         10826935 nanoseconds
glDeleteTextures(n, ...) -         18064 nanoseconds

n = 5
glGenTextures(1, ...) -         55181980 nanoseconds
glDeleteTextures(1, ...) -         65276 nanoseconds
glGenTextures(n, ...) -         52331555 nanoseconds
glDeleteTextures(n, ...) -         41054 nanoseconds

n = 10
glGenTextures(1, ...) -         116387091 nanoseconds
glDeleteTextures(1, ...) -          71845 nanoseconds
glGenTextures(n, ...) -         104555548 nanoseconds
glDeleteTextures(n, ...) -          76361 nanoseconds

n = 20
glGenTextures(1, ...) -         168469855 nanoseconds
glDeleteTextures(1, ...) -        3045845 nanoseconds
glGenTextures(n, ...) -          11285926 nanoseconds
glDeleteTextures(n, ...) -        1030061 nanoseconds

n = 40
glGenTextures(1, ...) -          86171271 nanoseconds
glDeleteTextures(1, ...) -        5139633 nanoseconds
glGenTextures(n, ...) -          85088659 nanoseconds
glDeleteTextures(n, ...) -        5790349 nanoseconds

n = 80
glGenTextures(1, ...) -         267078636 nanoseconds
glDeleteTextures(1, ...) -       22871962 nanoseconds
glGenTextures(n, ...) -         143067284 nanoseconds
glDeleteTextures(n, ...) -       24900473 nanoseconds

n = 160
glGenTextures(1, ...) -         421811602 nanoseconds
glDeleteTextures(1, ...) -       69399620 nanoseconds
glGenTextures(n, ...) -         457020080 nanoseconds
glDeleteTextures(n, ...) -       72811673 nanoseconds

n = 320
glGenTextures(1, ...) -         724011680 nanoseconds
glDeleteTextures(1, ...) -      123891883 nanoseconds
glGenTextures(n, ...) -         699070152 nanoseconds
glDeleteTextures(n, ...) -       98034015 nanoseconds

n = 320
glGenTextures(1, ...) -         668035292 nanoseconds
glDeleteTextures(1, ...) -      108630223 nanoseconds
glGenTextures(n, ...) -         592045577 nanoseconds
glDeleteTextures(n, ...) -      170131337 nanoseconds

n = 320
glGenTextures(1, ...) -         615115012 nanoseconds
glDeleteTextures(1, ...) -       84387856 nanoseconds
glGenTextures(n, ...) -         619615014 nanoseconds
glDeleteTextures(n, ...) -      157210204 nanoseconds

Zaznaczę że podane czasy są czasem utworzenia uchwytów i przypisania do nich tekstury, nie samym wywołaniem funkcji glGenTextures - chciałem sprawdzić wyniki w 'naturalnej' sytuacji.

Wynika z tego, że funkcje można wywoływać zamiennie, jak komu wygodniej ;P mam nadzieję, że dobrze przeprowadziłem test (nie sprawdzałem, czy tekstury się rysują..)
P-164299
1 « 2 »
Poprzednia strona Strona 2 z 2