⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gpumathlib.cpp

📁 这是在GPU上进行高速数字计算的数学库函数
💻 CPP
📖 第 1 页 / 共 2 页
字号:
            }
        }

        if (debug_level)
        {
            matrixPrint4x4(matrix, "MATRIX");
            matrixPrint4x4(inverse, "INVERSE");
        }
    }

    for (diagonal = 0; diagonal < 4; diagonal++)
    {
        // Normalize the diagonal 
        factor = matrix[(diagonal * 4) + diagonal];

        if (factor != 0.0f)
        {
            for (column = 0; column < 4; column++)
            {
                matrix[(diagonal * 4) + column] /= factor;
                inverse[(diagonal * 4) + column] /= factor;
            }
        }
    }

    if (debug_level)
    {
        matrixPrint4x4(matrix, "MATRIX after normalization");
        matrixPrint4x4(inverse, "INVERSE after normalization");
    }
}

int ImageLoad(char* filename, Image* image)
{
    FILE*							file;
    unsigned long 					size;   // size of the image in bytes.
    unsigned long 					i;     	// standard counter.
    unsigned short int 				planes; // number of planes in image (must be 1)
    unsigned short int 				bpp;    // number of bits per pixel (must be 24)
    char 							temp;   // temporary color storage for bgr-rgb conversion.

    // make sure the file is there.
    if ((file = fopen(filename, "rb")) == NULL)
    {
        printf("File Not Found : %s\n",filename);
        return 0;
    }

    // seek through the bmp header, up to the width/height:
    fseek(file, 18, SEEK_CUR);

    // read the width
    if ((i = fread(&image->sizeX, 4, 1, file)) != 1) 
    {
        printf("Error reading width from %s.\n", filename);
        return 0;
    }

//     printf("Width of %s: %lu\n", filename, image->sizeX);

    // read the height
    if ((i = fread(&image->sizeY, 4, 1, file)) != 1)
    {
        printf("Error reading height from %s.\n", filename);
        return 0;
    }

//     printf("Height of %s: %lu\n", filename, image->sizeY);

    // calculate the size (assuming 24 bits or 3 bytes per pixel).
    size = image->sizeX * image->sizeY * 3;
    int full_size = image->sizeX * image->sizeY * 4;

    // read the planes
    if ((fread(&planes, 2, 1, file)) != 1) 
    {
        printf("Error reading planes from %s.\n", filename);
        return 0;
    }

    if (planes != 1) 
    {
        printf("Planes from %s is not 1: %u\n", filename, planes);
        return 0;
    }

    // read the bpp
    if ((i = fread(&bpp, 2, 1, file)) != 1)
    {
        printf("Error reading bpp from %s.\n", filename);
        return 0;
    }

    if (bpp != 24) {
        printf("Bpp from %s is not 24: %u\n", filename, bpp);
        return 0;
    }

    // seek past the rest of the bitmap header.
    fseek(file, 24, SEEK_CUR);

    // read the data.
    //image->data = (char *) malloc(size);
    unsigned char* original_data = new unsigned char[size];
    image->data = new unsigned char[full_size];

    if (image->data == NULL) 
    {
        printf("Error allocating memory for color-corrected image data");
        return 0;
    }

    if ((i = fread(original_data, size, 1, file)) != 1) 
    {
        printf("Error reading image data from %s.\n", filename);
        return 0;
    }

    int		 write_ptr = 0;

    for (i = 0; i < size; i += 3)
    { // reverse all of the colors. (bgr -> rgb)
        image->data[write_ptr + 3] = 0xff;

        temp = original_data[i + 0];
        image->data[write_ptr + 0] = original_data[i + 2];
        image->data[write_ptr + 1] = original_data[i + 1];
        image->data[write_ptr + 2] = temp;
// 		image->data[write_ptr + 1] = 0xff;
// 		image->data[write_ptr + 2] = 0xff;

// 		if (image->data[write_ptr + 0] != 0)
// 		{
// 			printf("%d: %08x\n", i, *(unsigned int*)&image->data[write_ptr]);
// 		}

        write_ptr += 4;
    }

    delete original_data;

    // we're done.
    return 1;
}

///////////////////////////////////////////////////////////////
// arb support routines
void loadJahshakaBasicArb( GLint width, GLint height, float camera_distance,
                           unsigned char* vertex_program,
                           GLuint& vertex_program_handle )
{
    glGenProgramsARB(1, &vertex_program_handle);
    glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertex_program_handle);
    glProgramStringARB( GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
                        strlen((char*)vertex_program), (const GLubyte*)vertex_program);

    if (glGetError() == GL_INVALID_OPERATION)
    {
        find_shader_program_error(vertex_program, "jahshaka_basic_vert_arb.vp");
    }

    glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB, 12, width, 0, 0, 0);
    glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB, 13, height, 0, 0, 0);
    glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB, 14, camera_distance, 0, 0, 0);

    glEnable(GL_VERTEX_PROGRAM_ARB);

}

void debug_arbdata(void)
{
    int											max_fragment_instructions;
    int											max_vertex_instructions;


    glGetProgramivARB(  GL_FRAGMENT_PROGRAM_ARB,
                        GL_MAX_PROGRAM_INSTRUCTIONS_ARB,
                        &max_fragment_instructions);

    printf("Maximum number of instructions for a fragment program is: %d\n",
            max_fragment_instructions);

    glGetProgramivARB(  GL_VERTEX_PROGRAM_ARB,
                        GL_MAX_PROGRAM_INSTRUCTIONS_ARB,
                        &max_vertex_instructions);

    printf("Maximum number of instructions for a vertex program is: %d\n",
            max_vertex_instructions);
}

bool isAnARBFPInstruction(GLubyte* string)
{
    static const int NUM_ARBFP_INSTRUCTIONS = 33;

    static const char* arbfp_instruction_names[] =
    {
        "ABS", "ADD", "CMP", "COS", "DP3", "DP4", "DPH", "DST", "EX2",
        "FLR", "FRC", "KIL", "LG2", "LIT", "LRP", "MAD", "MAX", "MIN",
        "MOV", "MUL", "POW", "RCP", "RSQ", "SCS", "SGE", "SIN", "SLT",
        "SUB", "SWZ", "TEX", "TXB", "TXP", "XPD"
    };

    for (int i = 0; i < NUM_ARBFP_INSTRUCTIONS; i++)
    {
        if ( !strncmp((const char*)string, arbfp_instruction_names[i], 3) )
        {
            return true;
        }
    }

    return false;
}

int countARBFPInstructions(GLubyte* fragment_program)
{
    bool                    done = false;
    int                     count = 0;

    // This really should filter commented out instructions FIXME
    while (!done)
    {
        while (*fragment_program != '\0' && !isAnARBFPInstruction(fragment_program) )
        { 
            fragment_program++;
        }

        if (*fragment_program == '\0')
        {
            return count;
        }

        count++;
        fragment_program++;
    }

    // Keep the compiler from complaining-
    return count;
}


////////////////////////////////////////////////////////////
// check to see if complex arb is supported on the hardware

bool checkComplexArbSupport(QString name, int max_fp_instructions, int max_vp_instructions )
{
	//max_vp_instructions is ignored for now

	bool isSupported;

    unsigned char* fragment_program = loadshaderfile(name);

    int arb_fragment_instruction_count = countARBFPInstructions(fragment_program);

    fprintf(stderr, "%s instruction count = %d "
                    "max_fp_instructions = %d\n", 
                    name.ascii(), 
                    arb_fragment_instruction_count,
                    max_fp_instructions);

    isSupported = bool(arb_fragment_instruction_count <= max_fp_instructions);



    delete [] fragment_program;

	return isSupported;
}

///////////////////////////////////////////////////////////////////
// not implemented yet
///////////////////////////////////////////////////////////////////
/*
enum ARBVPInstructionType
{
    ABS, ADD, ARL, DP3, DP4, DPH, DST, EX2, EXP,
    FLR, FRC, LG2, LIT, LOG, MAD, MAX, MIN, MOV,
    MUL, POW, RCP, RSQ, SGE, SLT, SUB, SWZ, XPD,
    not_an_arbvp_instruction
};

///////////////////////////////////////////////////////////////////

static const int NUM_ARBVP_INSTRUCTIONS = 27;

static const char* arbvp_instruction_names[] =
{
    "ABS", "ADD", "ARL", "DP3", "DP4", "DPH", "DST", "EX2", "EXP",
    "FLR", "FRC", "LG2", "LIT", "LOG", "MAD", "MAX", "MIN", "MOV",
    "MUL", "POW", "RCP", "RSQ", "SGE", "SLT", "SUB", "SWZ", "XPD"
};
*/
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////
//gpgpu routines
//not sure which one of these to use...

#ifdef NVIDIA_GPGPU
void create_pbuffer(int width, int height)
{
    int                             iConfigCount;

    if (shader_display == 0)
    {
        shader_display = XOpenDisplay(NULL);
    }

    iScreen = DefaultScreen(shader_display);

    int pfAttribList[] =
        {
            GLX_RED_SIZE,               32,
            GLX_GREEN_SIZE,             32,
            GLX_BLUE_SIZE,              32,
            GLX_ALPHA_SIZE,             32,
            GLX_STENCIL_SIZE,           0,
            GLX_DEPTH_SIZE,             0,
            GLX_FLOAT_COMPONENTS_NV,    true,
            GLX_DRAWABLE_TYPE,          GLX_PBUFFER_BIT,
            0,
        };

    glxConfig = glXChooseFBConfig(shader_display, iScreen, pfAttribList, &iConfigCount);
    check_gl();

    int pbAttribList[] =
        {
            GLX_PRESERVED_CONTENTS, true,
            GLX_PBUFFER_WIDTH, width,
            GLX_PBUFFER_HEIGHT, height,
            0,
        };

    shader_pbuffer = glXCreatePbuffer(shader_display, glxConfig[0], pbAttribList);
    check_gl();

    shader_context = glXCreateNewContext(shader_display, glxConfig[0], GLX_RGBA_TYPE, 0, true);
    check_gl();
}
#endif // NVIDIA_GPU


#ifdef NVIDIA_GPGPU
#ifdef WIN32
//no gpgpu support on windows yet!
#else
void create_pbuffer(int width, int height)
{
    int                             iConfigCount;

    if (shader_display == 0)
    {
        shader_display = XOpenDisplay(NULL);
    }

    iScreen = DefaultScreen(shader_display);

    int pfAttribList[] =
    {
        GLX_RED_SIZE,               8,
        GLX_GREEN_SIZE,             8,
        GLX_BLUE_SIZE,              8,
        GLX_ALPHA_SIZE,             8,
        GLX_STENCIL_SIZE,           0,
        GLX_DEPTH_SIZE,             0,
        GLX_FLOAT_COMPONENTS_NV,    false,
        GLX_DRAWABLE_TYPE,          GLX_PBUFFER_BIT,
        0,
    };

    glxConfig = glXChooseFBConfig(shader_display, iScreen, pfAttribList, &iConfigCount);
    check_gl();

    int pbAttribList[] =  
    {
        GLX_PRESERVED_CONTENTS, true,
        GLX_PBUFFER_WIDTH, width,
        GLX_PBUFFER_HEIGHT, height,
        0,
    };

    shader_pbuffer = glXCreatePbuffer(shader_display, glxConfig[0], pbAttribList);
    check_gl();

    shader_context = glXCreateNewContext(shader_display, glxConfig[0], GLX_RGBA_TYPE, jahshaka_glx_context, true);

    check_gl();
}

#endif // !WIN32
#endif // NVIDIA_GPU

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -